Skip to content

APIResource mess with denormalization for underscore property #5641

@Grafikart

Description

@Grafikart

API Platform version(s) affected: 3.1

Description
I have a model with the following field

<?php

namespace App\Entity;

use ApiPlatform\Metadata\ApiProperty;
use App\Repository\UserRepository;
use App\State\DebugProcessor;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\Table(name: '`user`')]
class User
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 255)]
    #[Groups("write")]
    private ?string $username = null;

    #[ORM\Column(length: 255)]
    #[Groups("write")]
    private ?string $first_name = null;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getUsername(): ?string
    {
        return $this->username;
    }

    public function setUsername(string $username): static
    {
        $this->username = $username;

        return $this;
    }

    public function getFirstName(): ?string
    {
        return $this->first_name;
    }

    public function setFirstName(string $first_name): static
    {
        $this->first_name = $first_name;

        return $this;
    }
}

If I try to denormalize it I get a model filled with all the properties.

        $data = $denormalizer->denormalize([
            'username' => 'John',
            'first_name' => 'Doe',
        ], User::class, 'json', [
            'groups' => ['read']
        ]);
        dd($data);

Then, If I add APIResource attribute on my model, the denormalization is broken for fields with _ (here first_name)

#[ApiResource]
class User {
 // ...

How to reproduce
Add a proprerty with underscore to an entity and use the denormalizer to set the property. It will stay to null

Possible Solution
I'm currently debugging to understand what API Platform add.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions