全局定义部分和组件部分之间有什么区别?

时间:2019-08-20 04:49:29

标签: swagger-2.0 openapi

Swagger 2.0中的全局definitions部分和components部分之间有什么区别?

我遇到了一个标记为swagger: '2.0'的Swagger定义YAML文件。 它在definitions的正下方有一个称为parameters的部分。这与
中描述的相似 https://swagger.io/docs/specification/2-0/basic-structure/
在“输入和输出模型”部分。

在文件的最下方,它还有一个components部分和下面的schemas部分。这与
中描述的相似 https://swagger.io/docs/specification/components/
看起来像OAS3。

但是,此特定的YAML文件包含两个部分。我不确定definitions是否适用于Swagger 2.0,componentsschemas是否适用于OAS 3.0。是吗?

definitionscomponents是否可以在类型为swagger: '2.0'的同一个YAML文件中使用,还是应该坚持使用definitionscomponents? >

# definitions section looks like this
definitions:
  User:
    properties:
      id:
        type: integer
      name:
        type: string
    # Both properties are required
    required:  
      - id
      - name
# components section looks like this
components:
  schemas:
    Address:
      type: object
      properties:
        line1:
          type: string
        city:
          type: string

1 个答案:

答案 0 :(得分:0)

  

我不确定definitions是否适用于Swagger 2.0,componentsschemas是否适用于OAS 3.0。是吗?

是的。

definitions部分用于OpenAPI 2.0文件(swagger: '2.0')。

components部分在OpenAPI 3.0(openapi: 3.0.0)中使用。

  

definitionscomponents是否可以在类型为swagger: '2.0'的同一个YAML文件中使用,还是应该坚持使用definitionscomponents? >

否,您不能在同一文件中混合使用2.0和3.0语法。如果文件为swagger: '2.0',则必须使用2.0语法。如果是openapi: 3.0.0,则必须使用3.0语法。