core icon indicating copy to clipboard operation
core copied to clipboard

Using a datetime object as identifier

Open p-pichet opened this issue 2 years ago • 0 comments

API Platform version(s) affected: 3.0.0

Description

I try to use a datetime as identifier and got an error of the identifierExtractor

How to reproduce

<?php

declare(strict_types=1);

use ApiPlatform\Metadata\{
    ApiResource,
    Get,
    ApiProperty
};
#[ApiResource(
    operations: [
        new Get(uriVariables: ['id', 'date']),
    ],
)]
class Stuff
{
    #[ApiProperty(identifier: true)]
    protected ?string $id = null;

    #[ApiProperty(identifier: true)]    
    protected ?\DateTime $date = null;
    // ... other properties, setter, getter 
    public function getDate(): ?\DateTime
    {
        return $this->date;
    }
}

then, when i call /stuff/1/2023-09-09, i got this error

We were not able to resolve the identifier matching parameter "%s".

Possible Solution

On the IdentifiersExtractor::resoleIdentifierValue, allow the DateTime object.

p-pichet avatar Jan 09 '24 07:01 p-pichet