Skip to content

[HttpKernel] Map a list of items with MapRequestPayload attribute #54385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 6, 2024

Conversation

yceruto
Copy link
Member

@yceruto yceruto commented Mar 23, 2024

Q A
Branch? 7.1
Bug fix? no
New feature? yes
Deprecations? no
Issues -
License MIT

Request:

Content-Type: application/json
[
    {"price": 50},
    {"price": 23}
]

Controller:

class MyController
{
    public function __invoke(
        #[MapRequestPayload(type: Price::class)] array $prices,
    ): Response {
        // do something with $prices -> array<Price>
    }
}

Cheers!

@carsonbot carsonbot added this to the 7.1 milestone Mar 23, 2024
@yceruto yceruto force-pushed the feature/array_payload branch 2 times, most recently from ef669f4 to 24f3cc7 Compare March 24, 2024 16:00
@smnandre
Copy link
Member

Couple of questions there:

  • Would this work with forms, or XML ?
  • Does the validation still work ?

@yceruto
Copy link
Member Author

yceruto commented Mar 25, 2024

Couple of questions there:
Would this work with forms, or XML ?

The feature behind this improvement is completely agnostic to the request format. It will work with any data format supported by the Serializer component. See more here: https://symfony.com/doc/current/components/serializer.html#handling-arrays

Does the validation still work ?

Yes, the validator component does support any iterable value.

@smnandre
Copy link
Member

I did not see how a form could send data as flat array at first... hence my question. But i'm indeed not aware of all the possibilties offered by the serializer there.

(for the validation i was referring to the validationGroups attribute.. just to know how this would compare to the nested array of DTOs for instance)

@yceruto
Copy link
Member Author

yceruto commented Mar 25, 2024

(for the validation i was referring to the validationGroups attribute.. just to know how this would compare to the nested array of DTOs for instance)

If set, it will be applied to each element of the collection as implemented here:

private function validateEachObjectIn(iterable $collection, string $propertyPath, array $groups, ExecutionContextInterface $context): void

@yceruto yceruto force-pushed the feature/array_payload branch from 24f3cc7 to 39e1651 Compare March 25, 2024 15:29
@yceruto yceruto force-pushed the feature/array_payload branch 4 times, most recently from 42bbd07 to 38a7cdd Compare March 25, 2024 18:16
@yceruto yceruto force-pushed the feature/array_payload branch from 38a7cdd to 948e65b Compare March 25, 2024 19:49
@yceruto yceruto force-pushed the feature/array_payload branch from 948e65b to 00ec79a Compare March 25, 2024 21:47
@yceruto yceruto force-pushed the feature/array_payload branch from 00ec79a to 5bca3e0 Compare March 26, 2024 13:05
@chalasr
Copy link
Member

chalasr commented Mar 30, 2024

At some point, the new TypeInfo component could certainly help guessing what $type is (when possible)

@yceruto yceruto added the ❄️ Feature Freeze Important Pull Requests to finish before the next Symfony "feature freeze" label Apr 2, 2024
@yceruto yceruto force-pushed the feature/array_payload branch from 5bca3e0 to 3f72143 Compare April 5, 2024 19:59
@chalasr
Copy link
Member

chalasr commented Apr 6, 2024

Nice one. Thanks Yonel!

@chalasr chalasr merged commit 9549cc2 into symfony:7.1 Apr 6, 2024
7 of 10 checks passed
@yceruto yceruto deleted the feature/array_payload branch April 6, 2024 13:18
javiereguiluz added a commit to symfony/symfony-docs that referenced this pull request Apr 16, 2024
…e (yceruto)

This PR was merged into the 7.1 branch.

Discussion
----------

Documenting items type in the MapRequestPayload attribute

Fixes #19761

PR: symfony/symfony#54385

Commits
-------

fa1934c Documenting items type in the MapRequestPayload attribute
@fabpot fabpot mentioned this pull request May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature HttpKernel ❄️ Feature Freeze Important Pull Requests to finish before the next Symfony "feature freeze" Status: Reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants