api-platform / api-platform/core
Open-API (AKA Swagger) documentation does not observe the `Symfony\Component\Serializer\Annotation\SerializedName` name
- 主要语言
- PHP
- 星标
- 2.6k
- 派生
- 980
- 平均合并
- 2 天 2 小时
- 30 天内合并 PR
- 51
描述
**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`:

Note that the schema documentation is correct:

**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):

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**
贡献指南
评估
这个 Issue 还没有评估数据。