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
14 changes: 4 additions & 10 deletions src/Doctrine/Common/Filter/OpenApiFilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,12 @@ trait OpenApiFilterTrait
public function getOpenApiParameters(Parameter $parameter): OpenApiParameter|array|null
{
$schema = $parameter->getSchema();
$isArraySchema = 'array' === ($schema['type'] ?? null);
$castToArray = $parameter->getCastToArray();

// Use non-array notation if:
// 1. Schema type is explicitly set to a non-array type (string, number, etc.)
// 2. OR castToArray is explicitly false
$hasNonArraySchema = null !== $schema && !$isArraySchema;

if ($hasNonArraySchema || false === $castToArray) {
if (false === $parameter->getCastToArray() || (isset($schema['type']) && 'array' !== $schema['type'])) {
return new OpenApiParameter(name: $parameter->getKey(), in: 'query');
}

return new OpenApiParameter(name: $parameter->getKey().'[]', in: 'query', style: 'deepObject', explode: true);
$arraySchema = ['type' => 'array', 'items' => $schema ?? ['type' => 'string']];

return new OpenApiParameter(name: $parameter->getKey().'[]', in: 'query', style: 'deepObject', explode: true, schema: $arraySchema);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@
property: 'category',
castToArray: false
),
'tags' => new QueryParameter(
filter: new ExactFilter(),
property: 'tags',
schema: ['anyOf' => [['type' => 'array', 'items' => ['type' => 'string']], ['type' => 'string']]]
),
]
),
]
Expand Down
30 changes: 21 additions & 9 deletions tests/Functional/Parameters/DoctrineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ private function loadProductFixtures(string $resourceClass): void
}

#[DataProvider('openApiParameterDocumentationProvider')]
public function testOpenApiParameterDocumentation(string $parameterName, bool $shouldHaveArrayNotation, string $expectedStyle, bool $expectedExplode, ?string $expectedSchemaType = null, string $expectedDescription = ''): void
public function testOpenApiParameterDocumentation(string $parameterName, bool $shouldHaveArrayNotation, string $expectedStyle, bool $expectedExplode, string $expectedDescription = '', ?array $expectedSchema = null): void
{
if ($this->isMongoDB()) {
$this->markTestSkipped('Not tested with mongodb.');
Expand Down Expand Up @@ -335,13 +335,14 @@ public function testOpenApiParameterDocumentation(string $parameterName, bool $s
$this->assertSame('query', $foundParameter['in']);
$this->assertFalse($foundParameter['required']);

if ($expectedSchemaType) {
$this->assertSame($expectedSchemaType, $foundParameter['schema']['type'], \sprintf('Parameter schema type should be %s', $expectedSchemaType));
}

if (isset($foundParameter['expectedDescription'])) {
$this->assertSame($expectedDescription, $foundParameter['description'] ?? '', \sprintf('Description should be %s', $expectedDescription));
}

if ($expectedSchema) {
$this->assertSame($expectedSchema, $foundParameter['schema'], 'Parameter schema should match expected schema');
}

$this->assertSame($expectedStyle, $foundParameter['style'] ?? 'form', \sprintf('Style should be %s', $expectedStyle));
$this->assertSame($expectedExplode, $foundParameter['explode'] ?? false, \sprintf('Explode should be %s', $expectedExplode ? 'true' : 'false'));
}
Expand All @@ -354,29 +355,40 @@ public static function openApiParameterDocumentationProvider(): array
'shouldHaveArrayNotation' => true,
'expectedStyle' => 'deepObject',
'expectedExplode' => true,
'expectedSchemaType' => 'string',
'expectedDescription' => '',
'expectedSchema' => ['type' => 'array', 'items' => ['type' => 'string']],
],
'default behavior with an extra description' => [
'parameterName' => 'brandWithDescription',
'shouldHaveArrayNotation' => true,
'expectedStyle' => 'deepObject',
'expectedExplode' => true,
'expectedSchemaType' => 'string',
'expectedDescription' => 'Extra description about the filter',
'expectedSchema' => ['type' => 'array', 'items' => ['type' => 'string']],
],
'explicit schema type string should not use array notation' => [
'parameterName' => 'exactBrand',
'shouldHaveArrayNotation' => false,
'expectedStyle' => 'form',
'expectedExplode' => false,
'expectedSchemaType' => 'string',
'expectedDescription' => '',
'expectedSchema' => ['type' => 'string'],
],
'castToArray false should not use array notation' => [
'parameterName' => 'exactCategory',
'shouldHaveArrayNotation' => false,
'expectedStyle' => 'form',
'expectedExplode' => false,
'expectedSchemaType' => 'string',
'expectedDescription' => '',
'expectedSchema' => ['type' => 'string'],
],
'with schema and default castToArray should wrap schema in array type' => [
'parameterName' => 'tags',
'shouldHaveArrayNotation' => true,
'expectedStyle' => 'deepObject',
'expectedExplode' => true,
'expectedDescription' => '',
'expectedSchema' => ['type' => 'array', 'items' => ['anyOf' => [['type' => 'array', 'items' => ['type' => 'string']], ['type' => 'string']]]],
],
];
}
Expand Down
Loading