NelmioApiDocBundle icon indicating copy to clipboard operation
NelmioApiDocBundle copied to clipboard

OpenAPI Response Schema Not Reflecting Serialization Context for Nested Groups

Open ferhado opened this issue 2 years ago • 3 comments

Issue Description

The OpenAPI-generated response schema for my User entity does not properly reflect the serialization context defined for a nested property. Specifically, the company property within User should serialize with the company:relation group when the :company group is used.

Example for Clarification

In UserController.php:

#[OA\Response(
  response: 200,
  description: "",
  content: new Model(type: User::class, groups: ["detail", ":company"])
)]
#[Route('/users/{id<\d+>}', methods: ["GET"])]
public function getUserById(Request $request) {
  $id = $request->get("id");
  $user = $this->userRepo->find($id);
  return $this->json($user, context: ["groups" => ["detail", ":company"]]);
}

In User.php:

// User Entity:
class User implements UserInterface, PasswordAuthenticatedUserInterface {
  #[ORM\Id]
  #[ORM\GeneratedValue]
  #[ORM\Column]
  #[Groups(["user:rel", "list", "detail"])] // expected for main Entity
  private ?int $id = null;

  #[ORM\Column(length: 100)]
  #[Groups(["user:rel", "list", "detail", "write"])] // expected for main Entity
  private ?string $name = null;

  #[ORM\Column(length: 180, unique: true)]
  #[Groups(["user:rel", "list", "detail", "write", "auth"])] // expected for main Entity
  private ?string $username = null;

  #[ORM\Column]
  private array $roles = [];

  #[ORM\Column]
  #[Groups(["write", "auth"])]
  private ?string $password = null;

  #[Groups([":company"])] // expected for main Entity
  #[Context(context: ["groups" => ["company:relation"]])] // <------------- Not reflected Context
  #[ORM\ManyToOne
  #[ORM\JoinColumn(nullable: true)]
  private ?Company $company = null;

  #[ORM\Column(type: "boolean")]
  #[Groups(["list", "detail", "write"])] // expected for main Entity
  private bool $isActive = true;

In Company.php:

class Company {
  #[ORM\Id]
  #[ORM\GeneratedValue]
  #[ORM\Column]
  #[Groups(["list", "detail", "company:relation"])] // expected for nested Entity
  private ?int $id = null;

  #[ORM\Column(length: 100, nullable: true)]
  #[Groups(["list", "detail", "company:relation"])] // expected for nested Entity
  private ?string $name = null;

  #[ORM\Column(length: 10, nullable: true)]
  #[Groups(["list", "detail", "company:relation"])] // expected for nested Entity
  private ?string $shortName = null;

  #[Groups(["list", "detail"])]
  #[ORM\Column(length: 20, nullable: true)]
  private ?string $taxNumber = null;

  #[Groups(["list", "detail"])]
  #[ORM\Column(length: 20, nullable: true)]
  private ?string $vatId = null;

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

Expected Behavior in screenshot:

The expected schema should respect the company:relation group when :company is used, showing only the fields from the company entity that are in the company:relation group.

Actual Behavior

The actual schema ignores the company:relation group and shows a different set of fields.

Steps to Reproduce

  1. Define serialization groups in entities.
  2. Use the #[Context(context: ["groups" => ["..."]])] annotation to specify nested serialization groups.
  3. Generate OpenAPI schema.

Is there a way to make the OpenAPI schema respect the nested serialization context defined in the Symfony serializer annotations?

Screenshot 2023-11-07 165027

ferhado avatar Nov 07 '23 15:11 ferhado

Additionally to what was mentioned, it does not reflect the context for datetime objects with format description. For example,

#[Context([
    DateTimeNormalizer::FORMAT_KEY => 'Y-m-d',
])]

AlexMinaev19 avatar Feb 07 '24 17:02 AlexMinaev19

Bump. I'm experiencing this issue since upgrading from v2 to v3.

trobertsonacuity avatar Mar 13 '24 21:03 trobertsonacuity