diff --git a/src/SpecBaseObject.php b/src/SpecBaseObject.php index 4ae45395..29f70b42 100644 --- a/src/SpecBaseObject.php +++ b/src/SpecBaseObject.php @@ -484,4 +484,22 @@ public function getDocumentPosition(): ?JsonPointer { return $this->_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 + { + $extensions = []; + foreach ($this->_properties as $propertyKey => $extension) { + if (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 4448da32..b94c946e 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/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' 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: