api-platform / api-platform/core

Open-API (AKA Swagger) documentation does not observe the `Symfony\Component\Serializer\Annotation\SerializedName` name

Abierto
#8,035 0 comentarios 0 reacciones 0 asignados Ver en GitHub
doctrine Serializer
Lenguaje dominante
PHP
Estrellas
2.6k
Forks
980
Merge medio
2 d 2 h
PR fusionados (30 d)
51

Descripción

**API Platform version(s) affected**: 2.6.7

**Description**
I am working on a Multi-Tenenacy application where all entities are "owned" by a given Tenant. Instead of exposing the GeneratedValue surrogate ID common to all Tenants, I wish to have an auto-incrementing per Tenant and per entity type. It works as desired except that the Open-API (AKA Swagger) documentation does not observe the `Symfony\Component\Serializer\Annotation\SerializedName` name.

**How to reproduce**
For instance, take the below `Test1` entity:

```php
['test1:read']],
denormalizationContext: ['groups' => ['test1:write']],
)]
class Test1 implements HasPublicIdInterface, BelongsToTenantInterface
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
#[ApiProperty(identifier: false)]
//#[Ignore] //It appears that this is only needed if an ApiProperty identifier of the same name does not exist.
private ?int $id = null;

#[SerializedName('id')]
#[ApiProperty(identifier: true)]
#[ORM\Column(type: 'integer')]
#[Groups(['test1:read'])]
private ?int $publicId = null;

#[ORM\Column(type: 'string', length: 255)]
#[Groups(['test1:read', 'test1:write'])]
private $name = null;

#[ORM\Column(type: 'string', length: 255)]
#[Groups(['test1:read', 'test1:write'])]
private $color = null;

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

public function getPublicId(): ?int
{
return $this->publicId;
}

public function setPublicId(int $publicId): self
{
$this->publicId = $publicId;

return $this;
}

public function getName(): ?string
{
return $this->name;
}

public function setName(string $name): self
{
$this->name = $name;

return $this;
}

public function getColor(): ?string
{
return $this->color;
}

public function setColor(string $color): self
{
$this->color = $color;

return $this;
}

// The following is just used to update publicId each time an entity is saved and I tested that it is not causing the issue.

#[ORM\ManyToOne(targetEntity: Tenant::class)]
#[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
#[Ignore]
protected ?Tenant $tenant = null;

public function getTenant(): Tenant
{
return $this->tenant;
}

public function setTenant(Tenant $tenant): self
{
$this->tenant = $tenant;

return $this;
}
public function generatePublicId(): HasPublicIdInterface
{
$this->getTenant()->publicIdSetter($this);

return $this;
}
public function getPublicIdIndex(): ?string
{
return 'test1';
}
}
```
Upon making a request, the responses are correct and the `publicId` and not the GeneratedValue surrogate ID is returned (only GET Collection shown but typical to all):

```json
{
"@context": "/contexts/Test1",
"@id": "/test1s",
"@type": "hydra:Collection",
"hydra:member": [
{
"@id": "/test1s/1",
"@type": "Test1",
"id": 1,
"name": "string",
"color": "string"
},
{
"@id": "/test1s/6",
"@type": "Test1",
"id": 6,
"name": "string",
"color": "string"
},
{
"@id": "/test1s/7",
"@type": "Test1",
"id": 7,
"name": "string",
"color": "string"
},
{
"@id": "/test1s/8",
"@type": "Test1",
"id": 8,
"name": "string",
"color": "string"
},
{
"@id": "/test1s/9",
"@type": "Test1",
"id": 9,
"name": "string",
"color": "string"
}
],
"hydra:totalItems": 5
}
```

But the documentation shows `publicId` instead of just `id`:

![image](https://user-images.githubusercontent.com/2005437/148792548-6022cd71-ca8a-441b-ae25-791147c6c7e0.png)

Note that the schema documentation is correct:

![image](https://user-images.githubusercontent.com/2005437/148792915-bf74b4d1-2da9-447b-bbf6-042d40ce5aad.png)

**Possible Solution**
In an attempt to fix it, I ignored `publicId`'s setter:

```php
class Test1 implements HasPublicIdInterface, BelongsToTenantInterface
{
...

#[Ignore]
public function setPublicId(int $publicId): self
{
$this->publicId = $publicId;

return $this;
}

...
}
```

And now the documentation is correct (Schema documentation not shown but remains correct):

![image](https://user-images.githubusercontent.com/2005437/148794131-b27e0e43-2cc9-4a2f-b571-c83578859975.png)

But the response now returns the GeneratedValue surrogate ID instead of the desired `publicId` (only GET Collection shown but typical to all):
```json
{
"@context": "/contexts/Test1",
"@id": "/test1s",
"@type": "hydra:Collection",
"hydra:member": [
{
"@id": "/test1s/1",
"@type": "Test1",
"name": "string",
"color": "string"
},
{
"@id": "/test1s/2",
"@type": "Test1",
"name": "string",
"color": "string"
},
{
"@id": "/test1s/3",
"@type": "Test1",
"name": "string",
"color": "string"
},
{
"@id": "/test1s/4",
"@type": "Test1",
"name": "string",
"color": "string"
},
{
"@id": "/test1s/5",
"@type": "Test1",
"name": "string",
"color": "string"
}
],
"hydra:totalItems": 5
}
```

**Additional Context**

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.