@killb/openapi-alchemist 0.0.0-stage → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +25 -0
- package/README.md +249 -2
- package/dist/index.d.ts +15 -0
- package/dist/index.js +56 -0
- package/dist/lib/converters/openapi3_to_swagger2.d.ts +24 -0
- package/dist/lib/converters/openapi3_to_swagger2.js +551 -0
- package/dist/lib/formats/openapi_3.d.ts +15 -0
- package/dist/lib/formats/openapi_3.js +48 -0
- package/dist/lib/formats/swagger_2.d.ts +15 -0
- package/dist/lib/formats/swagger_2.js +57 -0
- package/dist/lib/refs.d.ts +6 -0
- package/dist/lib/refs.js +111 -0
- package/dist/lib/server-url.d.ts +7 -0
- package/dist/lib/server-url.js +45 -0
- package/dist/lib/util.d.ts +12 -0
- package/dist/lib/util.js +113 -0
- package/dist/types.d.ts +68 -0
- package/dist/types.js +306 -0
- package/package.json +100 -4
|
@@ -0,0 +1,551 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.OpenApi3ToSwagger2Converter = void 0;
|
|
37
|
+
const fs = __importStar(require("node:fs"));
|
|
38
|
+
const path = __importStar(require("node:path"));
|
|
39
|
+
const YAML = __importStar(require("js-yaml"));
|
|
40
|
+
const refs_1 = require("../refs");
|
|
41
|
+
const server_url_1 = require("../server-url");
|
|
42
|
+
function deepClone(obj) {
|
|
43
|
+
if (obj === null || typeof obj !== 'object') {
|
|
44
|
+
return obj;
|
|
45
|
+
}
|
|
46
|
+
if (obj instanceof Date) {
|
|
47
|
+
return new Date(obj.getTime());
|
|
48
|
+
}
|
|
49
|
+
if (obj instanceof Array) {
|
|
50
|
+
return obj.map(item => deepClone(item));
|
|
51
|
+
}
|
|
52
|
+
if (typeof obj === 'object') {
|
|
53
|
+
const cloned = {};
|
|
54
|
+
for (const key in obj) {
|
|
55
|
+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
56
|
+
(0, refs_1.assignOwn)(cloned, key, deepClone(obj[key]));
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return cloned;
|
|
60
|
+
}
|
|
61
|
+
return obj;
|
|
62
|
+
}
|
|
63
|
+
const HTTP_METHODS = ['get', 'put', 'post', 'delete', 'options', 'head', 'patch', 'trace'];
|
|
64
|
+
const SCHEMA_PROPERTIES = [
|
|
65
|
+
'format',
|
|
66
|
+
'minimum',
|
|
67
|
+
'maximum',
|
|
68
|
+
'exclusiveMinimum',
|
|
69
|
+
'exclusiveMaximum',
|
|
70
|
+
'minLength',
|
|
71
|
+
'maxLength',
|
|
72
|
+
'multipleOf',
|
|
73
|
+
'minItems',
|
|
74
|
+
'maxItems',
|
|
75
|
+
'uniqueItems',
|
|
76
|
+
'minProperties',
|
|
77
|
+
'maxProperties',
|
|
78
|
+
'additionalProperties',
|
|
79
|
+
'pattern',
|
|
80
|
+
'enum',
|
|
81
|
+
'default',
|
|
82
|
+
];
|
|
83
|
+
const ARRAY_PROPERTIES = ['type', 'items'];
|
|
84
|
+
const APPLICATION_JSON_REGEX = /^(application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(;.*)?$/i;
|
|
85
|
+
const SUPPORTED_MIME_TYPES = {
|
|
86
|
+
APPLICATION_X_WWW_URLENCODED: 'application/x-www-form-urlencoded',
|
|
87
|
+
MULTIPART_FORM_DATA: 'multipart/form-data',
|
|
88
|
+
};
|
|
89
|
+
class OpenApi3ToSwagger2Converter {
|
|
90
|
+
spec;
|
|
91
|
+
directory;
|
|
92
|
+
root;
|
|
93
|
+
constructor(data) {
|
|
94
|
+
this.spec = JSON.parse(JSON.stringify(data.spec));
|
|
95
|
+
if (data.source && !data.source.startsWith('http')) {
|
|
96
|
+
this.directory = fs.realpathSync(path.dirname(data.source));
|
|
97
|
+
this.root = data.refRoot ?? this.directory;
|
|
98
|
+
}
|
|
99
|
+
else if (data.refRoot) {
|
|
100
|
+
this.directory = data.refRoot;
|
|
101
|
+
this.root = data.refRoot;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
convert() {
|
|
105
|
+
this.spec.swagger = '2.0';
|
|
106
|
+
this.convertInfos();
|
|
107
|
+
this.convertOperations();
|
|
108
|
+
if (this.spec.components) {
|
|
109
|
+
this.convertSchemas();
|
|
110
|
+
this.convertSecurityDefinitions();
|
|
111
|
+
this.spec['x-components'] = this.spec.components;
|
|
112
|
+
delete this.spec.components;
|
|
113
|
+
this.fixRefs(this.spec);
|
|
114
|
+
}
|
|
115
|
+
return this.spec;
|
|
116
|
+
}
|
|
117
|
+
resolveReference(base, obj, shouldClone) {
|
|
118
|
+
if (!obj?.$ref)
|
|
119
|
+
return obj;
|
|
120
|
+
const ref = obj.$ref;
|
|
121
|
+
if (typeof ref !== 'string') {
|
|
122
|
+
throw new Error('Unresolvable $ref ' + JSON.stringify(ref));
|
|
123
|
+
}
|
|
124
|
+
if (ref.startsWith('#')) {
|
|
125
|
+
const keys = (0, refs_1.decodePointer)(ref);
|
|
126
|
+
keys.shift();
|
|
127
|
+
const cur = (0, refs_1.walkOwn)(base, keys, ref);
|
|
128
|
+
return shouldClone ? deepClone(cur) : cur;
|
|
129
|
+
}
|
|
130
|
+
else if (ref.startsWith('http') || !this.directory || !this.root) {
|
|
131
|
+
throw new Error('Remote $ref URLs are not currently supported for openapi_3');
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
const res = ref.split('#/', 2);
|
|
135
|
+
const file = (0, refs_1.resolveExternalFile)(ref, res[0], this.directory, this.root);
|
|
136
|
+
const content = fs.readFileSync(file, 'utf8');
|
|
137
|
+
let external = null;
|
|
138
|
+
try {
|
|
139
|
+
external = JSON.parse(content);
|
|
140
|
+
}
|
|
141
|
+
catch {
|
|
142
|
+
try {
|
|
143
|
+
external = YAML.load(content);
|
|
144
|
+
}
|
|
145
|
+
catch {
|
|
146
|
+
throw new Error('Could not parse path of $ref ' + res[0] + ' as JSON or YAML');
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
if (res.length > 1) {
|
|
150
|
+
external = (0, refs_1.walkOwn)(external, (0, refs_1.decodePointer)(res[1]), ref);
|
|
151
|
+
}
|
|
152
|
+
return external;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
fixRef(ref) {
|
|
156
|
+
return ref
|
|
157
|
+
.replace('#/components/schemas/', '#/definitions/')
|
|
158
|
+
.replace('#/components/', '#/x-components/');
|
|
159
|
+
}
|
|
160
|
+
fixRefs(obj) {
|
|
161
|
+
if (Array.isArray(obj)) {
|
|
162
|
+
obj.forEach((item) => this.fixRefs(item));
|
|
163
|
+
}
|
|
164
|
+
else if (typeof obj === 'object') {
|
|
165
|
+
for (const key in obj) {
|
|
166
|
+
if (key === '$ref') {
|
|
167
|
+
obj.$ref = this.fixRef(obj.$ref);
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
this.fixRefs(obj[key]);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
convertInfos() {
|
|
176
|
+
const server = this.spec.servers && this.spec.servers[0];
|
|
177
|
+
if (server) {
|
|
178
|
+
const location = (0, server_url_1.parseServerUrl)((0, server_url_1.substituteVariables)(typeof server.url === 'string' ? server.url : '', server.variables));
|
|
179
|
+
if (location.host === undefined) {
|
|
180
|
+
delete this.spec.host;
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
this.spec.host = location.host;
|
|
184
|
+
}
|
|
185
|
+
if (location.scheme === undefined) {
|
|
186
|
+
delete this.spec.schemes;
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
this.spec.schemes = [location.scheme];
|
|
190
|
+
}
|
|
191
|
+
this.spec.basePath = location.basePath;
|
|
192
|
+
}
|
|
193
|
+
delete this.spec.servers;
|
|
194
|
+
delete this.spec.openapi;
|
|
195
|
+
}
|
|
196
|
+
convertOperations() {
|
|
197
|
+
for (const path in this.spec.paths) {
|
|
198
|
+
const pathObject = (this.spec.paths[path] = this.resolveReference(this.spec, this.spec.paths[path], true));
|
|
199
|
+
this.convertParameters(pathObject);
|
|
200
|
+
for (const method in pathObject) {
|
|
201
|
+
if (HTTP_METHODS.indexOf(method) >= 0) {
|
|
202
|
+
const operation = (pathObject[method] = this.resolveReference(this.spec, pathObject[method], true));
|
|
203
|
+
this.convertOperationParameters(operation);
|
|
204
|
+
this.convertResponses(operation);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
convertOperationParameters(operation) {
|
|
210
|
+
let content, param, contentKey, mediaRanges, mediaTypes;
|
|
211
|
+
operation.parameters = operation.parameters || [];
|
|
212
|
+
if (operation.requestBody) {
|
|
213
|
+
param = this.resolveReference(this.spec, operation.requestBody, true);
|
|
214
|
+
if (operation.requestBody.content) {
|
|
215
|
+
const type = this.getSupportedMimeTypes(operation.requestBody.content)[0];
|
|
216
|
+
const structuredObj = { content: {} };
|
|
217
|
+
const data = operation.requestBody.content[type];
|
|
218
|
+
if (data && data.schema && data.schema.$ref && !data.schema.$ref.startsWith('#')) {
|
|
219
|
+
param = this.resolveReference(this.spec, data.schema, true);
|
|
220
|
+
structuredObj['content'][`${type}`] = { schema: param };
|
|
221
|
+
param = structuredObj;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
param.name = 'body';
|
|
225
|
+
content = param.content;
|
|
226
|
+
if (content && Object.keys(content).length) {
|
|
227
|
+
mediaRanges = Object.keys(content).filter((mediaRange) => mediaRange.indexOf('/') > 0);
|
|
228
|
+
mediaTypes = mediaRanges.filter((range) => range.indexOf('*') < 0);
|
|
229
|
+
contentKey = this.getSupportedMimeTypes(content)[0];
|
|
230
|
+
delete param.content;
|
|
231
|
+
if (contentKey === SUPPORTED_MIME_TYPES.APPLICATION_X_WWW_URLENCODED ||
|
|
232
|
+
contentKey === SUPPORTED_MIME_TYPES.MULTIPART_FORM_DATA) {
|
|
233
|
+
operation.consumes = mediaTypes;
|
|
234
|
+
param.in = 'formData';
|
|
235
|
+
param.schema = content[contentKey].schema;
|
|
236
|
+
param.schema = this.resolveReference(this.spec, param.schema, true);
|
|
237
|
+
if (param.schema.type === 'object' && param.schema.properties) {
|
|
238
|
+
const required = param.schema.required || [];
|
|
239
|
+
for (const name in param.schema.properties) {
|
|
240
|
+
const schema = param.schema.properties[name];
|
|
241
|
+
if (!schema.readOnly) {
|
|
242
|
+
const formDataParam = {
|
|
243
|
+
name,
|
|
244
|
+
in: 'formData',
|
|
245
|
+
schema,
|
|
246
|
+
};
|
|
247
|
+
if (required.indexOf(name) >= 0) {
|
|
248
|
+
formDataParam.required = true;
|
|
249
|
+
}
|
|
250
|
+
operation.parameters.push(formDataParam);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
else {
|
|
255
|
+
operation.parameters.push(param);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
else if (contentKey) {
|
|
259
|
+
operation.consumes = mediaTypes;
|
|
260
|
+
param.in = 'body';
|
|
261
|
+
param.schema = content[contentKey].schema;
|
|
262
|
+
operation.parameters.push(param);
|
|
263
|
+
}
|
|
264
|
+
else if (mediaRanges) {
|
|
265
|
+
operation.consumes = mediaTypes || ['application/octet-stream'];
|
|
266
|
+
param.in = 'body';
|
|
267
|
+
param.name = param.name || 'file';
|
|
268
|
+
delete param.type;
|
|
269
|
+
param.schema = content[mediaRanges[0]].schema || {
|
|
270
|
+
type: 'string',
|
|
271
|
+
format: 'binary',
|
|
272
|
+
};
|
|
273
|
+
operation.parameters.push(param);
|
|
274
|
+
}
|
|
275
|
+
if (param.schema) {
|
|
276
|
+
this.convertSchema(param.schema, 'request');
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
delete operation.requestBody;
|
|
280
|
+
}
|
|
281
|
+
this.convertParameters(operation);
|
|
282
|
+
}
|
|
283
|
+
convertParameters(obj) {
|
|
284
|
+
if (obj.parameters === undefined) {
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
obj.parameters = obj.parameters || [];
|
|
288
|
+
(obj.parameters || []).forEach((param, i) => {
|
|
289
|
+
param = obj.parameters[i] = this.resolveReference(this.spec, param, false);
|
|
290
|
+
if (param.in !== 'body') {
|
|
291
|
+
this.copySchemaProperties(param, SCHEMA_PROPERTIES);
|
|
292
|
+
this.copySchemaProperties(param, ARRAY_PROPERTIES);
|
|
293
|
+
this.copySchemaXProperties(param);
|
|
294
|
+
if (!param.description) {
|
|
295
|
+
const schema = this.resolveReference(this.spec, param.schema, false);
|
|
296
|
+
if (!!schema && schema.description) {
|
|
297
|
+
param.description = schema.description;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
delete param.schema;
|
|
301
|
+
delete param.allowReserved;
|
|
302
|
+
if (param.example !== undefined) {
|
|
303
|
+
param['x-example'] = param.example;
|
|
304
|
+
}
|
|
305
|
+
delete param.example;
|
|
306
|
+
}
|
|
307
|
+
if (param.type === 'array') {
|
|
308
|
+
const style = param.style || (param.in === 'query' || param.in === 'cookie' ? 'form' : 'simple');
|
|
309
|
+
if (style === 'matrix') {
|
|
310
|
+
param.collectionFormat = param.explode ? undefined : 'csv';
|
|
311
|
+
}
|
|
312
|
+
else if (style === 'label') {
|
|
313
|
+
param.collectionFormat = undefined;
|
|
314
|
+
}
|
|
315
|
+
else if (style === 'simple') {
|
|
316
|
+
param.collectionFormat = 'csv';
|
|
317
|
+
}
|
|
318
|
+
else if (style === 'spaceDelimited') {
|
|
319
|
+
param.collectionFormat = 'ssv';
|
|
320
|
+
}
|
|
321
|
+
else if (style === 'pipeDelimited') {
|
|
322
|
+
param.collectionFormat = 'pipes';
|
|
323
|
+
}
|
|
324
|
+
else if (style === 'deepOpbject') {
|
|
325
|
+
param.collectionFormat = 'multi';
|
|
326
|
+
}
|
|
327
|
+
else if (style === 'form') {
|
|
328
|
+
param.collectionFormat = param.explode === false ? 'csv' : 'multi';
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
delete param.style;
|
|
332
|
+
delete param.explode;
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
copySchemaProperties(obj, props) {
|
|
336
|
+
const schema = this.resolveReference(this.spec, obj.schema, true);
|
|
337
|
+
if (!schema)
|
|
338
|
+
return;
|
|
339
|
+
props.forEach((prop) => {
|
|
340
|
+
const value = schema[prop];
|
|
341
|
+
switch (prop) {
|
|
342
|
+
case 'additionalProperties':
|
|
343
|
+
if (typeof value === 'boolean')
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
if (value !== undefined) {
|
|
347
|
+
obj[prop] = value;
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
copySchemaXProperties(obj) {
|
|
352
|
+
const schema = this.resolveReference(this.spec, obj.schema, true);
|
|
353
|
+
if (!schema)
|
|
354
|
+
return;
|
|
355
|
+
for (const propName in schema) {
|
|
356
|
+
if (Object.prototype.hasOwnProperty.call(schema, propName) &&
|
|
357
|
+
!Object.prototype.hasOwnProperty.call(obj, propName) &&
|
|
358
|
+
propName.startsWith('x-')) {
|
|
359
|
+
obj[propName] = schema[propName];
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
convertResponses(operation) {
|
|
364
|
+
for (const code in operation.responses) {
|
|
365
|
+
const response = (operation.responses[code] = this.resolveReference(this.spec, operation.responses[code], true));
|
|
366
|
+
if (response.content) {
|
|
367
|
+
let anySchema = null, jsonSchema = null;
|
|
368
|
+
for (const mediaRange in response.content) {
|
|
369
|
+
const mediaType = mediaRange.indexOf('*') < 0 ? mediaRange : 'application/octet-stream';
|
|
370
|
+
if (!operation.produces) {
|
|
371
|
+
operation.produces = [mediaType];
|
|
372
|
+
}
|
|
373
|
+
else if (operation.produces.indexOf(mediaType) < 0) {
|
|
374
|
+
operation.produces.push(mediaType);
|
|
375
|
+
}
|
|
376
|
+
const content = response.content[mediaRange];
|
|
377
|
+
anySchema = anySchema || content.schema;
|
|
378
|
+
if (!jsonSchema && this.isJsonMimeType(mediaType)) {
|
|
379
|
+
jsonSchema = content.schema;
|
|
380
|
+
}
|
|
381
|
+
if (content.example) {
|
|
382
|
+
response.examples = response.examples || {};
|
|
383
|
+
response.examples[mediaType] = content.example;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
if (anySchema) {
|
|
387
|
+
response.schema = jsonSchema || anySchema;
|
|
388
|
+
const resolved = this.resolveReference(this.spec, response.schema, true);
|
|
389
|
+
if (resolved && response.schema.$ref && !response.schema.$ref.startsWith('#')) {
|
|
390
|
+
response.schema = resolved;
|
|
391
|
+
}
|
|
392
|
+
this.convertSchema(response.schema, 'response');
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
const headers = response.headers;
|
|
396
|
+
if (headers) {
|
|
397
|
+
for (const header in headers) {
|
|
398
|
+
const resolved = this.resolveReference(this.spec, headers[header], true);
|
|
399
|
+
if (resolved.schema) {
|
|
400
|
+
resolved.type = resolved.schema.type;
|
|
401
|
+
resolved.format = resolved.schema.format;
|
|
402
|
+
delete resolved.schema;
|
|
403
|
+
}
|
|
404
|
+
headers[header] = resolved;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
delete response.content;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
convertSchema(def, operationDirection) {
|
|
411
|
+
if (def.oneOf) {
|
|
412
|
+
delete def.oneOf;
|
|
413
|
+
if (def.discriminator) {
|
|
414
|
+
delete def.discriminator;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
if (def.anyOf) {
|
|
418
|
+
delete def.anyOf;
|
|
419
|
+
if (def.discriminator) {
|
|
420
|
+
delete def.discriminator;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
if (def.allOf) {
|
|
424
|
+
for (const i in def.allOf) {
|
|
425
|
+
this.convertSchema(def.allOf[i], operationDirection);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
if (def.discriminator) {
|
|
429
|
+
if (def.discriminator.mapping) {
|
|
430
|
+
this.convertDiscriminatorMapping(def.discriminator.mapping);
|
|
431
|
+
}
|
|
432
|
+
def.discriminator = def.discriminator.propertyName;
|
|
433
|
+
}
|
|
434
|
+
switch (def.type) {
|
|
435
|
+
case 'object':
|
|
436
|
+
if (def.properties) {
|
|
437
|
+
for (const propName in def.properties) {
|
|
438
|
+
if (def.properties[propName].writeOnly === true && operationDirection === 'response') {
|
|
439
|
+
delete def.properties[propName];
|
|
440
|
+
}
|
|
441
|
+
else {
|
|
442
|
+
this.convertSchema(def.properties[propName], operationDirection);
|
|
443
|
+
delete def.properties[propName].writeOnly;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
break;
|
|
448
|
+
case 'array':
|
|
449
|
+
if (def.items) {
|
|
450
|
+
this.convertSchema(def.items, operationDirection);
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
if (def.nullable) {
|
|
454
|
+
def['x-nullable'] = true;
|
|
455
|
+
delete def.nullable;
|
|
456
|
+
}
|
|
457
|
+
if (def['deprecated'] !== undefined) {
|
|
458
|
+
if (def['x-deprecated'] === undefined) {
|
|
459
|
+
def['x-deprecated'] = def.deprecated;
|
|
460
|
+
}
|
|
461
|
+
delete def.deprecated;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
convertSchemas() {
|
|
465
|
+
this.spec.definitions = this.spec.components.schemas;
|
|
466
|
+
for (const defName in this.spec.definitions) {
|
|
467
|
+
this.convertSchema(this.spec.definitions[defName], 'response');
|
|
468
|
+
}
|
|
469
|
+
delete this.spec.components.schemas;
|
|
470
|
+
}
|
|
471
|
+
convertDiscriminatorMapping(mapping) {
|
|
472
|
+
for (const payload in mapping) {
|
|
473
|
+
const schemaNameOrRef = mapping[payload];
|
|
474
|
+
if (typeof schemaNameOrRef !== 'string') {
|
|
475
|
+
console.warn(`Ignoring ${schemaNameOrRef} for ${payload} in discriminator.mapping.`);
|
|
476
|
+
continue;
|
|
477
|
+
}
|
|
478
|
+
let schema;
|
|
479
|
+
if (/^[a-zA-Z0-9._-]+$/.test(schemaNameOrRef)) {
|
|
480
|
+
try {
|
|
481
|
+
schema = this.resolveReference(this.spec, { $ref: `#/components/schemas/${schemaNameOrRef}` }, false);
|
|
482
|
+
}
|
|
483
|
+
catch (err) {
|
|
484
|
+
console.debug(`Error resolving ${schemaNameOrRef} for ${payload} as schema name in discriminator.mapping: ${err}`);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
if (!schema) {
|
|
488
|
+
try {
|
|
489
|
+
schema = this.resolveReference(this.spec, { $ref: schemaNameOrRef }, false);
|
|
490
|
+
}
|
|
491
|
+
catch (err) {
|
|
492
|
+
console.debug(`Error resolving ${schemaNameOrRef} for ${payload} in discriminator.mapping: ${err}`);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
if (schema && typeof schema === 'object') {
|
|
496
|
+
schema['x-discriminator-value'] = payload;
|
|
497
|
+
schema['x-ms-discriminator-value'] = payload;
|
|
498
|
+
}
|
|
499
|
+
else {
|
|
500
|
+
console.warn(`Unable to resolve ${schemaNameOrRef} for ${payload} in discriminator.mapping.`);
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
convertSecurityDefinitions() {
|
|
505
|
+
this.spec.securityDefinitions = this.spec.components.securitySchemes;
|
|
506
|
+
for (const secKey in this.spec.securityDefinitions) {
|
|
507
|
+
const security = this.spec.securityDefinitions[secKey];
|
|
508
|
+
if (security.type === 'http' && security.scheme === 'basic') {
|
|
509
|
+
security.type = 'basic';
|
|
510
|
+
delete security.scheme;
|
|
511
|
+
}
|
|
512
|
+
else if (security.type === 'http' && security.scheme === 'bearer') {
|
|
513
|
+
security.type = 'apiKey';
|
|
514
|
+
security.name = 'Authorization';
|
|
515
|
+
security.in = 'header';
|
|
516
|
+
delete security.scheme;
|
|
517
|
+
delete security.bearerFormat;
|
|
518
|
+
}
|
|
519
|
+
else if (security.type === 'oauth2') {
|
|
520
|
+
const flowName = Object.keys(security.flows)[0];
|
|
521
|
+
const flow = security.flows[flowName];
|
|
522
|
+
if (flowName === 'clientCredentials') {
|
|
523
|
+
security.flow = 'application';
|
|
524
|
+
}
|
|
525
|
+
else if (flowName === 'authorizationCode') {
|
|
526
|
+
security.flow = 'accessCode';
|
|
527
|
+
}
|
|
528
|
+
else {
|
|
529
|
+
security.flow = flowName;
|
|
530
|
+
}
|
|
531
|
+
security.authorizationUrl = flow.authorizationUrl;
|
|
532
|
+
security.tokenUrl = flow.tokenUrl;
|
|
533
|
+
security.scopes = flow.scopes;
|
|
534
|
+
delete security.flows;
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
delete this.spec.components.securitySchemes;
|
|
538
|
+
}
|
|
539
|
+
isJsonMimeType(type) {
|
|
540
|
+
return APPLICATION_JSON_REGEX.test(type);
|
|
541
|
+
}
|
|
542
|
+
getSupportedMimeTypes(content) {
|
|
543
|
+
const MIME_VALUES = Object.keys(SUPPORTED_MIME_TYPES).map(key => {
|
|
544
|
+
return SUPPORTED_MIME_TYPES[key];
|
|
545
|
+
});
|
|
546
|
+
return Object.keys(content).filter((key) => {
|
|
547
|
+
return MIME_VALUES.indexOf(key) > -1 || this.isJsonMimeType(key);
|
|
548
|
+
});
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
exports.OpenApi3ToSwagger2Converter = OpenApi3ToSwagger2Converter;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { BaseFormat } from '../../types';
|
|
2
|
+
export declare class OpenApi3 extends BaseFormat {
|
|
3
|
+
formatName: string;
|
|
4
|
+
supportedVersions: string[];
|
|
5
|
+
constructor(spec?: any);
|
|
6
|
+
getFormatVersion(): string;
|
|
7
|
+
fillMissing(dummyData?: any): void;
|
|
8
|
+
protected parsers: {
|
|
9
|
+
JSON: (data: string) => Promise<any>;
|
|
10
|
+
YAML: (data: string) => Promise<unknown>;
|
|
11
|
+
};
|
|
12
|
+
checkFormat(spec: any): boolean;
|
|
13
|
+
validate(callback?: (err: any, result: any) => void): Promise<any>;
|
|
14
|
+
protected fixSpec(): void;
|
|
15
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OpenApi3 = void 0;
|
|
4
|
+
const types_1 = require("../../types");
|
|
5
|
+
const util_1 = require("../util");
|
|
6
|
+
const openapi3_to_swagger2_1 = require("../converters/openapi3_to_swagger2");
|
|
7
|
+
class OpenApi3 extends types_1.BaseFormat {
|
|
8
|
+
formatName = 'openapi';
|
|
9
|
+
supportedVersions = ['3.0'];
|
|
10
|
+
constructor(spec) {
|
|
11
|
+
super(spec);
|
|
12
|
+
this.format = 'openapi_3';
|
|
13
|
+
this.converters['swagger_2'] = (oa) => {
|
|
14
|
+
const converter = new openapi3_to_swagger2_1.OpenApi3ToSwagger2Converter(oa);
|
|
15
|
+
return Promise.resolve(converter.convert());
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
getFormatVersion() {
|
|
19
|
+
const versionComponents = this.spec.openapi.split('.');
|
|
20
|
+
return versionComponents[0] + '.' + versionComponents[1];
|
|
21
|
+
}
|
|
22
|
+
fillMissing(dummyData) {
|
|
23
|
+
dummyData = dummyData || {
|
|
24
|
+
info: {
|
|
25
|
+
title: '< An API title here >',
|
|
26
|
+
version: '< An API version here >',
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
this.spec = Object.assign({}, dummyData, this.spec);
|
|
30
|
+
}
|
|
31
|
+
parsers = {
|
|
32
|
+
JSON: util_1.Util.parseJSON,
|
|
33
|
+
YAML: util_1.Util.parseYAML,
|
|
34
|
+
};
|
|
35
|
+
checkFormat(spec) {
|
|
36
|
+
return spec.openapi !== undefined;
|
|
37
|
+
}
|
|
38
|
+
validate(callback) {
|
|
39
|
+
const result = Promise.resolve({ errors: null, warnings: null });
|
|
40
|
+
if (callback) {
|
|
41
|
+
result.then(result => callback(null, result)).catch(err => callback(err, null));
|
|
42
|
+
}
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
45
|
+
fixSpec() {
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
exports.OpenApi3 = OpenApi3;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { BaseFormat } from '../../types';
|
|
2
|
+
export declare class Swagger2 extends BaseFormat {
|
|
3
|
+
formatName: string;
|
|
4
|
+
supportedVersions: string[];
|
|
5
|
+
constructor(spec?: any);
|
|
6
|
+
getFormatVersion(): string;
|
|
7
|
+
protected fixSpec(): void;
|
|
8
|
+
fillMissing(dummyData?: any): void;
|
|
9
|
+
protected parsers: {
|
|
10
|
+
JSON: (data: string) => Promise<any>;
|
|
11
|
+
YAML: (data: string) => Promise<unknown>;
|
|
12
|
+
};
|
|
13
|
+
checkFormat(spec: any): boolean;
|
|
14
|
+
validate(callback?: (err: any, result: any) => void): Promise<any>;
|
|
15
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Swagger2 = void 0;
|
|
4
|
+
const types_1 = require("../../types");
|
|
5
|
+
const util_1 = require("../util");
|
|
6
|
+
class Swagger2 extends types_1.BaseFormat {
|
|
7
|
+
formatName = 'swagger';
|
|
8
|
+
supportedVersions = ['2.0'];
|
|
9
|
+
constructor(spec) {
|
|
10
|
+
super(spec);
|
|
11
|
+
this.format = 'swagger_2';
|
|
12
|
+
}
|
|
13
|
+
getFormatVersion() {
|
|
14
|
+
return this.spec.swagger;
|
|
15
|
+
}
|
|
16
|
+
fixSpec() {
|
|
17
|
+
const swagger = this.spec;
|
|
18
|
+
const version = swagger.info?.version;
|
|
19
|
+
if (typeof version === 'number') {
|
|
20
|
+
swagger.info.version = version % 1 ? version.toString() : version.toFixed(1);
|
|
21
|
+
}
|
|
22
|
+
util_1.Util.removeNonValues(swagger);
|
|
23
|
+
const basePath = swagger.basePath;
|
|
24
|
+
if (typeof basePath === 'string') {
|
|
25
|
+
swagger.basePath = basePath.replace(/\/+/g, '/').replace(/\/$/, '') || '/';
|
|
26
|
+
}
|
|
27
|
+
Object.values(swagger.definitions || {}).forEach((schema) => {
|
|
28
|
+
if (schema.id !== undefined) {
|
|
29
|
+
delete schema.id;
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
fillMissing(dummyData) {
|
|
34
|
+
dummyData = dummyData || {
|
|
35
|
+
info: {
|
|
36
|
+
title: '< An API title here >',
|
|
37
|
+
version: '< An API version here >',
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
this.spec = Object.assign({}, dummyData, this.spec);
|
|
41
|
+
}
|
|
42
|
+
parsers = {
|
|
43
|
+
JSON: util_1.Util.parseJSON,
|
|
44
|
+
YAML: util_1.Util.parseYAML,
|
|
45
|
+
};
|
|
46
|
+
checkFormat(spec) {
|
|
47
|
+
return spec.swagger !== undefined;
|
|
48
|
+
}
|
|
49
|
+
validate(callback) {
|
|
50
|
+
const result = Promise.resolve({ errors: null, warnings: null });
|
|
51
|
+
if (callback) {
|
|
52
|
+
result.then(result => callback(null, result)).catch(err => callback(err, null));
|
|
53
|
+
}
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
exports.Swagger2 = Swagger2;
|