From 71b077315d0e340c4e476e315f2d8efe085ace1f Mon Sep 17 00:00:00 2001 From: Marcel Thole Date: Wed, 24 Jul 2024 16:02:39 +0200 Subject: [PATCH] Add reference normalize to path parameters on top level --- src/Merge/ReferenceNormalizer.php | 47 +++++++++++++------ .../Fixtures/expected/openapi-normalized.json | 22 +++++++++ .../Fixtures/openapi-with-reference.json | 22 +++++++++ .../Merge/Fixtures/pathOutsideOperation.json | 1 + tests/Merge/ReferenceNormalizerTest.php | 1 + 5 files changed, 79 insertions(+), 14 deletions(-) create mode 100644 tests/Merge/Fixtures/pathOutsideOperation.json diff --git a/src/Merge/ReferenceNormalizer.php b/src/Merge/ReferenceNormalizer.php index 5ddcfad..e8c8872 100644 --- a/src/Merge/ReferenceNormalizer.php +++ b/src/Merge/ReferenceNormalizer.php @@ -30,6 +30,17 @@ public function normalizeInlineReferences( ): ReferenceResolverResult { $refFileCollection = []; foreach ($openApiDefinition->paths as $path) { + foreach ($path->parameters as $parameterIndex => $parameter) { + $allParameters = $path->parameters; + if ($parameter instanceof Reference) { + $allParameters[$parameterIndex] = $this->normalizeReference($parameter, $refFileCollection); + } else { + $allParameters[$parameterIndex] = $this->normalizeParameters($parameter, $refFileCollection); + } + + $path->parameters = $allParameters; + } + foreach ($path->getOperations() as $operation) { foreach (($operation->parameters ?? []) as $parameterIndex => $parameter) { if (! $parameter instanceof Parameter) { @@ -37,20 +48,8 @@ public function normalizeInlineReferences( } /** @var array $allParameters */ - $allParameters = $operation->parameters; - if ($parameter->schema instanceof Reference) { - $allParameters[$parameterIndex]->schema = $this->normalizeReference( - $parameter->schema, - $refFileCollection, - ); - } - - if ($parameter->schema instanceof Schema) { - $allParameters[$parameterIndex]->schema = $this->normalizeProperties( - $parameter->schema, - $refFileCollection, - ); - } + $allParameters = $operation->parameters; + $allParameters[$parameterIndex] = $this->normalizeParameters($parameter, $refFileCollection); $operation->parameters = $allParameters; } @@ -265,4 +264,24 @@ public function normalizeProperty(Reference|Schema $property, array &$refFileCol return $property; } + + /** @param array $refFileCollection */ + public function normalizeParameters(Parameter $parameter, array &$refFileCollection): Parameter + { + if ($parameter->schema instanceof Reference) { + $parameter->schema = $this->normalizeReference( + $parameter->schema, + $refFileCollection, + ); + } + + if ($parameter->schema instanceof Schema) { + $parameter->schema = $this->normalizeProperties( + $parameter->schema, + $refFileCollection, + ); + } + + return $parameter; + } } diff --git a/tests/Merge/Fixtures/expected/openapi-normalized.json b/tests/Merge/Fixtures/expected/openapi-normalized.json index 971965f..3c9cff2 100644 --- a/tests/Merge/Fixtures/expected/openapi-normalized.json +++ b/tests/Merge/Fixtures/expected/openapi-normalized.json @@ -3,6 +3,28 @@ "info": {}, "paths": { "\/dummy": {}, + "\/path\/{id}\/{id2}": { + "get": { + "responses": { + "200": { + "description": "OK" + } + } + }, + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "integer" + } + }, + { + "$ref": "#\/components\/schemas\/pathOutsideOperation" + } + ] + }, "\/reference": { "get": { "parameters": [ diff --git a/tests/Merge/Fixtures/openapi-with-reference.json b/tests/Merge/Fixtures/openapi-with-reference.json index 36bc39c..9883a8c 100644 --- a/tests/Merge/Fixtures/openapi-with-reference.json +++ b/tests/Merge/Fixtures/openapi-with-reference.json @@ -3,6 +3,28 @@ "info": {}, "paths": { "/dummy": {}, + "/path/{id}/{id2}": { + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "integer" + } + }, + { + "$ref": "pathOutsideOperation.json#/components/schemas/pathOutsideOperation" + } + ], + "get": { + "responses": { + "200": { + "description": "OK" + } + } + } + }, "/reference": { "get": { "parameters": [ diff --git a/tests/Merge/Fixtures/pathOutsideOperation.json b/tests/Merge/Fixtures/pathOutsideOperation.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/tests/Merge/Fixtures/pathOutsideOperation.json @@ -0,0 +1 @@ +{} diff --git a/tests/Merge/ReferenceNormalizerTest.php b/tests/Merge/ReferenceNormalizerTest.php index bc72af3..85f037f 100644 --- a/tests/Merge/ReferenceNormalizerTest.php +++ b/tests/Merge/ReferenceNormalizerTest.php @@ -48,6 +48,7 @@ public function testReadFileWithResolvedReference(): void $foundRefFiles = $specificationResult->getFoundReferenceFiles(); $expectedRefFiles = [ + __DIR__ . '/Fixtures/pathOutsideOperation.json', __DIR__ . '/Fixtures/requestParam.json', __DIR__ . '/Fixtures/requestParamNullable.json', __DIR__ . '/Fixtures/responseModel.json',