Skip to content

Commit 2bde0f6

Browse files
authored
fix(openapi): skip uri variables absent from the path (#8915)
1 parent d5dbbef commit 2bde0f6

2 files changed

Lines changed: 68 additions & 2 deletions

File tree

‎src/OpenApi/Factory/OpenApiFactory.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,10 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection
287287
continue;
288288
}
289289

290+
if (!str_contains($path, '{'.$parameterName.'}')) {
291+
continue;
292+
}
293+
290294
$description = $uriVariable->getDescription();
291295
if (null === $description) {
292296
$fromClass = $uriVariable->getFromClass();

‎src/OpenApi/Tests/Factory/OpenApiFactoryTest.php‎

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
use Prophecy\Argument;
7474
use Prophecy\PhpUnit\ProphecyTrait;
7575
use Psr\Container\ContainerInterface;
76+
use Symfony\Component\Routing\RouterInterface;
7677
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
7778
use Symfony\Component\TypeInfo\Type;
7879

@@ -286,7 +287,7 @@ public function testInvoke(): void
286287

287288
$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');
288289
$parameterResource = (new ApiResource())->withOperations(new Operations([
289-
'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),
290+
'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),
290291
'parameters' => (new Put(uriTemplate: '/parameters', parameters: [
291292
'foo' => new HeaderParameter(description: 'hi', schema: ['type' => 'string', 'format' => 'uuid']),
292293
]))->withOperation($baseOperation),
@@ -1333,7 +1334,7 @@ public function testInvoke(): void
13331334
)
13341335
), $emptyResponsePath->getPost());
13351336

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

13391340
$parameter = $paths->getPath('/parameters')->getPut()->getParameters()[0];
@@ -1949,6 +1950,67 @@ public function testDeleteOperationWithDeserializeDocumentsRequestBody(): void
19491950
$this->assertCount(0, $plainInputSchema->getDefinitions());
19501951
}
19511952

1953+
public function testUriVariableAbsentFromPathIsSkipped(): void
1954+
{
1955+
$openApi = $this->createOpenApiForOperations([
1956+
(new Get())
1957+
->withUriTemplate('/things/fixed/{id}')
1958+
->withUriVariables([
1959+
'kind' => new Link(fromClass: Dummy::class, identifiers: ['kind']),
1960+
'id' => new Link(fromClass: Dummy::class, identifiers: ['id']),
1961+
])
1962+
->withShortName('Dummy')
1963+
->withName('things_fixed_get'),
1964+
]);
1965+
1966+
$parameters = $openApi->getPaths()->getPath('/things/fixed/{id}')->getGet()->getParameters();
1967+
1968+
$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()))));
1969+
}
1970+
1971+
/**
1972+
* @param HttpOperation[] $operations
1973+
*/
1974+
private function createOpenApiForOperations(array $operations, ?RouterInterface $router = null): OpenApi
1975+
{
1976+
$resourceNameCollectionFactory = $this->createStub(ResourceNameCollectionFactoryInterface::class);
1977+
$resourceCollectionMetadataFactory = $this->createStub(ResourceMetadataCollectionFactoryInterface::class);
1978+
$propertyNameCollectionFactory = $this->createStub(PropertyNameCollectionFactoryInterface::class);
1979+
$propertyMetadataFactory = $this->createStub(PropertyMetadataFactoryInterface::class);
1980+
1981+
$resourceCollectionMetadata = new ResourceMetadataCollection(Dummy::class, [(new ApiResource(operations: $operations))->withClass(Dummy::class)]);
1982+
1983+
$resourceCollectionMetadataFactory
1984+
->method('create')
1985+
->willReturnCallback(static fn (string $resourceClass): ResourceMetadataCollection => Dummy::class === $resourceClass ? $resourceCollectionMetadata : new ResourceMetadataCollection($resourceClass, []));
1986+
$resourceNameCollectionFactory->method('create')->willReturn(new ResourceNameCollection([Dummy::class]));
1987+
$propertyNameCollectionFactory->method('create')->willReturn(new PropertyNameCollection([]));
1988+
1989+
$schemaFactory = new SchemaFactory(
1990+
resourceMetadataFactory: $resourceCollectionMetadataFactory,
1991+
propertyNameCollectionFactory: $propertyNameCollectionFactory,
1992+
propertyMetadataFactory: $propertyMetadataFactory,
1993+
nameConverter: new CamelCaseToSnakeCaseNameConverter(),
1994+
definitionNameFactory: new DefinitionNameFactory(),
1995+
);
1996+
1997+
$factory = new OpenApiFactory(
1998+
$resourceNameCollectionFactory,
1999+
$resourceCollectionMetadataFactory,
2000+
$propertyNameCollectionFactory,
2001+
$propertyMetadataFactory,
2002+
$schemaFactory,
2003+
null,
2004+
[],
2005+
new Options('Test API', 'This is a test API.', '1.2.3'),
2006+
new PaginationOptions(),
2007+
$router,
2008+
['json' => ['application/problem+json']]
2009+
);
2010+
2011+
return $factory->__invoke();
2012+
}
2013+
19522014
/**
19532015
* @param Parameter[] $parameters
19542016
*/

0 commit comments

Comments
 (0)