Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
bendavies opened this issue Oct 18, 2023 · 2 comments
Closed
Labels

Comments

@bendavies
Copy link
Contributor

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

@soyuka soyuka added the bug label Oct 18, 2023
@soyuka
Copy link
Member

soyuka commented Oct 18, 2023

Mhh it's a bug due to the #5470 feature I'll investigate thanks.

soyuka added a commit to soyuka/core that referenced this issue Oct 18, 2023
soyuka added a commit to soyuka/core that referenced this issue Oct 19, 2023
soyuka added a commit to soyuka/core that referenced this issue Oct 19, 2023
soyuka added a commit to soyuka/core that referenced this issue Oct 19, 2023
@soyuka
Copy link
Member

soyuka commented Oct 26, 2023

will be tagged tomorrow

@soyuka soyuka closed this as completed Oct 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants