Skip to content

Commit 9805a9a

Browse files
committed
Add extensions for the SpecBaseObject
1 parent f45b3e6 commit 9805a9a

File tree

5 files changed

+33
-2
lines changed

5 files changed

+33
-2
lines changed

src/SpecBaseObject.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,4 +484,21 @@ public function getDocumentPosition(): ?JsonPointer
484484
{
485485
return $this->_jsonPointer;
486486
}
487+
488+
/**
489+
* Handle 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+
*/
493+
public function getExtensions(): array
494+
{
495+
$extensions = [];
496+
foreach($this->_properties as $propertyKey => $extension) {
497+
if (mb_strpos($propertyKey, 'x-') !== 0) {
498+
continue;
499+
}
500+
$extensions[$propertyKey] = $extension;
501+
}
502+
return $extensions;
503+
}
487504
}

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ enum:
508508
/something:
509509
get:
510510
responses:
511-
200:
511+
'200':
512512
description: 'OK if common params can be references'
513513
parameters:
514514
-

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'

0 commit comments

Comments
 (0)