Description
TLDR;
See gist linked in example .. implementation can change, it is just POC
Description
The idea is to have an ArgumentValueResolver
which would convert request body (form data, json string and/or query string) to a DTO.
(Initially shared this idea in symfony slack channel earlier today but thought it might get lost in a few hours so figured it is best to open an issue here on github)
So for example a request with body "{ id: 12, name:' foo' }"
is made to a route, we can simply type hint our controller action with a DTO say Person
(containing $id
and $name
property). This argument resolver will automatically inject the Person
DTO with $id
and $name
properties populated from request data.
In order to check whether DTO should be injected by argument resolver or not we can have an interface say RequestDTOInterface
(see the example linked below)
Among many benefits, it would save us from writing a lot of boilerplate code especially with large payloads.
The nice thing about this RFC is, it doesn't add any new annotations and just re-uses what we already have in symfony framework.
Need a neat way to add it to "default argument value resolvers" , cannot add this in HttpKernel
package as it depends upon Serializer
and Validator
components
Example
I made a gist with a sample implementation (and tests), it uses Symfony serializer (DenormalizerInterface
) to convert request content to DTO and also validates it using Symfony validator
https://gist.github.com/faizanakram99/33c97a5f5b834dceeb90003574c7b98d