UNPKG

15.5 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var JsonPointer = tslib_1.__importStar(require("../jsonPointer"));
5var schemaId_1 = tslib_1.__importDefault(require("./schemaId"));
6var typeNameConvertor_1 = require("./typeNameConvertor");
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 typeNameConvertor_1.normalizeTypeName(key.replace(/}/g, '').replace(/{/, '$'));
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(['application/json', 'application/x-www-form-urlencoded']), _c = _b.next(); !_c.done; _c = _b.next()) {
245 var mime = _c.value;
246 var mt = types[mime];
247 if (mt != null) {
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}
356exports.searchAllSubSchema = searchAllSubSchema;
357function 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//# sourceMappingURL=jsonSchema.js.map
\No newline at end of file