What is the difference between global definitions section and components section?
Asked Answered
B

1

14

What is the difference between the global definitions section and the components section in Swagger 2.0?

I came across a Swagger definition YAML file which is marked as swagger: '2.0'. It has one section called definitions just below parameters. This is similar to what is described in
https://swagger.io/docs/specification/2-0/basic-structure/
under the "Input and Output Models" section.

Also further down the file, it has the components section with the schemas section underneath it. That is similar to what is described in
https://swagger.io/docs/specification/components/
This looks like OAS3.

However, this specific YAML file has both sections. I am not exactly sure whether definitions is for Swagger 2.0 and components and schemas is in OAS 3.0. Is that so?

Can both definitions and components be used in the same YAML file of type swagger: '2.0' or should we stick to either definitions or 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
Blunge answered 20/8, 2019 at 4:49 Comment(0)
U
15

I am not exactly sure whether definitions is for Swagger 2.0 and components and schemas is in OAS 3.0. Is that so?

Yes, exactly.

The definitions section is used in OpenAPI 2.0 files (swagger: '2.0').

The components section is used in OpenAPI 3.x (openapi: 3.x.x).


Can both definitions and components be used in the same YAML file of type swagger: '2.0' or should we stick to either definitions or components?

No, you can't mix 2.0 and 3.x syntax in the same file. If your file is swagger: '2.0' you must use 2.0 syntax. If it's openapi: 3.0.0 you must use 3.0 syntax.

Unconditioned answered 20/8, 2019 at 7:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.