swagger-ui如何在schema数组中形成多个响应

3

我正在尝试以以下格式形成Swagger文档。响应是200 HTTP代码下的以下JSON。但是我无法形成如下所示的JSON。

[
  {
    "key":"test1", 
    "val":"val1" 
  },
  {
    "key":"test2", 
    "val":"val2" 
  },
  {
    "key":"test3", 
    "val":"val3" 
  }
]

到目前为止,我有这个:

"responses": {
                    "200": {
                        "description": "response",
                        "schema": {
                            "type": "array",
                            "items": {
                                "$ref": "#/definitions/res1",
                                "$ref": "#/definitions/res2"
                            }
                        }
                    }
                }

 "definitions": {
    "res1": {
                    "type": "object",
                    "properties": {
                            "key": {
                                    "type": "string",
                                    "example": "test1"
                            },
                            "keyURL": {
                                    "type": "string",
                                    "example": "val1"
                            }
                        }
                },
                "res2": {
                    "type": "object",
                    "properties": {
                            "key": {
                                    "type": "string",
                                    "example": "test2"
                            },
                            "val": {
                                    "type": "string",
                                    "example": "val2"
                            }
                        }
                }

但我根本看不到res2模块,只能看到res1

1个回答

4

JSON指针($ref)必须“单独”存在于对象中。因此,您不能在items块中有多个指针:

  "$ref": "#/definitions/res1",
  "$ref": "#/definitions/res2"

这将忽略第二个引用(res2),并仅应用第一个。

对于你想做的事情,有很多选择。可能最简单的是像这样:

type: array
items:
  $ref: '#/definitions/Pair'

并且具有以下此类定义:

definitions:
  Pair:
    properties:
      key:
        type: string
      value:
        type: string

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接