@f3liz/rescript-autogen-openapi 0.5.1 → 0.5.4
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.
|
@@ -10,6 +10,14 @@ import * as Primitive_string from "@rescript/runtime/lib/es6/Primitive_string.js
|
|
|
10
10
|
import * as GenerationContext from "../types/GenerationContext.mjs";
|
|
11
11
|
import * as ReferenceResolver from "../core/ReferenceResolver.mjs";
|
|
12
12
|
|
|
13
|
+
function nullableToOption(schemaCode) {
|
|
14
|
+
if (schemaCode.startsWith("S.nullableAsOption(")) {
|
|
15
|
+
return "S.option(" + schemaCode.slice("S.nullableAsOption(".length);
|
|
16
|
+
} else {
|
|
17
|
+
return `S.option(` + schemaCode + `)`;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
13
21
|
function applyConstraints(base, min, max, toString) {
|
|
14
22
|
let s1 = min !== undefined ? base + `->S.min(` + toString(Primitive_option.valFromOption(min)) + `)` : base;
|
|
15
23
|
if (max !== undefined) {
|
|
@@ -109,9 +117,9 @@ function generateSchemaWithContext(ctx, depthOpt, extractedTypeMap, irType) {
|
|
|
109
117
|
if (param[2]) {
|
|
110
118
|
return ` ` + camelName + `: s.field("` + name + `", ` + schemaCode + `),`;
|
|
111
119
|
} else if (alreadyNullable) {
|
|
112
|
-
return ` ` + camelName + `: s.
|
|
120
|
+
return ` ` + camelName + `: s.field("` + name + `", ` + nullableToOption(schemaCode) + `),`;
|
|
113
121
|
} else {
|
|
114
|
-
return ` ` + camelName + `: s.
|
|
122
|
+
return ` ` + camelName + `: s.field("` + name + `", S.option(` + schemaCode + `)),`;
|
|
115
123
|
}
|
|
116
124
|
}).join("\n");
|
|
117
125
|
return `S.object(s => {\n` + fields + `\n })`;
|
|
@@ -299,9 +307,9 @@ function generateSchemaWithContext(ctx, depthOpt, extractedTypeMap, irType) {
|
|
|
299
307
|
if (param[2]) {
|
|
300
308
|
return ` ` + camelName + `: s.field("` + name + `", ` + schemaCode + `),`;
|
|
301
309
|
} else if (alreadyNullable) {
|
|
302
|
-
return ` ` + camelName + `: s.
|
|
310
|
+
return ` ` + camelName + `: s.field("` + name + `", ` + nullableToOption(schemaCode) + `),`;
|
|
303
311
|
} else {
|
|
304
|
-
return ` ` + camelName + `: s.
|
|
312
|
+
return ` ` + camelName + `: s.field("` + name + `", S.option(` + schemaCode + `)),`;
|
|
305
313
|
}
|
|
306
314
|
}).join("\n");
|
|
307
315
|
return `S.object(s => ` + constructorName + `({\n` + fields + `\n }))`;
|
|
@@ -394,9 +402,9 @@ function generateSchemaWithContext(ctx, depthOpt, extractedTypeMap, irType) {
|
|
|
394
402
|
if (param[2]) {
|
|
395
403
|
return ` ` + camelName + `: s.field("` + name + `", ` + schemaCode + `),`;
|
|
396
404
|
} else if (alreadyNullable) {
|
|
397
|
-
return ` ` + camelName + `: s.
|
|
405
|
+
return ` ` + camelName + `: s.field("` + name + `", ` + nullableToOption(schemaCode) + `),`;
|
|
398
406
|
} else {
|
|
399
|
-
return ` ` + camelName + `: s.
|
|
407
|
+
return ` ` + camelName + `: s.field("` + name + `", S.option(` + schemaCode + `)),`;
|
|
400
408
|
}
|
|
401
409
|
}).join("\n");
|
|
402
410
|
return `S.object(s => {\n` + fields$1 + `\n })`;
|
|
@@ -445,9 +453,9 @@ function generateSchemaWithContext(ctx, depthOpt, extractedTypeMap, irType) {
|
|
|
445
453
|
if (param[2]) {
|
|
446
454
|
return ` ` + camelName + `: s.field("` + name + `", ` + schemaCode + `),`;
|
|
447
455
|
} else if (alreadyNullable) {
|
|
448
|
-
return ` ` + camelName + `: s.
|
|
456
|
+
return ` ` + camelName + `: s.field("` + name + `", ` + nullableToOption(schemaCode) + `),`;
|
|
449
457
|
} else {
|
|
450
|
-
return ` ` + camelName + `: s.
|
|
458
|
+
return ` ` + camelName + `: s.field("` + name + `", S.option(` + schemaCode + `)),`;
|
|
451
459
|
}
|
|
452
460
|
}).join("\n");
|
|
453
461
|
return `S.object(s => {\n` + fields$2 + `\n })`;
|
|
@@ -534,6 +542,7 @@ let addWarning = GenerationContext.addWarning;
|
|
|
534
542
|
|
|
535
543
|
export {
|
|
536
544
|
addWarning,
|
|
545
|
+
nullableToOption,
|
|
537
546
|
applyConstraints,
|
|
538
547
|
generateSchemaWithContext,
|
|
539
548
|
generateSchema,
|
package/package.json
CHANGED
|
@@ -5,6 +5,16 @@ open Types
|
|
|
5
5
|
|
|
6
6
|
let addWarning = GenerationContext.addWarning
|
|
7
7
|
|
|
8
|
+
// Sury can't set defaults for nullableAsOption schemas, so we use s.field with S.option instead of s.fieldOr
|
|
9
|
+
|
|
10
|
+
// Replace S.nullableAsOption(...) with S.option(...) to avoid double-option wrapping
|
|
11
|
+
let nullableToOption = (schemaCode: string) =>
|
|
12
|
+
if String.startsWith(schemaCode, "S.nullableAsOption(") {
|
|
13
|
+
"S.option(" ++ String.sliceToEnd(schemaCode, ~start=String.length("S.nullableAsOption("))
|
|
14
|
+
} else {
|
|
15
|
+
`S.option(${schemaCode})`
|
|
16
|
+
}
|
|
17
|
+
|
|
8
18
|
let applyConstraints = (base, min, max, toString) => {
|
|
9
19
|
let s1 = switch min {
|
|
10
20
|
| Some(v) => `${base}->S.min(${toString(v)})`
|
|
@@ -73,9 +83,9 @@ let rec generateSchemaWithContext = (~ctx: GenerationContext.t, ~depth=0, ~extra
|
|
|
73
83
|
if isRequired {
|
|
74
84
|
` ${camelName}: s.field("${name}", ${schemaCode}),`
|
|
75
85
|
} else if alreadyNullable {
|
|
76
|
-
` ${camelName}: s.
|
|
86
|
+
` ${camelName}: s.field("${name}", ${nullableToOption(schemaCode)}),`
|
|
77
87
|
} else {
|
|
78
|
-
` ${camelName}: s.
|
|
88
|
+
` ${camelName}: s.field("${name}", S.option(${schemaCode})),`
|
|
79
89
|
}
|
|
80
90
|
})
|
|
81
91
|
->Array.join("\n")
|
|
@@ -177,9 +187,9 @@ let rec generateSchemaWithContext = (~ctx: GenerationContext.t, ~depth=0, ~extra
|
|
|
177
187
|
if isRequired {
|
|
178
188
|
` ${camelName}: s.field("${name}", ${schemaCode}),`
|
|
179
189
|
} else if alreadyNullable {
|
|
180
|
-
` ${camelName}: s.
|
|
190
|
+
` ${camelName}: s.field("${name}", ${nullableToOption(schemaCode)}),`
|
|
181
191
|
} else {
|
|
182
|
-
` ${camelName}: s.
|
|
192
|
+
` ${camelName}: s.field("${name}", S.option(${schemaCode})),`
|
|
183
193
|
}
|
|
184
194
|
})->Array.join("\n")
|
|
185
195
|
`S.object(s => ${constructorName}({\n${fields}\n }))`
|
|
@@ -233,9 +243,9 @@ let rec generateSchemaWithContext = (~ctx: GenerationContext.t, ~depth=0, ~extra
|
|
|
233
243
|
if isRequired {
|
|
234
244
|
` ${camelName}: s.field("${name}", ${schemaCode}),`
|
|
235
245
|
} else if alreadyNullable {
|
|
236
|
-
` ${camelName}: s.
|
|
246
|
+
` ${camelName}: s.field("${name}", ${nullableToOption(schemaCode)}),`
|
|
237
247
|
} else {
|
|
238
|
-
` ${camelName}: s.
|
|
248
|
+
` ${camelName}: s.field("${name}", S.option(${schemaCode})),`
|
|
239
249
|
}
|
|
240
250
|
})
|
|
241
251
|
->Array.join("\n")
|
|
@@ -260,9 +270,9 @@ let rec generateSchemaWithContext = (~ctx: GenerationContext.t, ~depth=0, ~extra
|
|
|
260
270
|
if isRequired {
|
|
261
271
|
` ${camelName}: s.field("${name}", ${schemaCode}),`
|
|
262
272
|
} else if alreadyNullable {
|
|
263
|
-
` ${camelName}: s.
|
|
273
|
+
` ${camelName}: s.field("${name}", ${nullableToOption(schemaCode)}),`
|
|
264
274
|
} else {
|
|
265
|
-
` ${camelName}: s.
|
|
275
|
+
` ${camelName}: s.field("${name}", S.option(${schemaCode})),`
|
|
266
276
|
}
|
|
267
277
|
})
|
|
268
278
|
->Array.join("\n")
|