Skip to content

Commit 564a275

Browse files
authored
Merge pull request #27 from MasonM/add-shared-parameter-support
Add support for shared Path Item parameters
2 parents 0d2bc12 + 1a494ba commit 564a275

File tree

3 files changed

+63
-22
lines changed

3 files changed

+63
-22
lines changed

src/SchemaManager.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,12 +315,34 @@ public function getRequestSchema(string $path, string $method): stdClass
315315
*/
316316
public function getRequestParameters(string $path, string $method): array
317317
{
318+
$result = [];
319+
$pathItemParameters = [
320+
'paths',
321+
$path,
322+
'parameters',
323+
];
324+
325+
// See if there any parameters shared by all methods
326+
if ($this->hasPath($pathItemParameters)) {
327+
foreach ($this->getPath($pathItemParameters) as $parameter) {
328+
// Index by unique ID for later merging.
329+
// "A unique parameter is defined by a combination of a name and location."
330+
// - http://swagger.io/specification/#pathItemParameters
331+
$uniqueId = $parameter->name . ',' . $parameter->in;
332+
$result[$uniqueId] = $parameter;
333+
}
334+
}
335+
318336
$method = $this->getMethod($path, $method);
319-
if (!isset($method->parameters)) {
320-
return [];
337+
if (isset($method->parameters)) {
338+
foreach ($method->parameters as $parameter) {
339+
// Operation parameters override shared parameters
340+
$uniqueId = $parameter->name . ',' . $parameter->in;
341+
$result[$uniqueId] = $parameter;
342+
}
321343
}
322344

323-
return $method->parameters;
345+
return array_values($result);
324346
}
325347

326348
/**

tests/SchemaManagerTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,33 @@ public function requestMediaTypesProvider()
137137
];
138138
}
139139

140+
/**
141+
* @dataProvider requestParameters
142+
*/
143+
public function testGetRequestParameters($path, $method, $expectedParameters)
144+
{
145+
$parameters = $this->schemaManager->getRequestParameters($path, $method);
146+
147+
self::assertEquals($expectedParameters, json_encode($parameters));
148+
}
149+
150+
public function requestParameters()
151+
{
152+
$pets_id_shared_parameters = '[{"name":"id","in":"path","description":"ID of pet to fetch","required":true,"type":"integer","format":"int64"}';
153+
$pets_id_patch_parameters = $pets_id_shared_parameters . ',{"name":"X-Required-Header","in":"header","description":"Required header","required":true,"type":"string"},{"name":"X-Optional-Header","in":"header","description":"Optional header","type":"string"},{"name":"pet","in":"body","description":"Pet to update","required":true,"schema":{"type":"object","allOf":[{"type":"object","required":["id","name"],"externalDocs":{"description":"find more info here","url":"https:\/\/swagger.io\/about"},"properties":{"id":{"type":"integer","format":"int64"},"name":{"type":"string"},"tag":{"type":"string"}}},{"required":["id"],"properties":{"id":{"type":"integer","format":"int64"}}}]}}]';
154+
$pets_id_delete_parameter = '[{"name":"id","in":"path","description":"Override the shared ID parameter","required":true,"type":"integer","format":"int64"}]';
155+
156+
$dataSet = [
157+
// Description => [path, method, expectedParameters]
158+
'without parameters' => ['/food', 'get', '[]'],
159+
'with a shared parameter and operation parameters' => ['/pets/{id}', 'patch', $pets_id_patch_parameters],
160+
'with a operation parameter that overrides a shared parameter' => ['/pets/{id}', 'delete', $pets_id_delete_parameter],
161+
'with only a shared parameter' => ['/pets/{id}', 'get', $pets_id_shared_parameters . ']'],
162+
];
163+
164+
return $dataSet;
165+
}
166+
140167
/**
141168
* @dataProvider requestHeadersParameters
142169
*/

tests/fixture/petstore-with-external-docs.json

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,16 @@
148148
}
149149
},
150150
"/pets/{id}": {
151+
"parameters": [
152+
{
153+
"name": "id",
154+
"in": "path",
155+
"description": "ID of pet to fetch",
156+
"required": true,
157+
"type": "integer",
158+
"format": "int64"
159+
}
160+
],
151161
"patch": {
152162
"description": "Creates a new pet in the store. Duplicates are allowed",
153163
"operationId": "updatePet",
@@ -159,14 +169,6 @@
159169
"application/json"
160170
],
161171
"parameters": [
162-
{
163-
"name": "id",
164-
"in": "path",
165-
"description": "ID of pet to fetch",
166-
"required": true,
167-
"type": "integer",
168-
"format": "int64"
169-
},
170172
{
171173
"$ref": "#/parameters/required_header"
172174
},
@@ -213,16 +215,6 @@
213215
"text/xml",
214216
"text/html"
215217
],
216-
"parameters": [
217-
{
218-
"name": "id",
219-
"in": "path",
220-
"description": "ID of pet to fetch",
221-
"required": true,
222-
"type": "integer",
223-
"format": "int64"
224-
}
225-
],
226218
"responses": {
227219
"200": {
228220
"description": "pet response",
@@ -245,7 +237,7 @@
245237
{
246238
"name": "id",
247239
"in": "path",
248-
"description": "ID of pet to delete",
240+
"description": "Override the shared ID parameter",
249241
"required": true,
250242
"type": "integer",
251243
"format": "int64"

0 commit comments

Comments
 (0)