Description
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:
- It gathers all errors at the same time, which is a problem with the "type safety" feature: [Serializer] added the ability to gather all deserialization errors. #27136
- It betters fit with the Single Responsibility Principle
- It makes the code of the Serializer simple, and easier to maintain
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