Friday 17 April 2020

Openapi3 Components Section

Components Section

Often, multiple API operations have some common parameters or return the same response structure. To avoid code duplication, you can place the common definitions in the global components section and reference them using $ref. In the example below, duplicate definitions of a User object are replaced with a single component and references to that component. Before:
  1. paths:
  2. /users/{userId}:
  3. get:
  4. summary: Get a user by ID
  5. parameters:
  6. ...
  7. responses:
  8. '200':
  9. description: A single user.
  10. content:
  11. application/json:
  12. schema:
  13. type: object
  14. properties:
  15. id:
  16. type: integer
  17. name:
  18. type: string
  19. /users:
  20. get:
  21. summary: Get all users
  22. responses:
  23. '200':
  24. description: A list of users.
  25. content:
  26. application/json:
  27. schema:
  28. type: array
  29. items:
  30. type: object
  31. properties:
  32. id:
  33. type: integer
  34. name:
  35. type: string
After:
  1. paths:
  2. /users/{userId}:
  3. get:
  4. summary: Get a user by ID
  5. parameters:
  6. ...
  7. responses:
  8. '200':
  9. description: A single user.
  10. content:
  11. application/json:
  12. schema:
  13. $ref: '#/components/schemas/User'
  14. /users:
  15. get:
  16. summary: Get all users
  17. responses:
  18. '200':
  19. description: A list of users.
  20. content:
  21. application/json:
  22. schema:
  23. type: array
  24. items:
  25. $ref: '#/components/schemas/User'
  26. components:
  27. schemas:
  28. User:
  29. type: object
  30. properties:
  31. id:
  32. type: integer
  33. name:
  34. type: string

Components Structure

components serve as a container for various reusable definitions – schemas (data models), parameters, responses, examples, and others. The definitions in components have no direct effect on the API unless you explicitly reference them from somewhere outside the components. That is, components are not parameters and responses that apply to all operations; they are just pieces of information to be referenced elsewhere. Under components, the definitions are grouped by type – schemasparameters and so on. The following example lists the available subsections. All subsections are optional.
  1. components:
  2. # Reusable schemas (data models)
  3. schemas:
  4. ...
  5. # Reusable path, query, header and cookie parameters
  6. parameters:
  7. ...
  8. # Security scheme definitions (see Authentication)
  9. securitySchemes:
  10. ...
  11. # Reusable request bodies
  12. requestBodies:
  13. ...
  14. # Reusable responses, such as 401 Unauthorized or 400 Bad Request
  15. responses:
  16. ...
  17. # Reusable response headers
  18. headers:
  19. ...
  20. # Reusable examples
  21. examples:
  22. ...
  23. # Reusable links
  24. links:
  25. ...
  26. # Reusable callbacks
  27. callbacks:
  28. ...
Each subsection contains one or more named components (definitions):
  1. components:
  2. schemas:
  3. User:
  4. type: object
  5. ...
  6. Pet:
  7. type: object
  8. ...
The component names can consist of the following characters only:
  1. A..Z a..z 0..9 . _ -
Examples of valid names:
  1. User
  2. New_User
  3. org.example.User
  4. 401-Unauthorized
The component names are used to reference the components via $ref from other parts of the API specification:
  1. $ref: '#/components/<type>/<name>'
For example:
  1. $ref: '#/components/schemas/User'
An exception are definitions in securitySchemes which are referenced directly by name (see Authentication).

Components Example

Below is an example of components that contains reusable data schemas, parameters and responses. Other component types (links, examples, and others) are defined similarly.
  1. components:
  2. #-------------------------------
  3. # Reusable schemas (data models)
  4. #-------------------------------
  5. schemas:
  6. User: # Can be referenced as '#/components/schemas/User'
  7. type: object
  8. properties:
  9. id:
  10. type: integer
  11. format: int64
  12. name:
  13. type: string
  14. Error: # Can be referenced as '#/components/schemas/Error'
  15. type: object
  16. properties:
  17. code:
  18. type: integer
  19. message:
  20. type: string
  21. #-------------------------------
  22. # Reusable operation parameters
  23. #-------------------------------
  24. parameters:
  25. offsetParam: # Can be referenced via '#/components/parameters/offsetParam'
  26. name: offset
  27. in: query
  28. description: Number of items to skip before returning the results.
  29. required: false
  30. schema:
  31. type: integer
  32. format: int32
  33. minimum: 0
  34. default: 0
  35. limitParam: # Can be referenced as '#/components/parameters/limitParam'
  36. name: limit
  37. in: query
  38. description: Maximum number of items to return.
  39. required: false
  40. schema:
  41. type: integer
  42. format: int32
  43. minimum: 1
  44. maximum: 100
  45. default: 20
  46. #-------------------------------
  47. # Reusable responses
  48. #-------------------------------
  49. responses:
  50. 404NotFound: # Can be referenced as '#/components/responses/404NotFound'
  51. description: The specified resource was not found.
  52. ImageResponse: # Can be referenced as '#/components/responses/ImageResponse'
  53. description: An image.
  54. content:
  55. image/*:
  56. schema:
  57. type: string
  58. format: binary
  59. GenericError: # Can be referenced as '#/components/responses/GenericError'
  60. description: An error occurred.
  61. content:
  62. application/json:
  63. schema:
  64. $ref: '#/components/schemas/Error'

Externally Defined Components

Individual definitions in components can be specified either inline (as in the previous example) or using a $ref reference to an external definition:
  1. components:
  2. schemas:
  3. Pet:
  4. $ref: '../models/pet.yaml'
  5. # Can now use use '#/components/schemas/Pet' instead
  6. User:
  7. $ref: 'https://api.example.com/v2/openapi.yaml#/components/schemas/User'
  8. # Can now use '#/components/schemas/User' instead
  9. responses:
  10. GenericError:
  11. $ref: '../template-api.yaml#/components/responses/GenericError'
  12. # Can now use '#/components/responses/GenericError' instead
This way you can define local “aliases” for external definitions that you can use instead of repeating the external file paths in all references. If the location of the referenced file changes, you only need to change it in one place (in components) instead of in all references.

Differences From OpenAPI 2.0

OpenAPI 2.0 had separate sections for reusable components – definitionsparametersresponses and securityDefinitions. In OpenAPI 3.0, they all were moved inside components. Also, definitions were renamed to schemas and securityDefinitions were renamed to securitySchemes (note the different spelling: schemAs vs securitySchemEs). The references are changed accordingly to reflect the new structure:
  1. OpenAPI 2.0 OpenAPI 3.0
  2. '#/definitions/User' '#/components/schemas/User'
  3. '#/parameters/offsetParam' '#/components/parameters/offsetParam'
  4. '#/responses/ErrorResponse' '#/components/responses/ErrorResponse'

References


from : https://swagger.io/docs/specification/components/

No comments:

Post a Comment