Skip to content

fix(jsonschema): restore type factory usage - #5897

Merged
soyuka merged 1 commit into
api-platform:3.2from
soyuka:fix/json-schema-type-factory
Oct 23, 2023
Merged

fix(jsonschema): restore type factory usage #5897
soyuka merged 1 commit into
api-platform:3.2from
soyuka:fix/json-schema-type-factory

Conversation

@soyuka

@soyuka soyuka commented Oct 18, 2023

Copy link
Copy Markdown
Member
Q A
Branch? 3.2
Tickets fixes #5896
License MIT

We wrongly assumed in #5470 that the TypeFactory would be @internal which it is not. We moved the logic related to type computation to our Schema PropertyMetadataFactory so that the schema could get cached.

This patch restores the TypeFactory behavior.

I think that we should postpone the deprecation of the TypeFactory to 3.3 and provide a way to configure this on non-resources (see #5896) for example:

#[JsonSchema(['type' => 'string', 'format' => 'date'])
class LocalDate {}

Overriding the property metadata factory is also an option but requires more code user land.

@bendavies

bendavies commented Oct 18, 2023

Copy link
Copy Markdown
Contributor

thanks @soyuka
in my example, LocalDate lives in vendor of course, so i could not annotate it with JsonSchema directly

@soyuka

soyuka commented Oct 19, 2023

Copy link
Copy Markdown
Member Author

In that case you'd have to overwrite the property metadata factory:

class LocalDatePropertyMetadataFactory implements PropertyMetadataFactoryInterface
{
    public function __construct(private readonly ?PropertyMetadataFactoryInterface $decorated = null)
    {
    }

    public function create(string $resourceClass, string $property, array $options = []): ApiProperty
    {
        $propertyMetadata = $this->decorated->create($resourceClass, $property, $options);
        // check propertyMetadata->getBuiltinTypes();
        return $propertyMetadata->withSchema(['type' => 'string', 'format' => 'date']);
    }
}

@soyuka
soyuka force-pushed the fix/json-schema-type-factory branch 2 times, most recently from acc2150 to 519ec0f Compare October 19, 2023 09:47
@soyuka
soyuka force-pushed the fix/json-schema-type-factory branch from 519ec0f to 5e6ebb4 Compare October 19, 2023 09:53
@soyuka
soyuka merged commit c2824c1 into api-platform:3.2 Oct 23, 2023
@soyuka

soyuka commented Oct 23, 2023

Copy link
Copy Markdown
Member Author

I hope that works for you

@bendavies

Copy link
Copy Markdown
Contributor

@soyuka i tried 3.4 and my decorating of ApiPlatform\JsonSchema\TypeFactory no longer works. is this a bug or am i meat to decorate something else?

@bendavies

Copy link
Copy Markdown
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants