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

未关闭
#8,771 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 2 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
48/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
php, symfony

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

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! 🙌

主要语言
PHP
星标
2.6k
派生
988
平均合并
4 天 10 小时
30 天内合并 PR
92

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

api-platform/core 的其他 Issue

查看 api-platform/core 的全部 Issue

相似的 Issue

更多 PHP Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。