From f45b3e640a2066bb193e082ca0415a1c14cfaa94 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Mon, 24 May 2021 13:40:51 +0200 Subject: [PATCH 1/5] Added failing test case for #100 --- tests/spec/ReferenceTest.php | 17 +++++++++++++++++ .../ReferencedCommonParamsInReferencedPath.yml | 8 ++++++++ .../reference/paths/ReferencesCommonParams.yml | 1 + 3 files changed, 26 insertions(+) diff --git a/tests/spec/ReferenceTest.php b/tests/spec/ReferenceTest.php index 4448da32..96e1ad7a 100644 --- a/tests/spec/ReferenceTest.php +++ b/tests/spec/ReferenceTest.php @@ -504,6 +504,23 @@ public function testReferencedCommonParamsInReferencedPath() enum: - test type: string + x-something: something + /something: + get: + responses: + 200: + description: 'OK if common params can be references' + parameters: + - + name: test + in: header + description: 'Test parameter to be referenced' + required: true + schema: + enum: + - test + type: string + x-something: something YAML; // remove line endings to make string equal on windows diff --git a/tests/spec/data/reference/ReferencedCommonParamsInReferencedPath.yml b/tests/spec/data/reference/ReferencedCommonParamsInReferencedPath.yml index 68b6b86b..7a7c4aa2 100644 --- a/tests/spec/data/reference/ReferencedCommonParamsInReferencedPath.yml +++ b/tests/spec/data/reference/ReferencedCommonParamsInReferencedPath.yml @@ -5,3 +5,11 @@ info: paths: /example: $ref: 'paths/ReferencesCommonParams.yml' + /something: + parameters: + - $ref: './parameters/TestParameter.yml' + x-something: something + get: + responses: + 200: + description: OK if common params can be references diff --git a/tests/spec/data/reference/paths/ReferencesCommonParams.yml b/tests/spec/data/reference/paths/ReferencesCommonParams.yml index 5513a537..2fb096f5 100644 --- a/tests/spec/data/reference/paths/ReferencesCommonParams.yml +++ b/tests/spec/data/reference/paths/ReferencesCommonParams.yml @@ -1,6 +1,7 @@ parameters: - $ref: '../parameters/TestParameter.yml' +x-something: something get: responses: 200: From 6959de616998bd19bad4a5214a15ccb3d4715f14 Mon Sep 17 00:00:00 2001 From: Marcel Thole Date: Mon, 24 May 2021 16:12:11 +0200 Subject: [PATCH 2/5] Add extensions for the SpecBaseObject --- src/SpecBaseObject.php | 17 +++++++++++++++++ src/spec/PathItem.php | 6 ++++++ tests/spec/PathTest.php | 6 +++++- tests/spec/ReferenceTest.php | 2 +- tests/spec/data/paths/openapi.yaml | 4 ++++ 5 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/SpecBaseObject.php b/src/SpecBaseObject.php index 4ae45395..e469689d 100644 --- a/src/SpecBaseObject.php +++ b/src/SpecBaseObject.php @@ -484,4 +484,21 @@ public function getDocumentPosition(): ?JsonPointer { return $this->_jsonPointer; } + + /** + * Handle extension properties with `x-` prefix. + * See https://github.com/OAI/OpenAPI-Specification/blob/3.0.2/versions/3.0.2.md#specificationExtensions + * @return array + */ + public function getExtensions(): array + { + $extensions = []; + foreach ($this->_properties as $propertyKey => $extension) { + if (mb_strpos($propertyKey, 'x-') !== 0) { + continue; + } + $extensions[$propertyKey] = $extension; + } + return $extensions; + } } diff --git a/src/spec/PathItem.php b/src/spec/PathItem.php index db6b105e..4b2debcd 100644 --- a/src/spec/PathItem.php +++ b/src/spec/PathItem.php @@ -190,6 +190,12 @@ public function resolveReferences(ReferenceContext $context = null) } } } + + if ($pathItem instanceof SpecBaseObject) { + foreach ($pathItem->getExtensions() as $extensionKey => $extension) { + $this->{$extensionKey} = $extension; + } + } } parent::resolveReferences($context); } diff --git a/tests/spec/PathTest.php b/tests/spec/PathTest.php index cf50b19d..6c46e4b0 100644 --- a/tests/spec/PathTest.php +++ b/tests/spec/PathTest.php @@ -137,7 +137,7 @@ public function testInvalidPath() public function testPathItemReference() { $file = __DIR__ . '/data/paths/openapi.yaml'; - /** @var $openapi OpenApi */ + /** @var $openapi \cebe\openapi\spec\OpenApi */ $openapi = Reader::readFromYamlFile($file, \cebe\openapi\spec\OpenApi::class, false); $result = $openapi->validate(); @@ -147,6 +147,10 @@ public function testPathItemReference() $this->assertInstanceOf(Paths::class, $openapi->paths); $this->assertInstanceOf(PathItem::class, $fooPath = $openapi->paths['/foo']); $this->assertInstanceOf(PathItem::class, $barPath = $openapi->paths['/bar']); + $this->assertSame([ + 'x-extension-1' => 'Extension1', + 'x-extension-2' => 'Extension2' + ], $openapi->getExtensions()); $this->assertEmpty($fooPath->getOperations()); $this->assertEmpty($barPath->getOperations()); diff --git a/tests/spec/ReferenceTest.php b/tests/spec/ReferenceTest.php index 96e1ad7a..b94c946e 100644 --- a/tests/spec/ReferenceTest.php +++ b/tests/spec/ReferenceTest.php @@ -508,7 +508,7 @@ enum: /something: get: responses: - 200: + '200': description: 'OK if common params can be references' parameters: - diff --git a/tests/spec/data/paths/openapi.yaml b/tests/spec/data/paths/openapi.yaml index 1dedca4b..a96831de 100644 --- a/tests/spec/data/paths/openapi.yaml +++ b/tests/spec/data/paths/openapi.yaml @@ -3,6 +3,10 @@ openapi: 3.0.2 info: title: My API version: 1.0.0 +x-extension-1: Extension1 +x-extension-2: Extension2 +X-EXTENSION: Invalid because of Uppercase X- +xyz-extension: invalid extension paths: /foo: $ref: 'path-items.yaml#/~1foo' From b54138c8c826a207aee50320a524d5861b416e90 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Wed, 26 May 2021 10:02:37 +0200 Subject: [PATCH 3/5] Update src/SpecBaseObject.php --- src/SpecBaseObject.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/SpecBaseObject.php b/src/SpecBaseObject.php index e469689d..caafda27 100644 --- a/src/SpecBaseObject.php +++ b/src/SpecBaseObject.php @@ -486,8 +486,8 @@ public function getDocumentPosition(): ?JsonPointer } /** - * Handle extension properties with `x-` prefix. - * See https://github.com/OAI/OpenAPI-Specification/blob/3.0.2/versions/3.0.2.md#specificationExtensions + * Returns extension properties with `x-` prefix. + * @see https://github.com/OAI/OpenAPI-Specification/blob/3.0.2/versions/3.0.2.md#specificationExtensions * @return array */ public function getExtensions(): array From c603f11a117ce4df4e6870ecf84e07c29ed3ea92 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Wed, 26 May 2021 16:19:09 +0200 Subject: [PATCH 4/5] Update src/SpecBaseObject.php --- src/SpecBaseObject.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SpecBaseObject.php b/src/SpecBaseObject.php index caafda27..4eb77849 100644 --- a/src/SpecBaseObject.php +++ b/src/SpecBaseObject.php @@ -494,7 +494,7 @@ public function getExtensions(): array { $extensions = []; foreach ($this->_properties as $propertyKey => $extension) { - if (mb_strpos($propertyKey, 'x-') !== 0) { + if (strpos($propertyKey, 'x-') !== 0) { continue; } $extensions[$propertyKey] = $extension; From c37cca8dc89de3cf1c3c4e92ed4a3c87e0390b74 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Wed, 26 May 2021 17:43:42 +0200 Subject: [PATCH 5/5] Update src/SpecBaseObject.php --- src/SpecBaseObject.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/SpecBaseObject.php b/src/SpecBaseObject.php index 4eb77849..29f70b42 100644 --- a/src/SpecBaseObject.php +++ b/src/SpecBaseObject.php @@ -489,6 +489,7 @@ public function getDocumentPosition(): ?JsonPointer * Returns extension properties with `x-` prefix. * @see https://github.com/OAI/OpenAPI-Specification/blob/3.0.2/versions/3.0.2.md#specificationExtensions * @return array + * @since 1.5.3 */ public function getExtensions(): array {