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
Duplicate schemas when referencing with Attributes\Model
#2218
Comments
Model
Attributes\Model
I cannot seem to reproduce it. I do see a few typo's in your attributes though. Incorrect
|
@DjordyKoert sorry for the mistakes, I made them without looking at the code, but this is not the problem I really have. I'm going to start over, since it's harder to reproduce. I have 3 entities:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation as Serializer;
#[ORM\Entity]
class Member
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer', options: ['unsigned' => true])]
#[Serializer\Groups([SerializationGroup::MemberAll])]
private ?int $id = null;
// Other variables, constructor, getters and setters...
}
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation as Serializer;
#[ORM\Entity]
class Contact
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer', options: ['unsigned' => true])]
#[Serializer\Groups([SerializationGroup::ContactAll])]
private ?int $id = null;
#[ORM\ManyToOne(targetEntity: Member::class)]
#[Serializer\Ignore]
private Member $member;
// Other variables, constructor, getters and setters...
}
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation as Serializer;
#[ORM\Entity]
class Event
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer', options: ['unsigned' => true])]
#[Serializer\Groups([SerializationGroup::EventAll])]
private ?int $id = null;
#[ORM\ManyToOne(targetEntity: Member::class)]
#[Serializer\Ignore]
private Member $member;
// Other variables, constructor, getters and setters...
} Now i create 2
<?php
namespace App\Dto\Output;
use App\Entity\Contact;
use App\Entity\Member;
use App\Serialization\Groups\SerializationGroup;
use Symfony\Component\Serializer\Annotation as Serializer;
class ContactOutput
{
#[Serializer\Groups([SerializationGroup::ContactOutputAll])]
private readonly Contact $contact;
#[Serializer\Groups([SerializationGroup::ContactOutputAll])]
private readonly Member $member;
public function __construct(Contact $contact, Member $member)
{
$this->contact = $contact;
}
public function getContact(): Contact
{
return $this->contact;
}
public function getMember(): Member
{
return $this->member;
}
}
<?php
namespace App\Dto\Output;
use App\Entity\Event;
use App\Entity\Member;
use App\Serialization\Groups\SerializationGroup;
use Symfony\Component\Serializer\Annotation as Serializer;
class EventOutput
{
#[Serializer\Groups([SerializationGroup::EventOutputAll])]
private readonly Event $event;
#[Serializer\Groups([SerializationGroup::EventOutputAll])]
private readonly Member $member;
public function __construct(Event $event, Member $member)
{
$this->event = $event;
}
public function getEventt(): Event
{
return $this->event;
}
public function getMember(): Member
{
return $this->member;
}
} Now I want to use this outputs on the contact and event controllers <?php
namespace App\Controller;
use OpenApi\Attributes as OA;
class ContactController extends BaseController
{
#[OA\Response(
response: 200,
description: 'Success',
content: new OA\JsonContent(
type: 'array',
items: new OA\Items(ref: new Model(type: ContactOutput::class, groups: [
SerializationGroup::ContactOutputAll,
SerializationGroup::ContactAll,
SerializationGroup::MemberAll
]))
)
)]
#[Route(...)]
public function list(){}
#[OA\Response(
response: 200,
description: 'Success',
content: new OA\JsonContent(
ref: new Model(type: ContactOutput::class, groups: [
SerializationGroup::ContactOutputAll,
SerializationGroup::ContactAll,
SerializationGroup::MemberAll
])
)
)]
#[Route(...)]
public function show(){}
#[OA\Response(
response: 201,
description: 'Success',
content: new OA\JsonContent(
ref: new Model(type: ContactOutput::class, groups: [
SerializationGroup::ContactOutputAll,
SerializationGroup::ContactAll,
SerializationGroup::MemberAll
])
)
)]
#[Route(...)]
public function create(){}
#[OA\Response(
response: 200,
description: 'Success',
content: new OA\JsonContent(
ref: new Model(type: ContactOutput::class, groups: [
SerializationGroup::ContactOutputAll,
SerializationGroup::ContactAll,
SerializationGroup::MemberAll
])
)
)]
#[Route(...)]
public function update(){}
} <?php
namespace App\Controller;
use OpenApi\Attributes as OA;
class EventController extends BaseController
{
#[OA\Response(
response: 200,
description: 'Success',
content: new OA\JsonContent(
type: 'array',
items: new OA\Items(ref: new Model(type: EventOutput::class, groups: [
SerializationGroup::EventOutputAll,
SerializationGroup::EventAll,
SerializationGroup::MemberAll
]))
)
)]
#[Route(...)]
public function list(){}
#[OA\Response(
response: 200,
description: 'Success',
content: new OA\JsonContent(
ref: new Model(type: EventOutput::class, groups: [
SerializationGroup::EventOutputAll,
SerializationGroup::EventAll,
SerializationGroup::MemberAll
])
)
)]
#[Route(...)]
public function show(){}
#[OA\Response(
response: 201,
description: 'Success',
content: new OA\JsonContent(
ref: new Model(type: EventOutput::class, groups: [
SerializationGroup::EventOutputAll,
SerializationGroup::EventAll,
SerializationGroup::MemberAll
])
)
)]
#[Route(...)]
public function create(){}
#[OA\Response(
response: 200,
description: 'Success',
content: new OA\JsonContent(
ref: new Model(type: EventOutput::class, groups: [
SerializationGroup::EventOutputAll,
SerializationGroup::EventAll,
SerializationGroup::MemberAll
])
)
)]
#[Route(...)]
public function update(){}
} |
I have been dealing with the same issue- also using a response object to wraps my entities. I just assumed it was just incorrect usage somewhere else on my end. Coincidentally, this was the root cause for me writing up #2099 |
Ah I see why this is happening at least. This happens because These groups also get passed to the child classes (the You can see the setup that I used to replicate it in #2221. I am not yet sure what the proper way would be to change this behaviour or if this should stay as it is. |
@DjordyKoert Same issue on my side. |
@DjordyKoert Maybe an approach would be to only consider the groups that actually affect the model for creating the hash, and therefore unique models. In the example above, even though we are using 3 groups, the Of course we still need to keep track of the root groups for nested models down the serialization tree. I tried to look at the code, but it won't be an easy task and sadly now I don't have the time :( |
Maybe creating an optional swagger-php processor which removes duplicate schemas is an option? |
I have an entity called
Contact
with a serialization group interface namedContactAll
On Controller, I use the open api attributes with model from
Nelmio\ApiDocBundle\Annotation\Model
When I go to the
OpenApi
schemas I have 4 same schemas with namesContact
,Contact2
,Contact3
andContact4
.There's a way to not duplicate schemas with the same serialization groups?
The text was updated successfully, but these errors were encountered: