我正在使用帶有 DTO 的 Api 平臺。我想了解是否可以在 POST 中而不是在 PUT 中擁有一個欄位。在我的用例中,我有一個需要電子郵件欄位的客戶,因此該欄位應該存在于 GET 和 POST 中。無論如何,不??應更改電子郵件,因此該欄位不應出現在 PUT 中。這是定義我的資源的 YAML 檔案:
App\Entity\Customer:
attributes:
input: 'App\DTO\CustomerInput'
output: 'App\DTO\CustomerOutput'
collectionOperations:
get:
path: /customer
post:
path: /customer
itemOperations:
get:
path: /customer/{fiscalId}
put:
path: /customer/{fiscalId}
這是輸入 DTO:
class CustomerInput
{
public string $fiscalId;
public string $email;
public string $businessName;
}
我遇到的主要問題是不要在 Open API 檔案中將電子郵件欄位公開為 POST 的有效輸入欄位。提前致謝!
uj5u.com熱心網友回復:
首先,我認為您應該指定您使用的 api-platform 版本,因為處理 DTO 的方式在 2.6 和 2.7 版本之間發生了變化。因此,如果您使用的是 2.6 或更低版本,則可以像這樣(Api Platform docs)為每個操作指定自定義輸入和輸出:
App\Entity\Book:
collectionOperations:
create:
method: POST
input: App\Dto\CreateBook
output: App\Dto\BookOutput
itemOperations:
update:
method: PUT
input: App\Dto\UpdateBook
output: App\Dto\BookOutput
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/513954.html
上一篇:Symfony翻譯在沒有引數`%count%`或任何其他自定義引數的情況下獲取值
下一篇:查找具有指定屬性的實體
