Search code examples
apisymfonysymfony4putserialization

Symfony 4 API Rest PUT : Map data to database entity


I'm a begginer in Symfony 4 and I'm developing an API Rest. I want to create a PUT resource that can handle many update cases.

In my case, I have a user with many properties but I will take an example with 3 properties to keep things simple :

User {
    username,
    email,
    password
}

My PUT resource can be called in order to update Username, update Email or update Password. For example, to update Username, the user of my API will send a PUT request with only username :

{
    username: "New username"
}

Same for email and password, he will only send the property he wants to change.

My problem is in my Controller, I have to do things like this :

/**
 * @Rest\Put("/user/{id}")
 * @param Request $request
 * @return View
 */
public function putUserRequest(Request $request)
{
    $userId = $request->get('id');
    $user = $this->doctrine->getRepository(User::class)->findOneBy('id' => $userId);

    $userFromRequest = $this->serializer->deserialize($request->getContent(), User::class, 'json'); 

    if ($userFromRequest->getUsername() != NULL) {
        $user->setUsername($userFromRequest->getUsername())
    }
    if ($userFromRequest->getEmail() != NULL) {
        $user->setEmail($userFromRequest->getEmail())
    }
    if ($userFromRequest->getPassword() != NULL) {
        $user->setPassword($userFromRequest->getPassword())
    }
    // ...
}

In my example I have only 3 properties, but imagine when I have 30 properties.

With Symfony 3 I used forms to validate / save my datas :

$form->submit($request->request->all(), $clearMissing);

Where $clearMissing is false to keep datas not provided by the user of my API. I can't find a way to do it with serializer but I guess I'm doing things wrong.

Thanks.


Solution

  • If I understand correctly, You can use the validator Component like this :

    /**
     * @Rest\Put("/user/{id}")
     * @param Request $request
     * @return View 
     */
    public function putUserRequest(User $user, Request $request, ValidatorInterface $validator)
    {
    
     $data = $request->getContent();
    
     $this->serializer->deserialize($data, User::class, 'json', ['object_to_populate' => $user]); 
    
    //At this moment, the data you sent is merged with your user entity 
    
     /** @var ConstraintViolationList $errors */
      $errors = $validator->validate($user, null ,['groups' => 'user_update']);
    
       if (count($errors) > 0) {
           //return json reponse with formated errors;
       }
    
       //if ok $entityManager->flush() and Response Json with serialization group;
    
        ...
    
    }
    

    In your user class :

    class User implements UserInterface
    {
        /**
         * @Assert\Email(groups={"user_create", "user_update"})
         */
        private $email;
    
        /**
         * @Assert\NotBlank(groups={"user_create", "user_update"})
         * @Assert\Length(min=7, groups={"user_create", "user_update"})
         */
        private $password;
    
        /**
         * @Assert\Length(min=2, groups={"user_create", "user_update"} )
         */
        private $username;
    }
    

    Related Validator component documentation : https://symfony.com/doc/current/validation/groups.html

    You can also check this project : https://github.com/attineos/relation-deserializer