Skip to content

[Serializer] Deprecate type safety in favor of autovalidation #31487

Closed
@dunglas

Description

@dunglas

For Symfony 4.4, I propose to deprecate the "type safety" feature of the Serializer Component.

When a PropertyTypeExtractor is available, the normalizer will also check that the data to denormalize matches the type of the property (even for primitive types). For instance, if a string is provided, but the type of the property is int, an UnexpectedValueException will be thrown.

https://symfony.com/doc/current/components/serializer.html#recursive-denormalization-and-type-safety

This feature was helpful, but is no redundant with the new autovalidation system introduced in Symfony 4.3 (https://symfony.com/blog/new-in-symfony-4-3-automatic-validation). Under the hood, both systems rely on PropertyInfo, and will give the same results.
However, as autovalidation is built on top of the Validator component, it has several benefits:

Exemple of code change:

before:

$obj = $serializer->deserialize('{"foo": "invalid-type"}', 'MyClass'); // Throws during the first type mismatch

after:

$obj = $serializer->deserialize('{"foo": "invalid-type"}', 'MyClass');
$validator->validate($obj); // Contains all types mismatch, and potentially other validation erros

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions