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

Đang mở
#8,771 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 2 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
48/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
php, symfony
Lĩnh vực
api, backend-api-design

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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

Ngôn ngữ chính
PHP
Star
2.6k
Fork
988
Merge trung bình
4 ngày 10 giờ
Pull request đã merge (30 ngày)
92

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của api-platform/core

Tất cả issue của api-platform/core

Issue tương tự

Thêm issue về PHP

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.