Skip to content

Commit

Permalink
Merge pull request #113 from marcelthole/handle-x-extension-properties
Browse files Browse the repository at this point in the history
Handle x extension properties
  • Loading branch information
cebe authored May 26, 2021
2 parents 8f1f706 + c37cca8 commit 75cb852
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/SpecBaseObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, mixed>
* @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;
}
}
6 changes: 6 additions & 0 deletions src/spec/PathItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 5 additions & 1 deletion tests/spec/PathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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());
Expand Down
17 changes: 17 additions & 0 deletions tests/spec/ReferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions tests/spec/data/paths/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions tests/spec/data/reference/paths/ReferencesCommonParams.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
parameters:
- $ref: '../parameters/TestParameter.yml'

x-something: something
get:
responses:
200:
Expand Down

0 comments on commit 75cb852

Please sign in to comment.