PHP Swagger OpenAPI通用类型

发布于 2025-01-21 20:52:53 字数 1258 浏览 2 评论 0 原文

我正在使用 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);
    }
}

I'm using zircote/swagger-php for annotations to generate swagger for my php WebApi.
I have my generic model HttpResponse, it has 2 common properties (messages and statusCode) but data is generic and has different type for every endpoint.

I'm trying to annote it via zircote/swagger-php and use different model for this data property for every endpoint. Can anyone tell me how I can do this?

Right now I'm trying something like below, but its giving me first of those anyOf to every request instead allow me specific which one i want.

/**
 * @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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陪你到最终 2025-01-28 20:52:53

可能是您应该使用 Oneof 而不是 anyof

但是,现在设置了事物,现在每个端点都将返回 httpresponse ,并选择不理想的数据结构。

更好的是,恕我直言,是让每个端点准确定义其返回的数据(除非我误解了您的代码)。

在这种情况下,我建议您将响应架构分为基础,并明确 @OA \ response 每个端点的定义。

缺点可能是注释不能准确反映您的代码。但是,规格将更明确地反映出端点返回的数据结构。

/**
 * @OA\Schema()
 */
class BaseResponse
{
    /**
     * @OA\Property(type="array", @OA\Items(ref="#/components/schemas/Message"))
     */
    public $messages;

    /**
     * @OA\Property(ref="#/components/schemas/HttpResponseType")
     */
    public $statusCode;
}

/**
 * @OA\Schema()
 */
class HolidayRequestSpecificInput{
    /**
     * @OA\Property()
     */
    public $location;

}

/**
 * @OA\Get(path="/holiday",
 *     @OA\Response(
 *         response="200",
 *         description="OK",
 *         @OA\JsonContent(
 *             allOf={
 *                 @OA\Schema(ref="#/components/schemas/BaseResponse"),
 *                 @OA\Schema(
 *                     @OA\Property(property="data", ref="#/components/schemas/HolidayRequestSpecificInput")
 *                 )
 *             }
 *         )
 *     )
 * )
 */
class Controller{}

Might be that you should use oneOf rather than anyOf.

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.

/**
 * @OA\Schema()
 */
class BaseResponse
{
    /**
     * @OA\Property(type="array", @OA\Items(ref="#/components/schemas/Message"))
     */
    public $messages;

    /**
     * @OA\Property(ref="#/components/schemas/HttpResponseType")
     */
    public $statusCode;
}

/**
 * @OA\Schema()
 */
class HolidayRequestSpecificInput{
    /**
     * @OA\Property()
     */
    public $location;

}

/**
 * @OA\Get(path="/holiday",
 *     @OA\Response(
 *         response="200",
 *         description="OK",
 *         @OA\JsonContent(
 *             allOf={
 *                 @OA\Schema(ref="#/components/schemas/BaseResponse"),
 *                 @OA\Schema(
 *                     @OA\Property(property="data", ref="#/components/schemas/HolidayRequestSpecificInput")
 *                 )
 *             }
 *         )
 *     )
 * )
 */
class Controller{}

飘落散花 2025-01-28 20:52:53

也许您应该使用此处描述的歧视器:

maybe you should use a discriminator as described here :
https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文