2016-11-03 21 views
1

代碼:如何在定義中重用swagger定義?下面

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 

正如你所看到的,FindUIDFindUsername第一部分是一樣的Result。如何用Result替換那些重複的代碼?

回答

2

您可以撰寫使用allOf定義,這裏有一個完整的例子,其中的結果是FindUID和FindUsername使用:

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 

更多關於這家在這裏:https://apihandyman.io/writing-openapi-swagger-specification-tutorial-part-4-advanced-data-modeling/(披露:我寫這篇教程)

+0

它的工作原理。我會詳細閱讀你的教程。謝謝。 – goofansu