如何解决具有一个变量差异的通用输出模式
快速问题,我正在使用 swagger-php 来注释我的 API。 API 的输出始终具有相同的基本结构,并在多个端点上使用:
[
{
"status": "success","count": 0,"schema": "Hash.Hash","items": [
{ ... },{ ... },**LIST OF ITEMS IN THE schema Hash.Hash**
],"log": [
"string"
]
}
]
/**
* @OA\Schema(schema="OutputModel",* @OA\Property( property="status",default="success",type="string",format="string" ),* @OA\Property( property="count",default=0,type="integer",format="int32" ),* @OA\Property( property="schema",default="Hash.Hash",format="schema" ),* @OA\Property( property="items",type="array",@OA\Items(ref="#/components/schemas/Hash.Hash")),* @OA\Property( property="log",@OA\Items(type="string") ),* )
*/
/**
* @OA\Get(
* path="/api/v1/hash/",* @OA\Response(response="200",description="List of added hashes",* @OA\JsonContent(@OA\Items(ref="#/components/schemas/OutputTest")),* )
* )
*/
我在这里面临的挑战是,在输出模型中,属性 items
并不总是模式 Hash.Hash,而是每个输出都不同。我目前的解决方案是为每个端点创建一个新的输出模型,只需更改 ref="#/components/schemas/Hash.Hash" 以获得正确的架构,但这看起来效率不高。有没有办法使用一个通用的 OutputModel、具有不同模式的不同端点来完成这项工作?
解决方法
经过大量阅读/修改后,我找到了最适合我的解决方案:
已替换:
@OA\Property(
property="items",type="array",@OA\Items(ref="#/components/schemas/Hash.Hash")
)
用于:
@OA\Property( property="items",description="TODO",@OA\Items(anyOf={
@OA\Schema(ref="#/components/schemas/Hash.Hash"),@OA\Schema(ref="#/components/schemas/Show.ShowsFollow"),...etc etc...
}))
并替换:
@OA\Property( property="schema",default="Hash.Hash",type="string",format="schema" ),
用于:
@OA\Property(
property="schema",default="",format="schema",enum={"Hash.Hash","Show.Showsfollow",... etc,etc ...}
)
此定义对我有用,因为 schema
字符串定义了对象类型,而 items
填充了该对象。
领带。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。