source: node_modules/@swagger-api/apidom-ns-openapi-3-1/README.md@ e48199a

main
Last change on this file since e48199a was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 15.6 KB
Line 
1# @swagger-api/apidom-ns-openapi-3-1
2
3`@swagger-api/apidom-ns-openapi-3-1` contains ApiDOM namespace specific to [OpenApi 3.1.0 specification](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md).
4
5## Installation
6
7You can install this package via [npm CLI](https://docs.npmjs.com/cli) by running the following command:
8
9```sh
10 $ npm install @swagger-api/apidom-ns-openapi-3-1
11```
12
13## OpenAPI 3.1.0 namespace
14
15OpenAPI 3.1.0 namespace consists of [number of elements](https://github.com/swagger-api/apidom/tree/main/packages/apidom-ns-openapi-3-1/src/elements) implemented on top
16of [primitive ones](https://github.com/refractproject/minim/tree/master/lib/primitives).
17
18```js
19import { createNamespace } from '@swagger-api/apidom-core';
20import openApi3_1Namespace from '@swagger-api/apidom-ns-openapi-3-1';
21
22const namespace = createNamespace(openApi3_1Namespace);
23
24const objectElement = new namespace.elements.Object();
25const openApiElement = new namespace.elements.OpenApi3_1();
26```
27
28When namespace instance is created in this way, it will extend the base namespace
29with the namespace provided as an argument.
30
31Elements from the namespace can also be used directly by importing them.
32
33```js
34import { OpenApi3_1Element, InfoElement } from '@swagger-api/apidom-ns-openapi-3-1';
35
36const infoElement = new InfoElement();
37const openApiElement = new OpenApi3_1Element();
38```
39
40## Predicates
41
42This package exposes [predicates](https://github.com/swagger-api/apidom/blob/main/packages/apidom-ns-openapi-3-1/src/predicates.ts)
43for all higher order elements that are part of this namespace.
44
45```js
46import { isOpenApi3_1Element, OpenApi3_1Element } from '@swagger-api/apidom-ns-openapi-3-1';
47
48const openApiElement = new OpenApi3_1Element();
49
50isOpenApi3_1Element(openApiElement); // => true
51```
52
53## Traversal
54
55Traversing ApiDOM in this namespace is possible by using `visit` function from `apidom` package.
56This package comes with its own [keyMap](https://github.com/swagger-api/apidom/blob/main/packages/apidom-ns-openapi-3-1/src/traversal/visitor.ts#L11) and [nodeTypeGetter](https://github.com/swagger-api/apidom/blob/main/packages/apidom-ns-openapi-3-1/src/traversal/visitor.ts#L4).
57To learn more about these `visit` configuration options please refer to [@swagger-api/apidom-ast documentation](https://github.com/swagger-api/apidom/blob/main/packages/apidom-ast/README.md#visit).
58
59```js
60import { visit } from '@swagger-api/apidom-core';
61import { OpenApi3_1Element, keyMap, getNodeType } from '@swagger-api/apidom-ns-openapi-3-1';
62
63const element = new OpenApi3_1Element();
64
65const visitor = {
66 OpenApi3_1Element(openApiElement) {
67 console.dir(openApiElement);
68 },
69};
70
71visit(element, visitor, { keyMap, nodeTypeGetter: getNodeType });
72```
73
74## Refractors
75
76Refractor is a special layer inside the namespace that can transform either JavaScript structures
77or generic ApiDOM structures into structures built from elements of this namespace.
78
79**Refracting JavaScript structures**:
80
81```js
82import { InfoElement } from '@swagger-api/apidom-ns-openapi-3-1';
83
84const object = {
85 title: 'my title',
86 description: 'my description',
87 version: '0.1.0',
88};
89
90InfoElement.refract(object); // => InfoElement({ title, description, version })
91```
92
93**Refracting generic ApiDOM structures**:
94
95```js
96import { ObjectElement } from '@swagger-api/apidom-core';
97import { InfoElement } from '@swagger-api/apidom-ns-openapi-3-1';
98
99const objectElement = new ObjectElement({
100 title: 'my title',
101 description: 'my description',
102 version: '0.1.0',
103});
104
105InfoElement.refract(objectElement); // => InfoElement({ title = 'my title', description = 'my description', version = '0.1.0' })
106```
107
108### Refractor plugins
109
110Refractors can accept plugins as a second argument of refract static method.
111
112```js
113import { ObjectElement } from '@swagger-api/apidom-core';
114import { InfoElement } from '@swagger-api/apidom-ns-openapi-3-1';
115
116const objectElement = new ObjectElement({
117 title: 'my title',
118 description: 'my description',
119 version: '0.1.0',
120});
121
122const plugin = ({ predicates, namespace }) => ({
123 name: 'plugin',
124 pre() {
125 console.dir('runs before traversal');
126 },
127 visitor: {
128 InfoElement(infoElement) {
129 infoElement.version = '2.0.0';
130 },
131 },
132 post() {
133 console.dir('runs after traversal');
134 },
135});
136
137InfoElement.refract(objectElement, { plugins: [plugin] }); // => InfoElement({ title = 'my title', description = 'my description', version = '2.0.0' })
138```
139
140You can define as many plugins as needed to enhance the resulting namespaced ApiDOM structure.
141If multiple plugins with the same visitor method are defined, they run in parallel (just like in Babel).
142
143#### Replace Empty Element plugin
144
145This plugin is specific to YAML 1.2 format, which allows defining key-value pairs with empty key,
146empty value, or both. If the value is not provided in YAML format, this plugin compensates for
147this missing value with the most appropriate semantic element type.
148
149```js
150import { parse } from '@swagger-api/apidom-parser-adapter-yaml-1-2';
151import { refractorPluginReplaceEmptyElement, OpenApi3_1Element } from '@swagger-api/apidom-ns-openapi-3-1';
152
153const yamlDefinition = `
154openapi: 3.1.0
155info:
156`;
157const apiDOM = await parse(yamlDefinition);
158const openApiElement = OpenApi3_1Element.refract(apiDOM.result, {
159 plugins: [refractorPluginReplaceEmptyElement()],
160});
161
162// =>
163// (OpenApi3_1Element
164// (MemberElement
165// (StringElement)
166// (OpenapiElement))
167// (MemberElement
168// (StringElement)
169// (InfoElement)))
170
171// => without the plugin the result would be as follows:
172// (OpenApi3_1Element
173// (MemberElement
174// (StringElement)
175// (OpenapiElement))
176// (MemberElement
177// (StringElement)
178// (StringElement)))
179```
180
181#### Normalize Operation.operationId fields plugin
182
183Existing `Operation.operationId` fields are normalized into snake case form.
184Operation Objects, that do not define operationId field, are left untouched.
185Original operationId is stored in meta and as new `__originalOperationId` field.
186This plugin also guarantees the uniqueness of all defined Operation.operationId fields,
187and make sure Link.operationId fields are pointing to correct and normalized Operation.operationId fields.
188
189```js
190import { toValue } from '@swagger-api/apidom-core';
191import { parse } from '@swagger-api/apidom-parser-adapter-yaml-1-2';
192import { refractorPluginNormalizeOperationIds, OpenApi3_1Element } from '@swagger-api/apidom-ns-openapi-3-1';
193
194const yamlDefinition = `
195openapi: 3.1.0
196paths:
197 /:
198 get:
199 operationId: get operation ^
200`;
201const apiDOM = await parse(yamlDefinition);
202const openApiElement = OpenApi3_1Element.refract(apiDOM.result, {
203 plugins: [refractorPluginNormalizeOperationIds()],
204});
205
206toValue(openApiElement);
207// =>
208// {
209// "openapi": "3.1.0",
210// "paths": {
211// "/": {
212// "get": {
213// "operationId": "getoperation_"
214// }
215// }
216// }
217// }
218```
219This plugin also accepts custom normalization function that will determine how normalized Operation.operationId fields
220should look like.
221
222```typescript
223import { toValue } from '@swagger-api/apidom-core';
224import { parse } from '@swagger-api/apidom-parser-adapter-yaml-1-2';
225import { refractorPluginNormalizeOperationIds, OpenApi3_1Element } from '@swagger-api/apidom-ns-openapi-3-1';
226
227const yamlDefinition = `
228openapi: 3.1.0
229paths:
230 /:
231 get:
232 operationId: get operation ^
233`;
234const apiDOM = await parse(yamlDefinition);
235const openApiElement = OpenApi3_1Element.refract(apiDOM.result, {
236 plugins: [refractorPluginNormalizeOperationIds({
237 operationIdNormalizer: (operationId: string, path: string, method: string): string => {
238 // operationId - value of Original.operationId field
239 // path - field pattern of Paths Object under which Path Item containing this Operation is registered
240 // method - name of HTTP method under which the Operation is registered in Path Item
241 },
242 })],
243});
244
245toValue(openApiElement);
246// =>
247// {
248// "openapi": "3.1.0",
249// "paths": {
250// "/": {
251// "get": {
252// "operationId": "<normalized-operation-id>"
253// }
254// }
255// }
256// }
257```
258
259#### Normalize Parameter Objects plugin
260
261Duplicates Parameters from Path Items to Operation Objects using following rules:
262
263- If a parameter is already defined at the Path Item, the new definition will override it but can never remove it
264- The list MUST NOT include duplicated parameters
265- A unique parameter is defined by a combination of a name and location.
266
267```js
268import { toValue } from '@swagger-api/apidom-core';
269import { parse } from '@swagger-api/apidom-parser-adapter-yaml-1-2';
270import { refractorPluginNormalizeParameters, OpenApi3_1Element } from '@swagger-api/apidom-ns-openapi-3-1';
271
272const yamlDefinition = `
273openapi: 3.1.0
274paths:
275 /:
276 parameters:
277 - name: param1
278 in: query
279 - name: param2
280 in: query
281 get: {}
282`;
283const apiDOM = await parse(yamlDefinition);
284const openApiElement = OpenApi3_1Element.refract(apiDOM.result, {
285 plugins: [refractorPluginNormalizeParameters()],
286});
287
288toValue(openApiElement);
289// =>
290// {
291// "openapi": "3.1.0",
292// "paths": {
293// "/": {
294// "parameters": [
295// {
296// "name": "param1",
297// "in": "query"
298// },
299// {
300// "name": "param2",
301// "in": "query"
302// }
303// ],
304// "get": {
305// "parameters": [
306// {
307// "name": "param1",
308// "in": "query"
309// },
310// {
311// "name": "param2",
312// "in": "query"
313// }
314// ],
315// }
316// }
317// }
318```
319
320#### Normalize Security Requirements Objects plugin
321
322`Operation.security` definition overrides any declared top-level security from OpenAPI.security field.
323If Operation.security field is not defined, this field will inherit security from OpenAPI.security field.
324
325```js
326import { toValue } from '@swagger-api/apidom-core';
327import { parse } from '@swagger-api/apidom-parser-adapter-yaml-1-2';
328import { refractorPluginNormalizeSecurityRequirements, OpenApi3_1Element } from '@swagger-api/apidom-ns-openapi-3-1';
329
330const yamlDefinition = `
331openapi: 3.1.0
332security:
333 - petstore_auth:
334 - write:pets
335 - read:pets
336paths:
337 /:
338 get: {}
339`;
340const apiDOM = await parse(yamlDefinition);
341const openApiElement = OpenApi3_1Element.refract(apiDOM.result, {
342 plugins: [refractorPluginNormalizeSecurityRequirements()],
343});
344
345toValue(openApiElement);
346// =>
347// {
348// "openapi": "3.1.0",
349// "security": [
350// {
351// "petstore_auth": [
352// "write:pets",
353// "read:pets"
354// ]
355// }
356// ],
357// "paths": {
358// "/": {
359// "get": {
360// "security": [
361// {
362// "petstore_auth": [
363// "write:pets",
364// "read:pets"
365// ]
366// }
367// ]
368// }
369// }
370// }
371// }
372```
373
374#### Normalize Server Objects plugin
375
376List of Server Objects can be defined in OpenAPI 3.1 on multiple levels:
377
378- OpenAPI.servers
379- PathItem.servers
380- Operation.servers
381
382If an alternative server object is specified at the Path Item Object level, it will override OpenAPI.servers.
383If an alternative server object is specified at the Operation Object level, it will override PathItem.servers and OpenAPI.servers respectively.
384
385```js
386import { toValue } from '@swagger-api/apidom-core';
387import { parse } from '@swagger-api/apidom-parser-adapter-yaml-1-2';
388import { refractorPluginNormalizeServers, OpenApi3_1Element } from '@swagger-api/apidom-ns-openapi-3-1';
389
390const yamlDefinition = `
391openapi: 3.1.0
392servers:
393 - url: https://example.com/
394 description: production server
395paths:
396 /:
397 get: {}
398`;
399const apiDOM = await parse(yamlDefinition);
400const openApiElement = OpenApi3_1Element.refract(apiDOM.result, {
401 plugins: [refractorPluginNormalizeServers()],
402});
403
404toValue(openApiElement);
405// =>
406// {
407// "openapi": "3.1.0",
408// "servers": [
409// {
410// "url": "https://example.com/",
411// "description": "production server"
412// }
413// ],
414// "paths": {
415// "/": {
416// "servers": [
417// {
418// "url": "https://example.com/",
419// "description": "production server"
420// }
421// ],
422// "get": {
423// "servers": [
424// {
425// "url": "https://example.com/",
426// "description": "production server"
427// }
428// ]
429// }
430// }
431// }
432// }
433```
434
435## Implementation progress
436
437Only fully implemented specification objects should be checked here.
438
439- [x] [OpenAPI Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#oasObject)
440- [x] [Info Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#infoObject)
441- [x] [Contact Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#contactObject)
442- [x] [License Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#licenseObject)
443- [x] [Server Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#serverObject)
444- [x] [Server Variable Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#serverVariableObject)
445- [x] [Components](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#componentsObject)
446- [x] [Paths Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#pathsObject)
447- [x] [Path Item Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#pathItemObject)
448- [x] [Operation Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#operationObject)
449- [x] [External Documentation Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#externalDocumentationObject)
450- [x] [Parameter Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#parameterObject)
451- [x] [Request Body Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#requestBodyObject)
452- [x] [Media Type Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#mediaTypeObject)
453- [x] [Encoding Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#encodingObject)
454- [x] [Responses Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#responsesObject)
455- [x] [Callback Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#callbackObject)
456- [x] [Example Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#exampleObject)
457- [x] [Link Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#linkObject)
458- [x] [Header Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#headerObject)
459- [x] [Tag Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#tagObject)
460- [x] [Reference Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#referenceObject)
461- [x] [Schema Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#schemaObject)
462- [x] [Discriminator Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#discriminatorObject)
463- [x] [XML Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#xmlObject)
464- [x] [Security Scheme Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#securitySchemeObject)
465- [x] [OAuth Flows Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#oauthFlowsObject)
466- [x] [OAuth Flow Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#oauthFlowObject)
467- [x] [Security Requirement Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#securityRequirementObject)
468- [x] [Specification extensions](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#specificationExtensions)
Note: See TracBrowser for help on using the repository browser.