Skip to content

Fix writing empty array to YAML #59

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

Merged
merged 1 commit into from
Mar 6, 2020
Merged
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
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