PHP Symfony API DevelopmentLibrary DevelopmentBackend DevelopmentBackend Engineering 1 min read

Symfony 8.2 Enhances Serializer With Generic Type Resolution

Symfony 8.2 introduces generic type resolution, improved union type handling for the Serializer, and new TypeInfo features for more precise data validation.

Symfony 8.2 adds generic type resolution to the Serializer during denormalization. Yonel Ceruto contributed the feature, which requires PropertyInfo's PhpStanExtractor and phpstan/phpdoc-parser. You must declare concrete template types on the holding property.

The Serializer now denormalizes arrays of union types, handling elements like list<CardPayment|BankTransfer>. Pascal Cescon contributed this change, which throws an exception if an element or key fails to match the union or key type. Hubert Lenoir added a stricter rule for list properties. A JSON object assigned to a list<string> triggers a deprecation in 8.2 and will throw a NotNormalizableValueException in Symfony 9.0.

George Steel and Mathias Arlaud added support for @template-covariant and its prefixes in TypeInfo. The new Type::map() method traverses inner types before outer types. Nicolas Grekas added Type::getMismatches(), which returns a TypeMismatch object containing the property path, expected type, and actual type. You should call Type::getMismatches() only after Type::accepts() returns false.

Got it.