Skip to content

Commit 11bea88

Browse files
committed
change default value handling
do not change schema by adding default values but return these on __get() call instead. fixes #19
1 parent dbdd965 commit 11bea88

File tree

6 files changed

+115
-24
lines changed

6 files changed

+115
-24
lines changed

src/SpecBaseObject.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ abstract class SpecBaseObject implements SpecObjectInterface
2929
*/
3030
abstract protected function attributes(): array;
3131

32+
/**
33+
* @return array array of attributes default values.
34+
*/
35+
protected function attributeDefaults(): array
36+
{
37+
return [];
38+
}
39+
3240
/**
3341
* Perform validation on this object, check data against OpenAPI Specification rules.
3442
*
@@ -241,6 +249,9 @@ public function __get($name)
241249
if (isset($this->_properties[$name])) {
242250
return $this->_properties[$name];
243251
}
252+
if (isset(static::attributeDefaults()[$name])) {
253+
return static::attributeDefaults()[$name];
254+
}
244255
if (isset(static::attributes()[$name])) {
245256
if (is_array(static::attributes()[$name])) {
246257
return [];
@@ -259,7 +270,7 @@ public function __set($name, $value)
259270

260271
public function __isset($name)
261272
{
262-
if (isset($this->_properties[$name]) || isset(static::attributes()[$name])) {
273+
if (isset($this->_properties[$name]) || isset(static::attributeDefaults()[$name]) || isset(static::attributes()[$name])) {
263274
return $this->__get($name) !== null;
264275
}
265276

src/spec/OpenApi.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,28 @@ protected function attributes(): array
4545
}
4646

4747
/**
48-
* Create an object from spec data.
49-
* @param array $data spec data read from YAML or JSON
50-
* @throws TypeErrorException in case invalid data is supplied.
48+
* @return array array of attributes default values.
5149
*/
52-
public function __construct(array $data)
50+
protected function attributeDefaults(): array
5351
{
54-
if (empty($data['servers'])) {
55-
// Spec: If the servers property is not provided, or is an empty array, the default value would be a Server Object with a url value of /.
56-
$data['servers'] = [
57-
['url' => '/'],
58-
];
52+
return [
53+
// Spec: If the servers property is not provided, or is an empty array,
54+
// the default value would be a Server Object with a url value of /.
55+
'servers' => [
56+
new Server(['url' => '/'])
57+
],
58+
];
59+
}
60+
61+
public function __get($name)
62+
{
63+
$ret = parent::__get($name);
64+
// Spec: If the servers property is not provided, or is an empty array,
65+
// the default value would be a Server Object with a url value of /.
66+
if ($name === 'servers' && $ret === []) {
67+
return $this->attributeDefaults()['servers'];
5968
}
60-
parent::__construct($data);
69+
return $ret;
6170
}
6271

6372
/**

src/spec/Parameter.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,24 @@ protected function attributes(): array
5757
];
5858
}
5959

60+
private $_attributeDefaults = [];
61+
62+
/**
63+
* @return array array of attributes default values.
64+
*/
65+
protected function attributeDefaults(): array
66+
{
67+
return $this->_attributeDefaults;
68+
}
69+
6070
/**
6171
* Create an object from spec data.
6272
* @param array $data spec data read from YAML or JSON
6373
* @throws TypeErrorException in case invalid data is supplied.
6474
*/
6575
public function __construct(array $data)
6676
{
67-
if (!isset($data['style']) && isset($data['in'])) {
77+
if (isset($data['in'])) {
6878
// Spec: Default values (based on value of in):
6979
// for query - form;
7080
// for path - simple;
@@ -73,17 +83,19 @@ public function __construct(array $data)
7383
switch ($data['in']) {
7484
case 'query':
7585
case 'cookie':
76-
$data['style'] = 'form';
86+
$this->_attributeDefaults['style'] = 'form';
87+
$this->_attributeDefaults['explode'] = true;
7788
break;
7889
case 'path':
7990
case 'header':
80-
$data['style'] = 'simple';
91+
$this->_attributeDefaults['style'] = 'simple';
92+
$this->_attributeDefaults['explode'] = false;
8193
break;
8294
}
8395
}
84-
if (!isset($data['explode']) && isset($data['style'])) {
96+
if (isset($data['style'])) {
8597
// Spec: When style is form, the default value is true. For all other styles, the default value is false.
86-
$data['explode'] = ($data['style'] === 'form');
98+
$this->_attributeDefaults['explode'] = ($data['style'] === 'form');
8799
}
88100
parent::__construct($data);
89101
}

src/spec/Schema.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ protected function attributes(): array
9393
];
9494
}
9595

96+
/**
97+
* @return array array of attributes default values.
98+
*/
99+
protected function attributeDefaults(): array
100+
{
101+
return [
102+
'additionalProperties' => true,
103+
];
104+
}
105+
96106
/**
97107
* Create an object from spec data.
98108
* @param array $data spec data read from YAML or JSON
@@ -112,9 +122,6 @@ public function __construct(array $data)
112122
);
113123
}
114124
}
115-
} else {
116-
// additionalProperties defaults to true.
117-
$data['additionalProperties'] = true;
118125
}
119126
parent::__construct($data);
120127
}

tests/ReaderTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
<?php
22

3-
/**
4-
*
5-
*
6-
* @author Carsten Brandt <[email protected]>
7-
*/
83
class ReaderTest extends \PHPUnit\Framework\TestCase
94
{
105
public function testReadJson()

tests/WriterTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
class WriterTest extends \PHPUnit\Framework\TestCase
4+
{
5+
private function createOpenAPI()
6+
{
7+
return new \cebe\openapi\spec\OpenApi([
8+
'openapi' => '3.0.0',
9+
'info' => [
10+
'title' => 'Test API',
11+
'version' => '1.0.0',
12+
],
13+
'paths' => [],
14+
]);
15+
}
16+
17+
public function testWriteJson()
18+
{
19+
$openapi = $this->createOpenAPI();
20+
21+
$json = \cebe\openapi\Writer::writeToJson($openapi);
22+
23+
$this->assertEquals(preg_replace('~\R~', "\n", <<<JSON
24+
{
25+
"openapi": "3.0.0",
26+
"info": {
27+
"title": "Test API",
28+
"version": "1.0.0"
29+
},
30+
"paths": {}
31+
}
32+
JSON
33+
),
34+
$json
35+
);
36+
}
37+
38+
public function testWriteYaml()
39+
{
40+
$openapi = $this->createOpenAPI();
41+
42+
$yaml = \cebe\openapi\Writer::writeToYaml($openapi);
43+
44+
45+
$this->assertEquals(preg_replace('~\R~', "\n", <<<YAML
46+
openapi: 3.0.0
47+
info:
48+
title: 'Test API'
49+
version: 1.0.0
50+
paths: { }
51+
52+
YAML
53+
),
54+
$yaml
55+
);
56+
}
57+
}

0 commit comments

Comments
 (0)