如何重用swagger中另一个定义的例子?

时间:2016-01-28 22:01:33

标签: yaml swagger swagger-ui swagger-2.0

我有一个报告对象的定义。我有报告对象的另一个定义,它有一个报告对象数组(通过$ ref)。

报告定义中,我定义了一个示例,它可以在swagger UI中正常工作。

报告定义中,我希望它使用报告定义中的示例。

我该怎么做?我使用$ ref尝试了几件事,我得到的最接近的就是我在下面的YAML中所拥有的...

definitions:
  report:
    type: object
    properties:
      ID:
        type: number
        format: int
        description: "DB record ID of the report."
        readOnly: true
      ErrorContent:
        type: string
        description: "The actual problem or error infomation for this report. This can be exception stack, etc."
        readOnly: true
      UserComments:
        type: string
        description: "Any user comments collected by the app and submitted with the report."
        readOnly: true
      ReportedBy:
        type: string
        description: "The person using the app when it triggered the error this report is for."
        readOnly: true
      ReportedDateTime:
        type: string
        description: "The date/time the report was submitted."
        readOnly: true
    required:
      - ID
      - ErrorContent
      - ErrorType
      - UserComments
      - ReportedBy
      - ReportedDateTime
    example:
      ID: 11367
      ErrorContent: "Operation is not valid due to the current state of the object."
      ErrorType: "Exception"
      UserComments: "Was clicking this and that and then Boom!"
      ReportedBy: "domain\\name"
      ReportedDateTime: "2016-01-19 14:07:00"
  reports:
    properties:
      message:
        type: string
      reports:
        type: array
        items:
          $ref: '#/definitions/report'
    example:
      message: "success"
      reports:
        - $ref: '#/definitions/report'

但是,在Swagger UI中,上述结果为......

{
  "message": "success",
  "reports": [
    {
      "$ref": "#/definitions/report"
    }
  ]
}

一个有趣的注意事项,在Swagger UI中,当我查看模型视图时,它确实包含所有报告,即使有描述。

1 个答案:

答案 0 :(得分:0)

此行为是正确的 - 无法使用JSON指针引用examples部分。有关当前支持的内容,请参阅OAI Specification

如果您认为这是一个常见用例,请在规范存储库中打开一个问题。