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

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

Initial commit

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