Skip to content
Closed
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: 4 additions & 1 deletion src/OpenApi/Factory/OpenApiFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,10 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection
if ($d = $filter->getDescription($entityClass)) {
foreach ($d as $name => $description) {
if ($prop = $p->getProperty()) {
$name = str_replace($prop, $key, $name);
if (($description['property'] ?? null) !== $prop) {
continue;
}
$name = $key;
}

$openapiParameters[] = $this->getFilterParameter($name, $description, $operation->getShortName(), $f);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
use ApiPlatform\Doctrine\Orm\Filter\ExactFilter;
use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Doctrine\Orm\Filter\PartialSearchFilter;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\QueryParameter;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ApiFilter(OrderFilter::class, alias: 'product_order_filter', properties: ['rating'])]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ApiFilter will get deprecated/removed, did you try specifying the filter inside the QueryParameter instead?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah that's simpler, thanks!

Apologies for the distraction.

#[ApiResource(
operations: [
new GetCollection(
Expand All @@ -46,6 +48,10 @@
filter: new OrderFilter(),
properties: ['rating']
),
'order[sort_by]' => new QueryParameter(
filter: 'product_order_filter',
property: 'rating',
),
'exactBrand' => new QueryParameter(
filter: new ExactFilter(),
property: 'brand',
Expand Down
23 changes: 23 additions & 0 deletions tests/Functional/Parameters/DoctrineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -470,4 +470,27 @@ public static function openApiParameterDocumentationProvider(): array
],
];
}

public function testQueryParameterPropertyRemapOpenApiParameterName(): void
{
if ($this->isMongoDB()) {
$this->markTestSkipped('Not tested with mongodb.');
}

$resource = ProductWithQueryParameter::class;
$this->recreateSchema([$resource]);

$response = self::createClient()->request('GET', '/docs', [
'headers' => ['Accept' => 'application/vnd.openapi+json'],
]);

$this->assertResponseIsSuccessful();
$openApiDoc = $response->toArray();

$parameters = $openApiDoc['paths']['/product_with_query_parameters']['get']['parameters'];
$parameterNames = array_column($parameters, 'name');

$this->assertContains('order[sort_by]', $parameterNames, 'Parameter order[sort_by] should be present');
$this->assertNotContains('order[order[sort_by]]', $parameterNames, 'Parameter name should not be double-wrapped as order[order[sort_by]]');
}
}
Loading