Skip to content

Commit

Permalink
fix-openapi-subresource
Browse files Browse the repository at this point in the history
  • Loading branch information
GregoireHebert committed Aug 28, 2020
1 parent 158338b commit ffd05b5
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
9 changes: 5 additions & 4 deletions src/Operation/Factory/SubresourceOperationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,21 @@ private function computeSubresourceOperations(string $resourceClass, array &$tre
if (null === $parentOperation) {
$rootShortname = $rootResourceMetadata->getShortName();
$operation['identifiers'] = [['id', $rootResourceClass, true]];
$operation['operation_name'] = sprintf(

$suffix = sprintf(
'%s_%s%s',
RouteNameGenerator::inflector($operation['property'], $operation['collection'] ?? false),
$operationName,
self::SUBRESOURCE_SUFFIX
);

$subresourceOperation = $rootResourceMetadata->getSubresourceOperations()[$operation['operation_name']] ?? [];
$subresourceOperation = $rootResourceMetadata->getSubresourceOperations()[$suffix] ?? [];

$operation['route_name'] = sprintf(
$operation['operation_name'] = $operation['route_name'] = sprintf(
'%s%s_%s',
RouteNameGenerator::ROUTE_NAME_PREFIX,
RouteNameGenerator::inflector($rootShortname),
$operation['operation_name']
$suffix
);

$prefix = trim(trim($rootResourceMetadata->getAttribute('route_prefix', '')), '/');
Expand Down
2 changes: 1 addition & 1 deletion src/Swagger/Serializer/DocumentationNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ private function addPaginationParameters(bool $v3, ResourceMetadata $resourceMet
*/
private function addSubresourceOperation(bool $v3, array $subresourceOperation, \ArrayObject $definitions, string $operationId, ResourceMetadata $resourceMetadata): \ArrayObject
{
$operationName = 'get'; // TODO: we might want to extract that at some point to also support other subresource operations
$operationName = $subresourceOperation['operation_name']; // TODO: we might want to extract that at some point to also support other subresource operations
$collection = $subresourceOperation['collection'] ?? false;

$subResourceMetadata = $this->resourceMetadataFactory->create($subresourceOperation['resource_class']);
Expand Down
4 changes: 2 additions & 2 deletions tests/Swagger/Serializer/DocumentationNormalizerV2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2159,7 +2159,7 @@ public function testLegacyNormalizeWithSubResource(): void
{
$formatProviderProphecy = $this->prophesize(OperationAwareFormatsProviderInterface::class);
$formatProviderProphecy->getFormatsFromOperation(Question::class, 'get', OperationType::ITEM)->willReturn(['json' => ['application/json'], 'csv' => ['text/csv']]);
$formatProviderProphecy->getFormatsFromOperation(Answer::class, 'get', OperationType::SUBRESOURCE)->willReturn(['xml' => ['text/xml']]);
$formatProviderProphecy->getFormatsFromOperation(Answer::class, 'api_questions_answer_get_subresource', OperationType::SUBRESOURCE)->willReturn(['xml' => ['text/xml']]);

$this->doTestNormalizeWithSubResource($formatProviderProphecy->reveal());
}
Expand All @@ -2185,7 +2185,7 @@ private function doTestNormalizeWithSubResource(OperationAwareFormatsProviderInt
[],
['get' => ['method' => 'GET']] + self::OPERATION_FORMATS,
[],
['get' => ['method' => 'GET', 'input_formats' => ['xml' => ['text/xml']], 'output_formats' => ['xml' => ['text/xml']]]]
['api_questions_answer_get_subresource' => ['method' => 'GET', 'input_formats' => ['xml' => ['text/xml']], 'output_formats' => ['xml' => ['text/xml']]]]
);
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
$resourceMetadataFactoryProphecy->create(Question::class)->willReturn($questionMetadata);
Expand Down
4 changes: 2 additions & 2 deletions tests/Swagger/Serializer/DocumentationNormalizerV3Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2189,7 +2189,7 @@ public function testLegacytNormalizeWithSubResource(): void
{
$formatProviderProphecy = $this->prophesize(OperationAwareFormatsProviderInterface::class);
$formatProviderProphecy->getFormatsFromOperation(Question::class, 'get', OperationType::ITEM)->willReturn(['json' => ['application/json'], 'csv' => ['text/csv']]);
$formatProviderProphecy->getFormatsFromOperation(Answer::class, 'get', OperationType::SUBRESOURCE)->willReturn(['xml' => ['text/xml']]);
$formatProviderProphecy->getFormatsFromOperation(Answer::class, 'api_questions_answer_get_subresource', OperationType::SUBRESOURCE)->willReturn(['xml' => ['text/xml']]);

$this->doTestNormalizeWithSubResource($formatProviderProphecy->reveal());
}
Expand All @@ -2215,7 +2215,7 @@ private function doTestNormalizeWithSubResource(OperationAwareFormatsProviderInt
[],
['get' => ['method' => 'GET']] + self::OPERATION_FORMATS,
[],
['get' => ['method' => 'GET', 'input_formats' => ['xml' => ['text/xml']], 'output_formats' => ['xml' => ['text/xml']]]]
['api_questions_answer_get_subresource' => ['method' => 'GET', 'input_formats' => ['xml' => ['text/xml']], 'output_formats' => ['xml' => ['text/xml']]]]
);
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
$resourceMetadataFactoryProphecy->create(Question::class)->willReturn($questionMetadata);
Expand Down

0 comments on commit ffd05b5

Please sign in to comment.