-
-
Notifications
You must be signed in to change notification settings - Fork 913
fix: searchfilter with nested custom identifiers #5760
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -196,6 +196,7 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB | |
if ($metadata->hasField($field)) { | ||
if ('id' === $field) { | ||
$values = array_map($this->getIdFromValue(...), $values); | ||
// todo: handle composite IDs | ||
} | ||
|
||
if (!$this->hasValidValues($values, $this->getDoctrineFieldType($property, $resourceClass))) { | ||
|
@@ -216,13 +217,44 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB | |
return; | ||
} | ||
|
||
$values = array_map($this->getIdFromValue(...), $values); | ||
|
||
// association, let's fetch the entity (or reference to it) if we can so we can make sure we get its orm id | ||
$associationResourceClass = $metadata->getAssociationTargetClass($field); | ||
$associationFieldIdentifier = $metadata->getIdentifierFieldNames()[0]; | ||
$associationMetadata = $this->getClassMetadata($associationResourceClass); | ||
$associationFieldIdentifier = $associationMetadata->getIdentifierFieldNames()[0]; | ||
$doctrineTypeField = $this->getDoctrineFieldType($associationFieldIdentifier, $associationResourceClass); | ||
|
||
if (!$this->hasValidValues($values, $doctrineTypeField)) { | ||
$values = array_map(function ($value) use ($associationFieldIdentifier, $doctrineTypeField) { | ||
if (is_numeric($value)) { | ||
return $value; | ||
} | ||
try { | ||
$item = $this->getIriConverter()->getResourceFromIri($value, ['fetch_data' => false]); | ||
|
||
return $this->propertyAccessor->getValue($item, $associationFieldIdentifier); | ||
} catch (InvalidArgumentException) { | ||
/* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually have another version that handle that case better, but i'll leave it for another PR since it needs quite a lot more to work. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't worry let's go for this I think it's just find and maybe we can do better when working on #2400 next year! |
||
* Can we do better? This is not the ApiResource the call was made on, | ||
* so we don't get any kind of api metadata for it without (a lot of?) work elsewhere... | ||
* Let's just pretend it's always the ORM id for now. | ||
*/ | ||
if (!$this->hasValidValues([$value], $doctrineTypeField)) { | ||
$this->logger->notice('Invalid filter ignored', [ | ||
'exception' => new InvalidArgumentException(sprintf('Values for field "%s" are not valid according to the doctrine type.', $associationFieldIdentifier)), | ||
]); | ||
|
||
return null; | ||
} | ||
|
||
return $value; | ||
} | ||
}, $values); | ||
|
||
$expected = \count($values); | ||
$values = array_filter($values, static fn ($value) => null !== $value); | ||
if ($expected > \count($values)) { | ||
/* | ||
* Shouldn't this actually fail harder? | ||
*/ | ||
$this->logger->notice('Invalid filter ignored', [ | ||
'exception' => new InvalidArgumentException(sprintf('Values for field "%s" are not valid according to the doctrine type.', $field)), | ||
]); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue5605; | ||
|
||
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter; | ||
use ApiPlatform\Doctrine\Orm\State\Options; | ||
use ApiPlatform\Metadata\ApiFilter; | ||
use ApiPlatform\Metadata\ApiProperty; | ||
use ApiPlatform\Metadata\ApiResource; | ||
use ApiPlatform\Metadata\Get; | ||
use ApiPlatform\Metadata\GetCollection; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\DummyWithSubEntity; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\State\Issue5605\MainResourceProvider; | ||
|
||
#[ApiResource( | ||
operations : [ | ||
new Get(uriTemplate: '/dummy_with_subresource/{id}', uriVariables: ['id']), | ||
new GetCollection(uriTemplate: '/dummy_with_subresource'), | ||
], | ||
provider : MainResourceProvider::class, | ||
stateOptions: new Options(entityClass: DummyWithSubEntity::class) | ||
)] | ||
#[ApiFilter(SearchFilter::class, properties: ['subEntity'])] | ||
class MainResource | ||
{ | ||
#[ApiProperty(identifier: true)] | ||
public int $id; | ||
public string $name; | ||
public SubResource $subResource; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue5605; | ||
|
||
use ApiPlatform\Doctrine\Orm\State\Options; | ||
use ApiPlatform\Metadata\ApiProperty; | ||
use ApiPlatform\Metadata\ApiResource; | ||
use ApiPlatform\Metadata\Get; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\DummySubEntity; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\State\Issue5605\SubResourceProvider; | ||
|
||
#[ApiResource( | ||
operations : [ | ||
new Get( | ||
uriTemplate: '/dummy_subresource/{strId}', | ||
uriVariables: ['strId'] | ||
), | ||
], | ||
provider: SubResourceProvider::class, | ||
stateOptions: new Options(entityClass: DummySubEntity::class) | ||
)] | ||
class SubResource | ||
{ | ||
#[ApiProperty(identifier: true)] | ||
public string $strId; | ||
|
||
public string $name; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Tests\Fixtures\TestBundle\Entity; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
#[ORM\Entity] | ||
class DummySubEntity | ||
{ | ||
#[ORM\Id] | ||
#[ORM\Column(type: 'string')] | ||
private string $strId; | ||
|
||
#[ORM\Column] | ||
private string $name; | ||
|
||
#[ORM\OneToOne(inversedBy: 'subEntity', cascade: ['persist'])] | ||
private ?DummyWithSubEntity $mainEntity = null; | ||
|
||
public function __construct($strId, $name) | ||
{ | ||
$this->strId = $strId; | ||
$this->name = $name; | ||
} | ||
|
||
public function getStrId(): string | ||
{ | ||
return $this->strId; | ||
} | ||
|
||
public function getMainEntity(): ?DummyWithSubEntity | ||
{ | ||
return $this->mainEntity; | ||
} | ||
|
||
public function setMainEntity(DummyWithSubEntity $mainEntity): void | ||
{ | ||
$this->mainEntity = $mainEntity; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function setName(string $name): void | ||
{ | ||
$this->name = $name; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Tests\Fixtures\TestBundle\Entity; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
#[ORM\Entity] | ||
class DummyWithSubEntity | ||
{ | ||
#[ORM\Id] | ||
#[ORM\Column(type: 'integer')] | ||
#[ORM\GeneratedValue(strategy: 'AUTO')] | ||
private int $id; | ||
|
||
#[ORM\Column] | ||
private string $name; | ||
|
||
#[ORM\OneToOne(mappedBy: 'mainEntity', cascade: ['persist'], fetch: 'EAGER')] | ||
private ?DummySubEntity $subEntity = null; | ||
|
||
public function getId(): int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function setName(string $name): void | ||
{ | ||
$this->name = $name; | ||
} | ||
|
||
public function getSubEntity(): ?DummySubEntity | ||
{ | ||
return $this->subEntity; | ||
} | ||
|
||
public function setSubEntity(?DummySubEntity $subEntity): void | ||
{ | ||
if (null !== $subEntity && $subEntity->getMainEntity() !== $this) { | ||
$subEntity->setMainEntity($this); | ||
} | ||
|
||
$this->subEntity = $subEntity; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not good for performances, isn't there a way to prevent this exception?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep, that's not great for perfs but i can't see another way to check whether what i have is a valid IRI beforehand. That should not change much performance-wise though; I actually copied this logic from SearchFilterTrait::getIdFromValue, which was called before :
core/src/Doctrine/Common/Filter/SearchFilterTrait.php
Lines 121 to 132 in b58ec12