[Object Mapper] IriFilter / IriConverterParameterProvider fails to find items when filtering mapped resources

Aperta
#8,771 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
php, symfony

Direzione di ricerca

Start by reading Doctrine ORM's IriFilter and IriConverterParameterProvider, then trace how the bar query parameter is handled for the mapped FooResource relation relation.bar. The report says filtering should match the underlying entity relation for /v2/foos?bar=/v2/bars/2; verify whether the mapped resource value causes the mismatch and identify the relevant test coverage.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Environment
  • API Platform version: v5.0.5
  • Symfony version: 8.1.* (symfony/object-mapper)
  • PHP version: 8.5
  • Doctrine ORM: ^3.6.8

Description

Hey everyone!

I've been testing API Platform's new Object Mapper feature (Symfony\Component\ObjectMapper\Attribute\Map) with custom DTO resources acting as Entity DTOs, and it's honestly amazing. Everything works super smoothly—except when trying to filter a collection using an IriFilter combined with an IriConverterParameterProvider on a mapped relation (especially with nested property paths).

When calling an endpoint with an IRI filter like ?bar=/v2/bars/2, Doctrine successfully finds the target resource, but the filter ends up evaluating to false or failing to match because the IriFilter receives the already-mapped resource instead of resolving it against the underlying entity relation.

In the example below, when calling /v2/bars/2

the $value is an instance of BarResource.


Code Snippets
1. Collection Resource (FooResource)
namespace App\ApiResource\V2\Foo;

use ApiPlatform\Doctrine\Orm\Filter\IriFilter;
use ApiPlatform\Doctrine\Orm\State\Options;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\QueryParameter;
use ApiPlatform\State\ParameterProvider\IriConverterParameterProvider;
use App\ApiResource\V2\Bar\BarResource;
use App\Entity\FooEntity;
use Symfony\Component\ObjectMapper\Attribute\Map;

#[ApiResource(
    uriTemplate: '/v2/foos',
    operations: [
        new GetCollection(
            parameters: [
                'bar' => new QueryParameter(
                    provider: IriConverterParameterProvider::class,
                    filter: new IriFilter(),
                    property: 'relation.bar',
                )
            ],
            itemUriTemplate: FooResource::uriTemplate,
        ),
    ],
    stateOptions: new Options(entityClass: FooEntity::class)
)]
#[Map(source: FooEntity::class)]
class FooResource
{
    public const string uriTemplate = '/v2/foos/{id}';

    #[ApiProperty(identifier: true)]
    public int $id;
    public ?float $amount = null;

    #[Map(source: 'relation.bar.name')]
    public string $barName;

    #[Map(source: 'relation.bar')]
    public BarResource $bar;

    // ... other mappings
}

2. Related Resource (BarResource)
namespace App\ApiResource\V2\Bar;

use ApiPlatform\Doctrine\Orm\State\Options;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use App\Entity\BarEntity;
use Symfony\Component\ObjectMapper\Attribute\Map;

#[ApiResource(
    operations: [
        new Get(
            uriTemplate: self::uriTemplate
        )
    ],
    stateOptions: new Options(entityClass: BarEntity::class)
)]
#[Map(source: BarEntity::class)]
class BarResource
{
    public const string uriTemplate = '/v2/bars/{id}';

    #[ApiProperty(identifier: true)]
    public int $id;

    public string $name;
}


What happens?
  1. Request: GET /v2/foos?bar=/v2/bars/2
  2. The IriConverterParameterProvider or IriFilter expects a Doctrine entity to build the database query, but since the resource is mapped via Object Mapper, it gets confused by the IRI / mapped resource representation.
  3. The SQL query ends up generating a clause like:
WHERE f0_.bar_id = ?

where the parameter value evaluates to false or fails to link to entity ID 2.

Interestingly, exact filters on simple IDs work fine, and IRI filters on standard non-mapped entity-backed resources work smoothly too. It specifically comes up when combining Object Mapper with IRI-based query parameters.

Any thoughts on whether this is expected limitation right now, or if there's a recommended workaround / bridge we should use? Thanks a lot! 🙌

Lingua principale
PHP
Stelle
2.6k
Fork
988
Merge medio
4g 10h
PR unite (30g)
92

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di api-platform/core

Tutte le issue di api-platform/core

Issue simili

Altre issue su PHP

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.