source: node_modules/@swagger-api/apidom-ns-openapi-3-1/cjs/refractor/plugins/normalize-parameter-examples.cjs

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: 3.0 KB
Line 
1"use strict";
2
3exports.__esModule = true;
4exports.default = void 0;
5var _apidomCore = require("@swagger-api/apidom-core");
6/**
7 * Override of Schema.example and Schema.examples field inside the Parameter Objects.
8 *
9 * Parameter Object has two fixed fields:
10 * - `example` of type `Any`
11 * - `examples` of type `Map[string, Example Object | Reference Object]`
12 *
13 * OpenAPI 3.1 specification excerpt that defines the override behavior:
14 *
15 * The example value SHALL override the example provided by the schema.
16 * Furthermore, if referencing a schema that contains an example, the examples value SHALL override the example provided by the schema.
17 */
18/* eslint-disable no-param-reassign */
19const plugin = () => ({
20 predicates
21}) => {
22 return {
23 visitor: {
24 ParameterElement: {
25 leave(parameterElement, key, parent, path, ancestors) {
26 var _parameterElement$sch, _parameterElement$sch2;
27 // skip visiting this Parameter Object
28 if (ancestors.some(predicates.isComponentsElement)) {
29 return;
30 }
31
32 // no Parameter.schema field present
33 if (typeof parameterElement.schema === 'undefined' || !predicates.isSchemaElement(parameterElement.schema)) {
34 return;
35 }
36 // Schema contains no example
37 if (typeof ((_parameterElement$sch = parameterElement.schema) == null ? void 0 : _parameterElement$sch.example) === 'undefined' && typeof ((_parameterElement$sch2 = parameterElement.schema) == null ? void 0 : _parameterElement$sch2.examples) === 'undefined') {
38 return;
39 }
40
41 /**
42 * Parameter.examples and Schema.examples have preferences over the older
43 * and deprected `example` field.
44 */
45 if (typeof parameterElement.examples !== 'undefined' && predicates.isObjectElement(parameterElement.examples)) {
46 // @ts-ignore
47 const examples = parameterElement.examples.map(example => {
48 return _apidomCore.cloneDeep.safe(example.value);
49 });
50 if (typeof parameterElement.schema.examples !== 'undefined') {
51 parameterElement.schema.set('examples', examples);
52 }
53 if (typeof parameterElement.schema.example !== 'undefined') {
54 parameterElement.schema.set('example', examples);
55 }
56 return;
57 }
58
59 /**
60 * Handle deprecated `example` field.
61 */
62 if (typeof parameterElement.example !== 'undefined') {
63 if (typeof parameterElement.schema.examples !== 'undefined') {
64 parameterElement.schema.set('examples', [(0, _apidomCore.cloneDeep)(parameterElement.example)]);
65 }
66 if (typeof parameterElement.schema.example !== 'undefined') {
67 parameterElement.schema.set('example', (0, _apidomCore.cloneDeep)(parameterElement.example));
68 }
69 }
70 }
71 }
72 }
73 };
74};
75/* eslint-enable */
76var _default = exports.default = plugin;
Note: See TracBrowser for help on using the repository browser.