How using ref into examples Swagger?

假如想象 提交于 2019-12-23 21:17:33

问题


JSON spec:

"responses": {
          "200": {
            "description": "Успешный ответ сервиса",
            "schema": {
              "$ref": "#/definitions/BaseResponse"
            },
            "examples": {
              "application/json": {
                "status": true,
                "response": {
                  "$ref": "#/definitions/Product"
                },
                "errors": null
              }
            }
          }
}

Result:

But I need:

{
  "status": true,
  "response": {
      "ProductNumber": "number",
      "Barcode": "number",
      "Length": 12,
      "Width": 34,
      "Height": 423,
      "Volume": 1232
    }
  },
  "errors": null
}

How I can using $refs into example array for custom format response? It's a typical case, but I cannot found documentation for it. Thank you for you feedback.


回答1:


Inline examples do not support $ref - the example must be a complete example:

      "responses": {
        "200": {
          "description": "Успешный ответ сервиса",
          "schema": {
            "$ref": "#/definitions/BaseResponse"
          },
          "examples": {
            "application/json": {
              "status": true,
              "response": {
                "ProductNumber": "number",
                "Barcode": "number",
                "Length": 12,
                "Width": 34,
                "Height": 423,
                "Volume": 1232
              },
              "errors": null
            }
          }
        }
      }

Instead of using responses.<code>.examples, you can specify the example values in your BaseResponse schema, and Swagger UI will use those instead.

For example, you can add a complete example to your BaseResponse schema:

  "definitions": {
    "BaseResponse": {
      "type": "object",
      "properties": {
        "status": {
          "type": "boolean"
        },
        ...
      },
      "example": {    // <------ schema-level example
        "status": true,
        "response": {
          "ProductNumber": "number",
          "Barcode": "number",
          "Length": 12,
          "Width": 34,
          "Height": 423,
          "Volume": 1232
        },
        "errors": null
      }
    }
  }

or use property-level examples:

  "definitions": {
    "BaseResponse": {
      "type": "object",
      "properties": {
        "status": {
          "type": "boolean",
          "example": true           // <------
        },
        "response": {
          "$ref": "#/definitions/Product"
        },
        "errors": {
          "example": null           // <------
        }
      }
    },
    "Product": {
      "type": "object",
      "properties": {
        "ProductNumber": {
          "type": "string",
          "example": "number"       // <------
        },
        "Length": {
          "type": "integer",
          "example": 12             // <------
        },
        ...
      }
    }
  }

I'd like to note that "errors": null and "example": null are not actually valid in OpenAPI 2.0 (fka Swagger), because it does not support nullable types. Nullable types are supported in OpenAPI 3.0 only.



来源:https://stackoverflow.com/questions/47525254/how-using-ref-into-examples-swagger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!