【问题标题】:Swagger using the same model but different examplesSwagger 使用相同的模型但不同的示例
【发布时间】:2019-01-29 09:51:02
【问题描述】:

我的 API 可以返回 200 ok 或 400,发生错误。我希望他们都使用相同的模型,但使用不同的示例。在下面的代码中,我希望我的 200 信息说“一切正常”和 400 的信息“不正常”。

 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

【问题讨论】:

    标签: yaml swagger


    【解决方案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"}
                #         ]
                #       }
                #     ]
                #   }
    

    【讨论】:

    • 非常感谢。但是,例如 Fatal 是一个 kv 对数组,我该如何编写类似的东西。致命:[“自定义错误”:“休​​斯顿,我们有问题”]
    • 你的意思是Fatal: { "Custom error": "Houston, we have a problem" } 用大括号代替方括号吗?方括号 ["a": "b"] 中的键值对不是有效的 JSON 语法。
    猜你喜欢
    • 1970-01-01
    • 2022-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-09
    相关资源
    最近更新 更多