swagger-php 对递归对象属性引用时描述加不上去

运行环境:

laravel 10.42.0
php 8.3.2
darkaonline/l5-swagger 8.5.2
zircote/swagger-php 4.8.3

问题描述

我在学习使用 swagger-php 。目前碰到的问题如下:

我对分类资源做如下注解:

#[OA\Schema(
    schema: "Goods_CategoryResource",
    title: "Goods/CategoryResource",
    description: "分类资源",
    type: "object",
)]

#[OA\Property(property: "id", description: "ID", type: "integer", example: 1)]
    #[OA\Property(property: "name", description: "分类名称", type: "string", example: "分类1")]
    #[OA\Property(property: "parent_id", description: "上级分类ID", type: "integer", example: 0)]
    #[OA\Property(property: "created_at", description: "创建时间", type: "string", example: "2021-08-31 07:00:00")]
    #[OA\Property(property: "updated_at", description: "更新时间", type: "string", example: "2021-08-31 07:00:00")]
    #[OA\Property(
        property: "parent",
        ref: "#/components/schemas/Goods_CategoryResource",
        description: "父分类",
        type: "object",
        example: [
            "id" => 1,
            "name" => "分类1",
            "parent_id" => 0,
            "parent" => new stdClass(),
            "children" => [],
            "ancestors" => [],
            "created_at" => "2021-08-31 07:00:00.00",
            "updated_at" => "2021-08-31 07:00:00.00",
        ],
        additionalProperties: new OA\AdditionalProperties(
            ref: "#/components/schemas/Goods_CategoryResource",
            type: "object",
        ),
    )]
    #[OA\Property(
        property: "children",
        description: "子分类",
        type: "array",
        items: new OA\Items(ref: "#/components/schemas/Goods_CategoryResource"),
        example: [
            [
                "id" => 1,
                "name" => "分类1",
                "parent_id" => 0,
                "parent" => new stdClass(),
                "children" => [],
                "ancestors" => [],
                "created_at" => "2021-08-31 07:00:00.00",
                "updated_at" => "2021-08-31 07:00:00.00",
            ]
        ],
    )]
    #[OA\Property(
        property: "ancestors",
        description: "祖先分类",
        type: "array",
        items: new OA\Items(ref: "#/components/schemas/Goods_CategoryResource"),
        example: [
            [
                "id" => 1,
                "name" => "分类1",
                "parent_id" => 0,
                "parent" => new stdClass(),
                "children" => [],
                "ancestors" => [],
                "created_at" => "2021-08-31 07:00:00.00",
                "updated_at" => "2021-08-31 07:00:00.00",
            ]
        ],
    )]

期望获得其中的 parent 属性结果为:

                    "parent": {
                        "description": "上级分类",
                        "type": "object",
                        "additionalProperties": {
                            "$ref": "#/components/schemas/Goods_CategoryResource"
                        }
                    },

但目前通过 artisan l5-swagger:generate 生成的结果会丢失掉 description 这类属性直接生成:

                    "parent": {
                        "$ref": "#/components/schemas/Goods_CategoryResource"
                    },

有熟悉的小伙伴知道怎么生成我期望的结果吗?

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 1
sanders

:joy: 删包了,折磨了两天,感觉上 swagger-php 不能兼容 openapi 的全部特性。还是手搓 openapi 文档吧。

1个月前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!