Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 011fd48

Browse files
authored
feat(serializer): support for getSupportedTypes (symfony 6.3) (#5671)
1 parent e21e9fa commit 011fd48

4 files changed

Lines changed: 97 additions & 3 deletions

File tree

features/doctrine/search_filter.feature

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,3 +1033,12 @@ Feature: Search filter on collections
10331033
Then the response status code should be 200
10341034
And the response should be in JSON
10351035
And the JSON node "hydra:totalItems" should be equal to 1
1036+
1037+
@!mongodb
1038+
@createSchema
1039+
Scenario: Custom search filters can use Doctrine Expressions as join conditions
1040+
Given there is a dummy object with 3 relatedDummies and their thirdLevel
1041+
When I send a "GET" request to "/dummy_ressource_with_custom_filter?custom=3"
1042+
Then the response status code should be 200
1043+
And the response should be in JSON
1044+
And the JSON node "hydra:totalItems" should be equal to 1

src/Doctrine/Orm/Extension/FilterEagerLoadingExtension.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,13 @@ private function getQueryBuilderWithNewAliases(QueryBuilder $queryBuilder, Query
167167
/** @var Join $joinPart */
168168
$joinString = preg_replace($this->buildReplacePatterns($aliases), $replacements, $joinPart->getJoin());
169169
$pos = strpos($joinString, '.');
170+
$joinCondition = (string) $joinPart->getCondition();
170171
if (false === $pos) {
171-
if (null !== $joinPart->getCondition() && null !== $this->resourceClassResolver && $this->resourceClassResolver->isResourceClass($joinString)) {
172+
if ($joinCondition && $this->resourceClassResolver?->isResourceClass($joinString)) {
172173
$newAlias = $queryNameGenerator->generateJoinAlias($joinPart->getAlias());
173174
$aliases[] = "{$joinPart->getAlias()}.";
174175
$replacements[] = "$newAlias.";
175-
$condition = preg_replace($this->buildReplacePatterns($aliases), $replacements, $joinPart->getCondition());
176+
$condition = preg_replace($this->buildReplacePatterns($aliases), $replacements, $joinCondition);
176177
$join = new Join($joinPart->getJoinType(), $joinPart->getJoin(), $newAlias, $joinPart->getConditionType(), $condition);
177178
$queryBuilderClone->add('join', [$replacement => $join], true); // @phpstan-ignore-line
178179
}
@@ -184,7 +185,7 @@ private function getQueryBuilderWithNewAliases(QueryBuilder $queryBuilder, Query
184185
$newAlias = $queryNameGenerator->generateJoinAlias($association);
185186
$aliases[] = "{$joinPart->getAlias()}.";
186187
$replacements[] = "$newAlias.";
187-
$condition = preg_replace($this->buildReplacePatterns($aliases), $replacements, $joinPart->getCondition() ?? '');
188+
$condition = preg_replace($this->buildReplacePatterns($aliases), $replacements, $joinCondition);
188189
QueryBuilderHelper::addJoinOnce($queryBuilderClone, $queryNameGenerator, $alias, $association, $joinPart->getJoinType(), $joinPart->getConditionType(), $condition, $originAlias, $newAlias);
189190
}
190191

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue5648;
15+
16+
use ApiPlatform\Doctrine\Orm\Filter\AbstractFilter;
17+
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
18+
use ApiPlatform\Metadata\Operation;
19+
use Doctrine\ORM\Query\Expr\Join;
20+
use Doctrine\ORM\QueryBuilder;
21+
22+
class CustomFilter extends AbstractFilter
23+
{
24+
protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, Operation $operation = null, array $context = []): void
25+
{
26+
if ('custom' !== $property) {
27+
return;
28+
}
29+
30+
$alias = $queryBuilder->getRootAliases()[0];
31+
$secondAlias = $queryNameGenerator->generateJoinAlias('relatedDummies');
32+
33+
$joinCondition = $queryBuilder->expr()->like(sprintf('%s.name', $secondAlias), ':param');
34+
35+
$queryBuilder->join(sprintf('%s.relatedDummies', $alias), $secondAlias, Join::WITH, $joinCondition)
36+
->setParameter('param', '%'.$value.'%')
37+
->andWhere('1=1'); // problem only gets triggered when there is a where part.
38+
}
39+
40+
public function getDescription(string $resourceClass): array
41+
{
42+
return [];
43+
}
44+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue5648;
15+
16+
use ApiPlatform\Doctrine\Orm\State\Options;
17+
use ApiPlatform\Metadata\ApiFilter;
18+
use ApiPlatform\Metadata\ApiProperty;
19+
use ApiPlatform\Metadata\ApiResource;
20+
use ApiPlatform\Metadata\Get;
21+
use ApiPlatform\Metadata\GetCollection;
22+
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Dummy;
23+
24+
#[ApiResource(
25+
operations: [
26+
new GetCollection(uriTemplate: '/dummy_ressource_with_custom_filter', itemUriTemplate: '/dummy_ressource_with_custom_filter/{id}'),
27+
new Get(uriTemplate: '/dummy_ressource_with_custom_filter/{id}', uriVariables: ['id']),
28+
],
29+
stateOptions: new Options(entityClass: Dummy::class)
30+
)]
31+
#[ApiFilter(CustomFilter::class)]
32+
class DummyResource
33+
{
34+
#[ApiProperty(identifier: true)]
35+
public ?int $id = null;
36+
37+
public string $name;
38+
39+
public array $relatedDummies;
40+
}

0 commit comments

Comments
 (0)