Skip to content

Commit

Permalink
Merge pull request #59 from cebe/fix-writer-yaml-empty-array
Browse files Browse the repository at this point in the history
Fix writing empty array to YAML
  • Loading branch information
cebe authored Mar 6, 2020
2 parents 6d78a79 + 197ee86 commit a27ffc4
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static function writeToJson(SpecObjectInterface $object): string
*/
public static function writeToYaml(SpecObjectInterface $object): string
{
return Yaml::dump($object->getSerializableData(), 256, 2, Yaml::DUMP_OBJECT_AS_MAP);
return Yaml::dump($object->getSerializableData(), 256, 2, Yaml::DUMP_OBJECT_AS_MAP | Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
}

/**
Expand Down
110 changes: 107 additions & 3 deletions tests/WriterTest.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
<?php

use cebe\openapi\spec\SecurityRequirement;

class WriterTest extends \PHPUnit\Framework\TestCase
{
private function createOpenAPI()
private function createOpenAPI($merge = [])
{
return new \cebe\openapi\spec\OpenApi([
return new \cebe\openapi\spec\OpenApi(array_merge([
'openapi' => '3.0.0',
'info' => [
'title' => 'Test API',
'version' => '1.0.0',
],
'paths' => [],
]);
], $merge));
}

public function testWriteJson()
Expand Down Expand Up @@ -78,6 +80,108 @@ public function testWriteYaml()
version: 1.0.0
paths: { }
YAML
),
$yaml
);
}

public function testWriteEmptySecurityJson()
{
$openapi = $this->createOpenAPI([
'security' => [],
]);

$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": {},
"security": []
}
JSON
),
$json
);
}


public function testWriteEmptySecurityYaml()
{
$openapi = $this->createOpenAPI([
'security' => [],
]);

$yaml = \cebe\openapi\Writer::writeToYaml($openapi);


$this->assertEquals(preg_replace('~\R~', "\n", <<<YAML
openapi: 3.0.0
info:
title: 'Test API'
version: 1.0.0
paths: { }
security: []
YAML
),
$yaml
);
}

public function testWriteEmptySecurityPartJson()
{
$openapi = $this->createOpenAPI([
'security' => [new SecurityRequirement(['Bearer' => []])],
]);

$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": {},
"security": [
{
"Bearer": []
}
]
}
JSON
),
$json
);
}


public function testWriteEmptySecurityPartYaml()
{
$openapi = $this->createOpenAPI([
'security' => [new SecurityRequirement(['Bearer' => []])],
]);

$yaml = \cebe\openapi\Writer::writeToYaml($openapi);


$this->assertEquals(preg_replace('~\R~', "\n", <<<YAML
openapi: 3.0.0
info:
title: 'Test API'
version: 1.0.0
paths: { }
security:
-
Bearer: []
YAML
),
$yaml
Expand Down

0 comments on commit a27ffc4

Please sign in to comment.