我在 Symfony 中使用 Open Api 3(確切地說是 NelmioApiDocBundle)來記錄 API,我有以下代碼:
* @Route("/login", name="user_login", methods={"POST"})
*
* @OA\Response(
* response=200,
* @OA\JsonContent(
* type="object",
* @OA\Property(property="code", type="number"),
* @OA\Property(property="error", type="boolean"),
* @OA\Property(property="message", type="string")
* )
* )
這有效并向我展示了以下內容:

我想知道的是是否有更簡單的方法來定義這些回應,所以我不必在每個端點寫這么多行。
我希望有一種方法可以將 JSON 檔案定義為介面并從那里讀取。我已經嘗試了一千件事,但對我沒有任何作用,我不知道該怎么做,我已經放棄了。
非常感謝!
我曾嘗試使用@Model、@Schema、修改 YAML 檔案......等等......我已經閱讀了檔案,但在這個版本的 Open Api 中我沒有看到任何有效的例子。
uj5u.com熱心網友回復:
如果使用模型,可以放入以下
@OA\JsonContent(ref=@Model(type=LoginResponse::class))
你的回應類應該是這樣的:
use OpenApi\Annotations as OA;
class LoginResponse
{
/** @OA\Property(type="number") */
public $code;
/** @OA\Property(type="boolean") */
public $error;
/** @OA\Property(type="string") */
public $message;
}
確保這些屬性可公開訪問并且您已symfony/property-info安裝。您也可以使用 jms/serializer,但這需要更多的作業
更多資訊請訪問:https ://symfony.com/bundles/NelmioApiDocBundle/current/index.html#use-models
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/521197.html
標籤:php交响乐昂首阔步开放APInelmioapidocbundle
上一篇:查詢包含帶有術語的字串的陣列
下一篇:Symfony6.1Doctrine給出錯誤“無法決議類“App\Entity\Officecurrencymax”的列“id”的型別'
