1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | var JsonPointer = tslib_1.__importStar(require("../jsonPointer"));
|
5 | var schemaId_1 = tslib_1.__importDefault(require("./schemaId"));
|
6 | function parseSchema(content, url) {
|
7 | var _a = selectSchemaType(content), type = _a.type, openApiVersion = _a.openApiVersion;
|
8 | if (url != null) {
|
9 | setId(type, content, url);
|
10 | }
|
11 | var id = getId(type, content);
|
12 | return {
|
13 | type: type,
|
14 | openApiVersion: openApiVersion,
|
15 | id: id ? new schemaId_1.default(id) : schemaId_1.default.empty,
|
16 | content: content,
|
17 | };
|
18 | }
|
19 | exports.parseSchema = parseSchema;
|
20 | function getSubSchema(rootSchema, pointer, id) {
|
21 | var content = JsonPointer.get(rootSchema.content, JsonPointer.parse(pointer));
|
22 | if (id == null) {
|
23 | var subId = getId(rootSchema.type, content);
|
24 | var getParentIds_1 = function (s, result) {
|
25 | result.push(s.id.getAbsoluteId());
|
26 | return s.rootSchema == null ? result : getParentIds_1(s.rootSchema, result);
|
27 | };
|
28 | if (subId) {
|
29 | id = new schemaId_1.default(subId, getParentIds_1(rootSchema, []));
|
30 | }
|
31 | else {
|
32 | id = new schemaId_1.default(pointer, getParentIds_1(rootSchema, []));
|
33 | }
|
34 | }
|
35 | return {
|
36 | type: rootSchema.type,
|
37 | id: id,
|
38 | content: content,
|
39 | rootSchema: rootSchema,
|
40 | };
|
41 | }
|
42 | exports.getSubSchema = getSubSchema;
|
43 | function getId(type, content) {
|
44 | return content[getIdPropertyName(type)];
|
45 | }
|
46 | exports.getId = getId;
|
47 | function setId(type, content, id) {
|
48 | var key = getIdPropertyName(type);
|
49 | if (content[key] == null) {
|
50 | content[key] = id;
|
51 | }
|
52 | }
|
53 | function getIdPropertyName(type) {
|
54 | switch (type) {
|
55 | case 'Draft04': return 'id';
|
56 | case 'Draft07': return '$id';
|
57 | }
|
58 | }
|
59 | function searchAllSubSchema(schema, onFoundSchema, onFoundReference) {
|
60 | var walkArray = function (array, paths, parentIds) {
|
61 | if (array == null) {
|
62 | return;
|
63 | }
|
64 | array.forEach(function (item, index) {
|
65 | walk(item, paths.concat(index.toString()), parentIds);
|
66 | });
|
67 | };
|
68 | var walkObject = function (obj, paths, parentIds) {
|
69 | if (obj == null) {
|
70 | return;
|
71 | }
|
72 | Object.keys(obj).forEach(function (key) {
|
73 | var sub = obj[key];
|
74 | if (sub != null) {
|
75 | walk(sub, paths.concat(key), parentIds);
|
76 | }
|
77 | });
|
78 | };
|
79 | var walkMaybeArray = function (item, paths, parentIds) {
|
80 | if (Array.isArray(item)) {
|
81 | walkArray(item, paths, parentIds);
|
82 | }
|
83 | else {
|
84 | walk(item, paths, parentIds);
|
85 | }
|
86 | };
|
87 | var walk = function (s, paths, parentIds) {
|
88 | if (s == null || typeof s !== 'object') {
|
89 | return;
|
90 | }
|
91 | var id = getId(schema.type, s);
|
92 | if (id && typeof id === 'string') {
|
93 | var schemaId = new schemaId_1.default(id, parentIds);
|
94 | var subSchema = {
|
95 | type: schema.type,
|
96 | id: schemaId,
|
97 | content: s,
|
98 | rootSchema: schema,
|
99 | };
|
100 | onFoundSchema(subSchema);
|
101 | parentIds = parentIds.concat([schemaId.getAbsoluteId()]);
|
102 | }
|
103 | if (typeof s.$ref === 'string') {
|
104 | var schemaId = new schemaId_1.default(s.$ref, parentIds);
|
105 | s.$ref = schemaId.getAbsoluteId();
|
106 | onFoundReference(schemaId);
|
107 | }
|
108 | walkArray(s.allOf, paths.concat('allOf'), parentIds);
|
109 | walkArray(s.anyOf, paths.concat('anyOf'), parentIds);
|
110 | walkArray(s.oneOf, paths.concat('oneOf'), parentIds);
|
111 | walk(s.not, paths.concat('not'), parentIds);
|
112 | walkMaybeArray(s.items, paths.concat('items'), parentIds);
|
113 | walk(s.additionalItems, paths.concat('additionalItems'), parentIds);
|
114 | walk(s.additionalProperties, paths.concat('additionalProperties'), parentIds);
|
115 | walkObject(s.definitions, paths.concat('definitions'), parentIds);
|
116 | walkObject(s.properties, paths.concat('properties'), parentIds);
|
117 | walkObject(s.patternProperties, paths.concat('patternProperties'), parentIds);
|
118 | walkMaybeArray(s.dependencies, paths.concat('dependencies'), parentIds);
|
119 | if (schema.type === 'Draft07') {
|
120 | if ('propertyNames' in s) {
|
121 | walk(s.propertyNames, paths.concat('propertyNames'), parentIds);
|
122 | walk(s.contains, paths.concat('contains'), parentIds);
|
123 | walk(s.if, paths.concat('if'), parentIds);
|
124 | walk(s.then, paths.concat('then'), parentIds);
|
125 | walk(s.else, paths.concat('else'), parentIds);
|
126 | }
|
127 | }
|
128 | };
|
129 | function searchOpenApiSubSchema(openApi) {
|
130 | function createId(paths) {
|
131 | return '#/' + paths.join('/');
|
132 | }
|
133 | function convertKeyToTypeName(key) {
|
134 | key = key.replace(/\/(.)/g, function (_match, p1) {
|
135 | return p1.toUpperCase();
|
136 | });
|
137 | return key.replace(/}/g, '').replace(/{/, '$')
|
138 | .replace(/^\//, '').replace(/[^0-9A-Za-z_$]+/g, '_');
|
139 | }
|
140 | function setSubIdToAnyObject(f, obj, keys) {
|
141 | if (obj == null) {
|
142 | return;
|
143 | }
|
144 | Object.keys(obj).forEach(function (key) {
|
145 | var item = obj[key];
|
146 | f(item, keys.concat(convertKeyToTypeName(key)));
|
147 | });
|
148 | }
|
149 | var setSubIdToParameterObject = function (obj, keys) { return setSubIdToAnyObject(setSubIdToParameter, obj, keys); };
|
150 | function setSubIdToParameter(param, keys) {
|
151 | if ('schema' in param) {
|
152 | setSubId(param.schema, keys.concat(param.name));
|
153 | }
|
154 | }
|
155 | function setSubIdToParameters(array, keys) {
|
156 | if (array == null) {
|
157 | return;
|
158 | }
|
159 | var map = new Map();
|
160 | array.forEach(function (item) {
|
161 | if ('schema' in item) {
|
162 | setSubIdToParameter(item, keys);
|
163 | var work = map.get(item.in);
|
164 | if (work == null) {
|
165 | work = [];
|
166 | map.set(item.in, work);
|
167 | }
|
168 | work.push(item);
|
169 | }
|
170 | });
|
171 | addParameterSchema(map, keys);
|
172 | }
|
173 | function addParameterSchema(input, keys) {
|
174 | var e_1, _a;
|
175 | try {
|
176 | for (var input_1 = tslib_1.__values(input), input_1_1 = input_1.next(); !input_1_1.done; input_1_1 = input_1.next()) {
|
177 | var _b = tslib_1.__read(input_1_1.value, 2), key = _b[0], params = _b[1];
|
178 | var _c = tslib_1.__read(buildParameterSchema(key, params, keys), 2), paths = _c[0], obj = _c[1];
|
179 | setSubId(obj, paths);
|
180 | }
|
181 | }
|
182 | catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
183 | finally {
|
184 | try {
|
185 | if (input_1_1 && !input_1_1.done && (_a = input_1.return)) _a.call(input_1);
|
186 | }
|
187 | finally { if (e_1) throw e_1.error; }
|
188 | }
|
189 | }
|
190 | function buildParameterSchema(inType, params, keys) {
|
191 | var paths = keys.slice(0, keys.length - 1).concat(inType + 'Parameters');
|
192 | var properties = {};
|
193 | params.forEach(function (item) {
|
194 | properties[item.name] = { $ref: createId(keys.concat(item.name)) };
|
195 | });
|
196 | return [paths, {
|
197 | id: createId(paths),
|
198 | type: 'object',
|
199 | properties: properties,
|
200 | required: params.filter(function (item) { return item.required === true; }).map(function (item) { return item.name; }),
|
201 | }];
|
202 | }
|
203 | var setSubIdToResponsesV2 = function (responses, keys) { return setSubIdToAnyObject(setSubIdToResponseV2, responses, keys); };
|
204 | function setSubIdToResponseV2(response, keys) {
|
205 | if (response == null) {
|
206 | return;
|
207 | }
|
208 | if ('schema' in response) {
|
209 | var s = response.schema;
|
210 | if (s != null && s.type === 'file') {
|
211 | return;
|
212 | }
|
213 | setSubId(s, keys);
|
214 | }
|
215 | }
|
216 | function setSubIdToOperationV2(ops, keys) {
|
217 | if (ops == null) {
|
218 | return;
|
219 | }
|
220 | var operationId = ops.operationId;
|
221 | if (operationId) {
|
222 | keys = [keys[0], convertKeyToTypeName(operationId)];
|
223 | }
|
224 | setSubIdToParameters(ops.parameters, keys.concat('parameters'));
|
225 | setSubIdToResponsesV2(ops.responses, keys.concat('responses'));
|
226 | }
|
227 | var setSubIdToPathsV2 = function (paths, keys) { return setSubIdToAnyObject(setSubIdToPathItemV2, paths, keys); };
|
228 | function setSubIdToPathItemV2(pathItem, keys) {
|
229 | setSubIdToParameters(pathItem.parameters, keys.concat('parameters'));
|
230 | setSubIdToOperationV2(pathItem.get, keys.concat('get'));
|
231 | setSubIdToOperationV2(pathItem.put, keys.concat('put'));
|
232 | setSubIdToOperationV2(pathItem.post, keys.concat('post'));
|
233 | setSubIdToOperationV2(pathItem.delete, keys.concat('delete'));
|
234 | setSubIdToOperationV2(pathItem.options, keys.concat('options'));
|
235 | setSubIdToOperationV2(pathItem.head, keys.concat('head'));
|
236 | setSubIdToOperationV2(pathItem.patch, keys.concat('patch'));
|
237 | }
|
238 | function setSubIdToMediaTypes(types, keys) {
|
239 | var e_2, _a;
|
240 | if (types == null) {
|
241 | return;
|
242 | }
|
243 | try {
|
244 | for (var _b = tslib_1.__values(Object.keys(types)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
245 | var mime = _c.value;
|
246 | if (/^(?:application\/x-www-form-urlencoded|application\/([a-z0-9-_]+\+)?json)$/.test(mime)) {
|
247 | var mt = types[mime];
|
248 | setSubId(mt.schema, keys);
|
249 | }
|
250 | }
|
251 | }
|
252 | catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
253 | finally {
|
254 | try {
|
255 | if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
256 | }
|
257 | finally { if (e_2) throw e_2.error; }
|
258 | }
|
259 | }
|
260 | var setSubIdToRequestBodies = function (bodys, keys) { return setSubIdToAnyObject(setSubIdToRequestBody, bodys, keys); };
|
261 | function setSubIdToRequestBody(body, keys) {
|
262 | if (body == null) {
|
263 | return;
|
264 | }
|
265 | if ('content' in body) {
|
266 | setSubIdToMediaTypes(body.content, keys);
|
267 | }
|
268 | if ('$ref' in body) {
|
269 | setSubId(body, keys);
|
270 | }
|
271 | }
|
272 | var setSubIdToResponsesV3 = function (responses, keys) { return setSubIdToAnyObject(setSubIdToResponseV3, responses, keys); };
|
273 | function setSubIdToResponseV3(response, keys) {
|
274 | if (response == null) {
|
275 | return;
|
276 | }
|
277 | if ('content' in response) {
|
278 | setSubIdToMediaTypes(response.content, keys);
|
279 | }
|
280 | if ('$ref' in response) {
|
281 | setSubId(response, keys);
|
282 | }
|
283 | }
|
284 | function setSubIdToOperationV3(ops, keys) {
|
285 | if (ops == null) {
|
286 | return;
|
287 | }
|
288 | var operationId = ops.operationId;
|
289 | if (operationId) {
|
290 | keys = [keys[0], convertKeyToTypeName(operationId)];
|
291 | }
|
292 | setSubIdToParameters(ops.parameters, keys.concat('parameters'));
|
293 | setSubIdToRequestBody(ops.requestBody, keys.concat('requestBody'));
|
294 | setSubIdToResponsesV3(ops.responses, keys.concat('responses'));
|
295 | }
|
296 | var setSubIdToPathsV3 = function (paths, keys) { return setSubIdToAnyObject(setSubIdToPathItemV3, paths, keys); };
|
297 | function setSubIdToPathItemV3(pathItem, keys) {
|
298 | setSubIdToParameters(pathItem.parameters, keys.concat('parameters'));
|
299 | setSubIdToOperationV3(pathItem.get, keys.concat('get'));
|
300 | setSubIdToOperationV3(pathItem.put, keys.concat('put'));
|
301 | setSubIdToOperationV3(pathItem.post, keys.concat('post'));
|
302 | setSubIdToOperationV3(pathItem.delete, keys.concat('delete'));
|
303 | setSubIdToOperationV3(pathItem.options, keys.concat('options'));
|
304 | setSubIdToOperationV3(pathItem.head, keys.concat('head'));
|
305 | setSubIdToOperationV3(pathItem.patch, keys.concat('patch'));
|
306 | setSubIdToOperationV3(pathItem.trace, keys.concat('trace'));
|
307 | }
|
308 | function setSubIdToObject(obj, paths) {
|
309 | if (obj == null) {
|
310 | return;
|
311 | }
|
312 | Object.keys(obj).forEach(function (key) {
|
313 | var sub = obj[key];
|
314 | setSubId(sub, paths.concat(key));
|
315 | });
|
316 | }
|
317 | function setSubId(s, paths) {
|
318 | if (typeof s !== 'object') {
|
319 | return;
|
320 | }
|
321 | if (typeof s.$ref === 'string') {
|
322 | var schemaId = new schemaId_1.default(s.$ref);
|
323 | s.$ref = schemaId.getAbsoluteId();
|
324 | onFoundReference(schemaId);
|
325 | }
|
326 | var id = createId(paths);
|
327 | setId(schema.type, s, id);
|
328 | walk(s, paths, []);
|
329 | }
|
330 | if ('swagger' in openApi) {
|
331 | setSubIdToObject(openApi.definitions, ['definitions']);
|
332 | setSubIdToParameterObject(openApi.parameters, ['parameters']);
|
333 | setSubIdToResponsesV2(openApi.responses, ['responses']);
|
334 | setSubIdToPathsV2(openApi.paths, ['paths']);
|
335 | }
|
336 | else {
|
337 | if (openApi.components) {
|
338 | var components = openApi.components;
|
339 | setSubIdToObject(components.schemas, ['components', 'schemas']);
|
340 | setSubIdToResponsesV3(components.responses, ['components', 'responses']);
|
341 | setSubIdToParameterObject(components.parameters, ['components', 'parameters']);
|
342 | setSubIdToRequestBodies(components.requestBodies, ['components', 'requestBodies']);
|
343 | }
|
344 | if (openApi.paths) {
|
345 | setSubIdToPathsV3(openApi.paths, ['paths']);
|
346 | }
|
347 | }
|
348 | }
|
349 | if (schema.openApiVersion != null) {
|
350 | var obj = schema.content;
|
351 | searchOpenApiSubSchema(obj);
|
352 | return;
|
353 | }
|
354 | walk(schema.content, ['#'], []);
|
355 | }
|
356 | exports.searchAllSubSchema = searchAllSubSchema;
|
357 | function selectSchemaType(content) {
|
358 | if (content.$schema) {
|
359 | var schema = content.$schema;
|
360 | var match = schema.match(/http\:\/\/json-schema\.org\/draft-(\d+)\/schema#?/);
|
361 | if (match) {
|
362 | var version = Number(match[1]);
|
363 | if (version <= 4) {
|
364 | return { type: 'Draft04' };
|
365 | }
|
366 | else {
|
367 | return { type: 'Draft07' };
|
368 | }
|
369 | }
|
370 | }
|
371 | if (content.swagger === '2.0') {
|
372 | return {
|
373 | type: 'Draft04',
|
374 | openApiVersion: 2,
|
375 | };
|
376 | }
|
377 | if (content.openapi) {
|
378 | var openapi = content.openapi;
|
379 | if (/^3\.\d+\.\d+$/.test(openapi)) {
|
380 | return {
|
381 | type: 'Draft07',
|
382 | openApiVersion: 3,
|
383 | };
|
384 | }
|
385 | }
|
386 | return { type: 'Draft04' };
|
387 | }
|
388 |
|
\ | No newline at end of file |