@cerios/openapi-to-zod 1.3.0 → 1.3.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.d.mts CHANGED
@@ -1,5 +1,5 @@
1
- import { O as OpenApiGeneratorOptions } from './types-B3GgqGzM.mjs';
2
- export { C as CommonSchemaOptions, a as ConfigFile, E as ExecutionMode, b as OpenAPIParameter, c as OpenAPIRequestBody, d as OpenAPIResponse, e as OpenAPISchema, f as OpenAPISpec, g as OperationFilters, R as RequestOptions, h as ResponseOptions, i as defineConfig } from './types-B3GgqGzM.mjs';
1
+ import { O as OpenApiGeneratorOptions } from './types-DZ4Bw-D5.mjs';
2
+ export { C as CommonSchemaOptions, a as ConfigFile, E as ExecutionMode, b as OpenAPIParameter, c as OpenAPIRequestBody, d as OpenAPIResponse, e as OpenAPISchema, f as OpenAPISpec, g as OperationFilters, R as RequestOptions, h as ResponseOptions, i as defineConfig } from './types-DZ4Bw-D5.mjs';
3
3
 
4
4
  /**
5
5
  * Custom error classes for better error handling and debugging
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { O as OpenApiGeneratorOptions } from './types-B3GgqGzM.js';
2
- export { C as CommonSchemaOptions, a as ConfigFile, E as ExecutionMode, b as OpenAPIParameter, c as OpenAPIRequestBody, d as OpenAPIResponse, e as OpenAPISchema, f as OpenAPISpec, g as OperationFilters, R as RequestOptions, h as ResponseOptions, i as defineConfig } from './types-B3GgqGzM.js';
1
+ import { O as OpenApiGeneratorOptions } from './types-DZ4Bw-D5.js';
2
+ export { C as CommonSchemaOptions, a as ConfigFile, E as ExecutionMode, b as OpenAPIParameter, c as OpenAPIRequestBody, d as OpenAPIResponse, e as OpenAPISchema, f as OpenAPISpec, g as OperationFilters, R as RequestOptions, h as ResponseOptions, i as defineConfig } from './types-DZ4Bw-D5.js';
3
3
 
4
4
  /**
5
5
  * Custom error classes for better error handling and debugging
package/dist/index.js CHANGED
@@ -113,12 +113,12 @@ function toCamelCase(str, options) {
113
113
  name = words[0].charAt(0).toLowerCase() + words[0].slice(1) + words.slice(1).map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join("");
114
114
  }
115
115
  if (options == null ? void 0 : options.prefix) {
116
- const prefix = options.prefix.toLowerCase();
116
+ const prefix = options.prefix.charAt(0).toLowerCase() + options.prefix.slice(1);
117
117
  name = prefix + name.charAt(0).toUpperCase() + name.slice(1);
118
118
  }
119
119
  if (options == null ? void 0 : options.suffix) {
120
- const suffix = options.suffix;
121
- name = name + suffix.charAt(0).toUpperCase() + suffix.slice(1).toLowerCase();
120
+ const suffix = options.suffix.charAt(0).toUpperCase() + options.suffix.slice(1);
121
+ name = name + suffix;
122
122
  }
123
123
  return name;
124
124
  }
@@ -456,7 +456,7 @@ function generateUnion(schemas, discriminator, isNullable2, context, options, cu
456
456
  );
457
457
  }
458
458
  if (schemas.length === 1) {
459
- let singleSchema = context.generatePropertySchema(schemas[0], currentSchema);
459
+ let singleSchema = context.generatePropertySchema(schemas[0], currentSchema, false, true);
460
460
  if ((options == null ? void 0 : options.passthrough) && !singleSchema.includes(".catchall(")) {
461
461
  singleSchema = `${singleSchema}.catchall(z.unknown())`;
462
462
  }
@@ -472,7 +472,7 @@ function generateUnion(schemas, discriminator, isNullable2, context, options, cu
472
472
  console.warn(
473
473
  `[openapi-to-zod] Warning: Discriminator "${discriminator}" is not required in schemas: ${discriminatorCheck.invalidSchemas.join(", ")}. Falling back to z.union() instead of z.discriminatedUnion().`
474
474
  );
475
- let schemaStrings3 = resolvedSchemas.map((s) => context.generatePropertySchema(s, currentSchema));
475
+ let schemaStrings3 = resolvedSchemas.map((s) => context.generatePropertySchema(s, currentSchema, false, true));
476
476
  if (options == null ? void 0 : options.passthrough) {
477
477
  schemaStrings3 = schemaStrings3.map((s) => s.includes(".catchall(") ? s : `${s}.catchall(z.unknown())`);
478
478
  }
@@ -480,14 +480,14 @@ function generateUnion(schemas, discriminator, isNullable2, context, options, cu
480
480
  const union3 = `z.union([${schemaStrings3.join(", ")}]).describe("${fallbackDescription}")`;
481
481
  return wrapNullable(union3, isNullable2);
482
482
  }
483
- let schemaStrings2 = resolvedSchemas.map((s) => context.generatePropertySchema(s, currentSchema));
483
+ let schemaStrings2 = resolvedSchemas.map((s) => context.generatePropertySchema(s, currentSchema, false, true));
484
484
  if (options == null ? void 0 : options.passthrough) {
485
485
  schemaStrings2 = schemaStrings2.map((s) => s.includes(".catchall(") ? s : `${s}.catchall(z.unknown())`);
486
486
  }
487
487
  const union2 = `z.discriminatedUnion("${discriminator}", [${schemaStrings2.join(", ")}])`;
488
488
  return wrapNullable(union2, isNullable2);
489
489
  }
490
- let schemaStrings = schemas.map((s) => context.generatePropertySchema(s, currentSchema));
490
+ let schemaStrings = schemas.map((s) => context.generatePropertySchema(s, currentSchema, false, true));
491
491
  if (options == null ? void 0 : options.passthrough) {
492
492
  schemaStrings = schemaStrings.map((s) => s.includes(".catchall(") ? s : `${s}.catchall(z.unknown())`);
493
493
  }
@@ -549,7 +549,7 @@ function detectConflictingProperties(schemas, context) {
549
549
  }
550
550
  function generateAllOf(schemas, isNullable2, context, currentSchema) {
551
551
  if (schemas.length === 1) {
552
- const singleSchema = context.generatePropertySchema(schemas[0], currentSchema, false);
552
+ const singleSchema = context.generatePropertySchema(schemas[0], currentSchema, false, true);
553
553
  return wrapNullable(singleSchema, isNullable2);
554
554
  }
555
555
  const conflicts = detectConflictingProperties(schemas, context);
@@ -563,23 +563,23 @@ function generateAllOf(schemas, isNullable2, context, currentSchema) {
563
563
  const allObjects = schemas.every((s) => s.type === "object" || s.properties || s.$ref || s.allOf);
564
564
  let result;
565
565
  if (allObjects) {
566
- let merged = context.generatePropertySchema(schemas[0], currentSchema, false);
566
+ let merged = context.generatePropertySchema(schemas[0], currentSchema, false, true);
567
567
  for (let i = 1; i < schemas.length; i++) {
568
568
  const schema = schemas[i];
569
569
  if (schema.$ref) {
570
- const refSchema = context.generatePropertySchema(schema, currentSchema, false);
570
+ const refSchema = context.generatePropertySchema(schema, currentSchema, false, true);
571
571
  merged = `${merged}.extend(${refSchema}.shape)`;
572
572
  } else if (context.generateInlineObjectShape && (schema.properties || schema.type === "object")) {
573
573
  const inlineShape = context.generateInlineObjectShape(schema, currentSchema);
574
574
  merged = `${merged}.extend(${inlineShape})`;
575
575
  } else {
576
- const schemaString = context.generatePropertySchema(schema, currentSchema, false);
576
+ const schemaString = context.generatePropertySchema(schema, currentSchema, false, true);
577
577
  merged = `${merged}.extend(${schemaString}.shape)`;
578
578
  }
579
579
  }
580
580
  result = merged;
581
581
  } else {
582
- const schemaStrings = schemas.map((s) => context.generatePropertySchema(s, currentSchema, false));
582
+ const schemaStrings = schemas.map((s) => context.generatePropertySchema(s, currentSchema, false, true));
583
583
  let merged = schemaStrings[0];
584
584
  for (let i = 1; i < schemaStrings.length; i++) {
585
585
  merged = `${merged}.and(${schemaStrings[i]})`;
@@ -1364,12 +1364,21 @@ var _PropertyGenerator = class _PropertyGenerator {
1364
1364
  }
1365
1365
  /**
1366
1366
  * Generate Zod schema for a property
1367
+ * @param schema - The OpenAPI schema to generate
1368
+ * @param currentSchema - The name of the current schema being processed (for circular ref detection)
1369
+ * @param isTopLevel - Whether this is a top-level schema definition
1370
+ * @param suppressDefaultNullable - When true, don't apply defaultNullable (used when outer schema has explicit nullable: false)
1367
1371
  */
1368
- generatePropertySchema(schema, currentSchema, isTopLevel = false) {
1372
+ generatePropertySchema(schema, currentSchema, isTopLevel = false, suppressDefaultNullable = false) {
1369
1373
  var _a, _b, _c, _d, _e;
1370
1374
  const isCacheable = !schema.$ref && !schema.allOf && !schema.oneOf && !schema.anyOf && !currentSchema;
1371
1375
  if (isCacheable) {
1372
- const cacheKey = JSON.stringify({ schema, type: this.context.schemaType, mode: this.context.mode });
1376
+ const cacheKey = JSON.stringify({
1377
+ schema,
1378
+ type: this.context.schemaType,
1379
+ mode: this.context.mode,
1380
+ suppressDefaultNullable
1381
+ });
1373
1382
  const cached = this.schemaCache.get(cacheKey);
1374
1383
  if (cached) {
1375
1384
  return cached;
@@ -1378,10 +1387,9 @@ var _PropertyGenerator = class _PropertyGenerator {
1378
1387
  if ((this.context.schemaType === "request" || this.context.schemaType === "response") && schema.properties) {
1379
1388
  schema = this.filterNestedProperties(schema);
1380
1389
  }
1381
- const isSchemaRef = !!schema.$ref;
1382
1390
  const isEnum = !!schema.enum;
1383
1391
  const isConst = schema.const !== void 0;
1384
- const shouldApplyDefaultNullable = !isTopLevel && !isSchemaRef && !isEnum && !isConst;
1392
+ const shouldApplyDefaultNullable = !isTopLevel && !isEnum && !isConst && !suppressDefaultNullable;
1385
1393
  const effectiveDefaultNullable = shouldApplyDefaultNullable ? this.context.defaultNullable : false;
1386
1394
  const nullable = isNullable(schema, effectiveDefaultNullable);
1387
1395
  if (hasMultipleTypes(schema)) {
@@ -1432,9 +1440,10 @@ var _PropertyGenerator = class _PropertyGenerator {
1432
1440
  return wrapNullable(zodUnion, nullable);
1433
1441
  }
1434
1442
  if (schema.allOf) {
1443
+ const compositionNullable = isNullable(schema, false);
1435
1444
  let composition = generateAllOf(
1436
1445
  schema.allOf,
1437
- nullable,
1446
+ compositionNullable,
1438
1447
  {
1439
1448
  generatePropertySchema: this.generatePropertySchema.bind(this),
1440
1449
  generateInlineObjectShape: this.generateInlineObjectShape.bind(this),
@@ -1448,11 +1457,12 @@ var _PropertyGenerator = class _PropertyGenerator {
1448
1457
  return composition;
1449
1458
  }
1450
1459
  if (schema.oneOf) {
1460
+ const compositionNullable = isNullable(schema, false);
1451
1461
  const needsPassthrough = schema.unevaluatedProperties !== void 0;
1452
1462
  let composition = generateUnion(
1453
1463
  schema.oneOf,
1454
1464
  (_b = schema.discriminator) == null ? void 0 : _b.propertyName,
1455
- nullable,
1465
+ compositionNullable,
1456
1466
  {
1457
1467
  generatePropertySchema: this.generatePropertySchema.bind(this),
1458
1468
  resolveDiscriminatorMapping: this.resolveDiscriminatorMapping.bind(this),
@@ -1470,11 +1480,12 @@ var _PropertyGenerator = class _PropertyGenerator {
1470
1480
  return composition;
1471
1481
  }
1472
1482
  if (schema.anyOf) {
1483
+ const compositionNullable = isNullable(schema, false);
1473
1484
  const needsPassthrough = schema.unevaluatedProperties !== void 0;
1474
1485
  let composition = generateUnion(
1475
1486
  schema.anyOf,
1476
1487
  (_d = schema.discriminator) == null ? void 0 : _d.propertyName,
1477
- nullable,
1488
+ compositionNullable,
1478
1489
  {
1479
1490
  generatePropertySchema: this.generatePropertySchema.bind(this),
1480
1491
  resolveDiscriminatorMapping: this.resolveDiscriminatorMapping.bind(this),
@@ -1974,12 +1985,6 @@ var OpenApiGenerator = class {
1974
1985
  * Generate the complete output file
1975
1986
  */
1976
1987
  generate() {
1977
- if (!this.options.output) {
1978
- throw new ConfigurationError(
1979
- "Output path is required when calling generate(). Either provide an 'output' option or use generateString() to get the result as a string.",
1980
- { hasOutput: false }
1981
- );
1982
- }
1983
1988
  const output = this.generateString();
1984
1989
  const normalizedOutput = (0, import_node_path.normalize)(this.options.output);
1985
1990
  this.ensureDirectoryExists(normalizedOutput);