我創建了以下類(為示例進行了簡化)并用作@Model。
class Model
{
public string $name;
public function address(): string
{
return "$this->[email protected]";
}
public function isShort(): bool
{
return strlen($this->name) < 3;
}
}
ApiDoc 生成器嘗試將函式解釋為 addSomething 和 isSomething 所以我獲得了模型
{
"name": string,
"address": string,
"short": boolean
}
但我只想
{
"name": string
}
有沒有辦法注釋函式以使它們被 API 檔案渲染器忽略?
uj5u.com熱心網友回復:
為此目的,為您的物體使用序列化組
1.在你的控制器中,匯入
use OpenApi\Annotations as OA;
use Nelmio\ApiDocBundle\Annotation\Model;
2.用所需的模型和序列化組注釋您的方法。(在示例中,這是File:class物體和file:read組)
/**
* @Route("/api/files", methods={"GET"})
* @OA\Response(
* response=200,
* description="Returns the Files",
* @OA\JsonContent(
* type="array",
* @OA\Items(ref=@Model(type=File::class, groups={"file:read"}))
* )
* )
* @OA\Tag(name="files")
*/
public function getFiles(){
//...
}
3.最后在你的序列化組物體中指定告訴api使用哪些屬性。
class File
{
/**
* @Groups({"file:read"})
* @ORM\Column(name="filename", type="string")
*/
private string $filename;
/**
* @ORM\Column(name="extension", type="string")
*/
private string $extension;
}
4.結果。正如我們所見,api doc 忽略了未設定使用的序列化組的屬性,在示例中此屬性為extension.
{
"filename": string
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/448588.html
標籤:php 交响乐 昂首阔步 开放API nelmioapidocbundle
上一篇:.env檔案中何時需要引號?
