[d24f17c] | 1 | # @swagger-api/apidom-ns-openapi-3-0
|
---|
| 2 |
|
---|
| 3 | `@swagger-api/apidom-ns-openapi-3-0` contains ApiDOM namespace supports following OpenAPI specification versions:
|
---|
| 4 |
|
---|
| 5 | - [OpenAPI 3.0.3 specification](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md)
|
---|
| 6 | - [OpenAPI 3.0.2 specification](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.2.md)
|
---|
| 7 | - [OpenAPI 3.0.1 specification](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.1.md)
|
---|
| 8 | - [OpenAPI 3.0.0 specification](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.0.md)
|
---|
| 9 |
|
---|
| 10 | ## Installation
|
---|
| 11 |
|
---|
| 12 | You can install this package via [npm CLI](https://docs.npmjs.com/cli) by running the following command:
|
---|
| 13 |
|
---|
| 14 | ```sh
|
---|
| 15 | $ npm install @swagger-api/apidom-ns-openapi-3-0
|
---|
| 16 | ```
|
---|
| 17 |
|
---|
| 18 | ## OpenAPI 3.0.x namespace
|
---|
| 19 |
|
---|
| 20 | OpenAPI 3.0.x namespace consists of [number of elements](https://github.com/swagger-api/apidom/tree/main/packages/apidom-ns-openapi-3-0/src/elements) implemented on top
|
---|
| 21 | of [primitive ones](https://github.com/refractproject/minim/tree/master/lib/primitives).
|
---|
| 22 |
|
---|
| 23 | ```js
|
---|
| 24 | import { createNamespace } from '@swagger-api/apidom-core';
|
---|
| 25 | import openApi3_0Namespace from '@swagger-api/apidom-ns-openapi-3-0';
|
---|
| 26 |
|
---|
| 27 | const namespace = createNamespace(openApi3_0Namespace);
|
---|
| 28 |
|
---|
| 29 | const objectElement = new namespace.elements.Object();
|
---|
| 30 | const openApiElement = new namespace.elements.OpenApi3_0();
|
---|
| 31 | ```
|
---|
| 32 |
|
---|
| 33 | When namespace instance is created in this way, it will extend the base namespace
|
---|
| 34 | with the namespace provided as an argument.
|
---|
| 35 |
|
---|
| 36 | Elements from the namespace can also be used directly by importing them.
|
---|
| 37 |
|
---|
| 38 | ```js
|
---|
| 39 | import { OpenApi3_0Element, InfoElement } from '@swagger-api/apidom-ns-openapi-3-0';
|
---|
| 40 |
|
---|
| 41 | const infoElement = new InfoElement();
|
---|
| 42 | const openApiElement = new OpenApi3_0Element();
|
---|
| 43 | ```
|
---|
| 44 |
|
---|
| 45 | ## Predicates
|
---|
| 46 |
|
---|
| 47 | This package exposes [predicates](https://github.com/swagger-api/apidom/blob/main/packages/apidom-ns-openapi-3-0/src/predicates.ts)
|
---|
| 48 | for all higher order elements that are part of this namespace.
|
---|
| 49 |
|
---|
| 50 | ```js
|
---|
| 51 | import { isOpenApi3_0Element, OpenApi3_0Element } from '@swagger-api/apidom-ns-openapi-3-0';
|
---|
| 52 |
|
---|
| 53 | const openApiElement = new OpenApi3_0Element();
|
---|
| 54 |
|
---|
| 55 | isOpenApi3_0Element(openApiElement); // => true
|
---|
| 56 | ```
|
---|
| 57 |
|
---|
| 58 | ## Traversal
|
---|
| 59 |
|
---|
| 60 | Traversing ApiDOM in this namespace is possible by using `visit` function from `apidom` package.
|
---|
| 61 | This package comes with its own [keyMap](https://github.com/swagger-api/apidom/blob/main/packages/apidom-ns-openapi-3-0/src/traversal/visitor.ts#L11) and [nodeTypeGetter](https://github.com/swagger-api/apidom/blob/main/packages/apidom-ns-openapi-3-0/src/traversal/visitor.ts#L4).
|
---|
| 62 | To 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).
|
---|
| 63 |
|
---|
| 64 | ```js
|
---|
| 65 | import { visit } from '@swagger-api/apidom-core';
|
---|
| 66 | import { OpenApi3_0Element, keyMap, getNodeType } from '@swagger-api/apidom-ns-openapi-3-0';
|
---|
| 67 |
|
---|
| 68 | const element = new OpenApi3_0Element();
|
---|
| 69 |
|
---|
| 70 | const visitor = {
|
---|
| 71 | OpenApi3_0Element(openApiElement) {
|
---|
| 72 | console.dir(openApiElement);
|
---|
| 73 | },
|
---|
| 74 | };
|
---|
| 75 |
|
---|
| 76 | visit(element, visitor, { keyMap, nodeTypeGetter: getNodeType });
|
---|
| 77 | ```
|
---|
| 78 |
|
---|
| 79 | ## Refractors
|
---|
| 80 |
|
---|
| 81 | Refractor is a special layer inside the namespace that can transform either JavaScript structures
|
---|
| 82 | or generic ApiDOM structures into structures built from elements of this namespace.
|
---|
| 83 |
|
---|
| 84 | **Refracting JavaScript structures**:
|
---|
| 85 |
|
---|
| 86 | ```js
|
---|
| 87 | import { InfoElement } from '@swagger-api/apidom-ns-openapi-3-0';
|
---|
| 88 |
|
---|
| 89 | const object = {
|
---|
| 90 | title: 'my title',
|
---|
| 91 | description: 'my description',
|
---|
| 92 | version: '0.1.0',
|
---|
| 93 | };
|
---|
| 94 |
|
---|
| 95 | InfoElement.refract(object); // => InfoElement({ title, description, version })
|
---|
| 96 | ```
|
---|
| 97 |
|
---|
| 98 | **Refracting generic ApiDOM structures**:
|
---|
| 99 |
|
---|
| 100 | ```js
|
---|
| 101 | import { ObjectElement } from '@swagger-api/apidom-core';
|
---|
| 102 | import { InfoElement } from '@swagger-api/apidom-ns-openapi-3-0';
|
---|
| 103 |
|
---|
| 104 | const objectElement = new ObjectElement({
|
---|
| 105 | title: 'my title',
|
---|
| 106 | description: 'my description',
|
---|
| 107 | version: '0.1.0',
|
---|
| 108 | });
|
---|
| 109 |
|
---|
| 110 | InfoElement.refract(objectElement); // => InfoElement({ title = 'my title', description = 'my description', version = '0.1.0' })
|
---|
| 111 | ```
|
---|
| 112 |
|
---|
| 113 | ### Refractor plugins
|
---|
| 114 |
|
---|
| 115 | Refractors can accept plugins as a second argument of refract static method.
|
---|
| 116 |
|
---|
| 117 | ```js
|
---|
| 118 | import { ObjectElement } from '@swagger-api/apidom-core';
|
---|
| 119 | import { InfoElement } from '@swagger-api/apidom-ns-openapi-3-0';
|
---|
| 120 |
|
---|
| 121 | const objectElement = new ObjectElement({
|
---|
| 122 | title: 'my title',
|
---|
| 123 | description: 'my description',
|
---|
| 124 | version: '0.1.0',
|
---|
| 125 | });
|
---|
| 126 |
|
---|
| 127 | const plugin = ({ predicates, namespace }) => ({
|
---|
| 128 | name: 'plugin',
|
---|
| 129 | pre() {
|
---|
| 130 | console.dir('runs before traversal');
|
---|
| 131 | },
|
---|
| 132 | visitor: {
|
---|
| 133 | InfoElement(infoElement) {
|
---|
| 134 | infoElement.version = '2.0.0';
|
---|
| 135 | },
|
---|
| 136 | },
|
---|
| 137 | post() {
|
---|
| 138 | console.dir('runs after traversal');
|
---|
| 139 | },
|
---|
| 140 | });
|
---|
| 141 |
|
---|
| 142 | InfoElement.refract(objectElement, { plugins: [plugin] }); // => InfoElement({ title = 'my title', description = 'my description', version = '2.0.0' })
|
---|
| 143 | ```
|
---|
| 144 |
|
---|
| 145 | You can define as many plugins as needed to enhance the resulting namespaced ApiDOM structure.
|
---|
| 146 | If multiple plugins with the same visitor method are defined, they run in parallel (just like in Babel).
|
---|
| 147 |
|
---|
| 148 | #### Replace Empty Element plugin
|
---|
| 149 |
|
---|
| 150 | This plugin is specific to YAML 1.2 format, which allows defining key-value pairs with empty key,
|
---|
| 151 | empty value, or both. If the value is not provided in YAML format, this plugin compensates for
|
---|
| 152 | this missing value with the most appropriate semantic element type.
|
---|
| 153 |
|
---|
| 154 | ```js
|
---|
| 155 | import { parse } from '@swagger-api/apidom-parser-adapter-yaml-1-2';
|
---|
| 156 | import { refractorPluginReplaceEmptyElement, OpenApi3_0Element } from '@swagger-api/apidom-ns-openapi-3-0';
|
---|
| 157 |
|
---|
| 158 | const yamlDefinition = `
|
---|
| 159 | openapi: 3.0.3
|
---|
| 160 | info:
|
---|
| 161 | `;
|
---|
| 162 | const apiDOM = await parse(yamlDefinition);
|
---|
| 163 | const openApiElement = OpenApi3_0Element.refract(apiDOM.result, {
|
---|
| 164 | plugins: [refractorPluginReplaceEmptyElement()],
|
---|
| 165 | });
|
---|
| 166 |
|
---|
| 167 | // =>
|
---|
| 168 | // (OpenApi3_0Element
|
---|
| 169 | // (MemberElement
|
---|
| 170 | // (StringElement)
|
---|
| 171 | // (OpenapiElement))
|
---|
| 172 | // (MemberElement
|
---|
| 173 | // (StringElement)
|
---|
| 174 | // (InfoElement)))
|
---|
| 175 |
|
---|
| 176 | // => without the plugin the result would be as follows:
|
---|
| 177 | // (OpenApi3_0Element
|
---|
| 178 | // (MemberElement
|
---|
| 179 | // (StringElement)
|
---|
| 180 | // (OpenapiElement))
|
---|
| 181 | // (MemberElement
|
---|
| 182 | // (StringElement)
|
---|
| 183 | // (StringElement)))
|
---|
| 184 | ```
|
---|
| 185 |
|
---|
| 186 | ## Implementation progress
|
---|
| 187 |
|
---|
| 188 | Only fully implemented specification objects should be checked here.
|
---|
| 189 |
|
---|
| 190 | - [x] [OpenAPI Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#oasObject)
|
---|
| 191 | - [x] [Info Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#infoObject)
|
---|
| 192 | - [x] [Contact Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#contactObject)
|
---|
| 193 | - [x] [License Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#licenseObject)
|
---|
| 194 | - [x] [Server Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#serverObject)
|
---|
| 195 | - [x] [Server Variable Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#serverVariableObject)
|
---|
| 196 | - [x] [Components](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#componentsObject)
|
---|
| 197 | - [x] [Paths Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#pathsObject)
|
---|
| 198 | - [x] [Path Item Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#pathItemObject)
|
---|
| 199 | - [x] [Operation Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#operationObject)
|
---|
| 200 | - [x] [External Documentation Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#externalDocumentationObject)
|
---|
| 201 | - [x] [Parameter Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#parameterObject)
|
---|
| 202 | - [x] [Request Body Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#requestBodyObject)
|
---|
| 203 | - [x] [Media Type Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#mediaTypeObject)
|
---|
| 204 | - [x] [Encoding Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#encodingObject)
|
---|
| 205 | - [x] [Responses Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#responsesObject)
|
---|
| 206 | - [x] [Callback Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#callbackObject)
|
---|
| 207 | - [x] [Example Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#exampleObject)
|
---|
| 208 | - [x] [Link Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#linkObject)
|
---|
| 209 | - [x] [Header Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#headerObject)
|
---|
| 210 | - [x] [Tag Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#tagObject)
|
---|
| 211 | - [x] [Reference Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#referenceObject)
|
---|
| 212 | - [x] [Schema Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#schemaObject)
|
---|
| 213 | - [x] [Discriminator Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#discriminatorObject)
|
---|
| 214 | - [x] [XML Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#xmlObject)
|
---|
| 215 | - [x] [Security Scheme Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#securitySchemeObject)
|
---|
| 216 | - [x] [OAuth Flows Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#oauthFlowsObject)
|
---|
| 217 | - [x] [OAuth Flow Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#oauthFlowObject)
|
---|
| 218 | - [x] [Security Requirement Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#securityRequirementObject)
|
---|
| 219 | - [x] [Specification extensions](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#specificationExtensions)
|
---|
| 220 |
|
---|