Skip to content

Commit 1cd3709

Browse files
committed
refactor: better spec extensions support
1 parent e512e96 commit 1cd3709

23 files changed

+95
-40
lines changed

src/ExtensionProperty.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace erasys\OpenApi;
4+
5+
/**
6+
* Specification Extension property definition.
7+
*/
8+
class ExtensionProperty
9+
{
10+
/**
11+
* Exact name of the property that will be used in the generated schema.
12+
*
13+
* @var string
14+
*/
15+
public $name;
16+
17+
/**
18+
* Value of the property
19+
*
20+
* @var mixed
21+
*/
22+
public $value;
23+
24+
/**
25+
* @param string $name
26+
* @param mixed|null $value
27+
*/
28+
public function __construct(string $name, $value = null)
29+
{
30+
$this->name = $name;
31+
$this->value = $value;
32+
}
33+
}

src/Spec/v3/AbstractObject.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace erasys\OpenApi\Spec\v3;
44

55
use ArrayAccess;
6+
use erasys\OpenApi\ExtensionProperty;
67
use erasys\OpenApi\RawValue;
78
use Illuminate\Contracts\Support\Arrayable;
89
use Illuminate\Contracts\Support\Jsonable;
@@ -117,23 +118,47 @@ public function toArray()
117118
private function exportValue($value)
118119
{
119120
if ($value instanceof RawValue) {
121+
// Unwrap value and return it raw, as it is.
120122
return $value->value;
121123
}
122124

123125
if ($value instanceof AbstractObject) {
124126
return $value->toArray();
125127
}
126128

129+
if ($value instanceof ExtensionProperty) {
130+
return [
131+
$value->name => $this->exportValue($value->value),
132+
];
133+
}
134+
127135
if (is_array($value)) {
128136
$result = [];
129137
foreach ($value as $k => $v) {
130138
// Ignore null properties
131139
if (is_null($v)) {
132140
continue;
133141
}
134-
// Transform extension property names
135-
if (preg_match('/^x[A-Z]/', $k)) {
136-
$k = 'x-' . lcfirst(preg_replace('/^(x)/', '', $k));
142+
// Take key and value from extension property definition
143+
if ($v instanceof ExtensionProperty) {
144+
$result[$v->name] = $this->exportValue($v->value);
145+
continue;
146+
}
147+
if (in_array($k, ['xml'])) {
148+
$result[$k] = $this->exportValue($v);
149+
continue;
150+
}
151+
// Transform extension property names using the 'x-dashes-format'
152+
if (preg_match('/^x[_]/', $k)) {
153+
$k = str_replace('_', '-', $k);
154+
$result[$k] = $this->exportValue($v);
155+
continue;
156+
}
157+
// Transform extension property names using the 'x-camelCaseFormat'
158+
if (preg_match('/^x[A-Za-z]/', $k)) {
159+
$k = 'x-' . lcfirst(preg_replace('/^(x)/', '', $k));
160+
$result[$k] = $this->exportValue($v);
161+
continue;
137162
}
138163
// Transform reference property names
139164
if ($k === 'ref') {

src/Spec/v3/AbstractParameter.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
/**
66
* Common class for Parameter Object and Header Object.
77
*/
8-
abstract class AbstractParameter extends AbstractObject
8+
abstract class AbstractParameter extends AbstractObject implements ExtensibleInterface
99
{
10-
1110
/**
1211
* A brief description of the parameter. This could contain examples of use.
1312
* CommonMark syntax MAY be used for rich text representation.

src/Spec/v3/Components.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
*
99
* @see https://swagger.io/specification/#componentsObject
1010
*/
11-
class Components extends AbstractObject
11+
class Components extends AbstractObject implements ExtensibleInterface
1212
{
13-
1413
/**
1514
* A map to hold reusable Schema Objects.
1615
*

src/Spec/v3/Contact.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
*
88
* @see https://swagger.io/specification/#contactObject
99
*/
10-
class Contact extends AbstractObject
10+
class Contact extends AbstractObject implements ExtensibleInterface
1111
{
12-
1312
/**
1413
* The identifying name of the contact person/organization.
1514
*

src/Spec/v3/Document.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
*
88
* @see https://swagger.io/specification/
99
*/
10-
class Document extends AbstractObject
10+
class Document extends AbstractObject implements ExtensibleInterface
1111
{
12-
1312
const SCHEME_HTTP = 'http';
1413
const SCHEME_HTTPS = 'https';
1514
const SCHEME_WEBSOCKET = 'ws';
1615
const SCHEME_SECURE_WEBSOCKET = 'wss';
1716
const MIME_TYPE_JSON = 'application/json';
17+
const MIME_TYPE_JSON_SCHEMA = 'application/schema+json';
1818
const MIME_TYPE_URLENCODED = 'application/x-www-form-urlencoded';
1919
const MIME_TYPE_MULTIPART = 'multipart/form-data';
2020
const PARAM_IN_PATH = 'path';

src/Spec/v3/Encoding.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
*
88
* @see https://swagger.io/specification/#encodingObject
99
*/
10-
class Encoding extends AbstractObject
10+
class Encoding extends AbstractObject implements ExtensibleInterface
1111
{
12-
1312
/**
1413
* The Content-Type for encoding a specific property.
1514
* Default value depends on the property type: for string with format being

src/Spec/v3/Example.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
*
88
* @see https://swagger.io/specification/#exampleObject
99
*/
10-
class Example extends AbstractObject
10+
class Example extends AbstractObject implements ExtensibleInterface
1111
{
12-
1312
/**
1413
* Short description for the example.
1514
*
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace erasys\OpenApi\Spec\v3;
4+
5+
/**
6+
* Marker interface to identify al OpenAPI elements that MAY have
7+
* specification extension properties "x-".
8+
*
9+
* Currently this includes all elements excepting Discriminator and Reference objects.
10+
*/
11+
interface ExtensibleInterface
12+
{
13+
14+
}

src/Spec/v3/Info.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
*
99
* @see https://swagger.io/specification/#infoObject
1010
*/
11-
class Info extends AbstractObject
11+
class Info extends AbstractObject implements ExtensibleInterface
1212
{
13-
1413
/**
1514
* REQUIRED. The title of the application.
1615
*

0 commit comments

Comments
 (0)