next-openapi-gen 1.4.0 → 1.4.2

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/dist/cli.js CHANGED
@@ -2868,23 +2868,58 @@ var SymbolResolver = class {
2868
2868
  }
2869
2869
  /**
2870
2870
  * Returns a simple literal value (string/number/boolean/null) for a `const` declarator,
2871
- * or `null` if no such declarator exists.
2871
+ * following imports and re-exports when the name is not declared locally.
2872
2872
  */
2873
2873
  resolveLiteral(filePath, name) {
2874
+ const visited = /* @__PURE__ */ new Set();
2875
+ return this.resolveLiteralInternal(filePath, name, visited);
2876
+ }
2877
+ resolveLiteralInternal(filePath, name, visited) {
2878
+ if (visited.has(filePath))
2879
+ return void 0;
2880
+ visited.add(filePath);
2874
2881
  const index = this.getIndex(filePath);
2875
2882
  if (!index)
2876
2883
  return void 0;
2877
2884
  const literal2 = index.constLiterals.get(name);
2878
- if (!literal2)
2879
- return void 0;
2880
- if (t3.isStringLiteral(literal2))
2881
- return literal2.value;
2882
- if (t3.isNumericLiteral(literal2))
2883
- return literal2.value;
2884
- if (t3.isBooleanLiteral(literal2))
2885
- return literal2.value;
2886
- if (t3.isNullLiteral(literal2))
2887
- return null;
2885
+ if (literal2) {
2886
+ if (t3.isStringLiteral(literal2))
2887
+ return literal2.value;
2888
+ if (t3.isNumericLiteral(literal2))
2889
+ return literal2.value;
2890
+ if (t3.isBooleanLiteral(literal2))
2891
+ return literal2.value;
2892
+ if (t3.isNullLiteral(literal2))
2893
+ return null;
2894
+ }
2895
+ const imports = this.getImports(filePath);
2896
+ const importInfo = imports?.get(name);
2897
+ if (importInfo) {
2898
+ const resolved = this.resolveImportPath(filePath, importInfo.source);
2899
+ if (resolved) {
2900
+ const targetName = importInfo.importedName === "default" ? name : importInfo.importedName;
2901
+ const result = this.resolveLiteralInternal(resolved, targetName, visited);
2902
+ if (result !== void 0)
2903
+ return result;
2904
+ }
2905
+ }
2906
+ const reExport = index.namedReExports.get(name);
2907
+ if (reExport) {
2908
+ const resolved = this.resolveImportPath(filePath, reExport.source);
2909
+ if (resolved) {
2910
+ const result = this.resolveLiteralInternal(resolved, reExport.importedName, visited);
2911
+ if (result !== void 0)
2912
+ return result;
2913
+ }
2914
+ }
2915
+ for (const starSrc of index.exportsStar) {
2916
+ const resolved = this.resolveImportPath(filePath, starSrc);
2917
+ if (!resolved)
2918
+ continue;
2919
+ const result = this.resolveLiteralInternal(resolved, name, visited);
2920
+ if (result !== void 0)
2921
+ return result;
2922
+ }
2888
2923
  return void 0;
2889
2924
  }
2890
2925
  /**
@@ -13207,6 +13242,58 @@ var ZodSchemaConverter = class {
13207
13242
  this.symbolResolver.primeAST(this.currentFilePath, this.currentAST);
13208
13243
  return this.symbolResolver.resolveLiteral(this.currentFilePath, name);
13209
13244
  }
13245
+ /**
13246
+ * Unwrap a possible `TSAsExpression` / `TSSatisfiesExpression` to get the
13247
+ * underlying expression node. Returns the node itself when no wrapper is
13248
+ * present.
13249
+ */
13250
+ unwrapTypeAssertion(node) {
13251
+ if (!node)
13252
+ return void 0;
13253
+ if (t10.isTSAsExpression(node) || t10.isTSSatisfiesExpression(node)) {
13254
+ return node.expression;
13255
+ }
13256
+ return node;
13257
+ }
13258
+ /**
13259
+ * Resolve a numeric value from a call-expression argument.
13260
+ * Handles: numeric literals, identifier references to const numbers,
13261
+ * and `x as number` / `x satisfies number` wrappers around either.
13262
+ */
13263
+ resolveNumericArg(arg) {
13264
+ if (!arg)
13265
+ return void 0;
13266
+ const node = this.unwrapTypeAssertion(arg);
13267
+ if (t10.isNumericLiteral(node))
13268
+ return node.value;
13269
+ if (t10.isUnaryExpression(node) && node.operator === "-" && t10.isNumericLiteral(node.argument)) {
13270
+ return -node.argument.value;
13271
+ }
13272
+ if (t10.isIdentifier(node)) {
13273
+ const val = this.resolveLiteralValue(node.name);
13274
+ if (typeof val === "number")
13275
+ return val;
13276
+ }
13277
+ return void 0;
13278
+ }
13279
+ /**
13280
+ * Resolve a string value from a call-expression argument.
13281
+ * Handles: string literals, identifier references to const strings,
13282
+ * and `x as string` / `x satisfies string` wrappers around either.
13283
+ */
13284
+ resolveStringArg(arg) {
13285
+ if (!arg)
13286
+ return void 0;
13287
+ const node = this.unwrapTypeAssertion(arg);
13288
+ if (t10.isStringLiteral(node))
13289
+ return node.value;
13290
+ if (t10.isIdentifier(node)) {
13291
+ const val = this.resolveLiteralValue(node.name);
13292
+ if (typeof val === "string")
13293
+ return val;
13294
+ }
13295
+ return void 0;
13296
+ }
13210
13297
  /**
13211
13298
  * Resolve an identifier referring to a `z.object({...})` (or similar) call expression.
13212
13299
  * This lets callers inline the referenced object's shape.
@@ -13275,6 +13362,14 @@ var ZodSchemaConverter = class {
13275
13362
  if (t10.isNullLiteral(node)) {
13276
13363
  return null;
13277
13364
  }
13365
+ if (t10.isIdentifier(node)) {
13366
+ const val = this.resolveLiteralValue(node.name);
13367
+ if (val !== void 0)
13368
+ return val;
13369
+ }
13370
+ if (t10.isTSAsExpression(node) || t10.isTSSatisfiesExpression(node)) {
13371
+ return this.extractStaticJsonValue(node.expression);
13372
+ }
13278
13373
  if (t10.isArrayExpression(node)) {
13279
13374
  const values = [];
13280
13375
  for (const element of node.elements) {
@@ -13337,7 +13432,7 @@ var ZodSchemaConverter = class {
13337
13432
  return helperName.startsWith("coerce.") || helperName === "templateLiteral" || helperName === "stringbool";
13338
13433
  }
13339
13434
  if (t10.isMemberExpression(node.callee) && t10.isIdentifier(node.callee.property)) {
13340
- const runtimeMethods = /* @__PURE__ */ new Set(["pipe", "meta"]);
13435
+ const runtimeMethods = /* @__PURE__ */ new Set(["pipe"]);
13341
13436
  if (runtimeMethods.has(node.callee.property.name)) {
13342
13437
  return true;
13343
13438
  }
@@ -13373,53 +13468,60 @@ var ZodSchemaConverter = class {
13373
13468
  schema.nullable = true;
13374
13469
  }
13375
13470
  break;
13376
- case "describe":
13377
- if (node.arguments.length > 0 && t10.isStringLiteral(node.arguments[0])) {
13378
- const description = node.arguments[0].value;
13379
- if (description.startsWith("@deprecated")) {
13471
+ case "describe": {
13472
+ const descVal = this.resolveStringArg(node.arguments[0]);
13473
+ if (descVal !== void 0) {
13474
+ if (descVal.startsWith("@deprecated")) {
13380
13475
  schema.deprecated = true;
13381
- schema.description = description.replace("@deprecated", "").trim();
13476
+ schema.description = descVal.replace("@deprecated", "").trim();
13382
13477
  } else {
13383
- schema.description = description;
13478
+ schema.description = descVal;
13384
13479
  }
13385
13480
  }
13386
13481
  break;
13482
+ }
13387
13483
  case "deprecated":
13388
13484
  schema.deprecated = true;
13389
13485
  break;
13390
- case "min":
13391
- if (node.arguments.length > 0 && t10.isNumericLiteral(node.arguments[0])) {
13486
+ case "min": {
13487
+ const minVal = this.resolveNumericArg(node.arguments[0]);
13488
+ if (minVal !== void 0) {
13392
13489
  if (schema.type === "string") {
13393
- schema.minLength = node.arguments[0].value;
13490
+ schema.minLength = minVal;
13394
13491
  } else if (schema.type === "number" || schema.type === "integer") {
13395
- schema.minimum = node.arguments[0].value;
13492
+ schema.minimum = minVal;
13396
13493
  } else if (schema.type === "array") {
13397
- schema.minItems = node.arguments[0].value;
13494
+ schema.minItems = minVal;
13398
13495
  }
13399
13496
  }
13400
13497
  break;
13401
- case "max":
13402
- if (node.arguments.length > 0 && t10.isNumericLiteral(node.arguments[0])) {
13498
+ }
13499
+ case "max": {
13500
+ const maxVal = this.resolveNumericArg(node.arguments[0]);
13501
+ if (maxVal !== void 0) {
13403
13502
  if (schema.type === "string") {
13404
- schema.maxLength = node.arguments[0].value;
13503
+ schema.maxLength = maxVal;
13405
13504
  } else if (schema.type === "number" || schema.type === "integer") {
13406
- schema.maximum = node.arguments[0].value;
13505
+ schema.maximum = maxVal;
13407
13506
  } else if (schema.type === "array") {
13408
- schema.maxItems = node.arguments[0].value;
13507
+ schema.maxItems = maxVal;
13409
13508
  }
13410
13509
  }
13411
13510
  break;
13412
- case "length":
13413
- if (node.arguments.length > 0 && t10.isNumericLiteral(node.arguments[0])) {
13511
+ }
13512
+ case "length": {
13513
+ const lenVal = this.resolveNumericArg(node.arguments[0]);
13514
+ if (lenVal !== void 0) {
13414
13515
  if (schema.type === "string") {
13415
- schema.minLength = node.arguments[0].value;
13416
- schema.maxLength = node.arguments[0].value;
13516
+ schema.minLength = lenVal;
13517
+ schema.maxLength = lenVal;
13417
13518
  } else if (schema.type === "array") {
13418
- schema.minItems = node.arguments[0].value;
13419
- schema.maxItems = node.arguments[0].value;
13519
+ schema.minItems = lenVal;
13520
+ schema.maxItems = lenVal;
13420
13521
  }
13421
13522
  }
13422
13523
  break;
13524
+ }
13423
13525
  case "nonempty":
13424
13526
  if (schema.type === "array") {
13425
13527
  schema.minItems = Math.max(schema.minItems ?? 0, 1);
@@ -13495,34 +13597,38 @@ var ZodSchemaConverter = class {
13495
13597
  schema.pattern = node.arguments[0].pattern;
13496
13598
  }
13497
13599
  break;
13498
- case "startsWith":
13499
- if (node.arguments.length > 0 && t10.isStringLiteral(node.arguments[0])) {
13500
- schema.pattern = `^${this.escapeRegExp(node.arguments[0].value)}`;
13600
+ case "startsWith": {
13601
+ const swVal = this.resolveStringArg(node.arguments[0]);
13602
+ if (swVal !== void 0) {
13603
+ schema.pattern = `^${this.escapeRegExp(swVal)}`;
13501
13604
  }
13502
13605
  break;
13503
- case "endsWith":
13504
- if (node.arguments.length > 0 && t10.isStringLiteral(node.arguments[0])) {
13505
- schema.pattern = `${this.escapeRegExp(node.arguments[0].value)}$`;
13606
+ }
13607
+ case "endsWith": {
13608
+ const ewVal = this.resolveStringArg(node.arguments[0]);
13609
+ if (ewVal !== void 0) {
13610
+ schema.pattern = `${this.escapeRegExp(ewVal)}$`;
13506
13611
  }
13507
13612
  break;
13508
- case "includes":
13509
- if (node.arguments.length > 0 && t10.isStringLiteral(node.arguments[0])) {
13510
- schema.pattern = this.escapeRegExp(node.arguments[0].value);
13613
+ }
13614
+ case "includes": {
13615
+ const incVal = this.resolveStringArg(node.arguments[0]);
13616
+ if (incVal !== void 0) {
13617
+ schema.pattern = this.escapeRegExp(incVal);
13511
13618
  }
13512
13619
  break;
13620
+ }
13513
13621
  case "int":
13514
13622
  schema.type = "integer";
13515
13623
  break;
13516
13624
  case "positive":
13517
- schema.minimum = 0;
13518
- schema.exclusiveMinimum = true;
13625
+ schema.exclusiveMinimum = 0;
13519
13626
  break;
13520
13627
  case "nonnegative":
13521
13628
  schema.minimum = 0;
13522
13629
  break;
13523
13630
  case "negative":
13524
- schema.maximum = 0;
13525
- schema.exclusiveMaximum = true;
13631
+ schema.exclusiveMaximum = 0;
13526
13632
  break;
13527
13633
  case "nonpositive":
13528
13634
  schema.maximum = 0;
@@ -13535,17 +13641,22 @@ var ZodSchemaConverter = class {
13535
13641
  break;
13536
13642
  case "default":
13537
13643
  if (node.arguments.length > 0) {
13538
- if (t10.isStringLiteral(node.arguments[0])) {
13539
- schema.default = node.arguments[0].value;
13540
- } else if (t10.isNumericLiteral(node.arguments[0])) {
13541
- schema.default = node.arguments[0].value;
13542
- } else if (t10.isBooleanLiteral(node.arguments[0])) {
13543
- schema.default = node.arguments[0].value;
13544
- } else if (t10.isNullLiteral(node.arguments[0])) {
13644
+ const defaultArg = this.unwrapTypeAssertion(node.arguments[0]);
13645
+ if (t10.isStringLiteral(defaultArg)) {
13646
+ schema.default = defaultArg.value;
13647
+ } else if (t10.isNumericLiteral(defaultArg)) {
13648
+ schema.default = defaultArg.value;
13649
+ } else if (t10.isBooleanLiteral(defaultArg)) {
13650
+ schema.default = defaultArg.value;
13651
+ } else if (t10.isNullLiteral(defaultArg)) {
13545
13652
  schema.default = null;
13546
- } else if (t10.isObjectExpression(node.arguments[0])) {
13653
+ } else if (t10.isIdentifier(defaultArg)) {
13654
+ const val = this.resolveLiteralValue(defaultArg.name);
13655
+ if (val !== void 0)
13656
+ schema.default = val;
13657
+ } else if (t10.isObjectExpression(defaultArg)) {
13547
13658
  const defaultObj = {};
13548
- node.arguments[0].properties.forEach((prop) => {
13659
+ defaultArg.properties.forEach((prop) => {
13549
13660
  if (t10.isObjectProperty(prop) && (t10.isIdentifier(prop.key) || t10.isStringLiteral(prop.key)) && (t10.isStringLiteral(prop.value) || t10.isNumericLiteral(prop.value) || t10.isBooleanLiteral(prop.value))) {
13550
13661
  const key = t10.isIdentifier(prop.key) ? prop.key.name : prop.key.value;
13551
13662
  const value = t10.isStringLiteral(prop.value) ? prop.value.value : t10.isNumericLiteral(prop.value) ? prop.value.value : t10.isBooleanLiteral(prop.value) ? prop.value.value : null;
@@ -17378,6 +17489,7 @@ function applyExcludeSchemas(document, mergedSchemas, excludedSchemas) {
17378
17489
  if (excludedNames.size === 0)
17379
17490
  return;
17380
17491
  walkAndInline(document, excludedSchemas, excludedNames, /* @__PURE__ */ new Set());
17492
+ walkAndInline(mergedSchemas, excludedSchemas, excludedNames, /* @__PURE__ */ new Set());
17381
17493
  for (const name of excludedNames) {
17382
17494
  delete mergedSchemas[name];
17383
17495
  }
@@ -18157,96 +18269,100 @@ function typeToOpenApiSchema(type, checker, seen) {
18157
18269
  return { type: "object" };
18158
18270
  }
18159
18271
  seen.add(seenKey);
18160
- if (type.isStringLiteral()) {
18161
- return { type: "string", enum: [type.value] };
18162
- }
18163
- if (type.isNumberLiteral()) {
18164
- return { type: "number", enum: [type.value] };
18165
- }
18166
- if (type.flags & ts3.TypeFlags.BooleanLiteral) {
18167
- return {
18168
- type: "boolean",
18169
- enum: [checker.typeToString(type) === "true"]
18170
- };
18171
- }
18172
- if (type.flags & ts3.TypeFlags.TemplateLiteral) {
18173
- return { type: "string" };
18174
- }
18175
- if (type.flags & ts3.TypeFlags.StringLike) {
18176
- return { type: "string" };
18177
- }
18178
- if (type.flags & ts3.TypeFlags.NumberLike) {
18179
- return { type: "number" };
18180
- }
18181
- if (type.flags & ts3.TypeFlags.BooleanLike) {
18182
- return { type: "boolean" };
18183
- }
18184
- if (type.flags & ts3.TypeFlags.Null) {
18185
- return { type: "null" };
18186
- }
18187
- if (type.isUnion()) {
18188
- const nullable2 = type.types.some((member) => member.flags & ts3.TypeFlags.Null);
18189
- const nonNullTypes = type.types.filter((member) => !(member.flags & ts3.TypeFlags.Null));
18190
- const soleNonNullType = nonNullTypes[0];
18191
- if (nullable2 && soleNonNullType && nonNullTypes.length === 1) {
18272
+ try {
18273
+ if (type.isStringLiteral()) {
18274
+ return { type: "string", enum: [type.value] };
18275
+ }
18276
+ if (type.isNumberLiteral()) {
18277
+ return { type: "number", enum: [type.value] };
18278
+ }
18279
+ if (type.flags & ts3.TypeFlags.BooleanLiteral) {
18192
18280
  return {
18193
- ...typeToOpenApiSchema(soleNonNullType, checker, seen),
18194
- nullable: true
18281
+ type: "boolean",
18282
+ enum: [checker.typeToString(type) === "true"]
18195
18283
  };
18196
18284
  }
18197
- return {
18198
- oneOf: nonNullTypes.map((member) => typeToOpenApiSchema(member, checker, seen))
18199
- };
18200
- }
18201
- if (checker.isTupleType(type)) {
18202
- const itemTypes = checker.getTypeArguments(type);
18203
- return {
18204
- type: "array",
18205
- prefixItems: itemTypes.map((itemType) => typeToOpenApiSchema(itemType, checker, seen)),
18206
- items: false,
18207
- minItems: itemTypes.length,
18208
- maxItems: itemTypes.length
18209
- };
18210
- }
18211
- if (checker.isArrayType(type)) {
18212
- const elementType = checker.getTypeArguments(type)[0];
18213
- return {
18214
- type: "array",
18215
- items: elementType ? typeToOpenApiSchema(elementType, checker, seen) : { type: "object" }
18216
- };
18217
- }
18218
- const properties = checker.getPropertiesOfType(type);
18219
- if (properties.length > 0) {
18220
- const schemaProperties = {};
18221
- const required2 = [];
18222
- properties.forEach((property) => {
18223
- const propertyDeclaration = property.valueDeclaration || property.declarations?.[0];
18224
- if (!propertyDeclaration) {
18225
- return;
18226
- }
18227
- const propertyType = checker.getTypeOfSymbolAtLocation(property, propertyDeclaration);
18228
- schemaProperties[property.getName()] = typeToOpenApiSchema(propertyType, checker, seen);
18229
- if (!(property.flags & ts3.SymbolFlags.Optional)) {
18230
- required2.push(property.getName());
18285
+ if (type.flags & ts3.TypeFlags.TemplateLiteral) {
18286
+ return { type: "string" };
18287
+ }
18288
+ if (type.flags & ts3.TypeFlags.StringLike) {
18289
+ return { type: "string" };
18290
+ }
18291
+ if (type.flags & ts3.TypeFlags.NumberLike) {
18292
+ return { type: "number" };
18293
+ }
18294
+ if (type.flags & ts3.TypeFlags.BooleanLike) {
18295
+ return { type: "boolean" };
18296
+ }
18297
+ if (type.flags & ts3.TypeFlags.Null) {
18298
+ return { type: "null" };
18299
+ }
18300
+ if (type.isUnion()) {
18301
+ const nullable2 = type.types.some((member) => member.flags & ts3.TypeFlags.Null);
18302
+ const nonNullTypes = type.types.filter((member) => !(member.flags & ts3.TypeFlags.Null));
18303
+ const soleNonNullType = nonNullTypes[0];
18304
+ if (nullable2 && soleNonNullType && nonNullTypes.length === 1) {
18305
+ return {
18306
+ ...typeToOpenApiSchema(soleNonNullType, checker, seen),
18307
+ nullable: true
18308
+ };
18231
18309
  }
18232
- });
18233
- return required2.length > 0 ? {
18234
- type: "object",
18235
- properties: schemaProperties,
18236
- required: required2
18237
- } : {
18238
- type: "object",
18239
- properties: schemaProperties
18240
- };
18241
- }
18242
- const stringIndexType = type.getStringIndexType();
18243
- if (stringIndexType) {
18244
- return {
18245
- type: "object",
18246
- additionalProperties: typeToOpenApiSchema(stringIndexType, checker, seen)
18247
- };
18310
+ return {
18311
+ oneOf: nonNullTypes.map((member) => typeToOpenApiSchema(member, checker, seen))
18312
+ };
18313
+ }
18314
+ if (checker.isTupleType(type)) {
18315
+ const itemTypes = checker.getTypeArguments(type);
18316
+ return {
18317
+ type: "array",
18318
+ prefixItems: itemTypes.map((itemType) => typeToOpenApiSchema(itemType, checker, seen)),
18319
+ items: false,
18320
+ minItems: itemTypes.length,
18321
+ maxItems: itemTypes.length
18322
+ };
18323
+ }
18324
+ if (checker.isArrayType(type)) {
18325
+ const elementType = checker.getTypeArguments(type)[0];
18326
+ return {
18327
+ type: "array",
18328
+ items: elementType ? typeToOpenApiSchema(elementType, checker, seen) : { type: "object" }
18329
+ };
18330
+ }
18331
+ const properties = checker.getPropertiesOfType(type);
18332
+ if (properties.length > 0) {
18333
+ const schemaProperties = {};
18334
+ const required2 = [];
18335
+ properties.forEach((property) => {
18336
+ const propertyDeclaration = property.valueDeclaration || property.declarations?.[0];
18337
+ if (!propertyDeclaration) {
18338
+ return;
18339
+ }
18340
+ const propertyType = checker.getTypeOfSymbolAtLocation(property, propertyDeclaration);
18341
+ schemaProperties[property.getName()] = typeToOpenApiSchema(propertyType, checker, seen);
18342
+ if (!(property.flags & ts3.SymbolFlags.Optional)) {
18343
+ required2.push(property.getName());
18344
+ }
18345
+ });
18346
+ return required2.length > 0 ? {
18347
+ type: "object",
18348
+ properties: schemaProperties,
18349
+ required: required2
18350
+ } : {
18351
+ type: "object",
18352
+ properties: schemaProperties
18353
+ };
18354
+ }
18355
+ const stringIndexType = type.getStringIndexType();
18356
+ if (stringIndexType) {
18357
+ return {
18358
+ type: "object",
18359
+ additionalProperties: typeToOpenApiSchema(stringIndexType, checker, seen)
18360
+ };
18361
+ }
18362
+ return { type: "object" };
18363
+ } finally {
18364
+ seen.delete(seenKey);
18248
18365
  }
18249
- return { type: "object" };
18250
18366
  }
18251
18367
  function unwrapPromiseType(type, checker) {
18252
18368
  const symbolName = type.getSymbol()?.getName();