Description
API Platform version(s) affected: x3.1
Description
Imagine this property on an ApiResource
:
class Cake
{
// ...
#[ApiProperty(security: 'is_granted("FLAVOR", object)')]
private ?string $flavor = null;
}
During a GET
operation, the object
variable is the Cake
object. Expected! 🥇
However, during a PATCH
operation, the object
variable is null
during deserialization.
I believe this is a bug, as there IS an object available during deserialization for PUT/PATCH. The current behavior makes it impossible to make flavor
READable using the security expression without preventing it from always failing to be WRITEable because the object
is null
(and your voter probably needs the object
).
The workaround is bizarre:
class Cake
{
// ...
#[ApiProperty(security: 'object === null or is_granted("FLAVOR", object)')]
private ?string $flavor = null;
}
You have to allow the security to pass if the object is null
so that deserialization works for PUT/PATCH. Then, during serialization, object
will be populated so the voter will always run.
How to reproduce
Small reproducer! https://github.com/weaverryan/api_platform_null_object_security_reproducer
Possible Solution
Somewhere around
$object
is null, we look also for $context['object_to_populate']
.