Home > Back-end >  Symfony JsonResponse converts snake_case attributes to CamelCase
Symfony JsonResponse converts snake_case attributes to CamelCase

Time:10-04

I am new to symfony, and trying to build a json API. I am trying to return a Doctrine generated Entity called User as a response to one of the endpoints. However, when passing an instance to the json function :

class UserController extends AbstractController
{
    #[Route('/users/id')]
    public function getUser(Request $request, UsersRepository $usersRepository, SessionInterface $session): JsonResponse
    {
      $user = $usersRepository->getUserById(1);

      return $this->json($user, $status = 200, $headers = [], $context = []);
    }
}

I get in response the right User json object, but all parameters have been converted from snake_case to camelCase.

I am expecting this:

{"user_id":1,"email":"[email protected]","reset_token":null,"reset_token_expires":null,"active":null}

But I am getting this:

{"userId":1,"email":"[email protected]","resetToken":null,"resetTokenExpires":null,"active":null}

The parameters in the Entity class have a snake_case naming format, as do the column names in the database.

Why is JsonResponse behaving like this? Is there a way to fix this?

Thank you in advance for your help.

EDIT: below is the code for the Users Entity:

namespace App\Entity;

use App\Repository\UsersRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: UsersRepository::class)]
class Users
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $user_id = null;

    #[ORM\Column(length: 500)]
    private ?string $email = null;

    #[ORM\Column(length: 500, nullable: true)]
    private ?string $reset_token = null;

    #[ORM\Column(type: Types::DATETIME_MUTABLE)]
    private ?\DateTimeInterface $reset_token_expires = null;

    #[ORM\Column]
    private ?bool $active = null;

    public function getUserId(): ?int
    {
        return $this->user_id;
    }

    public function setUserId(int $user_id): self
    {
        $this->user_id = $user_id;

        return $this;
    }

    public function getEmail(): ?string
    {
        return $this->email;
    }

    public function setEmail(string $email): self
    {
        $this->email = $email;

        return $this;
    }

    public function getResetToken(): ?string
    {
        return $this->reset_token;
    }

    public function setResetToken(?string $reset_token): self
    {
        $this->reset_token = $reset_token;

        return $this;
    }

    public function getResetTokenExpires(): ?\DateTimeInterface
    {
        return $this->reset_token_expires;
    }

    public function setResetTokenExpires(\DateTimeInterface $reset_token_expires): self
    {
        $this->reset_token_expires = $reset_token_expires;

        return $this;
    }

    public function isActive(): ?bool
    {
        return $this->active;
    }

    public function setActive(bool $active): self
    {
        $this->active = $active;

        return $this;
    }
}

CodePudding user response:

You are experiencing a weird behavior since the convention is to have CamelCase attributes in Objects and Entities. Usually people ask the opposite: converting CamelCase to snake_case and that can be done using the CamelCaseToSnakeCaseNameConverter.

In your case, you can force the serialized name using this annotation on your Entity:

class User
{
    #[SerializedName('reset_token')]
    private $reset_token;
    //...

To avoid annotating your entity you can also use a yaml configuration (see https://symfony.com/doc/current/components/serializer.html) or a DTO.

CodePudding user response:

Please use name in annotation

#[ORM\Column(name:'email', length: 500)]
private ?string $email = null;

#[ORM\Column(name:'reset_token', length: 500, nullable: true)]
private ?string $reset_token = null;

#[ORM\Column(name:'reset_token_expires', type: Types::DATETIME_MUTABLE)]
private ?\DateTimeInterface $reset_token_expires = null;
  • Related