From ef0ee6427f8056bcb2617c228a7cf9ffd9d29ccd Mon Sep 17 00:00:00 2001 From: Nicolas PHILIPPE Date: Mon, 2 Sep 2024 13:16:14 +0200 Subject: [PATCH] fix(doctrine): use parameter.property for filter value (#6572) --- src/Doctrine/Common/ParameterValueExtractorTrait.php | 2 +- tests/Fixtures/TestBundle/Entity/SearchFilterParameter.php | 1 + tests/Functional/Parameters/DoctrineTest.php | 7 ++++++- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Doctrine/Common/ParameterValueExtractorTrait.php b/src/Doctrine/Common/ParameterValueExtractorTrait.php index 61ff1fdeb6..b50dede8f7 100644 --- a/src/Doctrine/Common/ParameterValueExtractorTrait.php +++ b/src/Doctrine/Common/ParameterValueExtractorTrait.php @@ -22,7 +22,7 @@ trait ParameterValueExtractorTrait */ private function extractParameterValue(Parameter $parameter, mixed $value): array { - $key = $parameter->getKey(); + $key = $parameter->getProperty() ?? $parameter->getKey(); if (!str_contains($key, ':property')) { return [$key => $value]; } diff --git a/tests/Fixtures/TestBundle/Entity/SearchFilterParameter.php b/tests/Fixtures/TestBundle/Entity/SearchFilterParameter.php index 133e98adcd..12077e1255 100644 --- a/tests/Fixtures/TestBundle/Entity/SearchFilterParameter.php +++ b/tests/Fixtures/TestBundle/Entity/SearchFilterParameter.php @@ -25,6 +25,7 @@ uriTemplate: 'search_filter_parameter{._format}', parameters: [ 'foo' => new QueryParameter(filter: 'app_search_filter_via_parameter'), + 'fooAlias' => new QueryParameter(filter: 'app_search_filter_via_parameter', property: 'foo'), 'order[:property]' => new QueryParameter(filter: 'app_search_filter_via_parameter.order_filter'), 'searchPartial[:property]' => new QueryParameter(filter: 'app_search_filter_partial'), diff --git a/tests/Functional/Parameters/DoctrineTest.php b/tests/Functional/Parameters/DoctrineTest.php index 51d017c830..4e4347fabf 100644 --- a/tests/Functional/Parameters/DoctrineTest.php +++ b/tests/Functional/Parameters/DoctrineTest.php @@ -33,12 +33,17 @@ public function testDoctrineEntitySearchFilter(): void $this->assertEquals('bar', $a['hydra:member'][1]['foo']); $this->assertArraySubset(['hydra:search' => [ - 'hydra:template' => \sprintf('/%s{?foo,order[order[id]],order[order[foo]],searchPartial[foo],searchExact[foo],searchOnTextAndDate[foo],searchOnTextAndDate[createdAt][before],searchOnTextAndDate[createdAt][strictly_before],searchOnTextAndDate[createdAt][after],searchOnTextAndDate[createdAt][strictly_after],q}', $route), + 'hydra:template' => \sprintf('/%s{?foo,fooAlias,order[order[id]],order[order[foo]],searchPartial[foo],searchExact[foo],searchOnTextAndDate[foo],searchOnTextAndDate[createdAt][before],searchOnTextAndDate[createdAt][strictly_before],searchOnTextAndDate[createdAt][after],searchOnTextAndDate[createdAt][strictly_after],q}', $route), 'hydra:mapping' => [ ['@type' => 'IriTemplateMapping', 'variable' => 'foo', 'property' => 'foo'], ], ]], $a); + $response = self::createClient()->request('GET', $route.'?fooAlias=baz'); + $a = $response->toArray(); + $this->assertCount(1, $a['hydra:member']); + $this->assertEquals('baz', $a['hydra:member'][0]['foo']); + $response = self::createClient()->request('GET', $route.'?order[foo]=asc'); $this->assertEquals($response->toArray()['hydra:member'][0]['foo'], 'bar'); $response = self::createClient()->request('GET', $route.'?order[foo]=desc');