Skip to content

Conversation

@sentik
Copy link
Contributor

@sentik sentik commented Nov 3, 2025

Summary

Fix #2511
Fix #1733
related to duplicate schema names when using nullable oneOf and anyOf with enum references. The changes ensure proper type generation and prevent duplicate exports.

Changes Made

1. Fixed oneOf enum const generation (combine.ts)

Problem: For oneOf schemas containing only enum references, the generator was incorrectly creating const objects instead of union types, causing type errors.

Solution: Added a check to exclude oneOf from the const generation logic:

if (isAllEnums && name && items.length > 1 && separator !== 'oneOf')

Result: oneOf with enums now correctly generates union types like HelloEnum | BlankEnum | NullEnum | null instead of const objects.

2. Schema deduplication (schema-definition.ts)

Problem: Different source schemas could produce the same normalized name after applying naming conventions, leading to duplicate exports.

Solution: Added deduplication logic that tracks normalized schema names using a Set:

  • Normalizes schema names using the configured naming convention
  • Filters out duplicates before generating exports
  • Ensures each schema name appears only once in the output

3. Improved export generation (schemas.ts)

Problem: Export statements in index files could contain duplicates or be improperly sorted.

Solution:

  • Improved deduplication using Set for unique schema names
  • Switched to toSorted() method (immutable sorting)
  • Cleaner export statement generation logic

4. Index-based naming for Zod schemas (zod/src/index.ts)

Problem: When processing oneOf/anyOf/allOf with nullable refs, schemas could get duplicate names.

Solution: Added index-based naming to ensure uniqueness:

const baseSchemas = schemas.map((schema, index) =>
  generateZodValidationSchemaDefinition(
    schema as SchemaObject,
    context,
    `${camel(name)}${pascal(getNumberWord(index + 1))}`, // Index-based naming
    ...
  )
);

5. Test Coverage

Added comprehensive test cases:

  • nullable-oneof-enums.yaml - Tests for oneOf with enum schemas
  • nullable-any-of-refs.yaml - Tests for anyOf with nullable refs
  • Updated test configurations for both default and zod outputs
  • Added tests in zod.test.ts to verify unique schema name generation

Impact

Fixed: Type errors when using oneOf with enum schemas
Fixed: Duplicate schema names with nullable oneOf/anyOf refs
Fixed: Duplicate exports in generated index files
Improved: Better uniqueness guarantees in Zod schema generation

Example

Before:

// Incorrect: const object for oneOf
export const Item3Hello = {...HelloEnum,...BlankEnum,...NullEnum,} as const

After:

// Correct: union type for oneOf
export type Item3Hello = HelloEnum | BlankEnum | NullEnum | null;

@sentik sentik marked this pull request as ready for review November 3, 2025 04:24
@melloware melloware merged commit 47917e0 into orval-labs:master Nov 3, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Duplicate schema names generated when using nullable: true with oneOf enums (since v6.29.0) Error: Duplicate schema names detected

2 participants