mirror of
https://github.com/symfony/http-kernel.git
synced 2026-03-24 01:12:09 +01:00
8.1
MapQueryString and MapRequestPayload (Jeroeny)
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
feature #52134 [HttpKernel] Add option to map empty data with
MapQueryString and MapRequestPayload (Jeroeny)
feature #52134 [HttpKernel] Add option to map empty data with
MapQueryString and MapRequestPayload (Jeroeny)
feature #52134 [HttpKernel] Add option to map empty data with
MapQueryString and MapRequestPayload (Jeroeny)
feature #52134 [HttpKernel] Add option to map empty data with
MapQueryString and MapRequestPayload (Jeroeny)
HttpKernel Component
The HttpKernel component provides a structured process for converting a Request into a Response by making use of the EventDispatcher component. It's flexible enough to create full-stack frameworks, micro-frameworks or advanced CMS systems like Drupal.
Resources
Languages
PHP
100%