2016-11-03 13 views
1

コード:定義でスワッガー定義を再利用するには?以下

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で使用されている完全な例です:https://apihandyman.io/writing-openapi-swagger-specification-tutorial-part-4-advanced-data-modeling/(開示:私はこのチュートリアルを書いた)、ここでこの程度

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 

もっと

+0

それは動作します。私はあなたのチュートリアルを詳細に読むでしょう。ありがとうございました。 – goofansu

関連する問題