2017-10-17 94 views
1

假設我有一個YAML的OpenAPI定義OpenAPI的再利用部分不定義一個新的

definitions: 
    User: 
    description: "User" 
    type: "object" 
    properties: 
     firstname: 
     type: "string" 
     lastname: 
     type: "string" 
     password: 
     type: "string" 
     email: 
     type: "string" 
     username: 
     type: "string" 

這個定義如果在參數規格,我需要一個定義的特定字段我怎麼能是指他們沒有定義另一種模式如下?

definitions: 
    UserLogin: 
    description: "User" 
    type: "object" 
    properties: 
     password: 
     type: "string" 
     email: 
     type: "string" 

回答

1

在你的問題,你使用definitions關鍵字什麼暗示,你的問題是關於OpenAPI v2 aka. Swagger。對於OpenAPI v3,下面提供的定義應在適當的Components Object部分內定義。

爲了達到此目的,您必須使用Composition和關鍵字allOf。有一個很好的例子涉及到你的問題here。首先,你必須定義一個更小的物體,然後包括它變成一個更大的定義如下:

definitions: 
    UserLogin: 
    description: User Login 
    type: object 
    properties: 
     password: 
     type: string 
     email: 
     type: string 
    User: 
    allOf: 
    - $ref: '#/definitions/UserLogin' 
    - description: User 
     type: object 
     properties: 
     firstname: 
      type: string 
     lastname: 
      type: string 
     username: 
      type: string 

值得注意的是:

  • 一些較輕的實現可能不支持allOf關鍵字。
  • 使用組合可能會增加或減少文檔的可讀性,具體取決於用於命名模式的單詞的複雜性和選擇。