Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpKernel] Enhance MapRequestPayload adding format and validation group #50029

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 20, 2023

Conversation

renanbr
Copy link
Contributor

@renanbr renanbr commented Apr 16, 2023

Q A
Branch? 6.3
Bug fix? no
New feature? yes
Deprecations? no
Tickets Implements #49138 (comment)
License MIT
Doc PR not yet

This PR enhances MapRequestPayload (merged via #49138) by allowing users to restrict format and pick validation groups.

# src/Controller/HelloController.php

namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Constraints as Assert;

#[AsController]
final class HelloController
{
    #[Route('/hello', methods: ['POST'])]
    public function __invoke(
        #[MapRequestPayload(acceptFormat: 'json', validationGroups: 'strict')] MyObject $object,
    ): Response {
        // framework will...
        //     1. deserialized HTTP content from json, and throws exception in case of mismatch
        //     2. validate ONLY constraint in the "strict" group, and throws exception in case of invalidation
    }
}

@renanbr renanbr force-pushed the map-request-content-enhancement branch from 3ae9131 to d3a290f Compare April 17, 2023 17:50
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost GTM

@renanbr renanbr force-pushed the map-request-content-enhancement branch 2 times, most recently from ba63b5b to 820d1b1 Compare April 18, 2023 20:09
@renanbr renanbr force-pushed the map-request-content-enhancement branch from 820d1b1 to a7a403c Compare April 19, 2023 17:15
…roup

apply feedbacks

revert argument type

better properties name

attempt to fix CI

static providers
@renanbr renanbr force-pushed the map-request-content-enhancement branch from 191a579 to 068fb4d Compare April 19, 2023 18:53
@nicolas-grekas
Copy link
Member

Thank you @renanbr.

@nicolas-grekas nicolas-grekas merged commit 887c960 into symfony:6.3 Apr 20, 2023
@renanbr renanbr deleted the map-request-content-enhancement branch April 20, 2023 09:39
fabpot added a commit that referenced this pull request Apr 21, 2023
…group (renanbr)

This PR was merged into the 6.3 branch.

Discussion
----------

[HttpKernel] Enhance MapQueryString adding validation group

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | continuity of #50029 and #49138
| License       | MIT
| Doc PR        | n/a

- Introduces `$validationGroups` property to the `MapQueryString` attribute to work as same as in `MapRequestPayload`
- Renames `MapQueryString` property `$context` to `$serializationContext` to harmonize with `MapRequestPayload`

```php
# src/Controller/HelloController.php

namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\HttpKernel\Attribute\MapQueryString;
use Symfony\Component\Routing\Annotation\Route;

#[AsController]
final class HelloController
{
    #[Route('/hello', methods: ['GET'])]
    public function __invoke(
        #[MapQueryString(validationGroups: 'strict')] MyObject $object,
    ): Response {
        // ...
    }
}
```

Commits
-------

8aad8b9 [HttpKernel] Enhance MapQueryString adding validation group
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants