@f3liz/rescript-autogen-openapi 0.3.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 +373 -0
- package/README.md +111 -0
- package/lib/es6/src/Codegen.d.ts +28 -0
- package/lib/es6/src/Codegen.mjs +423 -0
- package/lib/es6/src/Types.d.ts +286 -0
- package/lib/es6/src/Types.mjs +20 -0
- package/lib/es6/src/bindings/Toposort.mjs +12 -0
- package/lib/es6/src/core/CodegenUtils.mjs +261 -0
- package/lib/es6/src/core/DocOverride.mjs +399 -0
- package/lib/es6/src/core/FileSystem.d.ts +4 -0
- package/lib/es6/src/core/FileSystem.mjs +78 -0
- package/lib/es6/src/core/IRBuilder.mjs +201 -0
- package/lib/es6/src/core/OpenAPIParser.mjs +168 -0
- package/lib/es6/src/core/Pipeline.d.ts +6 -0
- package/lib/es6/src/core/Pipeline.mjs +150 -0
- package/lib/es6/src/core/ReferenceResolver.mjs +41 -0
- package/lib/es6/src/core/Result.mjs +378 -0
- package/lib/es6/src/core/SchemaIR.mjs +425 -0
- package/lib/es6/src/core/SchemaIRParser.mjs +683 -0
- package/lib/es6/src/core/SchemaRefResolver.mjs +146 -0
- package/lib/es6/src/core/SchemaRegistry.mjs +92 -0
- package/lib/es6/src/core/SpecDiffer.mjs +251 -0
- package/lib/es6/src/core/SpecMerger.mjs +237 -0
- package/lib/es6/src/generators/ComponentSchemaGenerator.mjs +207 -0
- package/lib/es6/src/generators/DiffReportGenerator.mjs +155 -0
- package/lib/es6/src/generators/EndpointGenerator.mjs +173 -0
- package/lib/es6/src/generators/IRToSuryGenerator.mjs +543 -0
- package/lib/es6/src/generators/IRToTypeGenerator.mjs +592 -0
- package/lib/es6/src/generators/IRToTypeScriptGenerator.mjs +143 -0
- package/lib/es6/src/generators/ModuleGenerator.mjs +285 -0
- package/lib/es6/src/generators/SchemaCodeGenerator.mjs +77 -0
- package/lib/es6/src/generators/ThinWrapperGenerator.mjs +97 -0
- package/lib/es6/src/generators/TypeScriptDtsGenerator.mjs +172 -0
- package/lib/es6/src/generators/TypeScriptWrapperGenerator.mjs +145 -0
- package/lib/es6/src/types/CodegenError.d.ts +66 -0
- package/lib/es6/src/types/CodegenError.mjs +79 -0
- package/lib/es6/src/types/Config.d.ts +31 -0
- package/lib/es6/src/types/Config.mjs +42 -0
- package/lib/es6/src/types/GenerationContext.mjs +47 -0
- package/package.json +53 -0
- package/rescript.json +26 -0
- package/src/Codegen.res +231 -0
- package/src/Types.res +222 -0
- package/src/bindings/Toposort.res +16 -0
- package/src/core/CodegenUtils.res +180 -0
- package/src/core/DocOverride.res +504 -0
- package/src/core/FileSystem.res +63 -0
- package/src/core/IRBuilder.res +66 -0
- package/src/core/OpenAPIParser.res +144 -0
- package/src/core/Pipeline.res +52 -0
- package/src/core/ReferenceResolver.res +41 -0
- package/src/core/Result.res +187 -0
- package/src/core/SchemaIR.res +291 -0
- package/src/core/SchemaIRParser.res +454 -0
- package/src/core/SchemaRefResolver.res +143 -0
- package/src/core/SchemaRegistry.res +107 -0
- package/src/core/SpecDiffer.res +270 -0
- package/src/core/SpecMerger.res +245 -0
- package/src/generators/ComponentSchemaGenerator.res +210 -0
- package/src/generators/DiffReportGenerator.res +152 -0
- package/src/generators/EndpointGenerator.res +176 -0
- package/src/generators/IRToSuryGenerator.res +386 -0
- package/src/generators/IRToTypeGenerator.res +423 -0
- package/src/generators/IRToTypeScriptGenerator.res +77 -0
- package/src/generators/ModuleGenerator.res +363 -0
- package/src/generators/SchemaCodeGenerator.res +84 -0
- package/src/generators/ThinWrapperGenerator.res +124 -0
- package/src/generators/TypeScriptDtsGenerator.res +193 -0
- package/src/generators/TypeScriptWrapperGenerator.res +166 -0
- package/src/types/CodegenError.res +85 -0
- package/src/types/Config.res +95 -0
- package/src/types/GenerationContext.res +56 -0
|
@@ -0,0 +1,683 @@
|
|
|
1
|
+
// Generated by ReScript, PLEASE EDIT WITH CARE
|
|
2
|
+
|
|
3
|
+
import * as SchemaIR from "./SchemaIR.mjs";
|
|
4
|
+
import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
|
|
5
|
+
|
|
6
|
+
function parseTypeString(rawType) {
|
|
7
|
+
switch (rawType) {
|
|
8
|
+
case "array" :
|
|
9
|
+
return {
|
|
10
|
+
TAG: "Array",
|
|
11
|
+
_0: "Unknown"
|
|
12
|
+
};
|
|
13
|
+
case "boolean" :
|
|
14
|
+
return "Boolean";
|
|
15
|
+
case "integer" :
|
|
16
|
+
return "Integer";
|
|
17
|
+
case "null" :
|
|
18
|
+
return "Null";
|
|
19
|
+
case "number" :
|
|
20
|
+
return "Number";
|
|
21
|
+
case "object" :
|
|
22
|
+
return "Object";
|
|
23
|
+
case "string" :
|
|
24
|
+
return "String";
|
|
25
|
+
default:
|
|
26
|
+
switch (rawType) {
|
|
27
|
+
case "Boolean" :
|
|
28
|
+
return "Boolean";
|
|
29
|
+
case "Integer" :
|
|
30
|
+
return "Integer";
|
|
31
|
+
case "Null" :
|
|
32
|
+
return "Null";
|
|
33
|
+
case "Number" :
|
|
34
|
+
return "Number";
|
|
35
|
+
case "Object" :
|
|
36
|
+
return "Object";
|
|
37
|
+
case "String" :
|
|
38
|
+
return "String";
|
|
39
|
+
default:
|
|
40
|
+
return "Unknown";
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function parseTypeAsArray(rawType) {
|
|
46
|
+
if (Array.isArray(rawType)) {
|
|
47
|
+
return rawType.map(parseTypeString);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function addWarning(ctx, warning) {
|
|
52
|
+
ctx.warnings.push(warning);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function parseJsonSchemaWithContext(ctx, depthOpt, schema) {
|
|
56
|
+
let depth = depthOpt !== undefined ? depthOpt : 0;
|
|
57
|
+
if (depth > 30) {
|
|
58
|
+
addWarning(ctx, {
|
|
59
|
+
TAG: "DepthLimitReached",
|
|
60
|
+
depth: depth,
|
|
61
|
+
path: ctx.path
|
|
62
|
+
});
|
|
63
|
+
return "Unknown";
|
|
64
|
+
}
|
|
65
|
+
let ref = schema.$ref;
|
|
66
|
+
if (ref !== undefined) {
|
|
67
|
+
return {
|
|
68
|
+
TAG: "Reference",
|
|
69
|
+
_0: ref
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
let isNullable = Stdlib_Option.getOr(schema.nullable, false);
|
|
73
|
+
let hasComposition = Stdlib_Option.isSome(schema.anyOf) || Stdlib_Option.isSome(schema.oneOf);
|
|
74
|
+
let typeAsArray = hasComposition ? undefined : Stdlib_Option.flatMap(schema.type, parseTypeAsArray);
|
|
75
|
+
let schema$1 = hasComposition && Stdlib_Option.isSome(Stdlib_Option.flatMap(schema.type, parseTypeAsArray)) ? ({
|
|
76
|
+
type: undefined,
|
|
77
|
+
properties: schema.properties,
|
|
78
|
+
items: schema.items,
|
|
79
|
+
required: schema.required,
|
|
80
|
+
enum: schema.enum,
|
|
81
|
+
$ref: schema.$ref,
|
|
82
|
+
allOf: schema.allOf,
|
|
83
|
+
oneOf: schema.oneOf,
|
|
84
|
+
anyOf: schema.anyOf,
|
|
85
|
+
description: schema.description,
|
|
86
|
+
format: schema.format,
|
|
87
|
+
minLength: schema.minLength,
|
|
88
|
+
maxLength: schema.maxLength,
|
|
89
|
+
minimum: schema.minimum,
|
|
90
|
+
maximum: schema.maximum,
|
|
91
|
+
pattern: schema.pattern,
|
|
92
|
+
nullable: schema.nullable
|
|
93
|
+
}) : schema;
|
|
94
|
+
if (typeAsArray !== undefined) {
|
|
95
|
+
if (typeAsArray.length > 1) {
|
|
96
|
+
let irTypes = typeAsArray.map(t => {
|
|
97
|
+
let subSchema_type = t;
|
|
98
|
+
let subSchema_properties = schema$1.properties;
|
|
99
|
+
let subSchema_items = schema$1.items;
|
|
100
|
+
let subSchema_required = schema$1.required;
|
|
101
|
+
let subSchema_enum = schema$1.enum;
|
|
102
|
+
let subSchema_$ref = schema$1.$ref;
|
|
103
|
+
let subSchema_allOf = schema$1.allOf;
|
|
104
|
+
let subSchema_oneOf = schema$1.oneOf;
|
|
105
|
+
let subSchema_anyOf = schema$1.anyOf;
|
|
106
|
+
let subSchema_description = schema$1.description;
|
|
107
|
+
let subSchema_format = schema$1.format;
|
|
108
|
+
let subSchema_minLength = schema$1.minLength;
|
|
109
|
+
let subSchema_maxLength = schema$1.maxLength;
|
|
110
|
+
let subSchema_minimum = schema$1.minimum;
|
|
111
|
+
let subSchema_maximum = schema$1.maximum;
|
|
112
|
+
let subSchema_pattern = schema$1.pattern;
|
|
113
|
+
let subSchema = {
|
|
114
|
+
type: subSchema_type,
|
|
115
|
+
properties: subSchema_properties,
|
|
116
|
+
items: subSchema_items,
|
|
117
|
+
required: subSchema_required,
|
|
118
|
+
enum: subSchema_enum,
|
|
119
|
+
$ref: subSchema_$ref,
|
|
120
|
+
allOf: subSchema_allOf,
|
|
121
|
+
oneOf: subSchema_oneOf,
|
|
122
|
+
anyOf: subSchema_anyOf,
|
|
123
|
+
description: subSchema_description,
|
|
124
|
+
format: subSchema_format,
|
|
125
|
+
minLength: subSchema_minLength,
|
|
126
|
+
maxLength: subSchema_maxLength,
|
|
127
|
+
minimum: subSchema_minimum,
|
|
128
|
+
maximum: subSchema_maximum,
|
|
129
|
+
pattern: subSchema_pattern,
|
|
130
|
+
nullable: undefined
|
|
131
|
+
};
|
|
132
|
+
return parseJsonSchemaWithContext(ctx, depth + 1 | 0, subSchema);
|
|
133
|
+
});
|
|
134
|
+
let baseType = {
|
|
135
|
+
TAG: "Union",
|
|
136
|
+
_0: irTypes
|
|
137
|
+
};
|
|
138
|
+
if (isNullable) {
|
|
139
|
+
return {
|
|
140
|
+
TAG: "Option",
|
|
141
|
+
_0: baseType
|
|
142
|
+
};
|
|
143
|
+
} else {
|
|
144
|
+
return baseType;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
if (typeAsArray.length === 1) {
|
|
148
|
+
let subSchema_type = typeAsArray[0];
|
|
149
|
+
let subSchema_properties = schema$1.properties;
|
|
150
|
+
let subSchema_items = schema$1.items;
|
|
151
|
+
let subSchema_required = schema$1.required;
|
|
152
|
+
let subSchema_enum = schema$1.enum;
|
|
153
|
+
let subSchema_$ref = schema$1.$ref;
|
|
154
|
+
let subSchema_allOf = schema$1.allOf;
|
|
155
|
+
let subSchema_oneOf = schema$1.oneOf;
|
|
156
|
+
let subSchema_anyOf = schema$1.anyOf;
|
|
157
|
+
let subSchema_description = schema$1.description;
|
|
158
|
+
let subSchema_format = schema$1.format;
|
|
159
|
+
let subSchema_minLength = schema$1.minLength;
|
|
160
|
+
let subSchema_maxLength = schema$1.maxLength;
|
|
161
|
+
let subSchema_minimum = schema$1.minimum;
|
|
162
|
+
let subSchema_maximum = schema$1.maximum;
|
|
163
|
+
let subSchema_pattern = schema$1.pattern;
|
|
164
|
+
let subSchema = {
|
|
165
|
+
type: subSchema_type,
|
|
166
|
+
properties: subSchema_properties,
|
|
167
|
+
items: subSchema_items,
|
|
168
|
+
required: subSchema_required,
|
|
169
|
+
enum: subSchema_enum,
|
|
170
|
+
$ref: subSchema_$ref,
|
|
171
|
+
allOf: subSchema_allOf,
|
|
172
|
+
oneOf: subSchema_oneOf,
|
|
173
|
+
anyOf: subSchema_anyOf,
|
|
174
|
+
description: subSchema_description,
|
|
175
|
+
format: subSchema_format,
|
|
176
|
+
minLength: subSchema_minLength,
|
|
177
|
+
maxLength: subSchema_maxLength,
|
|
178
|
+
minimum: subSchema_minimum,
|
|
179
|
+
maximum: subSchema_maximum,
|
|
180
|
+
pattern: subSchema_pattern,
|
|
181
|
+
nullable: undefined
|
|
182
|
+
};
|
|
183
|
+
let baseType$1 = parseJsonSchemaWithContext(ctx, depth + 1 | 0, subSchema);
|
|
184
|
+
if (isNullable) {
|
|
185
|
+
return {
|
|
186
|
+
TAG: "Option",
|
|
187
|
+
_0: baseType$1
|
|
188
|
+
};
|
|
189
|
+
} else {
|
|
190
|
+
return baseType$1;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
let normalizedType = Stdlib_Option.map(schema$1.type, parseTypeString);
|
|
195
|
+
let baseType$2;
|
|
196
|
+
if (normalizedType !== undefined) {
|
|
197
|
+
if (typeof normalizedType !== "object") {
|
|
198
|
+
switch (normalizedType) {
|
|
199
|
+
case "String" :
|
|
200
|
+
let constraints_minLength = schema$1.minLength;
|
|
201
|
+
let constraints_maxLength = schema$1.maxLength;
|
|
202
|
+
let constraints_pattern = schema$1.pattern;
|
|
203
|
+
let constraints = {
|
|
204
|
+
minLength: constraints_minLength,
|
|
205
|
+
maxLength: constraints_maxLength,
|
|
206
|
+
pattern: constraints_pattern
|
|
207
|
+
};
|
|
208
|
+
baseType$2 = {
|
|
209
|
+
TAG: "String",
|
|
210
|
+
constraints: constraints
|
|
211
|
+
};
|
|
212
|
+
break;
|
|
213
|
+
case "Number" :
|
|
214
|
+
let constraints_minimum = schema$1.minimum;
|
|
215
|
+
let constraints_maximum = schema$1.maximum;
|
|
216
|
+
let constraints$1 = {
|
|
217
|
+
minimum: constraints_minimum,
|
|
218
|
+
maximum: constraints_maximum,
|
|
219
|
+
multipleOf: undefined
|
|
220
|
+
};
|
|
221
|
+
baseType$2 = {
|
|
222
|
+
TAG: "Number",
|
|
223
|
+
constraints: constraints$1
|
|
224
|
+
};
|
|
225
|
+
break;
|
|
226
|
+
case "Integer" :
|
|
227
|
+
let constraints_minimum$1 = schema$1.minimum;
|
|
228
|
+
let constraints_maximum$1 = schema$1.maximum;
|
|
229
|
+
let constraints$2 = {
|
|
230
|
+
minimum: constraints_minimum$1,
|
|
231
|
+
maximum: constraints_maximum$1,
|
|
232
|
+
multipleOf: undefined
|
|
233
|
+
};
|
|
234
|
+
baseType$2 = {
|
|
235
|
+
TAG: "Integer",
|
|
236
|
+
constraints: constraints$2
|
|
237
|
+
};
|
|
238
|
+
break;
|
|
239
|
+
case "Boolean" :
|
|
240
|
+
baseType$2 = "Boolean";
|
|
241
|
+
break;
|
|
242
|
+
case "Object" :
|
|
243
|
+
let schemas = schema$1.allOf;
|
|
244
|
+
if (schemas !== undefined) {
|
|
245
|
+
let types = schemas.map(s => parseJsonSchemaWithContext(ctx, depth + 1 | 0, s));
|
|
246
|
+
baseType$2 = {
|
|
247
|
+
TAG: "Intersection",
|
|
248
|
+
_0: types
|
|
249
|
+
};
|
|
250
|
+
} else {
|
|
251
|
+
let propsDict = schema$1.properties;
|
|
252
|
+
let properties;
|
|
253
|
+
if (propsDict !== undefined) {
|
|
254
|
+
let required = Stdlib_Option.getOr(schema$1.required, []);
|
|
255
|
+
properties = Object.entries(propsDict).map(param => {
|
|
256
|
+
let name = param[0];
|
|
257
|
+
let isRequired = required.includes(name);
|
|
258
|
+
let propType = parseJsonSchemaWithContext(ctx, depth + 1 | 0, param[1]);
|
|
259
|
+
return [
|
|
260
|
+
name,
|
|
261
|
+
propType,
|
|
262
|
+
isRequired
|
|
263
|
+
];
|
|
264
|
+
});
|
|
265
|
+
} else {
|
|
266
|
+
properties = [];
|
|
267
|
+
}
|
|
268
|
+
baseType$2 = {
|
|
269
|
+
TAG: "Object",
|
|
270
|
+
properties: properties,
|
|
271
|
+
additionalProperties: undefined
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
break;
|
|
275
|
+
case "Null" :
|
|
276
|
+
baseType$2 = "Null";
|
|
277
|
+
break;
|
|
278
|
+
case "Unknown" :
|
|
279
|
+
baseType$2 = "Unknown";
|
|
280
|
+
break;
|
|
281
|
+
}
|
|
282
|
+
} else {
|
|
283
|
+
let itemSchema = schema$1.items;
|
|
284
|
+
let items = itemSchema !== undefined ? parseJsonSchemaWithContext(ctx, depth + 1 | 0, itemSchema) : "Unknown";
|
|
285
|
+
baseType$2 = {
|
|
286
|
+
TAG: "Array",
|
|
287
|
+
items: items,
|
|
288
|
+
constraints: {
|
|
289
|
+
minItems: undefined,
|
|
290
|
+
maxItems: undefined,
|
|
291
|
+
uniqueItems: false
|
|
292
|
+
}
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
} else {
|
|
296
|
+
let match = schema$1.enum;
|
|
297
|
+
let match$1 = schema$1.properties;
|
|
298
|
+
let match$2 = schema$1.allOf;
|
|
299
|
+
let match$3 = schema$1.oneOf;
|
|
300
|
+
let match$4 = schema$1.anyOf;
|
|
301
|
+
if (match !== undefined) {
|
|
302
|
+
let literals = match.map(value => {
|
|
303
|
+
if (value === null) {
|
|
304
|
+
return {
|
|
305
|
+
TAG: "Literal",
|
|
306
|
+
_0: "NullLiteral"
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
if (Array.isArray(value)) {
|
|
310
|
+
return "Unknown";
|
|
311
|
+
}
|
|
312
|
+
switch (typeof value) {
|
|
313
|
+
case "boolean" :
|
|
314
|
+
return {
|
|
315
|
+
TAG: "Literal",
|
|
316
|
+
_0: {
|
|
317
|
+
TAG: "BooleanLiteral",
|
|
318
|
+
_0: value
|
|
319
|
+
}
|
|
320
|
+
};
|
|
321
|
+
case "string" :
|
|
322
|
+
return {
|
|
323
|
+
TAG: "Literal",
|
|
324
|
+
_0: {
|
|
325
|
+
TAG: "StringLiteral",
|
|
326
|
+
_0: value
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
case "number" :
|
|
330
|
+
return {
|
|
331
|
+
TAG: "Literal",
|
|
332
|
+
_0: {
|
|
333
|
+
TAG: "NumberLiteral",
|
|
334
|
+
_0: value
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
case "object" :
|
|
338
|
+
return "Unknown";
|
|
339
|
+
}
|
|
340
|
+
});
|
|
341
|
+
baseType$2 = {
|
|
342
|
+
TAG: "Union",
|
|
343
|
+
_0: literals
|
|
344
|
+
};
|
|
345
|
+
} else if (match$1 !== undefined) {
|
|
346
|
+
baseType$2 = parseJsonSchemaWithContext(ctx, depth + 1 | 0, {
|
|
347
|
+
type: "Object",
|
|
348
|
+
properties: schema$1.properties,
|
|
349
|
+
items: schema$1.items,
|
|
350
|
+
required: schema$1.required,
|
|
351
|
+
enum: schema$1.enum,
|
|
352
|
+
$ref: schema$1.$ref,
|
|
353
|
+
allOf: schema$1.allOf,
|
|
354
|
+
oneOf: schema$1.oneOf,
|
|
355
|
+
anyOf: schema$1.anyOf,
|
|
356
|
+
description: schema$1.description,
|
|
357
|
+
format: schema$1.format,
|
|
358
|
+
minLength: schema$1.minLength,
|
|
359
|
+
maxLength: schema$1.maxLength,
|
|
360
|
+
minimum: schema$1.minimum,
|
|
361
|
+
maximum: schema$1.maximum,
|
|
362
|
+
pattern: schema$1.pattern,
|
|
363
|
+
nullable: undefined
|
|
364
|
+
});
|
|
365
|
+
} else if (match$2 !== undefined) {
|
|
366
|
+
let types$1 = match$2.map(s => parseJsonSchemaWithContext(ctx, depth + 1 | 0, s));
|
|
367
|
+
baseType$2 = {
|
|
368
|
+
TAG: "Intersection",
|
|
369
|
+
_0: types$1
|
|
370
|
+
};
|
|
371
|
+
} else if (match$3 !== undefined) {
|
|
372
|
+
let types$2 = match$3.map(s => parseJsonSchemaWithContext(ctx, depth + 1 | 0, s));
|
|
373
|
+
baseType$2 = {
|
|
374
|
+
TAG: "Union",
|
|
375
|
+
_0: types$2
|
|
376
|
+
};
|
|
377
|
+
} else if (match$4 !== undefined) {
|
|
378
|
+
let types$3 = match$4.map(s => parseJsonSchemaWithContext(ctx, depth + 1 | 0, s));
|
|
379
|
+
baseType$2 = {
|
|
380
|
+
TAG: "Union",
|
|
381
|
+
_0: types$3
|
|
382
|
+
};
|
|
383
|
+
} else {
|
|
384
|
+
baseType$2 = "Unknown";
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
if (isNullable) {
|
|
388
|
+
return {
|
|
389
|
+
TAG: "Option",
|
|
390
|
+
_0: baseType$2
|
|
391
|
+
};
|
|
392
|
+
} else {
|
|
393
|
+
return baseType$2;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
function parseJsonSchema(depthOpt, schema) {
|
|
398
|
+
let depth = depthOpt !== undefined ? depthOpt : 0;
|
|
399
|
+
let ctx = {
|
|
400
|
+
warnings: [],
|
|
401
|
+
path: "root"
|
|
402
|
+
};
|
|
403
|
+
let irType = parseJsonSchemaWithContext(ctx, depth, schema);
|
|
404
|
+
return [
|
|
405
|
+
irType,
|
|
406
|
+
ctx.warnings
|
|
407
|
+
];
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
function parseNamedSchema(name, schema) {
|
|
411
|
+
let ctx = {
|
|
412
|
+
warnings: [],
|
|
413
|
+
path: `components.schemas.` + name
|
|
414
|
+
};
|
|
415
|
+
let type_ = parseJsonSchemaWithContext(ctx, undefined, schema);
|
|
416
|
+
return [
|
|
417
|
+
{
|
|
418
|
+
name: name,
|
|
419
|
+
description: schema.description,
|
|
420
|
+
type_: type_
|
|
421
|
+
},
|
|
422
|
+
ctx.warnings
|
|
423
|
+
];
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
function normalizeReferences(availableNames, irType) {
|
|
427
|
+
if (typeof irType !== "object") {
|
|
428
|
+
return irType;
|
|
429
|
+
}
|
|
430
|
+
switch (irType.TAG) {
|
|
431
|
+
case "Array" :
|
|
432
|
+
return {
|
|
433
|
+
TAG: "Array",
|
|
434
|
+
items: normalizeReferences(availableNames, irType.items),
|
|
435
|
+
constraints: irType.constraints
|
|
436
|
+
};
|
|
437
|
+
case "Object" :
|
|
438
|
+
let newProperties = irType.properties.map(param => [
|
|
439
|
+
param[0],
|
|
440
|
+
normalizeReferences(availableNames, param[1]),
|
|
441
|
+
param[2]
|
|
442
|
+
]);
|
|
443
|
+
let newAdditional = Stdlib_Option.map(irType.additionalProperties, t => normalizeReferences(availableNames, t));
|
|
444
|
+
return {
|
|
445
|
+
TAG: "Object",
|
|
446
|
+
properties: newProperties,
|
|
447
|
+
additionalProperties: newAdditional
|
|
448
|
+
};
|
|
449
|
+
case "Union" :
|
|
450
|
+
return {
|
|
451
|
+
TAG: "Union",
|
|
452
|
+
_0: irType._0.map(t => normalizeReferences(availableNames, t))
|
|
453
|
+
};
|
|
454
|
+
case "Intersection" :
|
|
455
|
+
return {
|
|
456
|
+
TAG: "Intersection",
|
|
457
|
+
_0: irType._0.map(t => normalizeReferences(availableNames, t))
|
|
458
|
+
};
|
|
459
|
+
case "Reference" :
|
|
460
|
+
let parts = irType._0.split("/");
|
|
461
|
+
let name = Stdlib_Option.getOr(parts[parts.length - 1 | 0], "");
|
|
462
|
+
if (availableNames.includes(name)) {
|
|
463
|
+
return {
|
|
464
|
+
TAG: "Reference",
|
|
465
|
+
_0: name
|
|
466
|
+
};
|
|
467
|
+
} else {
|
|
468
|
+
return irType;
|
|
469
|
+
}
|
|
470
|
+
case "Option" :
|
|
471
|
+
return {
|
|
472
|
+
TAG: "Option",
|
|
473
|
+
_0: normalizeReferences(availableNames, irType._0)
|
|
474
|
+
};
|
|
475
|
+
default:
|
|
476
|
+
return irType;
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
function parseComponentSchemas(schemas) {
|
|
481
|
+
let namedSchemas = {};
|
|
482
|
+
let allWarnings = [];
|
|
483
|
+
Object.entries(schemas).forEach(param => {
|
|
484
|
+
let name = param[0];
|
|
485
|
+
let match = parseNamedSchema(name, param[1]);
|
|
486
|
+
namedSchemas[name] = match[0];
|
|
487
|
+
allWarnings.push(...match[1]);
|
|
488
|
+
});
|
|
489
|
+
let availableNames = Object.keys(namedSchemas);
|
|
490
|
+
Object.entries(namedSchemas).forEach(param => {
|
|
491
|
+
let namedSchema = param[1];
|
|
492
|
+
let resolved = normalizeReferences(availableNames, namedSchema.type_);
|
|
493
|
+
namedSchemas[param[0]] = {
|
|
494
|
+
name: namedSchema.name,
|
|
495
|
+
description: namedSchema.description,
|
|
496
|
+
type_: resolved
|
|
497
|
+
};
|
|
498
|
+
});
|
|
499
|
+
return [
|
|
500
|
+
{
|
|
501
|
+
schemas: namedSchemas
|
|
502
|
+
},
|
|
503
|
+
allWarnings
|
|
504
|
+
];
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
function resolveReference(context, ref) {
|
|
508
|
+
let parts = ref.split("/");
|
|
509
|
+
let schemaName = parts[parts.length - 1 | 0];
|
|
510
|
+
if (schemaName !== undefined) {
|
|
511
|
+
return context.schemas[schemaName];
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
function inlineSimpleReferences(context, _irType, _depthOpt, _maxDepthOpt) {
|
|
516
|
+
while (true) {
|
|
517
|
+
let depthOpt = _depthOpt;
|
|
518
|
+
let maxDepthOpt = _maxDepthOpt;
|
|
519
|
+
let irType = _irType;
|
|
520
|
+
let depth = depthOpt !== undefined ? depthOpt : 0;
|
|
521
|
+
let maxDepth = maxDepthOpt !== undefined ? maxDepthOpt : 2;
|
|
522
|
+
if (depth >= maxDepth) {
|
|
523
|
+
return irType;
|
|
524
|
+
}
|
|
525
|
+
if (typeof irType !== "object") {
|
|
526
|
+
return irType;
|
|
527
|
+
}
|
|
528
|
+
switch (irType.TAG) {
|
|
529
|
+
case "Array" :
|
|
530
|
+
return {
|
|
531
|
+
TAG: "Array",
|
|
532
|
+
items: inlineSimpleReferences(context, irType.items, depth, maxDepth),
|
|
533
|
+
constraints: irType.constraints
|
|
534
|
+
};
|
|
535
|
+
case "Object" :
|
|
536
|
+
let newProperties = irType.properties.map(param => [
|
|
537
|
+
param[0],
|
|
538
|
+
inlineSimpleReferences(context, param[1], depth, maxDepth),
|
|
539
|
+
param[2]
|
|
540
|
+
]);
|
|
541
|
+
let newAdditionalProps = Stdlib_Option.map(irType.additionalProperties, type_ => inlineSimpleReferences(context, type_, depth, maxDepth));
|
|
542
|
+
return {
|
|
543
|
+
TAG: "Object",
|
|
544
|
+
properties: newProperties,
|
|
545
|
+
additionalProperties: newAdditionalProps
|
|
546
|
+
};
|
|
547
|
+
case "Union" :
|
|
548
|
+
return {
|
|
549
|
+
TAG: "Union",
|
|
550
|
+
_0: irType._0.map(type_ => inlineSimpleReferences(context, type_, depth, maxDepth))
|
|
551
|
+
};
|
|
552
|
+
case "Intersection" :
|
|
553
|
+
return {
|
|
554
|
+
TAG: "Intersection",
|
|
555
|
+
_0: irType._0.map(type_ => inlineSimpleReferences(context, type_, depth, maxDepth))
|
|
556
|
+
};
|
|
557
|
+
case "Reference" :
|
|
558
|
+
let schema = resolveReference(context, irType._0);
|
|
559
|
+
if (schema === undefined) {
|
|
560
|
+
return irType;
|
|
561
|
+
}
|
|
562
|
+
if (!SchemaIR.isSimpleType(schema.type_)) {
|
|
563
|
+
return irType;
|
|
564
|
+
}
|
|
565
|
+
_maxDepthOpt = maxDepth;
|
|
566
|
+
_depthOpt = depth + 1 | 0;
|
|
567
|
+
_irType = schema.type_;
|
|
568
|
+
continue;
|
|
569
|
+
case "Option" :
|
|
570
|
+
return {
|
|
571
|
+
TAG: "Option",
|
|
572
|
+
_0: inlineSimpleReferences(context, irType._0, depth, maxDepth)
|
|
573
|
+
};
|
|
574
|
+
default:
|
|
575
|
+
return irType;
|
|
576
|
+
}
|
|
577
|
+
};
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
function optimizeIR(irType) {
|
|
581
|
+
if (typeof irType !== "object") {
|
|
582
|
+
return irType;
|
|
583
|
+
}
|
|
584
|
+
switch (irType.TAG) {
|
|
585
|
+
case "Array" :
|
|
586
|
+
return {
|
|
587
|
+
TAG: "Array",
|
|
588
|
+
items: optimizeIR(irType.items),
|
|
589
|
+
constraints: irType.constraints
|
|
590
|
+
};
|
|
591
|
+
case "Object" :
|
|
592
|
+
let newProperties = irType.properties.map(param => [
|
|
593
|
+
param[0],
|
|
594
|
+
optimizeIR(param[1]),
|
|
595
|
+
param[2]
|
|
596
|
+
]);
|
|
597
|
+
let newAdditionalProps = Stdlib_Option.map(irType.additionalProperties, optimizeIR);
|
|
598
|
+
return {
|
|
599
|
+
TAG: "Object",
|
|
600
|
+
properties: newProperties,
|
|
601
|
+
additionalProperties: newAdditionalProps
|
|
602
|
+
};
|
|
603
|
+
case "Union" :
|
|
604
|
+
let flattened = irType._0.flatMap(t => {
|
|
605
|
+
let inner = optimizeIR(t);
|
|
606
|
+
if (typeof inner !== "object") {
|
|
607
|
+
return [inner];
|
|
608
|
+
} else if (inner.TAG === "Union") {
|
|
609
|
+
return inner._0;
|
|
610
|
+
} else {
|
|
611
|
+
return [inner];
|
|
612
|
+
}
|
|
613
|
+
});
|
|
614
|
+
let unique = [];
|
|
615
|
+
flattened.forEach(type_ => {
|
|
616
|
+
let typeStr = SchemaIR.toString(type_);
|
|
617
|
+
let exists = unique.some(t => SchemaIR.toString(t) === typeStr);
|
|
618
|
+
if (!exists) {
|
|
619
|
+
unique.push(type_);
|
|
620
|
+
return;
|
|
621
|
+
}
|
|
622
|
+
});
|
|
623
|
+
let len = unique.length;
|
|
624
|
+
if (len !== 1) {
|
|
625
|
+
if (len !== 0) {
|
|
626
|
+
return {
|
|
627
|
+
TAG: "Union",
|
|
628
|
+
_0: unique
|
|
629
|
+
};
|
|
630
|
+
} else {
|
|
631
|
+
return "Unknown";
|
|
632
|
+
}
|
|
633
|
+
} else {
|
|
634
|
+
return unique[0];
|
|
635
|
+
}
|
|
636
|
+
case "Intersection" :
|
|
637
|
+
let flattened$1 = irType._0.flatMap(t => {
|
|
638
|
+
let inner = optimizeIR(t);
|
|
639
|
+
if (typeof inner !== "object") {
|
|
640
|
+
return [inner];
|
|
641
|
+
} else if (inner.TAG === "Intersection") {
|
|
642
|
+
return inner._0;
|
|
643
|
+
} else {
|
|
644
|
+
return [inner];
|
|
645
|
+
}
|
|
646
|
+
});
|
|
647
|
+
let len$1 = flattened$1.length;
|
|
648
|
+
if (len$1 !== 1) {
|
|
649
|
+
if (len$1 !== 0) {
|
|
650
|
+
return {
|
|
651
|
+
TAG: "Intersection",
|
|
652
|
+
_0: flattened$1
|
|
653
|
+
};
|
|
654
|
+
} else {
|
|
655
|
+
return "Unknown";
|
|
656
|
+
}
|
|
657
|
+
} else {
|
|
658
|
+
return flattened$1[0];
|
|
659
|
+
}
|
|
660
|
+
case "Option" :
|
|
661
|
+
return {
|
|
662
|
+
TAG: "Option",
|
|
663
|
+
_0: optimizeIR(irType._0)
|
|
664
|
+
};
|
|
665
|
+
default:
|
|
666
|
+
return irType;
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
export {
|
|
671
|
+
parseTypeString,
|
|
672
|
+
parseTypeAsArray,
|
|
673
|
+
addWarning,
|
|
674
|
+
parseJsonSchemaWithContext,
|
|
675
|
+
parseJsonSchema,
|
|
676
|
+
parseNamedSchema,
|
|
677
|
+
normalizeReferences,
|
|
678
|
+
parseComponentSchemas,
|
|
679
|
+
resolveReference,
|
|
680
|
+
inlineSimpleReferences,
|
|
681
|
+
optimizeIR,
|
|
682
|
+
}
|
|
683
|
+
/* No side effect */
|