|
1 | 1 | --- |
2 | 2 | title: "Components in OpenAPI best practices" |
3 | | -description: "Master the Components Object in OpenAPI to efficiently reuse schema definitions, parameters, responses, and more across your API specification." |
| 3 | +description: "Master the Components Object in OpenAPI to efficiently reuse schema definitions, parameters, responses, and more across API descriptions." |
4 | 4 | --- |
5 | 5 |
|
6 | | -# Components Object in OpenAPI |
| 6 | +# Components in OpenAPI |
7 | 7 |
|
8 | 8 | import { Table } from "@/mdx/components"; |
9 | 9 |
|
10 | | -The Components Object is a container for reusable objects that can be referenced across the API. These objects can be referenced using [References](/openapi/references), and generally are only valid if referenced by other parts of the API. |
| 10 | +The Components object in OpenAPI are reusable bits of OpenAPI description, which |
| 11 | +can then be [referenced](/openapi/references). Reusing components allows for |
| 12 | +smaller file-sizes, reduces conflicts, and improves consistency across the API. |
| 13 | +Components can even be shared them across multiple documents, allowing for |
| 14 | +improved reuse between multiple APIs. |
| 15 | + |
| 16 | +```yaml |
| 17 | +components: |
| 18 | + schemas: |
| 19 | + User: |
| 20 | + type: object |
| 21 | + properties: |
| 22 | + id: |
| 23 | + type: integer |
| 24 | + name: |
| 25 | + type: string |
| 26 | + email: |
| 27 | + type: string |
| 28 | + format: email |
| 29 | + parameters: |
| 30 | + userId: |
| 31 | + name: uuid |
| 32 | + in: path |
| 33 | + description: Unique UUIDv4 of the user |
| 34 | + required: true |
| 35 | + schema: |
| 36 | + type: string |
| 37 | + format: uuid |
| 38 | + responses: |
| 39 | + NotFound: |
| 40 | + description: User not found |
| 41 | + content: |
| 42 | + application/json: |
| 43 | + schema: |
| 44 | + $ref: '#/components/schemas/Error' |
| 45 | + requestBodies: |
| 46 | + User: |
| 47 | + content: |
| 48 | + application/json: |
| 49 | + schema: |
| 50 | + $ref: '#/components/schemas/User' |
| 51 | + securitySchemes: |
| 52 | + bearerAuth: |
| 53 | + type: http |
| 54 | + scheme: bearer |
| 55 | + bearerFormat: JWT |
| 56 | +``` |
| 57 | +
|
| 58 | +Components can be referenced in other parts of the OpenAPI document using the |
| 59 | +`$ref` keyword. The `$ref` keyword is a JSON Pointer to the component, which |
| 60 | +is a string that starts with `#/components/` and then the component type |
| 61 | +and name. For example, to reference the `User` schema defined in the |
| 62 | +Components Object, you would use the following `$ref`: |
| 63 | + |
| 64 | +```yaml |
| 65 | +responses: |
| 66 | + '200': |
| 67 | + description: User found |
| 68 | + content: |
| 69 | + application/json: |
| 70 | + schema: |
| 71 | + $ref: '#/components/schemas/User' |
| 72 | +``` |
| 73 | + |
| 74 | +To put it all together, here is an example of how to reference all the various components in that previous example: |
| 75 | + |
| 76 | +```yaml |
| 77 | +paths: |
| 78 | + /users/{userId}: |
| 79 | + get: |
| 80 | + summary: Get a user by UUID |
| 81 | + parameters: |
| 82 | + - $ref: '#/components/parameters/userId' |
| 83 | + responses: |
| 84 | + '200': |
| 85 | + description: User found |
| 86 | + content: |
| 87 | + application/json: |
| 88 | + schema: |
| 89 | + $ref: '#/components/schemas/User' |
| 90 | + '404': |
| 91 | + $ref: '#/components/responses/NotFound' |
| 92 | + security: |
| 93 | + - bearerAuth: [] |
| 94 | +``` |
| 95 | + |
| 96 | +## Components Object |
| 97 | + |
| 98 | +The Components Object is a map of reusable components broken down by type. |
| 99 | + |
| 100 | +```yaml |
| 101 | +components: |
| 102 | + <componentType>: |
| 103 | + <componentName>: |
| 104 | + <componentDefinition> |
| 105 | +``` |
| 106 | + |
| 107 | +The component name can be any valid string, but it is recommended to use a |
| 108 | +consistent naming convention across the API. A common naming convention is `PascalCase` or `camelCase`. |
| 109 | + |
| 110 | +```yaml |
| 111 | +components: |
| 112 | + schemas: |
| 113 | + Train: |
| 114 | + Station: |
| 115 | + BookingPayments: |
| 116 | +``` |
| 117 | + |
| 118 | +Here are the supported component types as of OpenAPI v3.1: |
11 | 119 |
|
12 | 120 | <Table |
13 | 121 | data={[ |
14 | 122 | { |
15 | 123 | field: "`schemas`", |
16 | 124 | type: "Map[string, [Schema Object](/openapi/schemas)]*", |
17 | | - required: "", |
18 | 125 | description: "A map of [Schema Objects](/openapi/schemas) that can be referenced by other parts of the API.\n\n**Note: OpenAPI 3.0.x does support [OpenAPI Reference Objects](/openapi/references#openapi-reference-object) as the value here, but `3.1.x` uses the [JSON Schema Referencing](/openapi/schemas#json-schema--openapi) format.**" |
19 | 126 | }, |
20 | 127 | { |
21 | 128 | field: "`securitySchemes`", |
22 | 129 | type: "Map[string, [Security Scheme Object](/openapi/security/security-schemes) \\| [OpenAPI Reference Object](/openapi/references#openapi-reference-object)]*", |
23 | | - required: "", |
24 | 130 | description: "A map of [Security Scheme Objects](/openapi/security/security-schemes) that can be referenced by other parts of the API." |
25 | 131 | }, |
26 | 132 | { |
27 | 133 | field: "`pathItems`", |
28 | 134 | type: "Map[string, [Path Item Object](/openapi/paths#path-item-object) \\| [OpenAPI Reference Object](/openapi/references#openapi-reference-object)]*", |
29 | | - required: "", |
30 | 135 | description: "A map of [Path Item Objects](/openapi/paths#path-item-object) that can be referenced by other parts of the API." |
31 | 136 | }, |
32 | 137 | { |
33 | 138 | field: "`parameters`", |
34 | 139 | type: "Map[string, [Parameter Object](/openapi/paths/parameters#parameter-object) \\| [OpenAPI Reference Object](/openapi/references#openapi-reference-object)]*", |
35 | | - required: "", |
36 | 140 | description: "A map of [Parameter Objects](/openapi/paths/parameters#parameter-object) that can be referenced by other parts of the API." |
37 | 141 | }, |
38 | 142 | { |
39 | 143 | field: "`requestBodies`", |
40 | 144 | type: "Map[string, [Request Body Object](/openapi/paths/operations/requests) \\| [OpenAPI Reference Object](/openapi/references#openapi-reference-object)]*", |
41 | | - required: "", |
42 | 145 | description: "A map of [Request Body Objects](/openapi/paths/operations/requests) that can be referenced by other parts of the API." |
43 | 146 | }, |
44 | 147 | { |
45 | 148 | field: "`responses`", |
46 | 149 | type: "Map[string, [Response Object](/openapi/paths/operations/responses#response-object) \\| [OpenAPI Reference Object](/openapi/references#openapi-reference-object)]*", |
47 | | - required: "", |
48 | 150 | description: "A map of [Response Objects](/openapi/paths/operations/responses#response-object) that can be referenced by other parts of the API." |
49 | 151 | }, |
50 | 152 | { |
51 | 153 | field: "`headers`", |
52 | 154 | type: "Map[string, [Header Object](/openapi/paths/operations/responses/headers) \\| [OpenAPI Reference Object](/openapi/references#openapi-reference-object)]*", |
53 | | - required: "", |
54 | 155 | description: "A map of [Header Objects](/openapi/paths/operations/responses/headers) that can be referenced by other parts of the API." |
55 | 156 | }, |
56 | 157 | { |
57 | 158 | field: "`examples`", |
58 | 159 | type: "Map[string, [Example Object](/openapi/examples) \\| [OpenAPI Reference Object](/openapi/references#openapi-reference-object)]*", |
59 | | - required: "", |
60 | 160 | description: "A map of [Example Objects](/openapi/examples) that can be referenced by other parts of the API." |
61 | 161 | }, |
62 | 162 | { |
63 | 163 | field: "`callbacks`", |
64 | 164 | type: "Map[string, [Callback Object](/openapi/paths/operations/callbacks#callback-object) \\| [OpenAPI Reference Object](/openapi/references#openapi-reference-object)]*", |
65 | | - required: "", |
66 | 165 | description: "A map of [Callback Objects](/openapi/paths/operations/callbacks#callback-object) that can be referenced by other parts of the API." |
67 | 166 | }, |
68 | 167 | { |
69 | 168 | field: "`links`", |
70 | 169 | type: "Map[string, [Link Object](/openapi/paths/operations/responses/links#link-object) \\| [OpenAPI Reference Object](/openapi/references#openapi-reference-object)]*", |
71 | | - required: "", |
72 | 170 | description: "A map of [Link Objects](/openapi/paths/operations/responses/links#link-object) that can be referenced by other parts of the API." |
73 | | - }, |
74 | | - { |
75 | | - field: "`x-*`", |
76 | | - type: "[Extensions](/openapi/extensions)", |
77 | | - required: "", |
78 | | - description: "Any number of extension fields can be added to the Components Object that can be used by tooling and vendors." |
79 | 171 | } |
80 | 172 | ]} |
81 | 173 | columns={[ |
82 | 174 | { key: "field", header: "Field" }, |
83 | 175 | { key: "type", header: "Type" }, |
84 | | - { key: "required", header: "Required" }, |
85 | 176 | { key: "description", header: "Description" } |
86 | 177 | ]} |
87 | 178 | /> |
0 commit comments