Skip to content

Commit a27ffc4

Browse files
authored
Merge pull request #59 from cebe/fix-writer-yaml-empty-array
Fix writing empty array to YAML
2 parents 6d78a79 + 197ee86 commit a27ffc4

File tree

2 files changed

+108
-4
lines changed

2 files changed

+108
-4
lines changed

src/Writer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static function writeToJson(SpecObjectInterface $object): string
3434
*/
3535
public static function writeToYaml(SpecObjectInterface $object): string
3636
{
37-
return Yaml::dump($object->getSerializableData(), 256, 2, Yaml::DUMP_OBJECT_AS_MAP);
37+
return Yaml::dump($object->getSerializableData(), 256, 2, Yaml::DUMP_OBJECT_AS_MAP | Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
3838
}
3939

4040
/**

tests/WriterTest.php

Lines changed: 107 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
<?php
22

3+
use cebe\openapi\spec\SecurityRequirement;
4+
35
class WriterTest extends \PHPUnit\Framework\TestCase
46
{
5-
private function createOpenAPI()
7+
private function createOpenAPI($merge = [])
68
{
7-
return new \cebe\openapi\spec\OpenApi([
9+
return new \cebe\openapi\spec\OpenApi(array_merge([
810
'openapi' => '3.0.0',
911
'info' => [
1012
'title' => 'Test API',
1113
'version' => '1.0.0',
1214
],
1315
'paths' => [],
14-
]);
16+
], $merge));
1517
}
1618

1719
public function testWriteJson()
@@ -78,6 +80,108 @@ public function testWriteYaml()
7880
version: 1.0.0
7981
paths: { }
8082
83+
YAML
84+
),
85+
$yaml
86+
);
87+
}
88+
89+
public function testWriteEmptySecurityJson()
90+
{
91+
$openapi = $this->createOpenAPI([
92+
'security' => [],
93+
]);
94+
95+
$json = \cebe\openapi\Writer::writeToJson($openapi);
96+
97+
$this->assertEquals(preg_replace('~\R~', "\n", <<<JSON
98+
{
99+
"openapi": "3.0.0",
100+
"info": {
101+
"title": "Test API",
102+
"version": "1.0.0"
103+
},
104+
"paths": {},
105+
"security": []
106+
}
107+
JSON
108+
),
109+
$json
110+
);
111+
}
112+
113+
114+
public function testWriteEmptySecurityYaml()
115+
{
116+
$openapi = $this->createOpenAPI([
117+
'security' => [],
118+
]);
119+
120+
$yaml = \cebe\openapi\Writer::writeToYaml($openapi);
121+
122+
123+
$this->assertEquals(preg_replace('~\R~', "\n", <<<YAML
124+
openapi: 3.0.0
125+
info:
126+
title: 'Test API'
127+
version: 1.0.0
128+
paths: { }
129+
security: []
130+
131+
YAML
132+
),
133+
$yaml
134+
);
135+
}
136+
137+
public function testWriteEmptySecurityPartJson()
138+
{
139+
$openapi = $this->createOpenAPI([
140+
'security' => [new SecurityRequirement(['Bearer' => []])],
141+
]);
142+
143+
$json = \cebe\openapi\Writer::writeToJson($openapi);
144+
145+
$this->assertEquals(preg_replace('~\R~', "\n", <<<JSON
146+
{
147+
"openapi": "3.0.0",
148+
"info": {
149+
"title": "Test API",
150+
"version": "1.0.0"
151+
},
152+
"paths": {},
153+
"security": [
154+
{
155+
"Bearer": []
156+
}
157+
]
158+
}
159+
JSON
160+
),
161+
$json
162+
);
163+
}
164+
165+
166+
public function testWriteEmptySecurityPartYaml()
167+
{
168+
$openapi = $this->createOpenAPI([
169+
'security' => [new SecurityRequirement(['Bearer' => []])],
170+
]);
171+
172+
$yaml = \cebe\openapi\Writer::writeToYaml($openapi);
173+
174+
175+
$this->assertEquals(preg_replace('~\R~', "\n", <<<YAML
176+
openapi: 3.0.0
177+
info:
178+
title: 'Test API'
179+
version: 1.0.0
180+
paths: { }
181+
security:
182+
-
183+
Bearer: []
184+
81185
YAML
82186
),
83187
$yaml

0 commit comments

Comments
 (0)