Swagger 如何在定义中重用招摇过市定义?

Swagger 如何在定义中重用招摇过市定义?,swagger,Swagger,代码如下: definitions: Result: type: object properties: code: type: integer format: int32 message: type: string FindUID: type: object properties: code: type: integer format: in

代码如下:

definitions:
  Result:
    type: object
    properties:
      code:
        type: integer
        format: int32
      message:
        type: string
  FindUID:
    type: object
    properties:
      code:
        type: integer
        format: int32
      message:
        type: string
      data:
        type: object
        properties:
          uid:
            type: integer
            format: int64
  FindUsername:
    type: object
    properties:
      code:
        type: integer
        format: int32
      message:
        type: string
      data:
        type: object
        properties:
          username:
            type: string

如您所见,
FindUID
FindUsername
的第一部分与
Result
相同。如何用
Result
替换那些重复的代码?

您可以使用
allOf
组合定义,下面是一个完整的示例,其中在FindUID和FindUsername中使用Result:

swagger: '2.0'
info:
  description: Example API Description
  title: Example Title
  version: 1.0.0
paths: {}
definitions:
  Result:
    type: object
    properties:
      code:
        type: integer
        format: int32
      message:
        type: string
  FindUID:
    allOf:
      - $ref: "#/definitions/Result"
      - type: object
        properties:
          data:
            type: object
            properties:
              uid:
                type: integer
                format: int64
  FindUsername:
    allOf:
      - $ref: "#/definitions/Result"
      - type: object
        properties:
          data:
            type: object
            properties:
              username:
                type: string
更多信息请参见:(披露:我编写了本教程)