Skip to content

Commit 75cb852

Browse files
authored
Merge pull request #113 from marcelthole/handle-x-extension-properties
Handle x extension properties
2 parents 8f1f706 + c37cca8 commit 75cb852

File tree

7 files changed

+59
-1
lines changed

7 files changed

+59
-1
lines changed

src/SpecBaseObject.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,4 +484,22 @@ public function getDocumentPosition(): ?JsonPointer
484484
{
485485
return $this->_jsonPointer;
486486
}
487+
488+
/**
489+
* Returns extension properties with `x-` prefix.
490+
* @see https://github.com/OAI/OpenAPI-Specification/blob/3.0.2/versions/3.0.2.md#specificationExtensions
491+
* @return array<string, mixed>
492+
* @since 1.5.3
493+
*/
494+
public function getExtensions(): array
495+
{
496+
$extensions = [];
497+
foreach ($this->_properties as $propertyKey => $extension) {
498+
if (strpos($propertyKey, 'x-') !== 0) {
499+
continue;
500+
}
501+
$extensions[$propertyKey] = $extension;
502+
}
503+
return $extensions;
504+
}
487505
}

src/spec/PathItem.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,12 @@ public function resolveReferences(ReferenceContext $context = null)
190190
}
191191
}
192192
}
193+
194+
if ($pathItem instanceof SpecBaseObject) {
195+
foreach ($pathItem->getExtensions() as $extensionKey => $extension) {
196+
$this->{$extensionKey} = $extension;
197+
}
198+
}
193199
}
194200
parent::resolveReferences($context);
195201
}

tests/spec/PathTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public function testInvalidPath()
137137
public function testPathItemReference()
138138
{
139139
$file = __DIR__ . '/data/paths/openapi.yaml';
140-
/** @var $openapi OpenApi */
140+
/** @var $openapi \cebe\openapi\spec\OpenApi */
141141
$openapi = Reader::readFromYamlFile($file, \cebe\openapi\spec\OpenApi::class, false);
142142

143143
$result = $openapi->validate();
@@ -147,6 +147,10 @@ public function testPathItemReference()
147147
$this->assertInstanceOf(Paths::class, $openapi->paths);
148148
$this->assertInstanceOf(PathItem::class, $fooPath = $openapi->paths['/foo']);
149149
$this->assertInstanceOf(PathItem::class, $barPath = $openapi->paths['/bar']);
150+
$this->assertSame([
151+
'x-extension-1' => 'Extension1',
152+
'x-extension-2' => 'Extension2'
153+
], $openapi->getExtensions());
150154

151155
$this->assertEmpty($fooPath->getOperations());
152156
$this->assertEmpty($barPath->getOperations());

tests/spec/ReferenceTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,23 @@ public function testReferencedCommonParamsInReferencedPath()
504504
enum:
505505
- test
506506
type: string
507+
x-something: something
508+
/something:
509+
get:
510+
responses:
511+
'200':
512+
description: 'OK if common params can be references'
513+
parameters:
514+
-
515+
name: test
516+
in: header
517+
description: 'Test parameter to be referenced'
518+
required: true
519+
schema:
520+
enum:
521+
- test
522+
type: string
523+
x-something: something
507524
508525
YAML;
509526
// remove line endings to make string equal on windows

tests/spec/data/paths/openapi.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ openapi: 3.0.2
33
info:
44
title: My API
55
version: 1.0.0
6+
x-extension-1: Extension1
7+
x-extension-2: Extension2
8+
X-EXTENSION: Invalid because of Uppercase X-
9+
xyz-extension: invalid extension
610
paths:
711
/foo:
812
$ref: 'path-items.yaml#/~1foo'

tests/spec/data/reference/ReferencedCommonParamsInReferencedPath.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,11 @@ info:
55
paths:
66
/example:
77
$ref: 'paths/ReferencesCommonParams.yml'
8+
/something:
9+
parameters:
10+
- $ref: './parameters/TestParameter.yml'
11+
x-something: something
12+
get:
13+
responses:
14+
200:
15+
description: OK if common params can be references

tests/spec/data/reference/paths/ReferencesCommonParams.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
parameters:
22
- $ref: '../parameters/TestParameter.yml'
33

4+
x-something: something
45
get:
56
responses:
67
200:

0 commit comments

Comments
 (0)