UNPKG

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