-
-
Notifications
You must be signed in to change notification settings - Fork 957
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
No resource returned when using nested filter with ULID type #1958
Comments
After some extended time researching this issue throughout the internet, it seems this behaviour is not going to get fixed soon, as it is quite niche. Workaround would be to create a Ulid filter, as described in this StackOverflow question. I'll paste the unedited code here as well, just in case the question is deleted in the future. Filter explanation: when you call for example I'll close this issue, so the maintainers' time could be spent fixing more pressing issues. The filter itself: <?php
namespace App\Api\Filter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\AbstractContextAwareFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Uid\Ulid;
final class UlidFilter extends AbstractContextAwareFilter
{
protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null)
{
// otherwise filter is applied to order and page as well
if (
!$this->isPropertyEnabled($property, $resourceClass) ||
!$this->isPropertyMapped($property, $resourceClass, true)
) {
return;
}
// Generate a unique parameter name to avoid collisions with other filters
$parameterName = $queryNameGenerator->generateParameterName($property);
$queryBuilder
->andWhere(sprintf('o.%s = :%s', $property, $parameterName))
->setParameter($parameterName, (new Ulid($value))->toBinary());
}
// This function is only used to hook in documentation generators (supported by Swagger and Hydra)
public function getDescription(string $resourceClass): array
{
if (!$this->properties) {
return [];
}
$description = [];
foreach ($this->properties as $property => $strategy) {
$description[$property] = [
'property' => $property,
'type' => 'string',
'required' => false,
'swagger' => [
'description' => 'Filter Ulid property.',
'name' => 'Ulid Search filter',
'type' => '',
],
];
}
return $description;
}
} Use in entity: use App\Api\Filter\UlidFilter;
...
/**
* @ApiResource()
* @ApiFilter(UlidFilter::class, properties={"Field": "exact"})
**/ Tagging in services.yaml (in my case it was not necessary):
|
This might help someone. To get this filter working with on postgres I had to use the following: $queryBuilder
->andWhere(sprintf('o.%s = :%s', $property, $parameterName))
->setParameter($parameterName, (Ulid::fromString($value))->toRfc4122()); |
API Platform version(s) affected: 2.6.5
Description
When filtering on nested properties of
Symfony\Component\Uid\Ulid
type, for example/api/orders?plan.id=01F9ERJY0Y0KHXC0Z8DHYJ47HS
no Order resource is returned, even though/api/plans/01F9ERJY0Y0KHXC0Z8DHYJ47HS
returns the Plan resource as expected.How to reproduce
Have 2 entities: entity Order is in ManyToOne relation with entity Plan. Both use
Symfony\Component\Uid\Ulid
as primary key.Additional Context
Symfony version is 5.3.3, PHP version is 8.0.7
Postman returned no records:
Postman returned single record (expected):
The text was updated successfully, but these errors were encountered: