yapi icon indicating copy to clipboard operation
yapi copied to clipboard

导入swagger3.0的接口文档时对于allof字段解析似乎有问题?

Open floating-yuan opened this issue 4 years ago • 4 comments

版本号

yapi: 由于不方便查看yapi项目的信息,所以版本未知,但是应该是比较新的版本,今年的项目,而且导入的时候显示支持swagger v2.0+

swagger/openapi: "3.0.0"

什么问题

两个接口的返回结果schema结构继承了基础的返回结果schema结构,即:
/components/schemas/List1Response/components/schemas/List2Response
继承了
/components/schemas/BaseResponse
然后导入后,两个接口的都互相包含了对方的接口字段,即接口1包含了接口2的返回字段,接口2包含了接口1的返回字段,在我的示例代码中,即为get-list1包含了p2字段,get-list2包含了p1字段,而期望的结果应该是get-list1只包含p1字段,get-list2只包含p2字段 ,我看各种资料,想要实现继承好像是用allof来实现吧?难道是我搞错了?麻烦yapi的开发人员大佬们帮忙看看!多谢了!

见截图: image

什么浏览器

chrome

代码

生成的接口文档json文件

{
    "openapi": "3.0.0",
    "info": {
        "title": "业务迭代",
        "version": "0.1"
    },
    "paths": {
        "/get-list1": {
            "post": {
                "summary": "列表1",
                "operationId": "/get-list1",
                "requestBody": {
                    "content": {
                        "application/json": {
                            "schema": {
                                "properties": {
                                    "app": {
                                        "type": "integer"
                                    }
                                },
                                "type": "object"
                            }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "OK",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/List1Response"
                                }
                            }
                        }
                    }
                }
            }
        },
        "/get-list2": {
            "post": {
                "summary": "列表2",
                "operationId": "/get-list2",
                "requestBody": {
                    "content": {
                        "application/json": {
                            "schema": {
                                "properties": {
                                    "app": {
                                        "type": "integer"
                                    }
                                },
                                "type": "object"
                            }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "OK",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/List2Response"
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    "components": {
        "schemas": {
            "BaseResponse": {
                "required": [
                    "code",
                    "msg",
                    "data"
                ],
                "properties": {
                    "code": {
                        "description": "结果码",
                        "type": "integer"
                    },
                    "msg": {
                        "description": "结果信息",
                        "type": "string"
                    },
                    "data": {
                        "description": "结果数据",
                        "type": "object"
                    }
                },
                "type": "object"
            },
            "List1Response": {
                "type": "object",
                "allOf": [
                    {
                        "$ref": "#/components/schemas/BaseResponse"
                    },
                    {
                        "properties": {
                            "data": {
                                "properties": {
                                    "p1": {
                                        "type": "string"
                                    }
                                },
                                "type": "object"
                            }
                        },
                        "type": "object"
                    }
                ]
            },
            "List2Response": {
                "type": "object",
                "allOf": [
                    {
                        "$ref": "#/components/schemas/BaseResponse"
                    },
                    {
                        "properties": {
                            "data": {
                                "properties": {
                                    "p2": {
                                        "type": "string"
                                    }
                                },
                                "type": "object"
                            }
                        },
                        "type": "object"
                    }
                ]
            }
        }
    }
}

定义代码


 统一接口返回结果结构体
 @OA\Schema(
  schema="BaseResponse",
  type="object",
  required={"code","msg","data"},
  @OA\Property(
      property="code",
      type="integer",
      description="结果码",
  ),
  @OA\Property(
      property="msg",
      type="string",
      description="结果信息",
  ),
  @OA\Property(
      property="data",
      type="object",
      description="结果数据",
  ),
)

@OA\Post(
    path="/get-list1",
    summary="列表1",
    @OA\RequestBody(
        @OA\MediaType(
            mediaType="application/json",
            @OA\Schema(
                @OA\Property(
                    property="app",
                    type="integer",
                    default=0,
                ),
            )
        )
    ),
    @OA\Response(
        response=200,
        description="OK",
        @OA\JsonContent(ref="#/components/schemas/List1Response")
    ),
)

 返回结果
 @OA\Schema(
  schema="List1Response",
  type="object",
  allOf={
      @OA\Schema(ref="#/components/schemas/BaseResponse"),
      @OA\Schema(
         @OA\Property(
             property="data",
             type="object",
                 @OA\Property(
                     property="p1",
                     type="string",
                 ),
         )
      )
  }
)

@OA\Post(
    path="/get-list2",
    summary="列表2",
    @OA\RequestBody(
        @OA\MediaType(
            mediaType="application/json",
            @OA\Schema(
                @OA\Property(
                    property="app",
                    type="integer",
                    default=0,
                ),
            )
        )
    ),
    @OA\Response(
        response=200,
        description="OK",
        @OA\JsonContent(ref="#/components/schemas/List2Response")
    ),
)

 返回结果
 @OA\Schema(
  schema="List2Response",
  type="object",
  allOf={
      @OA\Schema(ref="#/components/schemas/BaseResponse"),
      @OA\Schema(
         @OA\Property(
             property="data",
             type="object",
                 @OA\Property(
                     property="p2",
                     type="string",
                 ),
         )
      )
  }
)

floating-yuan avatar Oct 20 '20 10:10 floating-yuan

我的也遇到的同样的问题,你的解决了吗?

lzk97224 avatar Dec 30 '20 14:12 lzk97224

我导入Swagger 3.0的response数据yapi显示的都是OK,没有解析到返回实体,换回2.x版本才可以解析response实体

Wilson-He avatar May 12 '21 03:05 Wilson-He

我导入Swagger 3.0的response数据yapi显示的都是OK,没有解析到返回实体,换回2.x版本才可以解析response实体

我和你一样,导入OpenAPI 3.0的接口,界面显示ok,但是response object没有被自动解析。 2.0还是很ok的。

parkour99 avatar Jun 03 '21 09:06 parkour99

问题还存在,生成swagger文档没问题。 感觉yapi导入时,解引用的bug

huberfeng avatar Jun 01 '23 04:06 huberfeng