Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[RFC] Injecting DTO into controller actions (DTOArgumentValueResolver) #36093

Copy link
Copy link
Closed
@faizanakram99

Description

@faizanakram99
Issue body actions

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    FeatureHttpKernelRFCRFC = Request For Comments (proposals about features that you want to be discussed)RFC = Request For Comments (proposals about features that you want to be discussed)Stalled

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.