@@ -1160,240 +1160,6 @@ describe('generatePartOfSchemaGenerateZod', () => {
1160
1160
} ) ;
1161
1161
} ) ;
1162
1162
1163
- const apiSchemaWithDiscriminator : GeneratorOptions = {
1164
- pathRoute : '/dogs' ,
1165
- context : {
1166
- specKey : 'dog' ,
1167
- specs : {
1168
- dog : {
1169
- openapi : '3.0.0' ,
1170
- info : {
1171
- version : '1.0.0' ,
1172
- title : 'Dogs' ,
1173
- } ,
1174
- paths : {
1175
- '/dogs' : {
1176
- get : {
1177
- summary : 'List all dogs' ,
1178
- operationId : 'listDogs' ,
1179
- tags : [ 'dogs' ] ,
1180
- responses : {
1181
- '200' : {
1182
- description : 'A paged array of dogs' ,
1183
- content : {
1184
- 'application/json' : {
1185
- schema : {
1186
- $ref : '#/components/schemas/Dogs' ,
1187
- } ,
1188
- } ,
1189
- } ,
1190
- } ,
1191
- } ,
1192
- } ,
1193
- } ,
1194
- } ,
1195
- components : {
1196
- schemas : {
1197
- Dogs : {
1198
- type : 'array' ,
1199
- items : {
1200
- $ref : '#/components/schemas/Dog' ,
1201
- } ,
1202
- } ,
1203
- Dog : {
1204
- type : 'object' ,
1205
- oneOf : [
1206
- {
1207
- $ref : '#/components/schemas/Labradoodle' ,
1208
- } ,
1209
- {
1210
- $ref : '#/components/schemas/Dachshund' ,
1211
- } ,
1212
- ] ,
1213
- required : [ 'type' ] ,
1214
- properties : {
1215
- barksPerMinute : {
1216
- type : 'integer' ,
1217
- } ,
1218
- } ,
1219
- discriminator : {
1220
- propertyName : 'breed' ,
1221
- mapping : {
1222
- Labradoodle : '#/components/schemas/Labradoodle' ,
1223
- Dachshund : '#/components/schemas/Dachshund' ,
1224
- } ,
1225
- } ,
1226
- } ,
1227
- Labradoodle : {
1228
- type : 'object' ,
1229
- required : [ 'cuteness' ] ,
1230
- properties : {
1231
- cuteness : {
1232
- type : 'integer' ,
1233
- } ,
1234
- // in the real runner breed is added by getApiSchemas in import-open-api.ts, inferred from the discriminator
1235
- breed : {
1236
- type : 'string' ,
1237
- enum : [ 'Labradoodle' ] ,
1238
- } ,
1239
- } ,
1240
- } ,
1241
- Dachshund : {
1242
- type : 'object' ,
1243
- required : [ 'length' ] ,
1244
- properties : {
1245
- length : {
1246
- type : 'integer' ,
1247
- } ,
1248
- // in the real runner breed is added by getApiSchemas in import-open-api.ts, inferred from the discriminator
1249
- breed : {
1250
- type : 'string' ,
1251
- enum : [ 'Labradoodle' ] ,
1252
- } ,
1253
- } ,
1254
- } ,
1255
- } ,
1256
- } ,
1257
- } ,
1258
- } ,
1259
- output : {
1260
- override : {
1261
- zod : {
1262
- generateEachHttpStatus : false ,
1263
- } ,
1264
- } ,
1265
- } ,
1266
- } ,
1267
- } as const ;
1268
-
1269
- describe ( 'generateDiscriminatedUnionZod' , ( ) => {
1270
- it ( 'generates a discriminatedUnion zod schema' , async ( ) => {
1271
- const result = await generateZod (
1272
- {
1273
- pathRoute : '/dogs' ,
1274
- verb : 'get' ,
1275
- operationName : 'test' ,
1276
- override : {
1277
- zod : {
1278
- strict : {
1279
- param : false ,
1280
- body : false ,
1281
- response : false ,
1282
- query : false ,
1283
- header : false ,
1284
- } ,
1285
- generate : {
1286
- param : true ,
1287
- body : true ,
1288
- response : true ,
1289
- query : true ,
1290
- header : true ,
1291
- } ,
1292
- coerce : {
1293
- param : false ,
1294
- body : false ,
1295
- response : false ,
1296
- query : false ,
1297
- header : false ,
1298
- } ,
1299
- } ,
1300
- } ,
1301
- } ,
1302
- apiSchemaWithDiscriminator ,
1303
- { } ,
1304
- ) ;
1305
- expect ( result . implementation ) . toBe (
1306
- `export const testResponseItem = zod.discriminatedUnion('breed', [zod.object({\n "cuteness": zod.number(),\n "breed": zod.enum(['Labradoodle']).optional()\n}),zod.object({\n "length": zod.number(),\n "breed": zod.enum(['Labradoodle']).optional()\n})])\nexport const testResponse = zod.array(testResponseItem)\n\n` ,
1307
- ) ;
1308
- expect ( result . implementation ) . not . toContain ( '.or(zod.object' ) ;
1309
- } ) ;
1310
- it ( 'generated discriminatedUnion zod schema strict setting' , async ( ) => {
1311
- const result = await generateZod (
1312
- {
1313
- pathRoute : '/dogs' ,
1314
- verb : 'get' ,
1315
- operationName : 'test' ,
1316
- override : {
1317
- zod : {
1318
- strict : {
1319
- param : true ,
1320
- body : true ,
1321
- response : true ,
1322
- query : true ,
1323
- header : true ,
1324
- } ,
1325
- generate : {
1326
- param : true ,
1327
- body : true ,
1328
- response : true ,
1329
- query : true ,
1330
- header : true ,
1331
- } ,
1332
- coerce : {
1333
- param : false ,
1334
- body : false ,
1335
- response : false ,
1336
- query : false ,
1337
- header : false ,
1338
- } ,
1339
- } ,
1340
- } ,
1341
- } ,
1342
- apiSchemaWithDiscriminator ,
1343
- { } ,
1344
- ) ;
1345
- expect ( result . implementation ) . toBe (
1346
- `export const testResponseItem = zod.discriminatedUnion('breed', [zod.object({\n "cuteness": zod.number(),\n "breed": zod.enum(['Labradoodle']).optional()\n}).strict(),zod.object({\n "length": zod.number(),\n "breed": zod.enum(['Labradoodle']).optional()\n}).strict()])\nexport const testResponse = zod.array(testResponseItem)\n\n` ,
1347
- ) ;
1348
- } ) ;
1349
- it ( 'does not generate a discriminatedUnion zod schema when discriminator is absent' , async ( ) => {
1350
- const schemaWithoutDiscriminator = { ...apiSchemaWithDiscriminator } ;
1351
- const dogSchema = schemaWithoutDiscriminator . context . specs [ 'dog' ]
1352
- . components ! . schemas ! [ 'Dog' ] as SchemaObject ;
1353
- delete dogSchema . discriminator ;
1354
- const result = await generateZod (
1355
- {
1356
- pathRoute : '/dogs' ,
1357
- verb : 'get' ,
1358
- operationName : 'test' ,
1359
- override : {
1360
- zod : {
1361
- strict : {
1362
- param : false ,
1363
- body : false ,
1364
- response : false ,
1365
- query : false ,
1366
- header : false ,
1367
- } ,
1368
- generate : {
1369
- param : true ,
1370
- body : true ,
1371
- response : true ,
1372
- query : true ,
1373
- header : true ,
1374
- } ,
1375
- coerce : {
1376
- param : false ,
1377
- body : false ,
1378
- response : false ,
1379
- query : false ,
1380
- header : false ,
1381
- } ,
1382
- } ,
1383
- } ,
1384
- } ,
1385
- apiSchemaWithDiscriminator ,
1386
- { } ,
1387
- ) ;
1388
- expect ( result . implementation ) . toBe (
1389
- `export const testResponseItem = zod.object({\n "cuteness": zod.number(),\n "breed": zod.enum(['Labradoodle']).optional()\n}).or(zod.object({\n "length": zod.number(),\n "breed": zod.enum(['Labradoodle']).optional()\n}))\nexport const testResponse = zod.array(testResponseItem)\n\n` ,
1390
- ) ;
1391
- expect ( result . implementation ) . not . toContain (
1392
- "zod.discriminatedUnion('breed'" ,
1393
- ) ;
1394
- } ) ;
1395
- } ) ;
1396
-
1397
1163
describe ( 'parsePrefixItemsArrayAsTupleZod' , ( ) => {
1398
1164
it ( 'generates correctly' , async ( ) => {
1399
1165
const arrayWithPrefixItemsSchema : SchemaObject31 = {
0 commit comments