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

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

Initial commit

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