-
-
Notifications
You must be signed in to change notification settings - Fork 935
Description
API Platform version(s) affected: 4.2.2
Description
When I try to define a query parameter using ExistsFilter inside a QueryParameter, an exception is thrown if I use a simple array for the properties argument.
However, the same syntax works fine when using the #[ApiFilter] attribute.
ApiPlatform\Doctrine\Orm\Filter\ExistsFilter::isPropertyMapped(): Argument #1 ($property) must be of type string, int given, called in /home/pentiminax/youtube/symfony-api/vendor/api-platform/doctrine-common/Filter/ExistsFilterTrait.php on line 48
How to reproduce
#[ApiResource(operations: [ new Get(), new GetCollection( parameters: [ 'hasReviews' => new QueryParameter( filter: new ExistsFilter( properties: ['reviews'] // ❌ Throws exception ) ) ] ), new Post() ])]
Possible Solution
By using an associative array instead of a simple array, it works:
properties: ['reviews' => null] // ✅ Works fine
But this behavior is inconsistent with the attribute syntax, where this works perfectly:
#[ApiFilter(ExistsFilter::class, properties: ['reviews'])] // ✅ Works fine
By looking at the code below in ExistsFilterTrait, I saw that $property is the key of the array, it's an integer so that's why the exception is thrown.
By using the $unused variable instead of $property, the exception is not throw anymore but the filter is not working.
foreach ($properties as $property => $unused) { if (!$this->isPropertyMapped($property, $resourceClass, true) || !$this->isNullableField($property, $resourceClass)) { continue; } $propertyName = $this->normalizePropertyName($property); $description[\sprintf('%s[%s]', $this->existsParameterName, $propertyName)] = [ 'property' => $propertyName, 'type' => 'bool', 'required' => false, ]; }
Additional Context
Image