UNPKG

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