Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add convenience functions for extension properties #169

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/SpecBaseObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -525,4 +525,37 @@ public function getExtensions(): array
}
return $extensions;
}

/**
* Returns extension property with `x-` prefix.
* @see https://github.com/OAI/OpenAPI-Specification/blob/3.0.2/versions/3.0.2.md#specificationExtensions
* @param string $key The property key, e.g. 'x-prop' or 'prop' (the 'x-' prefix is added automatically if needed).
* @return mixed|null The property value, if present.
* @since 1.8.0
*/
public function getExtension(string $key)
{
if (strpos($key, 'x-') !== 0) {
$key = 'x-' . $key;
}

return $this->_properties[$key] ?? null;
}

/**
* Set extension property with `x-` prefix.
* @see https://github.com/OAI/OpenAPI-Specification/blob/3.0.2/versions/3.0.2.md#specificationExtensions
* @param string $key The property key, e.g. 'x-prop' or 'prop' (the 'x-' prefix is added automatically if needed).
* @param mixed $value The property value.
* @return void
* @since 1.8.0
*/
public function setExtension(string $key, $value): void
{
if (strpos($key, 'x-') !== 0) {
$key = 'x-' . $key;
}

$this->_properties[$key] = $value;
}
}
27 changes: 27 additions & 0 deletions tests/WriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,33 @@ public function testWriteJson()
);
}

public function testWriteJsonExtensions()
{
$openapi = $this->createOpenAPI();
$openapi->{'x-extra-var'} = 'foo';
$openapi->setExtension('x-another-var', 'bar');
$openapi->setExtension('short-var', 'baz');

$json = \cebe\openapi\Writer::writeToJson($openapi);

$this->assertEquals(preg_replace('~\R~', "\n", <<<JSON
{
"openapi": "3.0.0",
"info": {
"title": "Test API",
"version": "1.0.0"
},
"paths": {},
"x-extra-var": "foo",
"x-another-var": "bar",
"x-short-var": "baz"
}
JSON
),
$json
);
}

public function testWriteJsonMofify()
{
$openapi = $this->createOpenAPI();
Expand Down
8 changes: 7 additions & 1 deletion tests/spec/InfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function testRead()
name: Apache 2.0
url: https://www.apache.org/licenses/LICENSE-2.0.html
version: 1.0.1
x-extra-var: foo
YAML
, Info::class);

Expand All @@ -46,6 +47,11 @@ public function testRead()
$this->assertInstanceOf(License::class, $info->license);
$this->assertEquals('Apache 2.0', $info->license->name);
$this->assertEquals('https://www.apache.org/licenses/LICENSE-2.0.html', $info->license->url);

$this->assertEquals('foo', $info->getExtensions()['x-extra-var']);
$this->assertEquals('foo', $info->getExtension('x-extra-var'));
$this->assertEquals('foo', $info->getExtension('extra-var'));
$this->assertNull($info->getExtension('another-var'));
}

public function testReadInvalid()
Expand Down Expand Up @@ -117,4 +123,4 @@ public function testReadInvalidLicense()
$this->assertNull($info->contact);

}
}
}