Skip to content
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

Fix openapi normalization #4325

Merged
merged 3 commits into from
Jun 25, 2021
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
4 changes: 4 additions & 0 deletions src/OpenApi/Model/Components.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ final class Components

public function __construct(\ArrayObject $schemas = null, \ArrayObject $responses = null, \ArrayObject $parameters = null, \ArrayObject $examples = null, \ArrayObject $requestBodies = null, \ArrayObject $headers = null, \ArrayObject $securitySchemes = null, \ArrayObject $links = null, \ArrayObject $callbacks = null)
{
if ($schemas) {
$schemas->ksort();
}

$this->schemas = $schemas;
$this->responses = $responses;
$this->parameters = $parameters;
Expand Down
2 changes: 2 additions & 0 deletions src/OpenApi/Model/Paths.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ final class Paths
public function addPath(string $path, PathItem $pathItem)
{
$this->paths[$path] = $pathItem;

ksort($this->paths);
}

public function getPath(string $path): ?PathItem
Expand Down
22 changes: 7 additions & 15 deletions src/OpenApi/Serializer/OpenApiNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace ApiPlatform\Core\OpenApi\Serializer;

use ApiPlatform\Core\OpenApi\Model\Paths;
use ApiPlatform\Core\OpenApi\OpenApi;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
Expand All @@ -38,8 +39,14 @@ public function __construct(NormalizerInterface $decorated)
*/
public function normalize($object, $format = null, array $context = []): array
{
$pathsCallback = static function ($innerObject) {
return $innerObject instanceof Paths ? $innerObject->getPaths() : [];
};
$context[AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS] = true;
$context[AbstractObjectNormalizer::SKIP_NULL_VALUES] = true;
$context[AbstractObjectNormalizer::CALLBACKS] = [
'paths' => $pathsCallback,
];

return $this->recursiveClean($this->decorated->normalize($object, $format, $context));
}
Expand All @@ -54,23 +61,8 @@ private function recursiveClean($data): array
continue;
}

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

// Side effect of using getPaths(): Paths which itself contains the array
if ('paths' === $key) {
$value = $data['paths'] = $data['paths']['paths'];
if ($value) {
ksort($value);
}
unset($data['paths']['paths']);
}

if (\is_array($value)) {
$data[$key] = $this->recursiveClean($value);
// arrays must stay even if empty
continue;
}
}

Expand Down
4 changes: 3 additions & 1 deletion tests/OpenApi/Serializer/OpenApiNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function testNormalize()
$resourceNameCollectionFactoryProphecy->create()->shouldBeCalled()->willReturn(new ResourceNameCollection([Dummy::class, 'Zorro']));
$defaultContext = ['base_url' => '/app_dev.php/'];
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
$propertyNameCollectionFactoryProphecy->create(Dummy::class, Argument::any())->shouldBeCalled()->willReturn(new PropertyNameCollection(['id', 'name', 'description', 'dummyDate']));
$propertyNameCollectionFactoryProphecy->create(Dummy::class, Argument::any())->shouldBeCalled()->willReturn(new PropertyNameCollection(['id', 'name', 'description', 'dummyDate', 'paths']));
$propertyNameCollectionFactoryProphecy->create('Zorro', Argument::any())->shouldBeCalled()->willReturn(new PropertyNameCollection(['id']));

$dummyMetadata = new ResourceMetadata(
Expand Down Expand Up @@ -104,6 +104,8 @@ public function testNormalize()
$propertyMetadataFactoryProphecy->create(Dummy::class, 'name', Argument::any())->shouldBeCalled()->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING), 'This is a name.', true, true, true, true, false, false, null, null, [], null, null, null, null, ['minLength' => 3, 'maxLength' => 20, 'pattern' => '^dummyPattern$']));
$propertyMetadataFactoryProphecy->create(Dummy::class, 'description', Argument::any())->shouldBeCalled()->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING), 'This is an initializable but not writable property.', true, false, true, true, false, false, null, null, [], null, true));
$propertyMetadataFactoryProphecy->create(Dummy::class, 'dummyDate', Argument::any())->shouldBeCalled()->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_OBJECT, true, \DateTime::class), 'This is a \DateTimeInterface object.', true, true, true, true, false, false, null, null, []));
// Check reserved word "paths": when normalize->recursiveClean in OpenApi Component Schema.
$propertyMetadataFactoryProphecy->create(Dummy::class, 'paths', Argument::any())->shouldBeCalled()->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_ARRAY), 'This is a array.', true, true, true, true, false, false, null, null, []));

$propertyMetadataFactoryProphecy->create('Zorro', 'id', Argument::any())->shouldBeCalled()->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_INT), 'This is an id.', true, false, null, null, null, true));

Expand Down