core
core copied to clipboard
Using a datetime object as identifier
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.
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.
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;
}
}