Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

ExistsFilter: properties must be associative array when used with QueryParameter, but not with ApiFilter #7483

Open
@pentiminax

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

        AltStyle によって変換されたページ (->オリジナル) /