2017-10-17 147 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关键字。
  • 使用组合可能会增加或减少文档的可读性,具体取决于用于命名模式的单词的复杂性和选择。
相关问题