Skip to content

Commit

Permalink
Merge 4435484 into fa87557
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Feb 11, 2021
2 parents fa87557 + 4435484 commit 622b39a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/OpenApi/Serializer/OpenApiNormalizer.php
Expand Up @@ -54,10 +54,8 @@ private function recursiveClean($data): array
continue;
}

if ('schemas' === $key) {
if ($value) {
ksort($value);
}
if ('schemas' === $key && \is_array($value)) {
ksort($value);
}

// Side effect of using getPaths(): Paths which itself contains the array
Expand Down
32 changes: 32 additions & 0 deletions tests/OpenApi/Serializer/OpenApiNormalizerTest.php
Expand Up @@ -27,6 +27,7 @@
use ApiPlatform\Core\Metadata\Resource\ResourceNameCollection;
use ApiPlatform\Core\OpenApi\Factory\OpenApiFactory;
use ApiPlatform\Core\OpenApi\Model;
use ApiPlatform\Core\OpenApi\OpenApi;
use ApiPlatform\Core\OpenApi\Options;
use ApiPlatform\Core\OpenApi\Serializer\OpenApiNormalizer;
use ApiPlatform\Core\Operation\Factory\SubresourceOperationFactoryInterface;
Expand Down Expand Up @@ -188,4 +189,35 @@ public function testNormalize()
// Test name converter doesn't rename this property
$this->assertArrayHasKey('requestBody', $openApiAsArray['paths']['/dummies']['post']);
}

public function testNormalizeWithSchemas()
{
$openApi = new OpenApi(new Model\Info('My API', '1.0.0', 'An amazing API'), [new Model\Server('https://example.com')], new Model\Paths(), new Model\Components(new \ArrayObject(['z' => [], 'b' => []])));
$encoders = [new JsonEncoder()];
$normalizers = [new ObjectNormalizer()];

$serializer = new Serializer($normalizers, $encoders);
$normalizers[0]->setSerializer($serializer);

$normalizer = new OpenApiNormalizer($normalizers[0]);

$array = $normalizer->normalize($openApi);

$this->assertEquals(array_keys($array['components']['schemas']), ['b', 'z']);
}

public function testNormalizeWithEmptySchemas()
{
$openApi = new OpenApi(new Model\Info('My API', '1.0.0', 'An amazing API'), [new Model\Server('https://example.com')], new Model\Paths(), new Model\Components(new \ArrayObject()));
$encoders = [new JsonEncoder()];
$normalizers = [new ObjectNormalizer()];

$serializer = new Serializer($normalizers, $encoders);
$normalizers[0]->setSerializer($serializer);

$normalizer = new OpenApiNormalizer($normalizers[0]);

$array = $normalizer->normalize($openApi);
$this->assertCount(0, $array['components']['schemas']);
}
}

0 comments on commit 622b39a

Please sign in to comment.