Skip to content

Fix openapi sort #3996 #4013

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
Feb 2, 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
5 changes: 5 additions & 0 deletions src/OpenApi/Serializer/OpenApiNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,14 @@ private function recursiveClean($data): array
continue;
}

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

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

Expand Down
22 changes: 21 additions & 1 deletion tests/OpenApi/Serializer/OpenApiNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ class OpenApiNormalizerTest extends TestCase
public function testNormalize()
{
$resourceNameCollectionFactoryProphecy = $this->prophesize(ResourceNameCollectionFactoryInterface::class);
$resourceNameCollectionFactoryProphecy->create()->shouldBeCalled()->willReturn(new ResourceNameCollection([Dummy::class]));
$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('Zorro', Argument::any())->shouldBeCalled()->willReturn(new PropertyNameCollection(['id']));

$dummyMetadata = new ResourceMetadata(
'Dummy',
Expand All @@ -74,18 +75,34 @@ public function testNormalize()
[]
);

$zorroMetadata = new ResourceMetadata(
'Zorro',
'This is zorro.',
'http://schema.example.com/Zorro',
[
'get' => ['method' => 'GET'] + self::OPERATION_FORMATS,
],
[
'get' => ['method' => 'GET'] + self::OPERATION_FORMATS,
],
[]
);

$subresourceOperationFactoryProphecy = $this->prophesize(SubresourceOperationFactoryInterface::class);
$subresourceOperationFactoryProphecy->create(Argument::any(), Argument::any(), Argument::any())->willReturn([]);

$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
$resourceMetadataFactoryProphecy->create(Dummy::class)->shouldBeCalled()->willReturn($dummyMetadata);
$resourceMetadataFactoryProphecy->create('Zorro')->shouldBeCalled()->willReturn($zorroMetadata);

$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
$propertyMetadataFactoryProphecy->create(Dummy::class, 'id', Argument::any())->shouldBeCalled()->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_INT), 'This is an id.', true, false, null, null, null, true, null, null, null, null, null, null, null, ['minLength' => 3, 'maxLength' => 20, 'pattern' => '^dummyPattern$']));
$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));
$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, []));

$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));

$operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator()));
$filterLocatorProphecy = $this->prophesize(ContainerInterface::class);
$resourceMetadataFactory = $resourceMetadataFactoryProphecy->reveal();
Expand Down Expand Up @@ -165,5 +182,8 @@ public function testNormalize()
// Security can be disabled per-operation using an empty array
$this->assertEquals([], $openApiAsArray['paths']['/dummies']['post']['security']);
$this->assertEquals(['url' => '/test'], $openApiAsArray['paths']['/dummies']['post']['servers']);

// Make sure things are sorted
$this->assertEquals(array_keys($openApiAsArray['paths']), ['/dummies', '/dummies/{id}', '/zorros', '/zorros/{id}']);
}
}