挥舞着使用相同的模型但使用不同的示例

时间:2019-01-29 09:51:03

标签: yaml swagger

我的API可以重新调整200 ok或400,发生错误。我希望他们都使用相同的模型,但使用不同的示例。在下面的代码中,我希望我的200 Info表示类似“全部正常”和400的Info表示“不正常”。

 responses:
        '200':
          description: >-
            Invoice received for processing but beware of potentional warnings
            and information
          schema:
            $ref: '#/definitions/Response'
        '400':
          description: Invoice could not be processed due to fatal errors
          schema:
            $ref: '#/definitions/Response'

  Response:
    type: object
    properties:
      Info:
        type: string
          information
      Messages:
        type: object
        properties:
          Fatal:
            type: array
            items:
              type: object
          Warning:
            type: array
            items:
              type: object
          Information:
            type: array
            items:
              type: object

1 个答案:

答案 0 :(得分:1)

在OpenAPI 2.0中,响应支持examples关键字,以为不同的HTTP状态代码和媒体类型指定响应示例。

      responses:
        '200':
          description: >-
            Invoice received for processing but beware of potential warnings
            and information
          schema:
            $ref: '#/definitions/Response'
          examples:
            application/json:
              Info: OK
            # Or using JSON syntax for the example value:
            # application/json: {"Info": "OK"}

        '400':
          description: Invoice could not be processed due to fatal errors
          schema:
            $ref: '#/definitions/Response'
          examples:
            application/json:
              Info: Oops
              Messages:
                Fatal:
                  - Houston, we have a problem
            # Or using JSON syntax for the example value:
            # application/json:
            #   {
            #     "Info": "Oops",
            #     "Messages": [
            #       {
            #         "Fatal": [
            #           {"Custom error": "Houston, we have a problem"}
            #         ]
            #       }
            #     ]
            #   }