@f3liz/rescript-autogen-openapi 0.5.1 → 0.5.3
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,22 @@ 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 cannotSetDefault(schemaCode) {
|
|
14
|
+
if (schemaCode.includes("S.dict(")) {
|
|
15
|
+
return true;
|
|
16
|
+
} else {
|
|
17
|
+
return schemaCode.includes("S.object(");
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function nullableToOption(schemaCode) {
|
|
22
|
+
if (schemaCode.startsWith("S.nullableAsOption(")) {
|
|
23
|
+
return "S.option(" + schemaCode.slice("S.nullableAsOption(".length);
|
|
24
|
+
} else {
|
|
25
|
+
return `S.option(` + schemaCode + `)`;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
13
29
|
function applyConstraints(base, min, max, toString) {
|
|
14
30
|
let s1 = min !== undefined ? base + `->S.min(` + toString(Primitive_option.valFromOption(min)) + `)` : base;
|
|
15
31
|
if (max !== undefined) {
|
|
@@ -109,7 +125,13 @@ function generateSchemaWithContext(ctx, depthOpt, extractedTypeMap, irType) {
|
|
|
109
125
|
if (param[2]) {
|
|
110
126
|
return ` ` + camelName + `: s.field("` + name + `", ` + schemaCode + `),`;
|
|
111
127
|
} else if (alreadyNullable) {
|
|
112
|
-
|
|
128
|
+
if (cannotSetDefault(schemaCode)) {
|
|
129
|
+
return ` ` + camelName + `: s.field("` + name + `", ` + nullableToOption(schemaCode) + `),`;
|
|
130
|
+
} else {
|
|
131
|
+
return ` ` + camelName + `: s.fieldOr("` + name + `", ` + schemaCode + `, None),`;
|
|
132
|
+
}
|
|
133
|
+
} else if (cannotSetDefault(schemaCode)) {
|
|
134
|
+
return ` ` + camelName + `: s.field("` + name + `", S.option(` + schemaCode + `)),`;
|
|
113
135
|
} else {
|
|
114
136
|
return ` ` + camelName + `: s.fieldOr("` + name + `", S.nullableAsOption(` + schemaCode + `), None),`;
|
|
115
137
|
}
|
|
@@ -299,7 +321,13 @@ function generateSchemaWithContext(ctx, depthOpt, extractedTypeMap, irType) {
|
|
|
299
321
|
if (param[2]) {
|
|
300
322
|
return ` ` + camelName + `: s.field("` + name + `", ` + schemaCode + `),`;
|
|
301
323
|
} else if (alreadyNullable) {
|
|
302
|
-
|
|
324
|
+
if (cannotSetDefault(schemaCode)) {
|
|
325
|
+
return ` ` + camelName + `: s.field("` + name + `", ` + nullableToOption(schemaCode) + `),`;
|
|
326
|
+
} else {
|
|
327
|
+
return ` ` + camelName + `: s.fieldOr("` + name + `", ` + schemaCode + `, None),`;
|
|
328
|
+
}
|
|
329
|
+
} else if (cannotSetDefault(schemaCode)) {
|
|
330
|
+
return ` ` + camelName + `: s.field("` + name + `", S.option(` + schemaCode + `)),`;
|
|
303
331
|
} else {
|
|
304
332
|
return ` ` + camelName + `: s.fieldOr("` + name + `", S.nullableAsOption(` + schemaCode + `), None),`;
|
|
305
333
|
}
|
|
@@ -394,7 +422,13 @@ function generateSchemaWithContext(ctx, depthOpt, extractedTypeMap, irType) {
|
|
|
394
422
|
if (param[2]) {
|
|
395
423
|
return ` ` + camelName + `: s.field("` + name + `", ` + schemaCode + `),`;
|
|
396
424
|
} else if (alreadyNullable) {
|
|
397
|
-
|
|
425
|
+
if (cannotSetDefault(schemaCode)) {
|
|
426
|
+
return ` ` + camelName + `: s.field("` + name + `", ` + nullableToOption(schemaCode) + `),`;
|
|
427
|
+
} else {
|
|
428
|
+
return ` ` + camelName + `: s.fieldOr("` + name + `", ` + schemaCode + `, None),`;
|
|
429
|
+
}
|
|
430
|
+
} else if (cannotSetDefault(schemaCode)) {
|
|
431
|
+
return ` ` + camelName + `: s.field("` + name + `", S.option(` + schemaCode + `)),`;
|
|
398
432
|
} else {
|
|
399
433
|
return ` ` + camelName + `: s.fieldOr("` + name + `", S.nullableAsOption(` + schemaCode + `), None),`;
|
|
400
434
|
}
|
|
@@ -445,7 +479,13 @@ function generateSchemaWithContext(ctx, depthOpt, extractedTypeMap, irType) {
|
|
|
445
479
|
if (param[2]) {
|
|
446
480
|
return ` ` + camelName + `: s.field("` + name + `", ` + schemaCode + `),`;
|
|
447
481
|
} else if (alreadyNullable) {
|
|
448
|
-
|
|
482
|
+
if (cannotSetDefault(schemaCode)) {
|
|
483
|
+
return ` ` + camelName + `: s.field("` + name + `", ` + nullableToOption(schemaCode) + `),`;
|
|
484
|
+
} else {
|
|
485
|
+
return ` ` + camelName + `: s.fieldOr("` + name + `", ` + schemaCode + `, None),`;
|
|
486
|
+
}
|
|
487
|
+
} else if (cannotSetDefault(schemaCode)) {
|
|
488
|
+
return ` ` + camelName + `: s.field("` + name + `", S.option(` + schemaCode + `)),`;
|
|
449
489
|
} else {
|
|
450
490
|
return ` ` + camelName + `: s.fieldOr("` + name + `", S.nullableAsOption(` + schemaCode + `), None),`;
|
|
451
491
|
}
|
|
@@ -534,6 +574,8 @@ let addWarning = GenerationContext.addWarning;
|
|
|
534
574
|
|
|
535
575
|
export {
|
|
536
576
|
addWarning,
|
|
577
|
+
cannotSetDefault,
|
|
578
|
+
nullableToOption,
|
|
537
579
|
applyConstraints,
|
|
538
580
|
generateSchemaWithContext,
|
|
539
581
|
generateSchema,
|
package/package.json
CHANGED
|
@@ -5,6 +5,18 @@ open Types
|
|
|
5
5
|
|
|
6
6
|
let addWarning = GenerationContext.addWarning
|
|
7
7
|
|
|
8
|
+
// Sury can't set defaults for dict/object types, so we need s.field with S.option instead of s.fieldOr
|
|
9
|
+
let cannotSetDefault = (schemaCode: string) =>
|
|
10
|
+
String.includes(schemaCode, "S.dict(") || String.includes(schemaCode, "S.object(")
|
|
11
|
+
|
|
12
|
+
// Replace S.nullableAsOption(...) with S.option(...) to avoid double-option wrapping
|
|
13
|
+
let nullableToOption = (schemaCode: string) =>
|
|
14
|
+
if String.startsWith(schemaCode, "S.nullableAsOption(") {
|
|
15
|
+
"S.option(" ++ String.sliceToEnd(schemaCode, ~start=String.length("S.nullableAsOption("))
|
|
16
|
+
} else {
|
|
17
|
+
`S.option(${schemaCode})`
|
|
18
|
+
}
|
|
19
|
+
|
|
8
20
|
let applyConstraints = (base, min, max, toString) => {
|
|
9
21
|
let s1 = switch min {
|
|
10
22
|
| Some(v) => `${base}->S.min(${toString(v)})`
|
|
@@ -73,9 +85,17 @@ let rec generateSchemaWithContext = (~ctx: GenerationContext.t, ~depth=0, ~extra
|
|
|
73
85
|
if isRequired {
|
|
74
86
|
` ${camelName}: s.field("${name}", ${schemaCode}),`
|
|
75
87
|
} else if alreadyNullable {
|
|
76
|
-
|
|
88
|
+
if cannotSetDefault(schemaCode) {
|
|
89
|
+
` ${camelName}: s.field("${name}", ${nullableToOption(schemaCode)}),`
|
|
90
|
+
} else {
|
|
91
|
+
` ${camelName}: s.fieldOr("${name}", ${schemaCode}, None),`
|
|
92
|
+
}
|
|
77
93
|
} else {
|
|
78
|
-
|
|
94
|
+
if cannotSetDefault(schemaCode) {
|
|
95
|
+
` ${camelName}: s.field("${name}", S.option(${schemaCode})),`
|
|
96
|
+
} else {
|
|
97
|
+
` ${camelName}: s.fieldOr("${name}", S.nullableAsOption(${schemaCode}), None),`
|
|
98
|
+
}
|
|
79
99
|
}
|
|
80
100
|
})
|
|
81
101
|
->Array.join("\n")
|
|
@@ -177,9 +197,17 @@ let rec generateSchemaWithContext = (~ctx: GenerationContext.t, ~depth=0, ~extra
|
|
|
177
197
|
if isRequired {
|
|
178
198
|
` ${camelName}: s.field("${name}", ${schemaCode}),`
|
|
179
199
|
} else if alreadyNullable {
|
|
180
|
-
|
|
200
|
+
if cannotSetDefault(schemaCode) {
|
|
201
|
+
` ${camelName}: s.field("${name}", ${nullableToOption(schemaCode)}),`
|
|
202
|
+
} else {
|
|
203
|
+
` ${camelName}: s.fieldOr("${name}", ${schemaCode}, None),`
|
|
204
|
+
}
|
|
181
205
|
} else {
|
|
182
|
-
|
|
206
|
+
if cannotSetDefault(schemaCode) {
|
|
207
|
+
` ${camelName}: s.field("${name}", S.option(${schemaCode})),`
|
|
208
|
+
} else {
|
|
209
|
+
` ${camelName}: s.fieldOr("${name}", S.nullableAsOption(${schemaCode}), None),`
|
|
210
|
+
}
|
|
183
211
|
}
|
|
184
212
|
})->Array.join("\n")
|
|
185
213
|
`S.object(s => ${constructorName}({\n${fields}\n }))`
|
|
@@ -233,9 +261,17 @@ let rec generateSchemaWithContext = (~ctx: GenerationContext.t, ~depth=0, ~extra
|
|
|
233
261
|
if isRequired {
|
|
234
262
|
` ${camelName}: s.field("${name}", ${schemaCode}),`
|
|
235
263
|
} else if alreadyNullable {
|
|
236
|
-
|
|
264
|
+
if cannotSetDefault(schemaCode) {
|
|
265
|
+
` ${camelName}: s.field("${name}", ${nullableToOption(schemaCode)}),`
|
|
266
|
+
} else {
|
|
267
|
+
` ${camelName}: s.fieldOr("${name}", ${schemaCode}, None),`
|
|
268
|
+
}
|
|
237
269
|
} else {
|
|
238
|
-
|
|
270
|
+
if cannotSetDefault(schemaCode) {
|
|
271
|
+
` ${camelName}: s.field("${name}", S.option(${schemaCode})),`
|
|
272
|
+
} else {
|
|
273
|
+
` ${camelName}: s.fieldOr("${name}", S.nullableAsOption(${schemaCode}), None),`
|
|
274
|
+
}
|
|
239
275
|
}
|
|
240
276
|
})
|
|
241
277
|
->Array.join("\n")
|
|
@@ -260,9 +296,17 @@ let rec generateSchemaWithContext = (~ctx: GenerationContext.t, ~depth=0, ~extra
|
|
|
260
296
|
if isRequired {
|
|
261
297
|
` ${camelName}: s.field("${name}", ${schemaCode}),`
|
|
262
298
|
} else if alreadyNullable {
|
|
263
|
-
|
|
299
|
+
if cannotSetDefault(schemaCode) {
|
|
300
|
+
` ${camelName}: s.field("${name}", ${nullableToOption(schemaCode)}),`
|
|
301
|
+
} else {
|
|
302
|
+
` ${camelName}: s.fieldOr("${name}", ${schemaCode}, None),`
|
|
303
|
+
}
|
|
264
304
|
} else {
|
|
265
|
-
|
|
305
|
+
if cannotSetDefault(schemaCode) {
|
|
306
|
+
` ${camelName}: s.field("${name}", S.option(${schemaCode})),`
|
|
307
|
+
} else {
|
|
308
|
+
` ${camelName}: s.fieldOr("${name}", S.nullableAsOption(${schemaCode}), None),`
|
|
309
|
+
}
|
|
266
310
|
}
|
|
267
311
|
})
|
|
268
312
|
->Array.join("\n")
|