Skip to content

Commit db98ac8

Browse files
committed
Merge branch '155-referenced-parameters-compilng-to-object' of https://github.com/Aribros/php-openapi into 155-aribros-referenced-parameters-compiling-to-object-instead-of-array
2 parents 4c670b0 + c843243 commit db98ac8

File tree

4 files changed

+153
-0
lines changed

4 files changed

+153
-0
lines changed

tests/spec/PathTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,35 @@ public function testPathItemReference()
192192
$this->assertEquals('A bar', $barPath->get->responses['200']->description);
193193
$this->assertEquals('non-existing resource', $barPath->get->responses['404']->description);
194194
}
195+
196+
public function testPathParametersAreArrays()
197+
{
198+
$file = __DIR__ . '/data/path-params/openapi.yaml';
199+
/** @var $openapi \cebe\openapi\spec\OpenApi */
200+
$openapi = Reader::readFromYamlFile($file, \cebe\openapi\spec\OpenApi::class, true);
201+
202+
$result = $openapi->validate();
203+
$this->assertEquals([], $openapi->getErrors(), print_r($openapi->getErrors(), true));
204+
$this->assertTrue($result);
205+
206+
$this->assertInstanceOf(Paths::class, $openapi->paths);
207+
$this->assertIsArray($openapi->paths->getPaths());
208+
$this->assertInstanceOf(PathItem::class, $usersPath = $openapi->paths['/v1/{organizationId}/user']);
209+
$this->assertInstanceOf(PathItem::class, $userIdPath = $openapi->paths['/v1/{organizationId}/user/{id}']);
210+
211+
$result = $usersPath->validate();
212+
$this->assertTrue($result);
213+
$this->assertIsArray($usersPath->parameters);
214+
$this->assertInstanceOf(\cebe\openapi\spec\Parameter::class, $usersPath->parameters[0]);
215+
$this->assertInstanceOf(\cebe\openapi\spec\Parameter::class, $usersPath->parameters[1]);
216+
$this->assertEquals($usersPath->parameters[0]->name, 'api-version');
217+
218+
$result = $userIdPath->validate();
219+
$this->assertTrue($result);
220+
$this->assertIsArray($userIdPath->parameters);
221+
$this->assertInstanceOf(\cebe\openapi\spec\Parameter::class, $userIdPath->parameters[0]);
222+
$this->assertInstanceOf(\cebe\openapi\spec\Parameter::class, $userIdPath->parameters[1]);
223+
$this->assertEquals($userIdPath->parameters[2]->name, 'id');
224+
225+
}
195226
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
components:
2+
parameters:
3+
Version:
4+
in: header
5+
name: api-version
6+
required: false
7+
schema:
8+
type: string
9+
format: date
10+
example: '2021-05-18'
11+
description: The API version
12+
OrganizationId:
13+
in: path
14+
name: organizationId
15+
required: true
16+
schema:
17+
type: string
18+
format: uuid
19+
description: The Organization ID
20+
responses:
21+
BadRequest:
22+
description: Bad Request
23+
Forbidden:
24+
description: Forbidden
25+
NotFound:
26+
description: Not Found
27+
Success:
28+
description: Success
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
openapi: 3.0.0
2+
info:
3+
version: "2021-05-18"
4+
title: Test REST API
5+
description: Specifications for the Test REST API.
6+
contact:
7+
name: bplainia
8+
9+
10+
servers:
11+
- url: 'http://localhost:8000'
12+
description: 'Test'
13+
14+
paths:
15+
/v1/{organizationId}/user:
16+
$ref: 'user.yaml#/paths/Users'
17+
/v1/{organizationId}/user/{id}:
18+
$ref: 'user.yaml#/paths/UserId'

tests/spec/data/path-params/user.yaml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
paths:
2+
Users:
3+
parameters:
4+
- $ref: 'global.yaml#/components/parameters/Version'
5+
- $ref: 'global.yaml#/components/parameters/OrganizationId'
6+
post:
7+
summary: Creates a user
8+
security:
9+
- BearerAuth: []
10+
requestBody:
11+
required: true
12+
content:
13+
application/json:
14+
schema:
15+
type: object
16+
properties:
17+
data:
18+
$ref: '#/components/schemas/User'
19+
responses:
20+
'201':
21+
description: Created
22+
content:
23+
application/json:
24+
schema:
25+
type: object
26+
properties:
27+
data:
28+
$ref: '#/components/schemas/User'
29+
'400':
30+
$ref: 'global.yaml#/components/responses/BadRequest'
31+
'403':
32+
$ref: 'global.yaml#/components/responses/Forbidden'
33+
UserId:
34+
parameters:
35+
- $ref: 'global.yaml#/components/parameters/Version'
36+
- $ref: 'global.yaml#/components/parameters/OrganizationId'
37+
- $ref: '#/components/parameters/UserId'
38+
get:
39+
summary: Gets a user
40+
security:
41+
- BearerAuth: []
42+
responses:
43+
'200':
44+
description: A bar
45+
content:
46+
application/json:
47+
schema:
48+
type: object
49+
properties:
50+
data:
51+
$ref: '#/components/schemas/User'
52+
'400':
53+
$ref: 'global.yaml#/components/responses/BadRequest'
54+
'403':
55+
$ref: 'global.yaml#/components/responses/Forbidden'
56+
'404':
57+
$ref: 'global.yaml#/components/responses/NotFound'
58+
components:
59+
schemas:
60+
User:
61+
type: object
62+
properties:
63+
id:
64+
type: string
65+
format: uuid
66+
name:
67+
type: string
68+
parameters:
69+
UserId:
70+
in: path
71+
name: id
72+
required: true
73+
schema:
74+
type: string
75+
format: uuid
76+
description: User's ID

0 commit comments

Comments
 (0)