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,components
和schemas
是否适用于OAS 3.0。是吗?
definitions
和components
是否可以在类型为swagger: '2.0'
的同一个YAML文件中使用,还是应该坚持使用definitions
或components
? >
# 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
答案 0 :(得分:0)
我不确定
definitions
是否适用于Swagger 2.0,components
和schemas
是否适用于OAS 3.0。是吗?
是的。
definitions
部分用于OpenAPI 2.0文件(swagger: '2.0'
)。
components
部分在OpenAPI 3.0(openapi: 3.0.0
)中使用。
definitions
和components
是否可以在类型为swagger: '2.0'
的同一个YAML文件中使用,还是应该坚持使用definitions
或components
? >
否,您不能在同一文件中混合使用2.0和3.0语法。如果文件为swagger: '2.0'
,则必须使用2.0语法。如果是openapi: 3.0.0
,则必须使用3.0语法。