Skip to content
Merged
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
5 changes: 5 additions & 0 deletions src/OpenApi/Factory/OpenApiFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use ApiPlatform\Metadata\CollectionOperationInterface;
use ApiPlatform\Metadata\Error;
use ApiPlatform\Metadata\ErrorResource;
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
use ApiPlatform\Metadata\Exception\OperationNotFoundException;
use ApiPlatform\Metadata\Exception\ProblemExceptionInterface;
use ApiPlatform\Metadata\Exception\ResourceClassNotFoundException;
Expand Down Expand Up @@ -946,6 +947,10 @@ private function appendSchemaDefinitions(\ArrayObject $schemas, \ArrayObject $de
private function hasParameter(Operation $operation, Parameter $parameter): ?array
{
foreach ($operation->getParameters() as $key => $existingParameter) {
if (!$existingParameter instanceof Parameter) {
throw new InvalidArgumentException(\sprintf('OpenAPI operation parameters must be instances of "%s", "%s" given.', Parameter::class, get_debug_type($existingParameter)));
}

if ($existingParameter->getName() === $parameter->getName() && $existingParameter->getIn() === $parameter->getIn()) {
return [$key, $existingParameter];
}
Expand Down
60 changes: 60 additions & 0 deletions src/OpenApi/Tests/Factory/OpenApiFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Error as ErrorOperation;
use ApiPlatform\Metadata\ErrorResource;
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\HeaderParameter;
Expand All @@ -33,6 +34,7 @@
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
use ApiPlatform\Metadata\Property\PropertyNameCollection;
use ApiPlatform\Metadata\Put;
use ApiPlatform\Metadata\QueryParameter;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
Expand Down Expand Up @@ -1438,4 +1440,62 @@ public function testGetExtensionPropertiesWithFalseValue(): void

$openApi = $factory->__invoke();
}

public function testMetadataParameterInOpenApiOperationParametersThrows(): void
{
$resourceNameCollectionFactory = $this->createMock(ResourceNameCollectionFactoryInterface::class);
$resourceCollectionMetadataFactory = $this->createMock(ResourceMetadataCollectionFactoryInterface::class);
$propertyNameCollectionFactory = $this->createMock(PropertyNameCollectionFactoryInterface::class);
$propertyMetadataFactory = $this->createMock(PropertyMetadataFactoryInterface::class);
$definitionNameFactory = new DefinitionNameFactory([]);

$resourceCollectionMetadata = new ResourceMetadataCollection(Dummy::class, [(new ApiResource(operations: [
(new GetCollection())
->withClass(Dummy::class)
->withShortName('Dummy')
->withName('api_dummies_get_collection')
->withUriTemplate('/dummies')
->withOpenapi(new Operation(parameters: [new QueryParameter(key: 'bar')])),
]))->withClass(Dummy::class)]);

$resourceCollectionMetadataFactory
->method('create')
->willReturnCallback(static fn (string $resourceClass): ResourceMetadataCollection => match ($resourceClass) {
default => new ResourceMetadataCollection($resourceClass, []),
Dummy::class => $resourceCollectionMetadata,
});

$resourceNameCollectionFactory->expects($this->once())
->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: $definitionNameFactory,
);

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

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(Parameter::class);

$factory->__invoke();
}
}
Loading