Symfony 8.2 ObjectMapper Adds Enum Conversion, Nested Mapping and Faster Reuse
- Javier Eguiluz
- 825 reads
Symfony 8.2 removes common ObjectMapper workarounds and reuses mapping metadata, with reported speedups for repeated mappings.

Symfony 8.2 makes its ObjectMapper more useful in the cases that tend to produce repetitive glue code: translating scalar values to backed enums, mapping nested objects to the right view types, combining several sources into one target, and reusing metadata between calls. The release also changes how custom transformers receive mapping context. Together, these updates reduce manual work and improve repeated mapping performance, while leaving teams responsible for choosing appropriate mappings and transformers.

The ObjectMapper component copies data between objects using attributes such as #[Map]. Introduced in Symfony 7.3 and updated in 8.1, it now handles more common conversions and nested cases directly. The practical value depends on how often an application maps the same class pairs and how much custom transformation its data model needs.
Scalar values can map to backed enums
Applications often represent a status differently at their boundaries and in their domain objects. A message or API-facing DTO may carry a string such as "draft", while an entity stores a string-backed enum case. Previously, teams needed to provide a custom transformer for each direction of that conversion.

Symfony 8.2 checks the types of the source and destination properties and converts between a backed enum and its scalar value. The conversion works between enums and strings or integers, in both directions. It also applies when #[Map(target: '...')] routes a property to a differently named destination.
#[Map(target: Post::class)]
final class CreatePost
{
public function __construct(
public string $title,
public string $status,
) {}
}
// Post::$status is PostStatus, a string-backed enum.
$post = $objectMapper->map(new CreatePost('Hello world', 'draft'));
// $post->status is PostStatus::DraftThat removes a small but frequent piece of adapter code. The mapper can bridge the scalar representation at the boundary and the typed representation in the entity, without requiring a separate transformer for this standard conversion.

Nested mappings follow the destination property type
Symfony 8.1 added mappings declared on target classes with #[Map(source: ...)]. A gap remained for nested objects: when an object had no mapping explicitly attached to it, the mapper could copy it as-is. If the destination property expected a different class, that assignment could fail with a TypeError.
In 8.2, the mapper can infer the nested target from the type of the destination property. When that property has a single class or interface type, the mapper checks for mapping attributes on that type and uses them to construct the nested result.

#[Map(source: Product::class)]
final class ProductView
{
public string $name;
public CategoryView $category;
}
#[Map(source: Category::class)]
final class CategoryView
{
public string $name;
}
$view = $objectMapper->map($product, ProductView::class);
// $view->category is a CategoryViewThis helps when an entity contains related objects and the output model uses a corresponding set of view classes. Teams can describe those relationships through the property types and target-class mappings instead of adding configuration for every nested value. The inference has a defined scope: it applies to properties with one class or interface type. Explicit #[Map] attributes and the class map retain precedence, so existing specific mapping choices remain in control.
Several sources can fill one target object
Repeatable #[Map] attributes let multiple source properties contribute to a target property. Until now, mapping several objects into the same destination could replace the earlier result each time, leaving only the last mapped object’s data. Symfony 8.2 creates the target from the first source, then maps subsequent sources into that same object.

final class CustomerView
{
#[Map(source: 'identity')]
#[Map(source: 'contact')]
public Profile $profile;
}If identity supplies a name and contact supplies an email, the resulting Profile can hold both. This is useful when a destination shape consolidates information that the source represents in separate objects. The change addresses the overwrite behavior; teams should still ensure their mappings describe compatible fields and that later sources do not unintentionally overwrite values they also provide.
Transformers receive the mapping that invoked them
Custom transformers previously received the value, source object and target object, but not the particular #[Map] attribute that triggered the transformation. A transformer shared across multiple properties therefore had limited context and often needed per-property configuration or duplication.
The new MappingAwareTransformCallableInterface extends TransformCallableInterface and passes the active mapping as a fourth argument. A shared transformer can now inspect mapping details to choose its behavior. For example, one label transformer can use the target property name when translating different order fields.
public function __invoke(
mixed $value,
object $source,
?object $target,
?Map $mapping = null
): mixed {
return $this->translator->trans(
sprintf('order.%s.%s', $mapping?->target, $value)
);
}The mapper always supplies the fourth argument; it is nullable to satisfy PHP’s signature rules. When a mapping is declared on the target class with #[Map(source: ...)], its target value is null. Transformer implementations that depend on this context should account for that case.
Metadata reuse cuts repeated mapping work
Before 8.2, each call to map() repeated work that depends only on the source and destination classes: identifying properties, resolving applicable attributes, and determining how to read and write values. Symfony now computes this metadata once for each pair of classes and reuses it on later calls.
In the benchmark attached to the change, a DTO with 20 properties and two nested objects mapped 1.7 to 1.9 times faster from the second call for the same class pair. That result is specific to the benchmark setup, but the mechanism points to the workloads most likely to benefit: requests that map multiple objects of the same types, and long-running processes such as Messenger workers or FrankenPHP in worker mode.
The first mapping for a class pair still has to account for initialization. Symfony applications now warm the cache with metadata for classes that define #[Map] attributes and store it in a PHP file served by OPcache. The change makes that first call about 20% faster in the reported benchmark. As with Serializer metadata, this cache is disabled in debug mode so edits to mapping attributes take effect immediately.
For teams upgrading to 8.2, the key distinction is between less custom code and faster reuse: enum conversion and nested target inference can eliminate routine mapping logic, while metadata reuse rewards repeated work on the same class pairs. The reported timings are benchmark-specific, so applications with different object shapes should assess their own mapping paths. The release’s practical payoff will be clearest where those patterns already recur.