UNPKG

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