【问题标题】:How to provide a response example with this structure in OpenAPI 3?如何在 OpenAPI 3 中提供具有此结构的响应示例?
【发布时间】:2022-11-05 15:56:31
【问题描述】:

是否可以提供一个响应示例,它是一个具有数组属性和字符串属性的对象?

我有以下 OpenAPI 3 定义:

        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {


                "examples": {
                  "results": [
                    {
                        "abc": 20
                    }
                  ],
                  "totalCount": 69
                },

                "schema": {
                  "results":"array",
                  "totalCount": "integer"
                }
              }
            }
          }
        }

但是,当我将 "totalCount": 69 放入 examples 对象中时,Swagger UI 显示错误。

【问题讨论】:

  • 澄清一下 - 您使用的是 OpenAPI 3.x ("openapi": "3.x.x") 还是 OpenAPI 2.0 ("swagger": "2.0")?

标签: swagger-ui openapi


【解决方案1】:

examples(复数)更改为example(单数),并为响应提供适当的schema

  "responses": {
    "200": {
      "description": "OK",
      "content": {
        "application/json": {
          "example": {
            "results": [
              {
                "abc": 20
              }
            ],
            "totalCount": 69
          },

          "schema": {
            "type": "object",
            "properties": {
              "results": {
                "type": "array",
                "items": {
                  "type": "object",
                  "properties": {
                    "abc": {
                      "type": "integer"
                    }
                  }
                }
              },
              "totalCount": {
                "type": "integer"
              }
            }
          }
        }
      }
    }
  }

【讨论】:

  • 谢谢! OpenAPI 2.0 似乎需要“示例”(复数)。它将作为“示例”(单数)工作,但会给出错误should not have additional properties additionalProperty: example
  • @RyanLoggerythm OP 使用 OpenAPI 3.0。有关 2.0 示例语法,请参阅swagger.io/docs/specification/2-0/adding-examples
猜你喜欢
  • 2021-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多