Is it the expected behavio for custom controller ?
API Platform version(s) affected: 2.5.1
Description
I create a custom controller to modify an object, but the object is already modified before going through the controller.
Context I wanted to allow a user to modify his email address.
Full Example
So in my entity User i had a specific route to go through a controller as follow :
* "put_manager"={
* "method"="PUT",
* "access_control"="is_granted('ROLE_USER') and object == user",
* "access_control_message"="Only owner can change his email address",
* "path"="/users/{id}/email",
* "controller"=PutEmailAdressAction::class,
* "denormalization_context"={"groups"={"user:modify:email"}}
* }
And in my controller this is what i was doing :
$data = json_decode($request->getContent());
if(!isset($data->email))
throw new HttpException(400, "Parameter is missing");
$user = $user->setEmail($data->email);
But it seems it is not needed because the email is already changed before going through the controller and i wa wondering if it is the expected behavior ?
The thing is that we cannot compare the value of the email sent in the body request with the current email user.
This is a workaround to get the current email value of user and the new one.
In the Body request i changed the name of the value :
{
"new_email": "[email protected]"
}
Since new_email is not the correct variable name in my User entity Api Platform will not change it.
Then in my controller i can compare old value and the new one:
$data = json_decode($request->getContent());
if(!isset($data->new_email))
throw new HttpException(400, "Parameter is missing");
if($data->new_email === $user->getEmail())
throw new HttpException(403, "No Need to change email value");
$user->setEmail($data->new_email);
My question is It is normal that value change before going through controller ?
Thanks a lot for your time, Thomas.
Try changing operation name to something else and check if it's working. You can check registered routes and operations by debugging router and it could be case of overriding default operation provided by core. This is explained in docs while setting operation priority. If that's the case you need to set operation name to something else or disable internal operations in config so only custom logic gets fired. This is one of the perks that many people miss.
Closing per #2987. Questions are best asked on GitHub Discussions or Stack Overflow (api-platform tag). The framework now lives at api-platform/core.