core icon indicating copy to clipboard operation
core copied to clipboard

UniqueEntity not fully working on PUT

Open GHuygen opened this issue 1 year ago • 0 comments

i posted it on api-platform repo but maybe it's more suited here.

API Platform version(s) affected: 3.2.18

Description
If you have a unique property in your entity it is impossible to update other properties with PUT.

How to reproduce
Example entity:

#[ORM\Entity]
#[ORM\UniqueConstraint(columns: ['name'])]
#[UniqueEntity(fields: ['name'], errorPath: 'name', ignoreNull: false)]
#[ApiResource(
    normalizationContext: ['groups' => ['client_channel:read']],
    denormalizationContext: ['groups' => ['client_channel:write']],
    order: ['name' => 'ASC'],
)]
#[ApiFilter(filterClass: SearchFilter::class, properties: ['name' => 'ipartial'])]
#[ApiFilter(filterClass: OrderFilter::class, properties: ['name'])]
class ClientChannel
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    #[Groups(groups: ['client_channel:read'])]
    private ?int $id = null;

    #[ORM\Column(length: 255)]
    #[Groups(groups: ['client_channel:read', 'client_channel:write'])]
    #[Assert\Length(max: 255)]
    #[Assert\NotBlank]
    public string $name = '';
    
    #[ORM\Column(length: 255)]
    #[Groups(groups: ['client_channel:read', 'client_channel:write'])]
    #[Assert\Length(max: 255)]
    public string $description = '';
}

It is impossible to update the description of this created entity as it consistently returns the following response:

{
    "@id": "/validation_errors/23bd9dbf-6b9b-41cd-a99e-4844bcf3077f",
    "@type": "ConstraintViolationList",
    "status": 422,
    "violations": [
        {
            "propertyPath": "name",
            "message": "This value is already used.",
            "code": "23bd9dbf-6b9b-41cd-a99e-4844bcf3077f"
        }
    ],
    "detail": "name: This value is already used.",
    "hydra:title": "An error occurred",
    "hydra:description": "name: This value is already used.",
    "type": "/validation_errors/23bd9dbf-6b9b-41cd-a99e-4844bcf3077f",
    "title": "An error occurred"
}

Additional Context
It seems that the entity, which is being updated, is not fully loaded during validation. In the UniqueEntityValidator, there is this code to exclude its own

        if (!$result || (1 === \count($result) && current($result) === $entity)) {
            return;
        }

but $entity is not holding the id of the entity that needs updating.

Edit: image In the screenshot, it can be seen that I'm attempting to update the entity with an ID of "1". Interestingly, the cause of failure is reported to be the same entity with an ID of "1".

If I'm not using the intended way, please let me know the correct method.

GHuygen avatar Apr 04 '24 06:04 GHuygen