Skip to content

Decorating api_platform.json_schema.type_factory does not work in 3.2 #5896

Description

@bendavies

API Platform version(s) affected: 3.2.1

Description
In 3.1 decorating the service api_platform.json_schema.type_factory works for customising the json schema type for a custom userland class.

How to reproduce
Here, I decorate the type factory to add support for using Brick\DateTime\LocalDate as a date

<?php

declare(strict_types=1);

namespace App\ApiResource;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use Brick\DateTime\LocalDate;

#[ApiResource(
    operations: [
        new Get(),
    ],
)]
class Foo
{
    #[ApiProperty(readable: false, writable: false, identifier: true)]
    public ?int $id = null;
    public ?LocalDate $expiration;
}
<?php

declare(strict_types=1);

namespace App\ApiPlatform;

use ApiPlatform\JsonSchema\Schema;
use ApiPlatform\JsonSchema\TypeFactoryInterface;
use Brick\DateTime\LocalDate;
use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
use Symfony\Component\PropertyInfo\Type;

#[AsDecorator('api_platform.json_schema.type_factory')]
class TypeFactoryDecorator implements TypeFactoryInterface
{
    public function __construct(
        private readonly TypeFactoryInterface $decorated,
    ) {
    }

    /** @phpstan-ignore-next-line  */
    public function getType(Type $type, string $format = 'json', ?bool $readableLink = null, ?array $serializerContext = null, ?Schema $schema = null): array
    {
        /** @phpstan-ignore-next-line  */
        if (\is_a($type->getClassName(), LocalDate::class, true)) {
            return [
                'type' => 'string',
                'format' => 'date',
            ];
        }

        return $this->decorated->getType($type, $format, $readableLink, $serializerContext, $schema);
    }
}

in 3.1, the schemas are correct:

image

in 3.2, they are not:
image

Possible Solution
Unsure, tried to figure it out but failed.

Additional Context

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions