source: node_modules/@swagger-api/apidom-ns-openapi-3-0/es/refractor/plugins/replace-empty-element.mjs

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

Initial commit

  • Property mode set to 100644
File size: 19.0 KB
RevLine 
[d24f17c]1import { ArrayElement, ObjectElement, isStringElement, isArrayElement, isElement, isMemberElement, includesClasses, cloneDeep, toValue } from '@swagger-api/apidom-core';
2
3/**
4 * OpenAPI 3.0.3 specification elements.
5 */
6import InfoElement from "../../elements/Info.mjs";
7import ContactElement from "../../elements/Contact.mjs";
8import LicenseElement from "../../elements/License.mjs";
9import PathsElement from "../../elements/Paths.mjs";
10import PathItemElement from "../../elements/PathItem.mjs";
11import ComponentsElement from "../../elements/Components.mjs";
12import ExternalDocumentationElement from "../../elements/ExternalDocumentation.mjs";
13import OperationElement from "../../elements/Operation.mjs";
14import SchemaElement from "../../elements/Schema.mjs";
15import RequestBodyElement from "../../elements/RequestBody.mjs";
16import ResponsesElement from "../../elements/Responses.mjs";
17import ResponseElement from "../../elements/Response.mjs";
18import ServerElement from "../../elements/Server.mjs";
19import DiscriminatorElement from "../../elements/Discriminator.mjs";
20import XmlElement from "../../elements/Xml.mjs";
21import OAuthFlowsElement from "../../elements/OAuthFlows.mjs";
22import OAuthFlowElement from "../../elements/OAuthFlow.mjs";
23import ServerVariableElement from "../../elements/ServerVariable.mjs";
24import ParameterElement from "../../elements/Parameter.mjs";
25import ExampleElement from "../../elements/Example.mjs";
26import HeaderElement from "../../elements/Header.mjs";
27import SecuritySchemeElement from "../../elements/SecurityScheme.mjs";
28import LinkElement from "../../elements/Link.mjs";
29import CallbackElement from "../../elements/Callback.mjs";
30import MediaTypeElement from "../../elements/MediaType.mjs";
31import EncodingElement from "../../elements/Encoding.mjs";
32import SecurityRequirementElement from "../../elements/SecurityRequirement.mjs";
33import TagElement from "../../elements/Tag.mjs"; // non-concrete Elements (NCEs)
34import ServersElement from "../../elements/nces/Servers.mjs";
35import SecurityElement from "../../elements/nces/Security.mjs";
36import TagsElement from "../../elements/nces/Tags.mjs";
37import ServerVariablesElement from "../../elements/nces/ServerVariables.mjs";
38import ComponentsSchemasElement from "../../elements/nces/ComponentsSchemas.mjs";
39import ComponentsResponsesElement from "../../elements/nces/ComponentsResponses.mjs";
40import ComponentsParametersElement from "../../elements/nces/ComponentsParameters.mjs";
41import ComponentsExamplesElement from "../../elements/nces/ComponentsExamples.mjs";
42import ComponentsRequestBodiesElement from "../../elements/nces/ComponentsRequestBodies.mjs";
43import ComponentsHeadersElement from "../../elements/nces/ComponentsHeaders.mjs";
44import ComponentsSecuritySchemesElement from "../../elements/nces/ComponentsSecuritySchemes.mjs";
45import ComponentsLinksElement from "../../elements/nces/ComponentsLinks.mjs";
46import ComponentsCallbacksElement from "../../elements/nces/ComponentsCallbacks.mjs";
47import PathItemServersElement from "../../elements/nces/PathItemServers.mjs";
48import PathItemParametersElement from "../../elements/nces/PathItemParameters.mjs";
49import OperationParametersElement from "../../elements/nces/OperationParameters.mjs";
50import ParameterExamplesElement from "../../elements/nces/ParameterExamples.mjs";
51import ParameterContentElement from "../../elements/nces/ParameterContent.mjs";
52import OperationTagsElement from "../../elements/nces/OperationTags.mjs";
53import OperationCallbacksElement from "../../elements/nces/OperationCallbacks.mjs";
54import OperationSecurityElement from "../../elements/nces/OperationSecurity.mjs";
55import OperationServersElement from "../../elements/nces/OperationServers.mjs";
56import RequestBodyContentElement from "../../elements/nces/RequestBodyContent.mjs";
57import MediaTypeExamplesElement from "../../elements/nces/MediaTypeExamples.mjs";
58import MediaTypeEncodingElement from "../../elements/nces/MediaTypeEncoding.mjs";
59import EncodingHeadersElement from "../../elements/nces/EncodingHeaders.mjs";
60import ResponseHeadersElement from "../../elements/nces/ResponseHeaders.mjs";
61import ResponseContentElement from "../../elements/nces/ResponseContent.mjs";
62import ResponseLinksElement from "../../elements/nces/ResponseLinks.mjs";
63import DiscriminatorMappingElement from "../../elements/nces/DiscriminatorMapping.mjs";
64import OAuthFlowScopesElement from "../../elements/nces/OAuthFlowScopes.mjs";
65import LinkParametersElement from "../../elements/nces/LinkParameters.mjs";
66import HeaderExamplesElement from "../../elements/nces/HeaderExamples.mjs";
67import HeaderContentElement from "../../elements/nces/HeaderContent.mjs";
68import { getNodeType } from "../../traversal/visitor.mjs";
69/**
70 * This plugin is specific to YAML 1.2 format, which allows defining key-value pairs
71 * with empty key, empty value, or both. If the value is not provided in YAML format,
72 * this plugin compensates for this missing value with the most appropriate semantic element type.
73 *
74 * https://yaml.org/spec/1.2.2/#72-empty-nodes
75 *
76 * @example
77 *
78 * ```yaml
79 * openapi: 3.0.3
80 * info:
81 * ```
82 * Refracting result without this plugin:
83 *
84 * (OpenApi3_0Element
85 * (MemberElement
86 * (StringElement)
87 * (OpenapiElement))
88 * (MemberElement
89 * (StringElement)
90 * (StringElement))
91 *
92 * Refracting result with this plugin:
93 *
94 * (OpenApi3_0Element
95 * (MemberElement
96 * (StringElement)
97 * (OpenapiElement))
98 * (MemberElement
99 * (StringElement)
100 * (InfoElement))
101 */
102const isEmptyElement = element => isStringElement(element) && includesClasses(['yaml-e-node', 'yaml-e-scalar'], element);
103const schema = {
104 // concrete types handling (CTs)
105 OpenApi3_0Element: {
106 info(...args) {
107 return new InfoElement(...args);
108 },
109 servers(...args) {
110 return new ServersElement(...args);
111 },
112 paths(...args) {
113 return new PathsElement(...args);
114 },
115 components(...args) {
116 return new ComponentsElement(...args);
117 },
118 security(...args) {
119 return new SecurityElement(...args);
120 },
121 tags(...args) {
122 return new TagsElement(...args);
123 },
124 externalDocs(...args) {
125 return new ExternalDocumentationElement(...args);
126 }
127 },
128 InfoElement: {
129 contact(...args) {
130 return new ContactElement(...args);
131 },
132 license(...args) {
133 return new LicenseElement(...args);
134 }
135 },
136 ServerElement: {
137 variables(...args) {
138 return new ServerVariablesElement(...args);
139 }
140 },
141 ServerVariableElement: {
142 enum(...args) {
143 return new ArrayElement(...args);
144 }
145 },
146 PathsElement: {
147 '[key: *]': function key(...args) {
148 return new PathItemElement(...args);
149 }
150 },
151 PathItemElement: {
152 get(...args) {
153 return new OperationElement(...args);
154 },
155 put(...args) {
156 return new OperationElement(...args);
157 },
158 post(...args) {
159 return new OperationElement(...args);
160 },
161 delete(...args) {
162 return new OperationElement(...args);
163 },
164 options(...args) {
165 return new OperationElement(...args);
166 },
167 head(...args) {
168 return new OperationElement(...args);
169 },
170 patch(...args) {
171 return new OperationElement(...args);
172 },
173 trace(...args) {
174 return new OperationElement(...args);
175 },
176 servers(...args) {
177 return new PathItemServersElement(...args);
178 },
179 parameters(...args) {
180 return new PathItemParametersElement(...args);
181 }
182 },
183 OperationElement: {
184 tags(...args) {
185 return new OperationTagsElement(...args);
186 },
187 externalDocs(...args) {
188 return new ExternalDocumentationElement(...args);
189 },
190 parameters(...args) {
191 return new OperationParametersElement(...args);
192 },
193 requestBody(...args) {
194 return new RequestBodyElement(...args);
195 },
196 responses(...args) {
197 return new ResponsesElement(...args);
198 },
199 callbacks(...args) {
200 return new OperationCallbacksElement(...args);
201 },
202 security(...args) {
203 return new OperationSecurityElement(...args);
204 },
205 servers(...args) {
206 return new OperationServersElement(...args);
207 }
208 },
209 ParameterElement: {
210 schema(...args) {
211 return new SchemaElement(...args);
212 },
213 examples(...args) {
214 return new ParameterExamplesElement(...args);
215 },
216 content(...args) {
217 return new ParameterContentElement(...args);
218 }
219 },
220 RequestBodyElement: {
221 content(...args) {
222 return new RequestBodyContentElement(...args);
223 }
224 },
225 MediaTypeElement: {
226 schema(...args) {
227 return new SchemaElement(...args);
228 },
229 examples(...args) {
230 return new MediaTypeExamplesElement(...args);
231 },
232 encoding(...args) {
233 return new MediaTypeEncodingElement(...args);
234 }
235 },
236 EncodingElement: {
237 headers(...args) {
238 return new EncodingHeadersElement(...args);
239 }
240 },
241 ResponsesElement: {
242 '[key: *]': function key(...args) {
243 return new ResponseElement(...args);
244 }
245 },
246 ResponseElement: {
247 headers(...args) {
248 return new ResponseHeadersElement(...args);
249 },
250 content(...args) {
251 return new ResponseContentElement(...args);
252 },
253 links(...args) {
254 return new ResponseLinksElement(...args);
255 }
256 },
257 CallbackElement: {
258 '[key: *]': function key(...args) {
259 return new PathItemElement(...args);
260 }
261 },
262 LinkElement: {
263 parameters(...args) {
264 return new LinkParametersElement(...args);
265 },
266 server(...args) {
267 return new ServerElement(...args);
268 }
269 },
270 HeaderElement: {
271 schema(...args) {
272 return new SchemaElement(...args);
273 },
274 examples(...args) {
275 return new HeaderExamplesElement(...args);
276 },
277 content(...args) {
278 return new HeaderContentElement(...args);
279 }
280 },
281 ComponentsElement: {
282 schemas(...args) {
283 return new ComponentsSchemasElement(...args);
284 },
285 responses(...args) {
286 return new ComponentsResponsesElement(...args);
287 },
288 parameters(...args) {
289 return new ComponentsParametersElement(...args);
290 },
291 examples(...args) {
292 return new ComponentsExamplesElement(...args);
293 },
294 requestBodies(...args) {
295 return new ComponentsRequestBodiesElement(...args);
296 },
297 headers(...args) {
298 return new ComponentsHeadersElement(...args);
299 },
300 securitySchemes(...args) {
301 return new ComponentsSecuritySchemesElement(...args);
302 },
303 links(...args) {
304 return new ComponentsLinksElement(...args);
305 },
306 callbacks(...args) {
307 return new ComponentsCallbacksElement(...args);
308 }
309 },
310 SecurityRequirementElement: {
311 '[key: *]': function key(...args) {
312 return new ArrayElement(...args);
313 }
314 },
315 TagElement: {
316 externalDocs(...args) {
317 return new ExternalDocumentationElement(...args);
318 }
319 },
320 SchemaElement: {
321 definitions(...args) {
322 const element = new ObjectElement(...args);
323 element.classes.push('json-schema-definitions');
324 return element;
325 },
326 allOf(...args) {
327 const element = new ArrayElement(...args);
328 element.classes.push('json-schema-allOf');
329 return element;
330 },
331 anyOf(...args) {
332 const element = new ArrayElement(...args);
333 element.classes.push('json-schema-anyOf');
334 return element;
335 },
336 oneOf(...args) {
337 const element = new ArrayElement(...args);
338 element.classes.push('json-schema-oneOf');
339 return element;
340 },
341 not(...args) {
342 return new SchemaElement(...args);
343 },
344 items(...args) {
345 return new SchemaElement(...args);
346 },
347 properties(...args) {
348 const element = new ObjectElement(...args);
349 element.classes.push('json-schema-properties');
350 return element;
351 },
352 patternProperties(...args) {
353 const element = new ObjectElement(...args);
354 element.classes.push('json-schema-patternProperties');
355 return element;
356 },
357 additionalProperties(...args) {
358 return new SchemaElement(...args);
359 },
360 enum(...args) {
361 const element = new ArrayElement(...args);
362 element.classes.push('json-schema-enum');
363 return element;
364 },
365 required(...args) {
366 const element = new ArrayElement(...args);
367 element.classes.push('json-schema-required');
368 return element;
369 },
370 discriminator(...args) {
371 return new DiscriminatorElement(...args);
372 },
373 xml(...args) {
374 return new XmlElement(...args);
375 },
376 externalDocs(...args) {
377 return new ExternalDocumentationElement(...args);
378 }
379 },
380 DiscriminatorElement: {
381 mapping(...args) {
382 return new DiscriminatorMappingElement(...args);
383 }
384 },
385 SecuritySchemeElement: {
386 flows(...args) {
387 return new OAuthFlowsElement(...args);
388 }
389 },
390 OAuthFlowsElement: {
391 implicit(...args) {
392 return new OAuthFlowElement(...args);
393 },
394 password(...args) {
395 return new OAuthFlowElement(...args);
396 },
397 clientCredentials(...args) {
398 return new OAuthFlowElement(...args);
399 },
400 authorizationCode(...args) {
401 return new OAuthFlowElement(...args);
402 }
403 },
404 OAuthFlowElement: {
405 scopes(...args) {
406 return new OAuthFlowScopesElement(...args);
407 }
408 },
409 // non-concrete types handling (NCEs)
410 [ServerVariablesElement.primaryClass]: {
411 '[key: *]': function key(...args) {
412 return new ServerVariableElement(...args);
413 }
414 },
415 [ComponentsSchemasElement.primaryClass]: {
416 '[key: *]': function key(...args) {
417 return new SchemaElement(...args);
418 }
419 },
420 [ComponentsResponsesElement.primaryClass]: {
421 '[key: *]': function key(...args) {
422 return new ResponseElement(...args);
423 }
424 },
425 [ComponentsParametersElement.primaryClass]: {
426 '[key: *]': function key(...args) {
427 return new ParameterElement(...args);
428 }
429 },
430 [ComponentsExamplesElement.primaryClass]: {
431 '[key: *]': function key(...args) {
432 return new ExampleElement(...args);
433 }
434 },
435 [ComponentsRequestBodiesElement.primaryClass]: {
436 '[key: *]': function key(...args) {
437 return new RequestBodyElement(...args);
438 }
439 },
440 [ComponentsHeadersElement.primaryClass]: {
441 '[key: *]': function key(...args) {
442 return new HeaderElement(...args);
443 }
444 },
445 [ComponentsSecuritySchemesElement.primaryClass]: {
446 '[key: *]': function key(...args) {
447 return new SecuritySchemeElement(...args);
448 }
449 },
450 [ComponentsLinksElement.primaryClass]: {
451 '[key: *]': function key(...args) {
452 return new LinkElement(...args);
453 }
454 },
455 [ComponentsCallbacksElement.primaryClass]: {
456 '[key: *]': function key(...args) {
457 return new CallbackElement(...args);
458 }
459 },
460 [OperationCallbacksElement.primaryClass]: {
461 '[key: *]': function key(...args) {
462 return new CallbackElement(...args);
463 }
464 },
465 [ParameterExamplesElement.primaryClass]: {
466 '[key: *]': function key(...args) {
467 return new ExampleElement(...args);
468 }
469 },
470 [ParameterContentElement.primaryClass]: {
471 '[key: *]': function key(...args) {
472 return new MediaTypeElement(...args);
473 }
474 },
475 [RequestBodyContentElement.primaryClass]: {
476 '[key: *]': function key(...args) {
477 return new MediaTypeElement(...args);
478 }
479 },
480 [MediaTypeExamplesElement.primaryClass]: {
481 '[key: *]': function key(...args) {
482 return new ExampleElement(...args);
483 }
484 },
485 [MediaTypeEncodingElement.primaryClass]: {
486 '[key: *]': function key(...args) {
487 return new EncodingElement(...args);
488 }
489 },
490 [EncodingHeadersElement.primaryClass]: {
491 '[key: *]': function key(...args) {
492 return new HeaderElement(...args);
493 }
494 },
495 [ResponseHeadersElement.primaryClass]: {
496 '[key: *]': function key(...args) {
497 return new HeaderElement(...args);
498 }
499 },
500 [ResponseContentElement.primaryClass]: {
501 '[key: *]': function key(...args) {
502 return new MediaTypeElement(...args);
503 }
504 },
505 [ResponseLinksElement.primaryClass]: {
506 '[key: *]': function key(...args) {
507 return new LinkElement(...args);
508 }
509 },
510 'json-schema-$defs': {
511 '[key: *]': function key(...args) {
512 return new SchemaElement(...args);
513 }
514 },
515 'json-schema-dependentSchemas': {
516 '[key: *]': function key(...args) {
517 return new SchemaElement(...args);
518 }
519 },
520 'json-schema-properties': {
521 '[key: *]': function key(...args) {
522 return new SchemaElement(...args);
523 }
524 },
525 [ServersElement.primaryClass]: {
526 '<*>': function asterisk(...args) {
527 return new ServerElement(...args);
528 }
529 },
530 [SecurityElement.primaryClass]: {
531 '<*>': function asterisk(...args) {
532 return new SecurityRequirementElement(...args);
533 }
534 },
535 [TagsElement.primaryClass]: {
536 '<*>': function asterisk(...args) {
537 return new TagElement(...args);
538 }
539 },
540 [PathItemServersElement.primaryClass]: {
541 '<*>': function asterisk(...args) {
542 return new ServerElement(...args);
543 }
544 },
545 [PathItemParametersElement.primaryClass]: {
546 '<*>': function asterisk(...args) {
547 return new ParameterElement(...args);
548 }
549 },
550 [OperationParametersElement.primaryClass]: {
551 '<*>': function asterisk(...args) {
552 return new ParameterElement(...args);
553 }
554 },
555 [OperationSecurityElement.primaryClass]: {
556 '<*>': function asterisk(...args) {
557 return new SecurityRequirementElement(...args);
558 }
559 },
560 [OperationServersElement.primaryClass]: {
561 '<*>': function asterisk(...args) {
562 return new ServerElement(...args);
563 }
564 },
565 'json-schema-allOf': {
566 '<*>': function asterisk(...args) {
567 return new SchemaElement(...args);
568 }
569 },
570 'json-schema-anyOf': {
571 '<*>': function asterisk(...args) {
572 return new SchemaElement(...args);
573 }
574 },
575 'json-schema-oneOf': {
576 '<*>': function asterisk(...args) {
577 return new SchemaElement(...args);
578 }
579 },
580 'json-schema-prefixItems': {
581 '<*>': function asterisk(...args) {
582 return new SchemaElement(...args);
583 }
584 }
585};
586const findElementFactory = (ancestor, keyName) => {
587 const elementType = getNodeType(ancestor); // @ts-ignore
588 const keyMapping = schema[elementType] || schema[toValue(ancestor.classes.first)];
589 return typeof keyMapping === 'undefined' ? undefined : Object.prototype.hasOwnProperty.call(keyMapping, '[key: *]') ? keyMapping['[key: *]'] : keyMapping[keyName];
590};
591const plugin = () => () => ({
592 visitor: {
593 StringElement(element, key, parent, path, ancestors) {
594 if (!isEmptyElement(element)) return undefined;
595 const lineage = [...ancestors, parent].filter(isElement);
596 const parentElement = lineage[lineage.length - 1]; // @TODO(vladimir.gorej@gmail.com): can be replaced by Array.prototype.at in future
597 let elementFactory;
598 let context;
599 if (isArrayElement(parentElement)) {
600 context = element;
601 elementFactory = findElementFactory(parentElement, '<*>');
602 } else if (isMemberElement(parentElement)) {
603 context = lineage[lineage.length - 2]; // @TODO(vladimir.gorej@gmail.com): can be replaced by Array.prototype.at in future
604 elementFactory = findElementFactory(context, toValue(parentElement.key));
605 }
606
607 // no element factory found
608 if (typeof elementFactory !== 'function') return undefined;
609 return elementFactory.call({
610 context
611 }, undefined, cloneDeep(element.meta), cloneDeep(element.attributes));
612 }
613 }
614});
615export default plugin;
Note: See TracBrowser for help on using the repository browser.