Skip to the stories
AI & ML

Symfony 8.2 Serializer Adds Extensible Type Maps and Safer Input Handling

  • Javier Eguiluz
  • 955 reads

Symfony 8.2 updates type maps, invalid input handling, CSV exports and inherited type detection, with practical changes for serializer users.

Symfony 8.2 Serializer Adds Extensible Type Maps and Safer Input Handling

Symfony 8.2 makes the Serializer more adaptable at the points where application data tends to get awkward: shared class hierarchies, imperfect user input, export formats and inherited PHP types. The changes add room for extension and reduce surprises, though the new type inference can also change how existing properties are denormalized.

Matthias Schmidt

The Serializer component converts objects to formats such as JSON, XML and CSV, and turns those formats back into objects. This release improves both directions. Teams working with polymorphic data can add child types without modifying a shared parent, while CSV exports can present selected fields under reader-friendly column names.

Child classes can extend discriminator maps

Discriminator maps tell the Serializer which concrete class to create when input identifies a parent class or interface. Previously, the map had to list its supported types in the parent declaration. That creates friction when the parent lives in a shared package: adding a type in an application means changing code the application may not own.

Symfony 8.2 adds the repeatable #[DiscriminatorMapType] attribute. A child class can use it to register itself under a type name, referring to the parent or interface whose map it extends. The mapping argument on #[DiscriminatorMap] is now optional, so a parent can declare the discriminator property and leave type registration to its children.

These registrations combine with entries already declared in attributes, YAML or XML. A class can register several names, which gives teams room to support multiple input labels for one implementation. Symfony reports duplicate names and classes that do not extend the declared parent when building the container. That moves these configuration mistakes to startup rather than leaving them to appear during data handling.

Ousama Ben Younes

Discriminator aliases keep their names on output

Multiple discriminator names can point to one class, a common way to keep an older name working as systems evolve. Before this release, the Serializer accepted an alias while denormalizing input but could replace it during serialization. For example, input using bill for an Invoice could come back as invoice.

Symfony 8.2 checks the object's discriminator property when several map entries refer to its class. If the property contains a matching string, integer or backed enum value, serialization uses that name. If the value is unset or does not match, the first declared type remains the fallback. This makes a deserialize-and-serialize round trip better preserve the label carried by the object, while keeping existing fallback behavior.

Invalid fields can fall back to defaults

Denormalization has often treated a type error in any supplied field as a reason to fail the entire operation. That can make a query parameter such as ?page=foo reject a request, even when the target class defines a sensible default page number.

The new skip_invalid_attributes option treats an invalid value as though the field was absent. In a query object with page defaulting to 1 and sort defaulting to date, an invalid page value can leave the default in place while a valid sort value is still applied. The option can be enabled in a serializer context or in #[MapQueryString].

Nicolas Grekas

There is a boundary: skipping an invalid value does not supply a missing required constructor argument. If that argument has no default, denormalization still fails. And values skipped this way are not included in collected denormalization errors, so applications that depend on those error reports should account for the change.

CSV exports can rename and select columns

The CSV encoder's csv_headers option could already set column order, but column labels came from data keys, and unlisted fields could still be appended. Symfony 8.2 lets the option take an associative array: each output label maps to the data value to place in that column.

For example, a nested value at address.city can appear under a City heading, followed by a Customer column sourced from name. Only the mapped columns are written, in the order declared. Nested paths use the same separator as flattened column names, a period by default, and absent values produce empty cells. The change applies to encoding; a simple list of headers continues to work as before.

Inherited generics and PHPDoc types reach the Serializer

Symfony 8.2 extends type resolution for generic classes that pass template parameters through parent classes, interfaces and traits. The TypeInfo component reads PHPDoc tags such as @extends, @implements and @use. If ProductResponse extends ApiResponse<Product>, the Serializer can now infer that the response's generic data field contains a Product when it denormalizes JSON. The fix also covers child classes that rename or reorder a parent's template parameters.

PropertyInfo and TypeInfo also inherit PHPDoc types when a child redeclares a parent member without repeating its PHPDoc. This applies to properties, getters, setters and constructor arguments, drawing on declarations in parent classes, interfaces and abstract methods of traits. Inheritance occurs when both members have the same native type, or when the parent has no native type.

That richer type information has a direct effect: a redeclared property documented by its parent as list<OrderLine> can now be denormalized into OrderLine objects, even if the child adds an attribute but omits the PHPDoc. Projects that need the old broad array behavior can state that explicitly on the child, for example with @var array<mixed>. Generic resolution requires the PhpStanExtractor and phpstan/phpdoc-parser package.

The practical theme in Symfony 8.2 is more faithful translation between application types and external data. Before upgrading, teams should review child properties whose inherited PHPDoc is more specific than their native type: those are the places where denormalization behavior may change. For new integrations, the expanded maps and CSV column mapping offer a cleaner way to adapt shared models to application-specific inputs and exports.

Source: Javier Eguiluz · symfony.com

From the same set