PHP Swagger OpenAPI通用类型
我正在使用 Zircote/Swagger-php
用于注释以生成我的php webapi的Swagger。
我有我的通用模型 httpresponse
,它具有2个常见属性(消息
和 statuscode
),但是 data
是通用和每个端点都有不同的类型。
我正在尝试通过 Zircote/Swagger-PHP
对其进行报道,并为此 Data
属性使用不同的模型。谁能告诉我如何做到这一点?
现在,我正在尝试以下类似的事情,但是它首先给了我每个请求的 anyof ,而是让我具体说明我想要哪个请求。
/**
* @OA\Schema()
*/
class HttpResponse
{
/**
* @OA\Property(anyOf={
* @OA\Schema(ref="#/components/schemas/HolidayRequestSpecificInput"),
* @OA\Schema(ref="#/components/schemas/AvailableHolidayDatesApiModel")
* })
*/
public $data;
/**
* @OA\Property(type="array", @OA\Items(ref="#/components/schemas/Message"))
*/
public $messages;
/**
* @OA\Property(ref="#/components/schemas/HttpResponseType")
*/
public $statusCode;
public function __construct($statusCode = HttpResponseType::Ok, $data = null)
{
$this->data = $data;
$this->messages = [];
$this->statusCode = $statusCode;
}
public function addMessages($messages)
{
foreach ($messages as $msg)
array_push($this->messages, $msg);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能是您应该使用
Oneof
而不是anyof
。但是,现在设置了事物,现在每个端点都将返回
httpresponse
,并选择不理想的数据结构。更好的是,恕我直言,是让每个端点准确定义其返回的数据(除非我误解了您的代码)。
在这种情况下,我建议您将响应架构分为基础,并明确
@OA \ response
每个端点的定义。缺点可能是注释不能准确反映您的代码。但是,规格将更明确地反映出端点返回的数据结构。
Might be that you should use
oneOf
rather thananyOf
.However, this way things are set up right now each endpoint would return
HttpResponse
with a choice of data structures which is not ideal.Better, IMHO, would be to have each endpoint define exactly the data that it returns (unless I am misunderstanding your code).
In that case I'd suggest you split the response schema into a base and explicit
@OA\Response
definitions for each endpoint.The downside might be that the annotations do not reflect exactly your code. However, the spec would reflect more explicit the data structures returned by the endpoints.
也许您应该使用此处描述的歧视器:
maybe you should use a discriminator as described here :
https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/