Skip to content

Commit 1dd4bdb

Browse files
author
abluchet
committed
Correct prefix path swagger subresources
1 parent 6c7f7db commit 1dd4bdb

File tree

5 files changed

+18
-8
lines changed

5 files changed

+18
-8
lines changed

src/Bridge/Symfony/Routing/RouterOperationPathResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function resolveOperationPath(string $resourceShortName, array $operation
5353
if (isset($operation['route_name'])) {
5454
$routeName = $operation['route_name'];
5555
} elseif (OperationType::SUBRESOURCE === $operationType) {
56-
throw new InvalidArgumentException('Subresource operations are not supported by the RouterOperationPathResolver.');
56+
throw new InvalidArgumentException('Subresource operations are not supported by the RouterOperationPathResolver without a route name.');
5757
} else {
5858
if (null !== $operationName) {
5959
$routeName = RouteNameGenerator::generate($operationName, $resourceShortName, $operationType);

src/Operation/PathSegmentNameGeneratorInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ interface PathSegmentNameGeneratorInterface
2323
/**
2424
* Transforms a given string to a valid path name which can be pluralized (eg. for collections).
2525
*
26-
* @param string $name usually a ResourceMetadata shortname
26+
* @param string $name usually a ResourceMetadata shortname
2727
* @param bool $collection
2828
*
2929
* @return string A string that is a part of the route name

src/Swagger/Serializer/DocumentationNormalizer.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
2626
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
2727
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
28-
use ApiPlatform\Core\Operation\Factory\SubresourceOperationFactory;
2928
use ApiPlatform\Core\Operation\Factory\SubresourceOperationFactoryInterface;
3029
use ApiPlatform\Core\PathResolver\OperationPathResolverInterface;
3130
use Psr\Container\ContainerInterface;
@@ -140,7 +139,7 @@ public function normalize($object, $format = null, array $context = [])
140139
}
141140
}
142141

143-
$paths[str_replace(SubresourceOperationFactory::FORMAT_SUFFIX, '', $subresourceOperation['path'])] = new \ArrayObject(['get' => $pathOperation]);
142+
$paths[$this->getPath($subresourceOperation['shortNames'][0], $subresourceOperation['route_name'], $subresourceOperation, OperationType::SUBRESOURCE)] = new \ArrayObject(['get' => $pathOperation]);
144143
}
145144
}
146145

tests/PathResolver/CustomOperationPathResolverTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
/**
2121
* @author Baptiste Meyer <[email protected]>
22-
* @group legacy
2322
*/
2423
class CustomOperationPathResolverTest extends \PHPUnit_Framework_TestCase
2524
{
@@ -41,6 +40,7 @@ public function testResolveOperationPathWithDeferred()
4140
}
4241

4342
/**
43+
* @group legacy
4444
* @expectedDeprecation Using a boolean for the Operation Type is deprecrated since API Platform 2.1 and will not be possible anymore in API Platform 3
4545
*/
4646
public function testLegacyResolveOperationPath()

tests/Swagger/Serializer/DocumentationNormalizerTest.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use ApiPlatform\Core\Api\OperationMethodResolverInterface;
1818
use ApiPlatform\Core\Api\ResourceClassResolverInterface;
1919
use ApiPlatform\Core\Api\UrlGeneratorInterface;
20+
use ApiPlatform\Core\Bridge\Symfony\Routing\RouterOperationPathResolver;
2021
use ApiPlatform\Core\Documentation\Documentation;
2122
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
2223
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
@@ -41,6 +42,9 @@
4142
use Prophecy\Argument;
4243
use Psr\Container\ContainerInterface;
4344
use Symfony\Component\PropertyInfo\Type;
45+
use Symfony\Component\Routing\Route;
46+
use Symfony\Component\Routing\RouteCollection;
47+
use Symfony\Component\Routing\RouterInterface;
4448
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
4549

4650
/**
@@ -1507,7 +1511,14 @@ public function testNormalizeWithSubResource()
15071511
$operationMethodResolverProphecy = $this->prophesize(OperationMethodResolverInterface::class);
15081512
$operationMethodResolverProphecy->getItemOperationMethod(Question::class, 'get')->shouldBeCalled()->willReturn('GET');
15091513

1510-
$operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator()));
1514+
$routeCollection = new RouteCollection();
1515+
$routeCollection->add('api_questions_answer_get_subresource', new Route('/api/questions/{id}/answer.{_format}'));
1516+
$routeCollection->add('api_questions_get_item', new Route('/api/questions/{id}.{_format}'));
1517+
1518+
$routerProphecy = $this->prophesize(RouterInterface::class);
1519+
$routerProphecy->getRouteCollection()->shouldBeCalled()->willReturn($routeCollection);
1520+
1521+
$operationPathResolver = new RouterOperationPathResolver($routerProphecy->reveal(), new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())));
15111522

15121523
$resourceMetadataFactory = $resourceMetadataFactoryProphecy->reveal();
15131524
$propertyNameCollectionFactory = $propertyNameCollectionFactoryProphecy->reveal();
@@ -1535,7 +1546,7 @@ public function testNormalizeWithSubResource()
15351546
'version' => '1.2.3',
15361547
],
15371548
'paths' => new \ArrayObject([
1538-
'/questions/{id}' => [
1549+
'/api/questions/{id}' => [
15391550
'get' => new \ArrayObject([
15401551
'tags' => ['Question'],
15411552
'operationId' => 'getQuestionItem',
@@ -1558,7 +1569,7 @@ public function testNormalizeWithSubResource()
15581569
],
15591570
]),
15601571
],
1561-
'/questions/{id}/answer' => new \ArrayObject([
1572+
'/api/questions/{id}/answer' => new \ArrayObject([
15621573
'get' => new \ArrayObject([
15631574
'tags' => ['Answer', 'Question'],
15641575
'operationId' => 'ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Question-answer-ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Answer',

0 commit comments

Comments
 (0)