source: node_modules/swagger-client/es/resolver/strategies/openapi-3-1-apidom/resolve.js

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: 5.3 KB
Line 
1/* eslint-disable camelcase */
2import { toValue, transclude, ParseResultElement } from '@swagger-api/apidom-core';
3import { compile as jsonPointerCompile, evaluate as jsonPointerEvaluate, EvaluationJsonPointerError, InvalidJsonPointerError } from '@swagger-api/apidom-json-pointer';
4import { OpenApi3_1Element, mediaTypes } from '@swagger-api/apidom-ns-openapi-3-1';
5import { dereferenceApiDOM, url, ReferenceSet, Reference } from '@swagger-api/apidom-reference/configuration/empty';
6import BinaryParser from '@swagger-api/apidom-reference/parse/parsers/binary';
7import OpenApi3_1ResolveStrategy from '@swagger-api/apidom-reference/resolve/strategies/openapi-3-1';
8import { DEFAULT_BASE_URL } from '../../../constants.js';
9import * as optionsUtil from '../../utils/options.js';
10import normalize from './normalize.js';
11import HttpResolverSwaggerClient from '../../apidom/reference/resolve/resolvers/http-swagger-client/index.js';
12import JsonParser from '../../apidom/reference/parse/parsers/json/index.js';
13import YamlParser from '../../apidom/reference/parse/parsers/yaml-1-2/index.js';
14import OpenApiJson3_1Parser from '../../apidom/reference/parse/parsers/openapi-json-3-1/index.js';
15import OpenApiYaml3_1Parser from '../../apidom/reference/parse/parsers/openapi-yaml-3-1/index.js';
16import OpenApi3_1SwaggerClientDereferenceStrategy from '../../apidom/reference/dereference/strategies/openapi-3-1-swagger-client/index.js';
17const resolveOpenAPI31Strategy = async options => {
18 const {
19 spec,
20 timeout,
21 redirects,
22 requestInterceptor,
23 responseInterceptor,
24 pathDiscriminator = [],
25 allowMetaPatches = false,
26 useCircularStructures = false,
27 skipNormalization = false,
28 parameterMacro = null,
29 modelPropertyMacro = null,
30 mode = 'non-strict'
31 } = options;
32 try {
33 const {
34 cache
35 } = resolveOpenAPI31Strategy;
36
37 // determining BaseURI
38 const cwd = url.isHttpUrl(url.cwd()) ? url.cwd() : DEFAULT_BASE_URL;
39 const retrievalURI = optionsUtil.retrievalURI(options);
40 const baseURI = url.resolve(cwd, retrievalURI);
41
42 // prepare spec for dereferencing
43 let openApiElement;
44 if (cache.has(spec)) {
45 openApiElement = cache.get(spec);
46 } else {
47 openApiElement = OpenApi3_1Element.refract(spec);
48 openApiElement.classes.push('result');
49 cache.set(spec, openApiElement);
50 }
51 const openApiParseResultElement = new ParseResultElement([openApiElement]);
52
53 // prepare fragment for dereferencing
54 const jsonPointer = jsonPointerCompile(pathDiscriminator);
55 const jsonPointerURI = jsonPointer === '' ? '' : `#${jsonPointer}`;
56 const fragmentElement = jsonPointerEvaluate(jsonPointer, openApiElement);
57
58 // prepare reference set for dereferencing
59 const openApiElementReference = Reference({
60 uri: baseURI,
61 value: openApiParseResultElement
62 });
63 const refSet = ReferenceSet({
64 refs: [openApiElementReference]
65 });
66 if (jsonPointer !== '') refSet.rootRef = null; // reset root reference as we want fragment to become the root reference
67
68 // prepare ancestors; needed for cases where fragment is not OpenAPI element
69 const ancestors = [new Set([fragmentElement])];
70 const errors = [];
71 const dereferenced = await dereferenceApiDOM(fragmentElement, {
72 resolve: {
73 /**
74 * swagger-client only supports resolving HTTP(S) URLs or spec objects.
75 * If runtime env is detected as non-browser one,
76 * and baseURI was not provided as part of resolver options,
77 * then below baseURI check will make sure that constant HTTPS URL is used as baseURI.
78 */
79 baseURI: `${baseURI}${jsonPointerURI}`,
80 resolvers: [HttpResolverSwaggerClient({
81 timeout: timeout || 10000,
82 redirects: redirects || 10
83 })],
84 resolverOpts: {
85 swaggerHTTPClientConfig: {
86 requestInterceptor,
87 responseInterceptor
88 }
89 },
90 strategies: [OpenApi3_1ResolveStrategy()]
91 },
92 parse: {
93 mediaType: mediaTypes.latest(),
94 parsers: [OpenApiJson3_1Parser({
95 allowEmpty: false,
96 sourceMap: false
97 }), OpenApiYaml3_1Parser({
98 allowEmpty: false,
99 sourceMap: false
100 }), JsonParser({
101 allowEmpty: false,
102 sourceMap: false
103 }), YamlParser({
104 allowEmpty: false,
105 sourceMap: false
106 }), BinaryParser({
107 allowEmpty: false,
108 sourceMap: false
109 })]
110 },
111 dereference: {
112 maxDepth: 100,
113 strategies: [OpenApi3_1SwaggerClientDereferenceStrategy({
114 allowMetaPatches,
115 useCircularStructures,
116 parameterMacro,
117 modelPropertyMacro,
118 mode,
119 ancestors
120 })],
121 refSet,
122 dereferenceOpts: {
123 errors
124 }
125 }
126 });
127 const transcluded = transclude(fragmentElement, dereferenced, openApiElement);
128 const normalized = skipNormalization ? transcluded : normalize(transcluded);
129 return {
130 spec: toValue(normalized),
131 errors
132 };
133 } catch (error) {
134 if (error instanceof InvalidJsonPointerError || error instanceof EvaluationJsonPointerError) {
135 return {
136 spec: null,
137 errors: []
138 };
139 }
140 throw error;
141 }
142};
143resolveOpenAPI31Strategy.cache = new WeakMap();
144export default resolveOpenAPI31Strategy;
145/* eslint-enable camelcase */
Note: See TracBrowser for help on using the repository browser.