Skip to content
Open
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
9 changes: 8 additions & 1 deletion src/OpenApi/Factory/OpenApiFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,10 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection
$this->routeCollection = $this->router->getRouteCollection();
}

if ($this->routeCollection && $routeName && $route = $this->routeCollection->get($routeName)) {
$extraProperties = $operation->getExtraProperties();
if ($operation->getRouteName() && ($extraProperties['user_defined_uri_template'] ?? false) && true === ($extraProperties['openapi_use_uri_template'] ?? false)) {
$path = rtrim($operation->getRoutePrefix() ?? '', '/').'/'.ltrim($operation->getUriTemplate() ?? '', '/');
} elseif ($this->routeCollection && $routeName && $route = $this->routeCollection->get($routeName)) {
$path = $route->getPath();
} else {
$path = rtrim($operation->getRoutePrefix() ?? '', '/').'/'.ltrim($operation->getUriTemplate() ?? '', '/');
Expand Down Expand Up @@ -312,6 +315,10 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection
continue;
}

if (!str_contains($path, '{'.$parameterName.'}')) {
continue;
}

$description = $uriVariable->getDescription();
if (null === $description) {
$fromClass = $uriVariable->getFromClass();
Expand Down
87 changes: 85 additions & 2 deletions src/OpenApi/Tests/Factory/OpenApiFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Psr\Container\ContainerInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
use Symfony\Component\TypeInfo\Type;

Expand Down Expand Up @@ -287,7 +290,7 @@ public function testInvoke(): void

$baseOperation = (new HttpOperation())->withTypes(['http://schema.example.com/Dummy'])->withInputFormats(self::OPERATION_FORMATS['input_formats'])->withOutputFormats(self::OPERATION_FORMATS['output_formats'])->withClass(Dummy::class)->withShortName('Parameter')->withDescription('This is a dummy');
$parameterResource = (new ApiResource())->withOperations(new Operations([
'uriVariableSchema' => (new Get(uriTemplate: '/uri_variable_uuid', uriVariables: ['id' => new Link(schema: ['type' => 'string', 'format' => 'uuid'], description: 'hello', required: true, openApi: new Parameter('id', 'path'))]))->withOperation($baseOperation),
'uriVariableSchema' => (new Get(uriTemplate: '/uri_variable_uuid/{id}', uriVariables: ['id' => new Link(schema: ['type' => 'string', 'format' => 'uuid'], description: 'hello', required: true, openApi: new Parameter('id', 'path'))]))->withOperation($baseOperation),
'parameters' => (new Put(uriTemplate: '/parameters', parameters: [
'foo' => new HeaderParameter(description: 'hi', schema: ['type' => 'string', 'format' => 'uuid']),
]))->withOperation($baseOperation),
Expand Down Expand Up @@ -1334,7 +1337,7 @@ public function testInvoke(): void
)
), $emptyResponsePath->getPost());

$parameter = $paths->getPath('/uri_variable_uuid')->getGet()->getParameters()[0];
$parameter = $paths->getPath('/uri_variable_uuid/{id}')->getGet()->getParameters()[0];
$this->assertEquals(['type' => 'string', 'format' => 'uuid'], $parameter->getSchema());

$parameter = $paths->getPath('/parameters')->getPut()->getParameters()[0];
Expand Down Expand Up @@ -2004,6 +2007,86 @@ public function testDeleteOperationWithDeserializeDocumentsRequestBody(): void
$this->assertCount(0, $plainInputSchema->getDefinitions());
}

public function testUriVariableAbsentFromPathIsSkipped(): void
{
$openApi = $this->createOpenApiForOperations([
(new Get())
->withUriTemplate('/things/fixed/{id}')
->withUriVariables([
'kind' => new Link(fromClass: Dummy::class, identifiers: ['kind']),
'id' => new Link(fromClass: Dummy::class, identifiers: ['id']),
])
->withShortName('Dummy')
->withName('things_fixed_get'),
]);

$parameters = $openApi->getPaths()->getPath('/things/fixed/{id}')->getGet()->getParameters();

$this->assertSame(['id'], array_map(static fn (Parameter $parameter): string => $parameter->getName(), array_values(array_filter($parameters, static fn (Parameter $parameter): bool => 'path' === $parameter->getIn()))));
}

public function testUserDefinedUriTemplateIsDocumentedInsteadOfSharedRoutePathWhenOptedIn(): void
{
$routeCollection = new RouteCollection();
$routeCollection->add('shared_route', new Route('/shared/{kind}'));
$router = $this->createStub(RouterInterface::class);
$router->method('getRouteCollection')->willReturn($routeCollection);

$operations = static fn (array $extraProperties): array => [
(new Get())->withUriTemplate('/shared/a')->withRouteName('shared_route')->withShortName('Dummy')->withName('shared_a')->withExtraProperties($extraProperties),
(new Get())->withUriTemplate('/shared/b')->withRouteName('shared_route')->withShortName('Dummy')->withName('shared_b')->withExtraProperties($extraProperties),
];

$optedIn = $this->createOpenApiForOperations($operations(['user_defined_uri_template' => true, 'openapi_use_uri_template' => true]), $router);
$this->assertSame(['/shared/a', '/shared/b'], array_keys($optedIn->getPaths()->getPaths()));

$default = $this->createOpenApiForOperations($operations(['user_defined_uri_template' => true]), $router);
$this->assertSame(['/shared/{kind}'], array_keys($default->getPaths()->getPaths()));
}

/**
* @param HttpOperation[] $operations
*/
private function createOpenApiForOperations(array $operations, ?RouterInterface $router = null): OpenApi
{
$resourceNameCollectionFactory = $this->createStub(ResourceNameCollectionFactoryInterface::class);
$resourceCollectionMetadataFactory = $this->createStub(ResourceMetadataCollectionFactoryInterface::class);
$propertyNameCollectionFactory = $this->createStub(PropertyNameCollectionFactoryInterface::class);
$propertyMetadataFactory = $this->createStub(PropertyMetadataFactoryInterface::class);

$resourceCollectionMetadata = new ResourceMetadataCollection(Dummy::class, [(new ApiResource(operations: $operations))->withClass(Dummy::class)]);

$resourceCollectionMetadataFactory
->method('create')
->willReturnCallback(static fn (string $resourceClass): ResourceMetadataCollection => Dummy::class === $resourceClass ? $resourceCollectionMetadata : new ResourceMetadataCollection($resourceClass, []));
$resourceNameCollectionFactory->method('create')->willReturn(new ResourceNameCollection([Dummy::class]));
$propertyNameCollectionFactory->method('create')->willReturn(new PropertyNameCollection([]));

$schemaFactory = new SchemaFactory(
resourceMetadataFactory: $resourceCollectionMetadataFactory,
propertyNameCollectionFactory: $propertyNameCollectionFactory,
propertyMetadataFactory: $propertyMetadataFactory,
nameConverter: new CamelCaseToSnakeCaseNameConverter(),
definitionNameFactory: new DefinitionNameFactory(),
);

$factory = new OpenApiFactory(
$resourceNameCollectionFactory,
$resourceCollectionMetadataFactory,
$propertyNameCollectionFactory,
$propertyMetadataFactory,
$schemaFactory,
null,
[],
new Options('Test API', 'This is a test API.', '1.2.3'),
new PaginationOptions(),
$router,
['json' => ['application/problem+json']]
);

return $factory->__invoke();
}

/**
* @param Parameter[] $parameters
*/
Expand Down
1 change: 1 addition & 0 deletions src/OpenApi/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"api-platform/doctrine-orm": "^5.0",
"api-platform/doctrine-odm": "^5.0",
"api-platform/serializer": "^5.0",
"symfony/routing": "^7.4 || ^8.0",
"symfony/type-info": "^7.4 || ^8.0"
},
"autoload": {
Expand Down
Loading