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

Specific order of operations in #ApiResource annotation causes incorrect API schema (using wrong serialization group) #2695

Open
jhogervorst opened this issue Apr 24, 2024 · 0 comments

Comments

@jhogervorst
Copy link

API Platform version(s) affected: v3.2.21

Description / How to reproduce

<?php

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Post;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;

#[ApiResource(
    operations: [
        new GetCollection(normalizationContext: ['groups' => ['test-get-collection']]),
        new Post(denormalizationContext: ['groups' => ['test-post']], validationContext: ['groups' => ['test-post']]),
        new Get(),
    ],
    normalizationContext: ['groups' => ['test-get']],
)]
class TestEntity
{
    #[Groups(['test-get-collection', 'test-get'])]
    private int $id;

    #[Assert\Length(max: 20)]
    #[Assert\Length(max: 10, groups: ['test-post'])]
    #[Groups(['test-get-collection', 'test-get', 'test-post'])]
    private ?string $property = null;

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

    public function getProperty(): ?string
    {
        return $this->property;
    }

    public function setProperty(?string $property): self
    {
        $this->property = $property;
        return $this;
    }
}

CleanShot 2024-04-24 at 17 20 26@2x


When moving new Get above new Post, it works correctly (no other changes made):

#[ApiResource(
    operations: [
        new GetCollection(normalizationContext: ['groups' => ['test-get-collection']]),
        new Get(),
        new Post(denormalizationContext: ['groups' => ['test-post']], validationContext: ['groups' => ['test-post']]),
    ],
    normalizationContext: ['groups' => ['test-get']],
)]

CleanShot 2024-04-24 at 17 22 12@2x

(Sorry for the lack of a clear textual description, but I'm not familiar enough with API Platform and Symfony to use all the right terms 😊 I tried to make a good minimal reproducible example, hopefully this is clear enough.)

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