Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UniqueEntity not fully working on PUT #6287

Open
GHuygen opened this issue Apr 4, 2024 · 0 comments
Open

UniqueEntity not fully working on PUT #6287

GHuygen opened this issue Apr 4, 2024 · 0 comments

Comments

@GHuygen
Copy link

GHuygen commented Apr 4, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant