Skip to content

Commit

Permalink
Update Swagger's definition keys (more verbose)
Browse files Browse the repository at this point in the history
  • Loading branch information
meyerbaptiste committed Jun 27, 2017
1 parent cadf763 commit ccb7b0b
Show file tree
Hide file tree
Showing 3 changed files with 32 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_groups_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_groups_input" exists
And the Swagger class "CustomNormalizedDummy_groups_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_groups_overridden_operation_dummy_get" exists
And the Swagger class "OverriddenOperationDummy_groups_overridden_operation_dummy_put" exists
And the Swagger class "OverriddenOperationDummy_groups_overridden_operation_dummy_read" exists
And the Swagger class "OverriddenOperationDummy_groups_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_groups_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_groups_barcelona" exists
And the Swagger class "RelationEmbedder_groups_chicago" exists
And the Swagger class "User_groups_user_user-read" exists
And the Swagger class "User_groups_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
17 changes: 12 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'] ?? null));

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

/**
* @param string $resourceShortName
* @param array $groups
*
* @return string
*/
private function getDefinitionKey(string $resourceShortName, array $groups): string
{
return !$groups ? $resourceShortName : $resourceShortName.'_groups_'.implode('_', $groups);
}

/**
* 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_groups_'.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_groups_dummy'],
],
],
'responses' => [
Expand All @@ -723,7 +723,7 @@ public function testNormalizeWithOnlyDenormalizationGroups()
]),
],
]),
'Dummy_be35824b9d92d1dfc6f78fe086649b8f' => new \ArrayObject([
'Dummy_groups_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_groups_dummy'],
],
],
'responses' => [
200 => [
'description' => 'Dummy resource updated',
'schema' => ['$ref' => '#/definitions/Dummy_be35824b9d92d1dfc6f78fe086649b8f'],
'schema' => ['$ref' => '#/definitions/Dummy_groups_dummy'],
],
400 => ['description' => 'Invalid input'],
404 => ['description' => 'Resource not found'],
Expand All @@ -908,7 +908,7 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups()
]),
],
]),
'Dummy_be35824b9d92d1dfc6f78fe086649b8f' => new \ArrayObject([
'Dummy_groups_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_groups_'.implode('_', $groups);
$relatedDummyRef = 'RelatedDummy_groups_'.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 ccb7b0b

Please sign in to comment.