DTO Api 平台 v3

huangapple go评论60阅读模式
英文:

DTO Api Platform v3

问题

使用Api Platform的v3版本时,在处理DTO时遇到以下问题:

  • @id字段看起来像"/.well-known/genid/01e546d3f38c0b5d3b8a",但我找不到获取实体的IRI的方法。
  • 在使用PUT/PATCH方法时,如何初始化DTO,在v2中,我使用了ApiPlatform\Core\DataTransformer\DataTransformerInitializerInterface。

在提问之前,我尝试了不同的解决方案,并深入研究了源代码,但我找不到解决这两个问题的方法。

英文:

Using the v3 of Api Platform, I'm encountering the following problems when working with DTO:

  • The @id field looks like "/.well-known/genid/01e546d3f38c0b5d3b8a", and I don't find anyway to get the IRI of the entity instead.
  • How to initialize the DTO when working with PUT/PATCH methods, in v2 I was using ApiPlatform\Core\DataTransformer\DataTransformerInitializerInterface.

I've tried different solutions and digged in into the source code before asking, and i don't find any way to solve these two problems.

答案1

得分: 1

你现在需要在DTO上设置GET操作,以便正确生成@id字段,参见此问题以供参考。

因此,如果你有这个实体:

<?php
#[Get(
        output: MyEntityDTO::class,
        provider: MyEntityProvider::class
)]
class MyEntity {
    private int $id;
}

你将会有这个DTO:

<?php
#[Get(shortName: 'MyEntity')]
class MyEntityDTO {
    public int $id;
}

请注意,你需要设置shortName以便在Swagger文档中将所有操作放在同一个MyEntity标签下。

至于你的第二个问题,你现在需要使用Providers和Processors来处理api-platform v3中数据的状态,请查看文档。在进行PUT/PATCH操作时,你将需要使用一个Processor。

英文:

You now have to set your GET operation on the DTO in order to have the @id field correctly generated, see this issue for reference.

So if you have this entity:

<?php
#[Get(
        output: MyEntityDTO::class,
        provider: MyEntityProvider::class
)]
class MyEntity {
    private int $id;
}

You'll have this DTO:

<?php
#[Get(shortName: 'MyEntity')]
class MyEntityDTO {
    public int $id;
}

Note that you have to set the shortName to allow having all your operation under the same MyEntity tag in the swagger documentation.

For your second question, you now have to use Providers and Processors to handle the state of your data in api-platform v3, see the documentation. In the case of a PUT/PATCH operation, you'll have to use a processor.

huangapple
  • 本文由 发表于 2023年3月3日 21:34:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75627741.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定