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 • 2 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

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Mar 09 '24 09:03 stale[bot]

Works fine:

<?php

namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6091;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Operation;

#[Get(uriVariables: ['date'], provider: [DateIdentifier::class, 'provide'])]
class DateIdentifier
{
    #[ApiProperty(identifier: true)]
    public \DateTimeImmutable $date;

    static public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null {
        dd($uriVariables);
        return null;
    }
}

soyuka avatar Mar 15 '24 09:03 soyuka