Symfony 8.2 Expands Controller Argument Mapping and Service Autowiring
- Javier Eguiluz
- 872 reads
Symfony 8.2 broadens typed argument support, simplifies entity collections and JSON filters, and adds payload authorization and media type fallbacks.

Symfony 8.2 extends controller argument resolution across more PHP types and input formats, with several of the same changes available to console commands. The practical shift is that developers can describe more of the desired argument in the method signature and mapping attributes, instead of adding custom conversion or repository lookup code.

The release also changes when mapped payloads reach authorization voters. That has a direct compatibility implication for applications whose voters currently expect a mapping attribute rather than the object produced from it.
Service injection handles compound types
Controller actions and invokable commands can now autowire union and intersection types using the same rules as constructors. This helps when a service must satisfy multiple interfaces, such as a serializer that implements both NormalizerInterface and DenormalizerInterface.

Symfony resolves a compound type when every member points to an alias of the same service. Built-in types are ignored, so A|string resolves as A. Nullable types and #[Target] remain supported. A type combining unions and intersections, such as (A&B)|C, needs an explicit #[Autowire(service: '...')] selection.
This brings controller and command method signatures in line with constructor injection. Teams that already rely on compound types in constructors can use them in these entry points without restructuring the service or adding manual lookup code.

Query and header values can use other resolvers
#[MapQueryParameter] and #[MapRequestHeader] now accept types that other argument resolvers know how to build. That includes date objects and Doctrine entities, as well as values such as a ULID read from a request header. For dates, a parameter can pair #[MapQueryParameter] with #[MapDateTime] to specify a format.
When either mapping attribute reads a value it cannot convert itself, Symfony stores that raw value in the request attributes under the argument name. The remaining resolvers can then process it as though it came from the route. The new SourceValueResolverInterface defines this handoff, so custom resolvers can use the same approach.

Authorization can inspect validated objects
Previously, #[IsGranted] checks ran before #[MapRequestPayload], #[MapQueryString] and #[MapUploadedFile] created their arguments. That kept unauthorized requests from triggering payload parsing, but it also meant a voter could not inspect the mapped object because it did not yet exist.
Symfony 8.2 maps and validates an argument first when an #[IsGranted] subject refers to it. Attribute order still matters: an earlier check such as IS_AUTHENTICATED runs before parsing, while a later check can pass the resulting object to a voter. Invalid input now produces a validation error before that voter is called.

There is a compatibility change here. Voters that previously received the mapping attribute as their subject will now receive the mapped object when the authorization subject names that argument. Those voters may need to be updated to work with the new value.
Entity mapping supports collections and closures
#[MapEntity] can now populate an array argument by calling the repository's findBy() method instead of findOneBy(), using the supplied mapping options. If no matches exist, the result is an empty array rather than a 404 response. This also applies to array arguments in console commands.
The collection behavior covers retrieval, while sorting or limiting still requires the expr option. That option now accepts a closure as well as a string expression, allowing repository calls to be written as PHP that IDEs and static analysis tools can inspect. The closure receives the request and entity repository in a controller; in a console command, it receives the command input instead. Closures in attributes require PHP 8.5.
Query filters accept JSON strings
#[MapQueryString] with its key option can map one query parameter into an object. Until now, that value had to use PHP style array notation, such as ?search[term]=keyboard. Symfony 8.2 also deserializes a string value as JSON, so an API client can send the same filter as ?search={"term":"keyboard","category":"accessories"}.
Validation groups, serialization context and the other mapping options work as before. Malformed JSON returns a 400 response. Requests that use #[MapQueryString] without the key option keep their existing behavior.
Structured media types get a format fallback
APIs often send JSON or XML under structured media types, including application/vnd.api+json and application/ld+json. Symfony associates these media types with named formats such as jsonapi or jsonld; without a matching encoder, #[MapRequestPayload] previously rejected the request with a 415 error.
When the serializer cannot decode the format associated with the media type, Symfony 8.2 now falls back to the format indicated by its structured suffix, such as +json or +xml. Existing encoders for the named formats still take precedence. Applications can also call the new Request::getStructuredSuffixFormat() method to apply the same suffix logic in their own code.
Taken together, these changes reduce the glue code needed between request values, typed arguments and application services. The main migration checks are the updated voter subject and whether existing resolvers or encoders already handle the formats and types an application uses.