Skip to content

Commit

Permalink
Merge pull request #27 from MasonM/add-shared-parameter-support
Browse files Browse the repository at this point in the history
Add support for shared Path Item parameters
  • Loading branch information
Maks3w authored May 8, 2017
2 parents 0d2bc12 + 1a494ba commit 564a275
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 22 deletions.
28 changes: 25 additions & 3 deletions src/SchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,34 @@ public function getRequestSchema(string $path, string $method): stdClass
*/
public function getRequestParameters(string $path, string $method): array
{
$result = [];
$pathItemParameters = [
'paths',
$path,
'parameters',
];

// See if there any parameters shared by all methods
if ($this->hasPath($pathItemParameters)) {
foreach ($this->getPath($pathItemParameters) as $parameter) {
// Index by unique ID for later merging.
// "A unique parameter is defined by a combination of a name and location."
// - http://swagger.io/specification/#pathItemParameters
$uniqueId = $parameter->name . ',' . $parameter->in;
$result[$uniqueId] = $parameter;
}
}

$method = $this->getMethod($path, $method);
if (!isset($method->parameters)) {
return [];
if (isset($method->parameters)) {
foreach ($method->parameters as $parameter) {
// Operation parameters override shared parameters
$uniqueId = $parameter->name . ',' . $parameter->in;
$result[$uniqueId] = $parameter;
}
}

return $method->parameters;
return array_values($result);
}

/**
Expand Down
27 changes: 27 additions & 0 deletions tests/SchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,33 @@ public function requestMediaTypesProvider()
];
}

/**
* @dataProvider requestParameters
*/
public function testGetRequestParameters($path, $method, $expectedParameters)
{
$parameters = $this->schemaManager->getRequestParameters($path, $method);

self::assertEquals($expectedParameters, json_encode($parameters));
}

public function requestParameters()
{
$pets_id_shared_parameters = '[{"name":"id","in":"path","description":"ID of pet to fetch","required":true,"type":"integer","format":"int64"}';
$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"}}}]}}]';
$pets_id_delete_parameter = '[{"name":"id","in":"path","description":"Override the shared ID parameter","required":true,"type":"integer","format":"int64"}]';

$dataSet = [
// Description => [path, method, expectedParameters]
'without parameters' => ['/food', 'get', '[]'],
'with a shared parameter and operation parameters' => ['/pets/{id}', 'patch', $pets_id_patch_parameters],
'with a operation parameter that overrides a shared parameter' => ['/pets/{id}', 'delete', $pets_id_delete_parameter],
'with only a shared parameter' => ['/pets/{id}', 'get', $pets_id_shared_parameters . ']'],
];

return $dataSet;
}

/**
* @dataProvider requestHeadersParameters
*/
Expand Down
30 changes: 11 additions & 19 deletions tests/fixture/petstore-with-external-docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@
}
},
"/pets/{id}": {
"parameters": [
{
"name": "id",
"in": "path",
"description": "ID of pet to fetch",
"required": true,
"type": "integer",
"format": "int64"
}
],
"patch": {
"description": "Creates a new pet in the store. Duplicates are allowed",
"operationId": "updatePet",
Expand All @@ -159,14 +169,6 @@
"application/json"
],
"parameters": [
{
"name": "id",
"in": "path",
"description": "ID of pet to fetch",
"required": true,
"type": "integer",
"format": "int64"
},
{
"$ref": "#/parameters/required_header"
},
Expand Down Expand Up @@ -213,16 +215,6 @@
"text/xml",
"text/html"
],
"parameters": [
{
"name": "id",
"in": "path",
"description": "ID of pet to fetch",
"required": true,
"type": "integer",
"format": "int64"
}
],
"responses": {
"200": {
"description": "pet response",
Expand All @@ -245,7 +237,7 @@
{
"name": "id",
"in": "path",
"description": "ID of pet to delete",
"description": "Override the shared ID parameter",
"required": true,
"type": "integer",
"format": "int64"
Expand Down

0 comments on commit 564a275

Please sign in to comment.