api-platform / core

The server component of API Platform: hypermedia and GraphQL APIs in minutes
https://api-platform.com
MIT License
2.42k stars 863 forks source link

Separate DTOs for input and output with different id fields fail #6355

Closed xdrew closed 4 months ago

xdrew commented 4 months ago

API Platform version(s) affected: ^3.3

Description
I have a dto and api resource, updating order product count and returning the order dto:

#[ApiResource(
    shortName: 'OrderProduct',
    operations: [
        new Get(),
        new Patch(
            uriTemplate: '/order_products/{id}/count',
            controller: UpdateOrderProductCountController::class,
            exceptionToStatus: [\Exception::class => 400],
            class: OrderDto::class,
            input: OrderProductCountDto::class,
            output: OrderDto::class,
            read: false,
            write: false,
            name: 'order_product_update_count',
        ),
    ],
    order: ['position' => 'ASC'],
    provider: EntityToDtoStateProvider::class,
    stateOptions: new Options(entityClass: OrderProduct::class),
)]
class OrderProductCountDto
{
    #[ApiProperty(writable: false, identifier: true)]
    public ?int $id = null;

    public ?int $count = null;
}
class OrderDto
{
    #[ApiProperty(identifier: false)]
    public ?int $id = null;

    #[ApiProperty(identifier: true)]
    public ?Uuid $uuid = null;
//...
}
#[AsController]
class UpdateOrderProductCountController extends AbstractController
{
    public function __invoke(OrderProductCountDto $data, Request $request): OrderDto
    {

        $dto = new OrderDto();
        $dto->id = 1;
        // ...
        return $dto;
    }

Calling this controller leads to 400 Invalid identifier value or configuration error.

How to reproduce
Use dtos with different id fields for input/output with custom controller as in my example

Possible Solution
Place this check https://github.com/api-platform/core/commit/cc9f6a518222598d20556fc1ec62b7c4be52bf52#diff-6723d777fc70f000cb59deecf75e9a948f114ffd9ae63e284ca5821735b7cea3R105 before this one https://github.com/api-platform/core/commit/cc9f6a518222598d20556fc1ec62b7c4be52bf52#diff-6723d777fc70f000cb59deecf75e9a948f114ffd9ae63e284ca5821735b7cea3R77

Additional Context
The problem occures when you have different dtos for input/output and they also have different ids for example id and uuid. Platform then uses operation class for fetching getOperationUriVariables instead of resource_class. Maybe it's ok, and I'm doing this in a wrong way.

soyuka commented 4 months ago

Can you try my patch at https://github.com/api-platform/core/pull/6357 ?

xdrew commented 4 months ago

Yep. It fixes my issue. Thanks!

soyuka commented 4 months ago

will be released soon thanks for the report