Skip to content

Commit

Permalink
Merge pull request api-platform#1207 from meyerbaptiste/update_defini…
Browse files Browse the repository at this point in the history
…tion_key

[Proposal] Update Swagger's definition keys (more verbose)
  • Loading branch information
dunglas committed Jun 29, 2017
2 parents 5ee98de + 3b1c021 commit 4b74f92
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
24 changes: 12 additions & 12 deletions features/swagger/docs.feature
Expand Up @@ -17,31 +17,31 @@ Feature: Documentation support
# Supported classes
And the Swagger class "AbstractDummy" exists
And the Swagger class "CircularReference" exists
And the Swagger class "CircularReference_a0dd2858dcb0d966f739c1ac906afa2e" exists
And the Swagger class "CircularReference-circular" exists
And the Swagger class "CompositeItem" exists
And the Swagger class "CompositeLabel" exists
And the Swagger class "ConcreteDummy" exists
And the Swagger class "CustomIdentifierDummy" exists
And the Swagger class "CustomNormalizedDummy_601856395b57c6b15175297eb6c9890e" exists
And the Swagger class "CustomNormalizedDummy_db9cba1a967111a02380774784c47722" exists
And the Swagger class "CustomNormalizedDummy-input" exists
And the Swagger class "CustomNormalizedDummy-output" exists
And the Swagger class "CustomWritableIdentifierDummy" exists
And the Swagger class "Dummy" exists
And the Swagger class "RelatedDummy" exists
And the Swagger class "DummyTableInheritance" exists
And the Swagger class "DummyTableInheritanceChild" exists
And the Swagger class "OverriddenOperationDummy_441e1f98db3d0250bcb18dca087687c3" exists
And the Swagger class "OverriddenOperationDummy_45f46ed6dc6f412229a8c12cd5583586" exists
And the Swagger class "OverriddenOperationDummy_868796b9924a520acbb96f8b75dade9f" exists
And the Swagger class "OverriddenOperationDummy_ff74003f36aebfe31c696fae1f701ae4" exists
And the Swagger class "OverriddenOperationDummy-overridden_operation_dummy_get" exists
And the Swagger class "OverriddenOperationDummy-overridden_operation_dummy_put" exists
And the Swagger class "OverriddenOperationDummy-overridden_operation_dummy_read" exists
And the Swagger class "OverriddenOperationDummy-overridden_operation_dummy_write" exists
And the Swagger class "RelatedDummy" exists
And the Swagger class "NoCollectionDummy" exists
And the Swagger class "RelatedToDummyFriend" exists
And the Swagger class "RelatedToDummyFriend_ad38b7a2760884e744c577a92e02b8c4" exists
And the Swagger class "RelatedToDummyFriend-fakemanytomany" exists
And the Swagger class "DummyFriend" exists
And the Swagger class "RelationEmbedder_ced9cba177bf3134e609fccf878df9a7" exists
And the Swagger class "RelationEmbedder_f02fd88a2291463447338402aee9a220" exists
And the Swagger class "User_4320517091b72c69e9f0c72aac0141e8" exists
And the Swagger class "User_7ce91261c0e731d95bb24b83b1f637b2" exists
And the Swagger class "RelationEmbedder-barcelona" exists
And the Swagger class "RelationEmbedder-chicago" exists
And the Swagger class "User-user_user-read" exists
And the Swagger class "User-user_user-write" exists
And the Swagger class "UuidIdentifierDummy" exists
And the Swagger class "ThirdLevel" exists
And the Swagger class "ParentDummy" doesn't exist
Expand Down
11 changes: 6 additions & 5 deletions src/Swagger/Serializer/DocumentationNormalizer.php
Expand Up @@ -371,11 +371,7 @@ private function updateDeleteOperation(\ArrayObject $pathOperation, string $reso
*/
private function getDefinition(\ArrayObject $definitions, ResourceMetadata $resourceMetadata, string $resourceClass, array $serializerContext = null): string
{
if (isset($serializerContext['groups'])) {
$definitionKey = sprintf('%s_%s', $resourceMetadata->getShortName(), md5(serialize($serializerContext['groups'])));
} else {
$definitionKey = $resourceMetadata->getShortName();
}
$definitionKey = $this->getDefinitionKey($resourceMetadata->getShortName(), (array) ($serializerContext['groups'] ?? []));

if (!isset($definitions[$definitionKey])) {
$definitions[$definitionKey] = []; // Initialize first to prevent infinite loop
Expand All @@ -385,6 +381,11 @@ private function getDefinition(\ArrayObject $definitions, ResourceMetadata $reso
return $definitionKey;
}

private function getDefinitionKey(string $resourceShortName, array $groups): string
{
return $groups ? sprintf('%s-%s', $resourceShortName, implode('_', $groups)) : $resourceShortName;
}

/**
* Gets a definition Schema Object.
*
Expand Down
16 changes: 8 additions & 8 deletions tests/Swagger/Serializer/DocumentationNormalizerTest.php
Expand Up @@ -382,7 +382,7 @@ public function testNormalizeWithOnlyNormalizationGroups()
$documentation = new Documentation(new ResourceNameCollection([Dummy::class]), $title, $description, $version, $formats);
$groups = ['dummy', 'foo', 'bar'];

$ref = 'Dummy_'.md5(serialize($groups));
$ref = 'Dummy-'.implode('_', $groups);

$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
$propertyNameCollectionFactoryProphecy->create(Dummy::class, ['serializer_groups' => $groups])->shouldBeCalled(1)->willReturn(new PropertyNameCollection(['gerard']));
Expand Down Expand Up @@ -697,7 +697,7 @@ public function testNormalizeWithOnlyDenormalizationGroups()
'name' => 'dummy',
'in' => 'body',
'description' => 'The updated Dummy resource',
'schema' => ['$ref' => '#/definitions/Dummy_be35824b9d92d1dfc6f78fe086649b8f'],
'schema' => ['$ref' => '#/definitions/Dummy-dummy'],
],
],
'responses' => [
Expand All @@ -723,7 +723,7 @@ public function testNormalizeWithOnlyDenormalizationGroups()
]),
],
]),
'Dummy_be35824b9d92d1dfc6f78fe086649b8f' => new \ArrayObject([
'Dummy-dummy' => new \ArrayObject([
'type' => 'object',
'description' => 'This is a dummy.',
'externalDocs' => ['url' => 'http://schema.example.com/Dummy'],
Expand Down Expand Up @@ -882,13 +882,13 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups()
'name' => 'dummy',
'in' => 'body',
'description' => 'The updated Dummy resource',
'schema' => ['$ref' => '#/definitions/Dummy_be35824b9d92d1dfc6f78fe086649b8f'],
'schema' => ['$ref' => '#/definitions/Dummy-dummy'],
],
],
'responses' => [
200 => [
'description' => 'Dummy resource updated',
'schema' => ['$ref' => '#/definitions/Dummy_be35824b9d92d1dfc6f78fe086649b8f'],
'schema' => ['$ref' => '#/definitions/Dummy-dummy'],
],
400 => ['description' => 'Invalid input'],
404 => ['description' => 'Resource not found'],
Expand All @@ -908,7 +908,7 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups()
]),
],
]),
'Dummy_be35824b9d92d1dfc6f78fe086649b8f' => new \ArrayObject([
'Dummy-dummy' => new \ArrayObject([
'type' => 'object',
'description' => 'This is a dummy.',
'externalDocs' => ['url' => 'http://schema.example.com/Dummy'],
Expand Down Expand Up @@ -1136,8 +1136,8 @@ public function testNormalizeWithNestedNormalizationGroups()
$version = '1.2.3';
$documentation = new Documentation(new ResourceNameCollection([Dummy::class]), $title, $description, $version, $formats);
$groups = ['dummy', 'foo', 'bar'];
$ref = 'Dummy_'.md5(serialize($groups));
$relatedDummyRef = 'RelatedDummy_'.md5(serialize($groups));
$ref = 'Dummy-'.implode('_', $groups);
$relatedDummyRef = 'RelatedDummy-'.implode('_', $groups);

$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
$propertyNameCollectionFactoryProphecy->create(Dummy::class, ['serializer_groups' => $groups])->shouldBeCalled(1)->willReturn(new PropertyNameCollection(['name', 'relatedDummy']));
Expand Down

0 comments on commit 4b74f92

Please sign in to comment.