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/index.js CHANGED
@@ -2417,23 +2417,58 @@ var SymbolResolver = class {
2417
2417
  }
2418
2418
  /**
2419
2419
  * Returns a simple literal value (string/number/boolean/null) for a `const` declarator,
2420
- * or `null` if no such declarator exists.
2420
+ * following imports and re-exports when the name is not declared locally.
2421
2421
  */
2422
2422
  resolveLiteral(filePath, name) {
2423
+ const visited = /* @__PURE__ */ new Set();
2424
+ return this.resolveLiteralInternal(filePath, name, visited);
2425
+ }
2426
+ resolveLiteralInternal(filePath, name, visited) {
2427
+ if (visited.has(filePath))
2428
+ return void 0;
2429
+ visited.add(filePath);
2423
2430
  const index = this.getIndex(filePath);
2424
2431
  if (!index)
2425
2432
  return void 0;
2426
2433
  const literal2 = index.constLiterals.get(name);
2427
- if (!literal2)
2428
- return void 0;
2429
- if (t3.isStringLiteral(literal2))
2430
- return literal2.value;
2431
- if (t3.isNumericLiteral(literal2))
2432
- return literal2.value;
2433
- if (t3.isBooleanLiteral(literal2))
2434
- return literal2.value;
2435
- if (t3.isNullLiteral(literal2))
2436
- return null;
2434
+ if (literal2) {
2435
+ if (t3.isStringLiteral(literal2))
2436
+ return literal2.value;
2437
+ if (t3.isNumericLiteral(literal2))
2438
+ return literal2.value;
2439
+ if (t3.isBooleanLiteral(literal2))
2440
+ return literal2.value;
2441
+ if (t3.isNullLiteral(literal2))
2442
+ return null;
2443
+ }
2444
+ const imports = this.getImports(filePath);
2445
+ const importInfo = imports?.get(name);
2446
+ if (importInfo) {
2447
+ const resolved = this.resolveImportPath(filePath, importInfo.source);
2448
+ if (resolved) {
2449
+ const targetName = importInfo.importedName === "default" ? name : importInfo.importedName;
2450
+ const result = this.resolveLiteralInternal(resolved, targetName, visited);
2451
+ if (result !== void 0)
2452
+ return result;
2453
+ }
2454
+ }
2455
+ const reExport = index.namedReExports.get(name);
2456
+ if (reExport) {
2457
+ const resolved = this.resolveImportPath(filePath, reExport.source);
2458
+ if (resolved) {
2459
+ const result = this.resolveLiteralInternal(resolved, reExport.importedName, visited);
2460
+ if (result !== void 0)
2461
+ return result;
2462
+ }
2463
+ }
2464
+ for (const starSrc of index.exportsStar) {
2465
+ const resolved = this.resolveImportPath(filePath, starSrc);
2466
+ if (!resolved)
2467
+ continue;
2468
+ const result = this.resolveLiteralInternal(resolved, name, visited);
2469
+ if (result !== void 0)
2470
+ return result;
2471
+ }
2437
2472
  return void 0;
2438
2473
  }
2439
2474
  /**
@@ -12756,6 +12791,58 @@ var ZodSchemaConverter = class {
12756
12791
  this.symbolResolver.primeAST(this.currentFilePath, this.currentAST);
12757
12792
  return this.symbolResolver.resolveLiteral(this.currentFilePath, name);
12758
12793
  }
12794
+ /**
12795
+ * Unwrap a possible `TSAsExpression` / `TSSatisfiesExpression` to get the
12796
+ * underlying expression node. Returns the node itself when no wrapper is
12797
+ * present.
12798
+ */
12799
+ unwrapTypeAssertion(node) {
12800
+ if (!node)
12801
+ return void 0;
12802
+ if (t10.isTSAsExpression(node) || t10.isTSSatisfiesExpression(node)) {
12803
+ return node.expression;
12804
+ }
12805
+ return node;
12806
+ }
12807
+ /**
12808
+ * Resolve a numeric value from a call-expression argument.
12809
+ * Handles: numeric literals, identifier references to const numbers,
12810
+ * and `x as number` / `x satisfies number` wrappers around either.
12811
+ */
12812
+ resolveNumericArg(arg) {
12813
+ if (!arg)
12814
+ return void 0;
12815
+ const node = this.unwrapTypeAssertion(arg);
12816
+ if (t10.isNumericLiteral(node))
12817
+ return node.value;
12818
+ if (t10.isUnaryExpression(node) && node.operator === "-" && t10.isNumericLiteral(node.argument)) {
12819
+ return -node.argument.value;
12820
+ }
12821
+ if (t10.isIdentifier(node)) {
12822
+ const val = this.resolveLiteralValue(node.name);
12823
+ if (typeof val === "number")
12824
+ return val;
12825
+ }
12826
+ return void 0;
12827
+ }
12828
+ /**
12829
+ * Resolve a string value from a call-expression argument.
12830
+ * Handles: string literals, identifier references to const strings,
12831
+ * and `x as string` / `x satisfies string` wrappers around either.
12832
+ */
12833
+ resolveStringArg(arg) {
12834
+ if (!arg)
12835
+ return void 0;
12836
+ const node = this.unwrapTypeAssertion(arg);
12837
+ if (t10.isStringLiteral(node))
12838
+ return node.value;
12839
+ if (t10.isIdentifier(node)) {
12840
+ const val = this.resolveLiteralValue(node.name);
12841
+ if (typeof val === "string")
12842
+ return val;
12843
+ }
12844
+ return void 0;
12845
+ }
12759
12846
  /**
12760
12847
  * Resolve an identifier referring to a `z.object({...})` (or similar) call expression.
12761
12848
  * This lets callers inline the referenced object's shape.
@@ -12824,6 +12911,14 @@ var ZodSchemaConverter = class {
12824
12911
  if (t10.isNullLiteral(node)) {
12825
12912
  return null;
12826
12913
  }
12914
+ if (t10.isIdentifier(node)) {
12915
+ const val = this.resolveLiteralValue(node.name);
12916
+ if (val !== void 0)
12917
+ return val;
12918
+ }
12919
+ if (t10.isTSAsExpression(node) || t10.isTSSatisfiesExpression(node)) {
12920
+ return this.extractStaticJsonValue(node.expression);
12921
+ }
12827
12922
  if (t10.isArrayExpression(node)) {
12828
12923
  const values = [];
12829
12924
  for (const element of node.elements) {
@@ -12886,7 +12981,7 @@ var ZodSchemaConverter = class {
12886
12981
  return helperName.startsWith("coerce.") || helperName === "templateLiteral" || helperName === "stringbool";
12887
12982
  }
12888
12983
  if (t10.isMemberExpression(node.callee) && t10.isIdentifier(node.callee.property)) {
12889
- const runtimeMethods = /* @__PURE__ */ new Set(["pipe", "meta"]);
12984
+ const runtimeMethods = /* @__PURE__ */ new Set(["pipe"]);
12890
12985
  if (runtimeMethods.has(node.callee.property.name)) {
12891
12986
  return true;
12892
12987
  }
@@ -12922,53 +13017,60 @@ var ZodSchemaConverter = class {
12922
13017
  schema.nullable = true;
12923
13018
  }
12924
13019
  break;
12925
- case "describe":
12926
- if (node.arguments.length > 0 && t10.isStringLiteral(node.arguments[0])) {
12927
- const description = node.arguments[0].value;
12928
- if (description.startsWith("@deprecated")) {
13020
+ case "describe": {
13021
+ const descVal = this.resolveStringArg(node.arguments[0]);
13022
+ if (descVal !== void 0) {
13023
+ if (descVal.startsWith("@deprecated")) {
12929
13024
  schema.deprecated = true;
12930
- schema.description = description.replace("@deprecated", "").trim();
13025
+ schema.description = descVal.replace("@deprecated", "").trim();
12931
13026
  } else {
12932
- schema.description = description;
13027
+ schema.description = descVal;
12933
13028
  }
12934
13029
  }
12935
13030
  break;
13031
+ }
12936
13032
  case "deprecated":
12937
13033
  schema.deprecated = true;
12938
13034
  break;
12939
- case "min":
12940
- if (node.arguments.length > 0 && t10.isNumericLiteral(node.arguments[0])) {
13035
+ case "min": {
13036
+ const minVal = this.resolveNumericArg(node.arguments[0]);
13037
+ if (minVal !== void 0) {
12941
13038
  if (schema.type === "string") {
12942
- schema.minLength = node.arguments[0].value;
13039
+ schema.minLength = minVal;
12943
13040
  } else if (schema.type === "number" || schema.type === "integer") {
12944
- schema.minimum = node.arguments[0].value;
13041
+ schema.minimum = minVal;
12945
13042
  } else if (schema.type === "array") {
12946
- schema.minItems = node.arguments[0].value;
13043
+ schema.minItems = minVal;
12947
13044
  }
12948
13045
  }
12949
13046
  break;
12950
- case "max":
12951
- if (node.arguments.length > 0 && t10.isNumericLiteral(node.arguments[0])) {
13047
+ }
13048
+ case "max": {
13049
+ const maxVal = this.resolveNumericArg(node.arguments[0]);
13050
+ if (maxVal !== void 0) {
12952
13051
  if (schema.type === "string") {
12953
- schema.maxLength = node.arguments[0].value;
13052
+ schema.maxLength = maxVal;
12954
13053
  } else if (schema.type === "number" || schema.type === "integer") {
12955
- schema.maximum = node.arguments[0].value;
13054
+ schema.maximum = maxVal;
12956
13055
  } else if (schema.type === "array") {
12957
- schema.maxItems = node.arguments[0].value;
13056
+ schema.maxItems = maxVal;
12958
13057
  }
12959
13058
  }
12960
13059
  break;
12961
- case "length":
12962
- if (node.arguments.length > 0 && t10.isNumericLiteral(node.arguments[0])) {
13060
+ }
13061
+ case "length": {
13062
+ const lenVal = this.resolveNumericArg(node.arguments[0]);
13063
+ if (lenVal !== void 0) {
12963
13064
  if (schema.type === "string") {
12964
- schema.minLength = node.arguments[0].value;
12965
- schema.maxLength = node.arguments[0].value;
13065
+ schema.minLength = lenVal;
13066
+ schema.maxLength = lenVal;
12966
13067
  } else if (schema.type === "array") {
12967
- schema.minItems = node.arguments[0].value;
12968
- schema.maxItems = node.arguments[0].value;
13068
+ schema.minItems = lenVal;
13069
+ schema.maxItems = lenVal;
12969
13070
  }
12970
13071
  }
12971
13072
  break;
13073
+ }
12972
13074
  case "nonempty":
12973
13075
  if (schema.type === "array") {
12974
13076
  schema.minItems = Math.max(schema.minItems ?? 0, 1);
@@ -13044,34 +13146,38 @@ var ZodSchemaConverter = class {
13044
13146
  schema.pattern = node.arguments[0].pattern;
13045
13147
  }
13046
13148
  break;
13047
- case "startsWith":
13048
- if (node.arguments.length > 0 && t10.isStringLiteral(node.arguments[0])) {
13049
- schema.pattern = `^${this.escapeRegExp(node.arguments[0].value)}`;
13149
+ case "startsWith": {
13150
+ const swVal = this.resolveStringArg(node.arguments[0]);
13151
+ if (swVal !== void 0) {
13152
+ schema.pattern = `^${this.escapeRegExp(swVal)}`;
13050
13153
  }
13051
13154
  break;
13052
- case "endsWith":
13053
- if (node.arguments.length > 0 && t10.isStringLiteral(node.arguments[0])) {
13054
- schema.pattern = `${this.escapeRegExp(node.arguments[0].value)}$`;
13155
+ }
13156
+ case "endsWith": {
13157
+ const ewVal = this.resolveStringArg(node.arguments[0]);
13158
+ if (ewVal !== void 0) {
13159
+ schema.pattern = `${this.escapeRegExp(ewVal)}$`;
13055
13160
  }
13056
13161
  break;
13057
- case "includes":
13058
- if (node.arguments.length > 0 && t10.isStringLiteral(node.arguments[0])) {
13059
- schema.pattern = this.escapeRegExp(node.arguments[0].value);
13162
+ }
13163
+ case "includes": {
13164
+ const incVal = this.resolveStringArg(node.arguments[0]);
13165
+ if (incVal !== void 0) {
13166
+ schema.pattern = this.escapeRegExp(incVal);
13060
13167
  }
13061
13168
  break;
13169
+ }
13062
13170
  case "int":
13063
13171
  schema.type = "integer";
13064
13172
  break;
13065
13173
  case "positive":
13066
- schema.minimum = 0;
13067
- schema.exclusiveMinimum = true;
13174
+ schema.exclusiveMinimum = 0;
13068
13175
  break;
13069
13176
  case "nonnegative":
13070
13177
  schema.minimum = 0;
13071
13178
  break;
13072
13179
  case "negative":
13073
- schema.maximum = 0;
13074
- schema.exclusiveMaximum = true;
13180
+ schema.exclusiveMaximum = 0;
13075
13181
  break;
13076
13182
  case "nonpositive":
13077
13183
  schema.maximum = 0;
@@ -13084,17 +13190,22 @@ var ZodSchemaConverter = class {
13084
13190
  break;
13085
13191
  case "default":
13086
13192
  if (node.arguments.length > 0) {
13087
- if (t10.isStringLiteral(node.arguments[0])) {
13088
- schema.default = node.arguments[0].value;
13089
- } else if (t10.isNumericLiteral(node.arguments[0])) {
13090
- schema.default = node.arguments[0].value;
13091
- } else if (t10.isBooleanLiteral(node.arguments[0])) {
13092
- schema.default = node.arguments[0].value;
13093
- } else if (t10.isNullLiteral(node.arguments[0])) {
13193
+ const defaultArg = this.unwrapTypeAssertion(node.arguments[0]);
13194
+ if (t10.isStringLiteral(defaultArg)) {
13195
+ schema.default = defaultArg.value;
13196
+ } else if (t10.isNumericLiteral(defaultArg)) {
13197
+ schema.default = defaultArg.value;
13198
+ } else if (t10.isBooleanLiteral(defaultArg)) {
13199
+ schema.default = defaultArg.value;
13200
+ } else if (t10.isNullLiteral(defaultArg)) {
13094
13201
  schema.default = null;
13095
- } else if (t10.isObjectExpression(node.arguments[0])) {
13202
+ } else if (t10.isIdentifier(defaultArg)) {
13203
+ const val = this.resolveLiteralValue(defaultArg.name);
13204
+ if (val !== void 0)
13205
+ schema.default = val;
13206
+ } else if (t10.isObjectExpression(defaultArg)) {
13096
13207
  const defaultObj = {};
13097
- node.arguments[0].properties.forEach((prop) => {
13208
+ defaultArg.properties.forEach((prop) => {
13098
13209
  if (t10.isObjectProperty(prop) && (t10.isIdentifier(prop.key) || t10.isStringLiteral(prop.key)) && (t10.isStringLiteral(prop.value) || t10.isNumericLiteral(prop.value) || t10.isBooleanLiteral(prop.value))) {
13099
13210
  const key = t10.isIdentifier(prop.key) ? prop.key.name : prop.key.value;
13100
13211
  const value = t10.isStringLiteral(prop.value) ? prop.value.value : t10.isNumericLiteral(prop.value) ? prop.value.value : t10.isBooleanLiteral(prop.value) ? prop.value.value : null;
@@ -16927,6 +17038,7 @@ function applyExcludeSchemas(document, mergedSchemas, excludedSchemas) {
16927
17038
  if (excludedNames.size === 0)
16928
17039
  return;
16929
17040
  walkAndInline(document, excludedSchemas, excludedNames, /* @__PURE__ */ new Set());
17041
+ walkAndInline(mergedSchemas, excludedSchemas, excludedNames, /* @__PURE__ */ new Set());
16930
17042
  for (const name of excludedNames) {
16931
17043
  delete mergedSchemas[name];
16932
17044
  }
@@ -17859,96 +17971,100 @@ function typeToOpenApiSchema(type, checker, seen) {
17859
17971
  return { type: "object" };
17860
17972
  }
17861
17973
  seen.add(seenKey);
17862
- if (type.isStringLiteral()) {
17863
- return { type: "string", enum: [type.value] };
17864
- }
17865
- if (type.isNumberLiteral()) {
17866
- return { type: "number", enum: [type.value] };
17867
- }
17868
- if (type.flags & ts3.TypeFlags.BooleanLiteral) {
17869
- return {
17870
- type: "boolean",
17871
- enum: [checker.typeToString(type) === "true"]
17872
- };
17873
- }
17874
- if (type.flags & ts3.TypeFlags.TemplateLiteral) {
17875
- return { type: "string" };
17876
- }
17877
- if (type.flags & ts3.TypeFlags.StringLike) {
17878
- return { type: "string" };
17879
- }
17880
- if (type.flags & ts3.TypeFlags.NumberLike) {
17881
- return { type: "number" };
17882
- }
17883
- if (type.flags & ts3.TypeFlags.BooleanLike) {
17884
- return { type: "boolean" };
17885
- }
17886
- if (type.flags & ts3.TypeFlags.Null) {
17887
- return { type: "null" };
17888
- }
17889
- if (type.isUnion()) {
17890
- const nullable2 = type.types.some((member) => member.flags & ts3.TypeFlags.Null);
17891
- const nonNullTypes = type.types.filter((member) => !(member.flags & ts3.TypeFlags.Null));
17892
- const soleNonNullType = nonNullTypes[0];
17893
- if (nullable2 && soleNonNullType && nonNullTypes.length === 1) {
17974
+ try {
17975
+ if (type.isStringLiteral()) {
17976
+ return { type: "string", enum: [type.value] };
17977
+ }
17978
+ if (type.isNumberLiteral()) {
17979
+ return { type: "number", enum: [type.value] };
17980
+ }
17981
+ if (type.flags & ts3.TypeFlags.BooleanLiteral) {
17894
17982
  return {
17895
- ...typeToOpenApiSchema(soleNonNullType, checker, seen),
17896
- nullable: true
17983
+ type: "boolean",
17984
+ enum: [checker.typeToString(type) === "true"]
17897
17985
  };
17898
17986
  }
17899
- return {
17900
- oneOf: nonNullTypes.map((member) => typeToOpenApiSchema(member, checker, seen))
17901
- };
17902
- }
17903
- if (checker.isTupleType(type)) {
17904
- const itemTypes = checker.getTypeArguments(type);
17905
- return {
17906
- type: "array",
17907
- prefixItems: itemTypes.map((itemType) => typeToOpenApiSchema(itemType, checker, seen)),
17908
- items: false,
17909
- minItems: itemTypes.length,
17910
- maxItems: itemTypes.length
17911
- };
17912
- }
17913
- if (checker.isArrayType(type)) {
17914
- const elementType = checker.getTypeArguments(type)[0];
17915
- return {
17916
- type: "array",
17917
- items: elementType ? typeToOpenApiSchema(elementType, checker, seen) : { type: "object" }
17918
- };
17919
- }
17920
- const properties = checker.getPropertiesOfType(type);
17921
- if (properties.length > 0) {
17922
- const schemaProperties = {};
17923
- const required2 = [];
17924
- properties.forEach((property) => {
17925
- const propertyDeclaration = property.valueDeclaration || property.declarations?.[0];
17926
- if (!propertyDeclaration) {
17927
- return;
17928
- }
17929
- const propertyType = checker.getTypeOfSymbolAtLocation(property, propertyDeclaration);
17930
- schemaProperties[property.getName()] = typeToOpenApiSchema(propertyType, checker, seen);
17931
- if (!(property.flags & ts3.SymbolFlags.Optional)) {
17932
- required2.push(property.getName());
17987
+ if (type.flags & ts3.TypeFlags.TemplateLiteral) {
17988
+ return { type: "string" };
17989
+ }
17990
+ if (type.flags & ts3.TypeFlags.StringLike) {
17991
+ return { type: "string" };
17992
+ }
17993
+ if (type.flags & ts3.TypeFlags.NumberLike) {
17994
+ return { type: "number" };
17995
+ }
17996
+ if (type.flags & ts3.TypeFlags.BooleanLike) {
17997
+ return { type: "boolean" };
17998
+ }
17999
+ if (type.flags & ts3.TypeFlags.Null) {
18000
+ return { type: "null" };
18001
+ }
18002
+ if (type.isUnion()) {
18003
+ const nullable2 = type.types.some((member) => member.flags & ts3.TypeFlags.Null);
18004
+ const nonNullTypes = type.types.filter((member) => !(member.flags & ts3.TypeFlags.Null));
18005
+ const soleNonNullType = nonNullTypes[0];
18006
+ if (nullable2 && soleNonNullType && nonNullTypes.length === 1) {
18007
+ return {
18008
+ ...typeToOpenApiSchema(soleNonNullType, checker, seen),
18009
+ nullable: true
18010
+ };
17933
18011
  }
17934
- });
17935
- return required2.length > 0 ? {
17936
- type: "object",
17937
- properties: schemaProperties,
17938
- required: required2
17939
- } : {
17940
- type: "object",
17941
- properties: schemaProperties
17942
- };
17943
- }
17944
- const stringIndexType = type.getStringIndexType();
17945
- if (stringIndexType) {
17946
- return {
17947
- type: "object",
17948
- additionalProperties: typeToOpenApiSchema(stringIndexType, checker, seen)
17949
- };
18012
+ return {
18013
+ oneOf: nonNullTypes.map((member) => typeToOpenApiSchema(member, checker, seen))
18014
+ };
18015
+ }
18016
+ if (checker.isTupleType(type)) {
18017
+ const itemTypes = checker.getTypeArguments(type);
18018
+ return {
18019
+ type: "array",
18020
+ prefixItems: itemTypes.map((itemType) => typeToOpenApiSchema(itemType, checker, seen)),
18021
+ items: false,
18022
+ minItems: itemTypes.length,
18023
+ maxItems: itemTypes.length
18024
+ };
18025
+ }
18026
+ if (checker.isArrayType(type)) {
18027
+ const elementType = checker.getTypeArguments(type)[0];
18028
+ return {
18029
+ type: "array",
18030
+ items: elementType ? typeToOpenApiSchema(elementType, checker, seen) : { type: "object" }
18031
+ };
18032
+ }
18033
+ const properties = checker.getPropertiesOfType(type);
18034
+ if (properties.length > 0) {
18035
+ const schemaProperties = {};
18036
+ const required2 = [];
18037
+ properties.forEach((property) => {
18038
+ const propertyDeclaration = property.valueDeclaration || property.declarations?.[0];
18039
+ if (!propertyDeclaration) {
18040
+ return;
18041
+ }
18042
+ const propertyType = checker.getTypeOfSymbolAtLocation(property, propertyDeclaration);
18043
+ schemaProperties[property.getName()] = typeToOpenApiSchema(propertyType, checker, seen);
18044
+ if (!(property.flags & ts3.SymbolFlags.Optional)) {
18045
+ required2.push(property.getName());
18046
+ }
18047
+ });
18048
+ return required2.length > 0 ? {
18049
+ type: "object",
18050
+ properties: schemaProperties,
18051
+ required: required2
18052
+ } : {
18053
+ type: "object",
18054
+ properties: schemaProperties
18055
+ };
18056
+ }
18057
+ const stringIndexType = type.getStringIndexType();
18058
+ if (stringIndexType) {
18059
+ return {
18060
+ type: "object",
18061
+ additionalProperties: typeToOpenApiSchema(stringIndexType, checker, seen)
18062
+ };
18063
+ }
18064
+ return { type: "object" };
18065
+ } finally {
18066
+ seen.delete(seenKey);
17950
18067
  }
17951
- return { type: "object" };
17952
18068
  }
17953
18069
  function unwrapPromiseType(type, checker) {
17954
18070
  const symbolName = type.getSymbol()?.getName();