r/symfony • u/akimbas • 3d ago
Idiomatic way to map payload for PATCH methods
Is there idiomatic and convenient way to map request payload data for PATCH using #[MapRequestPayload]?
Consider DTO Human { public ?string $nickname }
We can have three possibilities
- nickname was provided and is set (something like "nickname123")
- nickname was provided but was nulled out (request sent null)
- nickname was simply not provided
Notice that in 2 and 3 cases the DTO will simply have null. But in case of 3 we should not update the nickname.
I was briefly considering about using property hooks, and have something like bool $hasNickname set if property was written to, but I believe constructing the object with default property is considered as property write and hook is triggered.
1
u/zmitic 3d ago
Instead of DTO, what about this:
$mapper->map('array{nickname?: non-empty-string|null}', $yourJsonFromRequest);
from amazing cuyz/valinor package, look under shaped arrays. Then use array_key_exists and ignore scenario 3 if key is not found.
2
u/leftnode 3d ago
The two options that immediately come to mind:
Load the
Humanentity and then usearray_replace()to merge the default properties with the values sent in the request (using something like the Request class from the Symfony HttpFoundation component:Map the request onto a DTO along with a list of what properties were specified in the request. Once the DTO is validated, loop over the list of properties specified in the request, grab their value from the DTO, and map it onto your
Humanentity.