This PR was merged into the 8.1 branch.
Discussion
----------
[HttpKernel] Add option to map empty data with `MapQueryString` and `MapRequestPayload`
| Q | A
| ------------- | ---
| Branch? | 8.1
| Bug fix? | no
| New feature? | yes
| Deprecations? | no
| Issues | -
| License | MIT
When `#[MapQueryString]` or `#[MapRequestPayload]` is used on a nullable/default-valued parameter and the query string or request body is empty, the resolver short-circuits and returns `null` without ever calling the serializer. This prevents custom denormalizers from constructing the object.
This PR adds `bool $mapWhenEmpty = false` to both attributes. When `true`, the resolver passes `[]` to `denormalize()` even when no data is present, giving custom denormalizers a chance to populate the DTO.
```php
public function __construct(
#[MapRequestPayload(mapWhenEmpty: true)] SearchFilters $filters,
) {}
```
**Use case:** a DTO where some fields come from the request and others are injected by a custom denormalizer (e.g. from the security context or session):
```php
class SearchFilters {
public function __construct(
public ?string $keyword = null, // from query string
public int $userId = 0, // set by a custom denormalizer
) {}
}
```
Without `mapWhenEmpty`, an empty query string yields `null`, the denormalizer never runs. With `mapWhenEmpty: true`, denormalization proceeds and the custom denormalizer can populate `$userId`.
Commits
-------
5117bb6f178 [HttpKernel] Add argument `$mapWhenEmpty` to `MapQueryString` and `MapRequestPayload` for always attempting denormalization with empty query and request payload
This PR was merged into the 8.1 branch.
Discussion
----------
Allow usage of expressions for defining validation groups in `#[MapRequestPayload]`
| Q | A
| ------------- | ---
| Branch? | 7.4
| Bug fix? | no
| New feature? | yes
| Deprecations? | no
| License | MIT
This PR adds option to use Expression for defining validation groups when using `#[MapRequestPayload]` and
`#[MapQueryString]`
Inspired by expression options when using `#[IsGranted]`, request and args are available.
Example:
We have route which is used to update user info. Entity `User` has property `type` which can be `admin` or `regular`.
If user is admin, his password must be very strong and they must have image while regular user can have weaker password and image is not required.
Payload used in request:
```php
final readonly class UpdateUserDTO
{
public function __construct(
#[Assert\NotNull(message: 'Admin user must have image', groups: ['admin'])]
public ?string $image,
#[Assert\PasswordStrength(minScore: Assert\PasswordStrength::STRENGTH_MEDIUM, groups: ['regular'])]
#[Assert\PasswordStrength(minScore: Assert\PasswordStrength::STRENGTH_VERY_STRONG, groups: ['admin'])]
public string $password,
) {
}
}
```
We need to dynamically determine which validation group to use based on context (User that is being updated).
Current way of doing this is to manually call `validate` method:
```php
$violations = $this->validator->validate($dto, groups: [$user->getType()]);
// handle violations...
```
This PR allows usage of Expression with access to arguments so above code can be replaced with `#[MapRequestPayload]`
```php
#[Route('/user/{id}', methods: ['PUT'])]
public function updateUserAction(
User $user,
#[MapRequestPayload(
validationGroups: [new Expression('args["user"].getType()')],
)] UpdateUserDTO $dto
)
```
Commits
-------
61c92e545d4 [HttpKernel] Allow usage of expressions for defining validation groups in `#[MapRequestPayload]`
* 7.2: (37 commits)
fix dumped markup
improve amqp connection issues
[Serializer] [ObjectNormalizer] Filter int when using FILTER_BOOL
Fix#53778
Issue 59387-2: make check with prefix more robust
[PropertyInfo] Add missing test
fix tests
[Security][Validators] Review translations.
[validator] Updated Dutch translation
[FrameworkBundle] Fix wiring ConsoleProfilerListener
[HttpKernel] Fix link to php doc
[Lock] Make sure RedisStore will also support Valkey
[Validator] Update sr_Cyrl 120:This value is not a valid slug.
[Validator] Update sr_Latn 120:This value is not a valid slug.
6.4 Missing translations for Italian (it) #59419
tests(notifier): avoid failing SNS test with local AWS configuration
Fix typo ratio comment
chore: PropertyAccess - fix typo in DocBlock
[Validator] Missing translations for Brazilian Portuguese (pt_BR)
fix(dependency-injection): reset env vars with kernel.reset
...
* 7.1: (34 commits)
improve amqp connection issues
[Serializer] [ObjectNormalizer] Filter int when using FILTER_BOOL
Fix#53778
[PropertyInfo] Add missing test
fix tests
[Security][Validators] Review translations.
[validator] Updated Dutch translation
[FrameworkBundle] Fix wiring ConsoleProfilerListener
[HttpKernel] Fix link to php doc
[Validator] Update sr_Cyrl 120:This value is not a valid slug.
[Validator] Update sr_Latn 120:This value is not a valid slug.
6.4 Missing translations for Italian (it) #59419
tests(notifier): avoid failing SNS test with local AWS configuration
Fix typo ratio comment
chore: PropertyAccess - fix typo in DocBlock
[Validator] Missing translations for Brazilian Portuguese (pt_BR)
fix(dependency-injection): reset env vars with kernel.reset
[Translation][Validator] Review Russian translation (114 - 120)
Review validator-related persian translation with id 120
[Scheduler] Clarify description of exclusion time
...
* 6.4: (29 commits)
Fix#53778
[PropertyInfo] Add missing test
fix tests
[Security][Validators] Review translations.
[validator] Updated Dutch translation
[FrameworkBundle] Fix wiring ConsoleProfilerListener
[HttpKernel] Fix link to php doc
[Validator] Update sr_Cyrl 120:This value is not a valid slug.
[Validator] Update sr_Latn 120:This value is not a valid slug.
6.4 Missing translations for Italian (it) #59419
tests(notifier): avoid failing SNS test with local AWS configuration
[Validator] Missing translations for Brazilian Portuguese (pt_BR)
[Translation][Validator] Review Russian translation (114 - 120)
Review validator-related persian translation with id 120
[Scheduler] Clarify description of exclusion time
[HttpFoundation][FrameworkBundle] Reset Request's formats using the service resetter
[Mailer] Fix SMTP stream EOF handling on Windows by using feof()
[Translations] Make sure PL translations validators.pl.xlf follow the same style
[Validator] Checked Turkish validators translations and confirmed
[VarDumper] Fix blank strings display
...
This PR was merged into the 7.1 branch.
Discussion
----------
[DependencyInjection][HttpKernel] Add PHPDoc to attribute classes and properties
| Q | A
| ------------- | ---
| Branch? | 7.1
| Bug fix? | no
| New feature? | yes
| Deprecations? | no
| Tickets | Part of https://github.com/symfony/symfony/issues/51920
| License | MIT
Friendly ping `@GromNaN`, is that what you had in mind? 🙂
Commits
-------
a9030f1aea [DependencyInjection][HttpKernel] Add PHPDoc to attribute classes and properties