source: node_modules/@swagger-api/apidom-ns-openapi-3-1/es/refractor/plugins/normalize-header-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.8 KB
Line 
1import { cloneDeep } from '@swagger-api/apidom-core';
2/**
3 * Override of Schema.example and Schema.examples field inside the Header Objects.
4 *
5 * Header 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 HeaderElement: {
21 leave(headerElement, key, parent, path, ancestors) {
22 var _headerElement$schema, _headerElement$schema2;
23 // skip visiting this Header Object
24 if (ancestors.some(predicates.isComponentsElement)) {
25 return;
26 }
27
28 // no Header.schema field present
29 if (typeof headerElement.schema === 'undefined' || !predicates.isSchemaElement(headerElement.schema)) {
30 return;
31 }
32 // Schema contains no example
33 if (typeof ((_headerElement$schema = headerElement.schema) === null || _headerElement$schema === void 0 ? void 0 : _headerElement$schema.example) === 'undefined' && typeof ((_headerElement$schema2 = headerElement.schema) === null || _headerElement$schema2 === void 0 ? void 0 : _headerElement$schema2.examples) === 'undefined') {
34 return;
35 }
36
37 /**
38 * Header.examples and Schema.examples have preferences over the older
39 * and deprected `example` field.
40 */
41 if (typeof headerElement.examples !== 'undefined' && predicates.isObjectElement(headerElement.examples)) {
42 // @ts-ignore
43 const examples = headerElement.examples.map(example => {
44 return cloneDeep.safe(example.value);
45 });
46 if (typeof headerElement.schema.examples !== 'undefined') {
47 headerElement.schema.set('examples', examples);
48 }
49 if (typeof headerElement.schema.example !== 'undefined') {
50 headerElement.schema.set('example', examples);
51 }
52 return;
53 }
54
55 /**
56 * Handle deprecated `example` field.
57 */
58 if (typeof headerElement.example !== 'undefined') {
59 if (typeof headerElement.schema.examples !== 'undefined') {
60 headerElement.schema.set('examples', [cloneDeep(headerElement.example)]);
61 }
62 if (typeof headerElement.schema.example !== 'undefined') {
63 headerElement.schema.set('example', cloneDeep(headerElement.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.