1 | "use strict";
|
---|
2 |
|
---|
3 | exports.__esModule = true;
|
---|
4 | exports.default = void 0;
|
---|
5 | var _apidomCore = require("@swagger-api/apidom-core");
|
---|
6 | /**
|
---|
7 | * Override of Schema.example and Schema.examples field inside the Header Objects.
|
---|
8 | *
|
---|
9 | * Header 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 */
|
---|
19 | const plugin = () => ({
|
---|
20 | predicates
|
---|
21 | }) => {
|
---|
22 | return {
|
---|
23 | visitor: {
|
---|
24 | HeaderElement: {
|
---|
25 | leave(headerElement, key, parent, path, ancestors) {
|
---|
26 | var _headerElement$schema, _headerElement$schema2;
|
---|
27 | // skip visiting this Header Object
|
---|
28 | if (ancestors.some(predicates.isComponentsElement)) {
|
---|
29 | return;
|
---|
30 | }
|
---|
31 |
|
---|
32 | // no Header.schema field present
|
---|
33 | if (typeof headerElement.schema === 'undefined' || !predicates.isSchemaElement(headerElement.schema)) {
|
---|
34 | return;
|
---|
35 | }
|
---|
36 | // Schema contains no example
|
---|
37 | if (typeof ((_headerElement$schema = headerElement.schema) == null ? void 0 : _headerElement$schema.example) === 'undefined' && typeof ((_headerElement$schema2 = headerElement.schema) == null ? void 0 : _headerElement$schema2.examples) === 'undefined') {
|
---|
38 | return;
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Header.examples and Schema.examples have preferences over the older
|
---|
43 | * and deprected `example` field.
|
---|
44 | */
|
---|
45 | if (typeof headerElement.examples !== 'undefined' && predicates.isObjectElement(headerElement.examples)) {
|
---|
46 | // @ts-ignore
|
---|
47 | const examples = headerElement.examples.map(example => {
|
---|
48 | return _apidomCore.cloneDeep.safe(example.value);
|
---|
49 | });
|
---|
50 | if (typeof headerElement.schema.examples !== 'undefined') {
|
---|
51 | headerElement.schema.set('examples', examples);
|
---|
52 | }
|
---|
53 | if (typeof headerElement.schema.example !== 'undefined') {
|
---|
54 | headerElement.schema.set('example', examples);
|
---|
55 | }
|
---|
56 | return;
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Handle deprecated `example` field.
|
---|
61 | */
|
---|
62 | if (typeof headerElement.example !== 'undefined') {
|
---|
63 | if (typeof headerElement.schema.examples !== 'undefined') {
|
---|
64 | headerElement.schema.set('examples', [(0, _apidomCore.cloneDeep)(headerElement.example)]);
|
---|
65 | }
|
---|
66 | if (typeof headerElement.schema.example !== 'undefined') {
|
---|
67 | headerElement.schema.set('example', (0, _apidomCore.cloneDeep)(headerElement.example));
|
---|
68 | }
|
---|
69 | }
|
---|
70 | }
|
---|
71 | }
|
---|
72 | }
|
---|
73 | };
|
---|
74 | };
|
---|
75 | /* eslint-enable */
|
---|
76 | var _default = exports.default = plugin; |
---|