Skip to content

Commit 88fab3b

Browse files
authored
fix(openapi): don't suffix [] on bracketed array keys (#8636)
fixes #8634
1 parent c3eabbb commit 88fab3b

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

‎src/OpenApi/Factory/OpenApiFactory.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection
371371
$parameterDescription = $p->getDescription() ?? "$resourceShortName $key";
372372
$parameterRequired = $p->getRequired() ?? false;
373373
$linkParameter = $p->getOpenApi();
374-
$canSplitToArray = null === $linkParameter && 'query' === $in && 'array' === ($parameterSchema['type'] ?? null);
374+
$canSplitToArray = null === $linkParameter && 'query' === $in && 'array' === ($parameterSchema['type'] ?? null) && !str_ends_with($key, '[]');
375375

376376
$defaultParameters = [];
377377
if (!$canSplitToArray || true !== $castToArray) {

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1751,6 +1751,25 @@ public function testFilterlessArrayQueryParameterHonoursCastToArray(): void
17511751
$this->assertCount(1, array_filter($linkParameterGuardParameters, static fn (Parameter $parameter): bool => 'foobar[]' === $parameter->getName()));
17521752
}
17531753

1754+
public function testFilterlessArrayQueryParameterWithBracketKeyIsNotSuffixedTwice(): void
1755+
{
1756+
$arraySchema = ['type' => 'array', 'items' => ['type' => 'string']];
1757+
1758+
foreach ([null, true, false] as $castToArray) {
1759+
$parameters = $this->getGeneratedQueryParameters(
1760+
['status[]' => new QueryParameter(key: 'status[]', schema: $arraySchema, castToArray: $castToArray)],
1761+
null,
1762+
);
1763+
1764+
$statusParameters = array_values(array_filter($parameters, static fn (Parameter $parameter): bool => str_starts_with($parameter->getName(), 'status')));
1765+
1766+
$this->assertCount(1, $statusParameters, \sprintf('castToArray: %s', var_export($castToArray, true)));
1767+
$this->assertSame('status[]', $statusParameters[0]->getName());
1768+
$this->assertSame($arraySchema, $statusParameters[0]->getSchema());
1769+
$this->assertCount(0, array_filter($parameters, static fn (Parameter $parameter): bool => 'status[][]' === $parameter->getName()));
1770+
}
1771+
}
1772+
17541773
public function testDeleteOperationWithDeserializeDocumentsRequestBody(): void
17551774
{
17561775
$resourceNameCollectionFactory = $this->createMock(ResourceNameCollectionFactoryInterface::class);

0 commit comments

Comments
 (0)