core icon indicating copy to clipboard operation
core copied to clipboard

`ApiFilter` does not respect `SerializerName`

Open bendavies opened this issue 1 year ago • 3 comments

API Platform version(s) affected: v3.1.18

Description
The following will not work:

#[ApiResource(
    operations: [
        new GetCollection(),
    ],
)]

#[ApiFilter(DateFilter::class, properties: [
    'date',
])]
class Foo
{
    #[SerializedName('date')]
    public \DateTimeInterface $reallyBadlyNamedDateForLegacyReasons;
}

beacuse the filters do not consider the SerializerName of the attribute we want to filter on

How to reproduce

Possible Solution

  1. Some code here which takes into account the SerializedName here? https://github.com/api-platform/core/blob/main/src/Doctrine/Orm/Filter/AbstractFilter.php#L89

bendavies avatar Nov 20 '23 14:11 bendavies

I really need to tackle this with #2400 as once we reach doctrine filters we should already know the correct property right?

soyuka avatar Nov 21 '23 08:11 soyuka

@soyuka having thought about this, i think this is tricky to solve. what if we had a Resource like this.


#[ApiResource(
    operations: [
        new GetCollection(),
    ],
)]

#[ApiFilter(DateFilter::class, properties: [
    'date',
])]
class Foo
{
    #[SerializedName('date')]
    public \DateTimeInterface $reallyBadlyNamedDateForLegacyReasons;

    public \DateTimeInterface $date;
}

what then?

i think there will be many other edge cases which we can't envisage, like groups and SerializedPath.

and this won't work with stateOptions: new Options(entityClass: ...::class) where your entity is not the serialized class

Instead, i don't think we should support serializer attributes at all, and instead https://github.com/api-platform/core/issues/5981 is a better, far more flexible solution.

bendavies avatar Nov 21 '23 12:11 bendavies

Hello!

I think it's needed to handle also the possibility to use this name for the validation constrains and also when using attributes to specify the attributes to get.

maySaghira avatar Dec 13 '23 10:12 maySaghira