Bugs/2511 duplicate nullable schema names #2537
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fix #2511
Fix #1733
related to duplicate schema names when using nullable
oneOfandanyOfwith enum references. The changes ensure proper type generation and prevent duplicate exports.Changes Made
1. Fixed
oneOfenum const generation (combine.ts)Problem: For
oneOfschemas containing only enum references, the generator was incorrectly creatingconstobjects instead of union types, causing type errors.Solution: Added a check to exclude
oneOffrom the const generation logic:Result:
oneOfwith enums now correctly generates union types likeHelloEnum | BlankEnum | NullEnum | nullinstead 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:3. Improved export generation (
schemas.ts)Problem: Export statements in index files could contain duplicates or be improperly sorted.
Solution:
Setfor unique schema namestoSorted()method (immutable sorting)4. Index-based naming for Zod schemas (
zod/src/index.ts)Problem: When processing
oneOf/anyOf/allOfwith nullable refs, schemas could get duplicate names.Solution: Added index-based naming to ensure uniqueness:
5. Test Coverage
Added comprehensive test cases:
nullable-oneof-enums.yaml- Tests for oneOf with enum schemasnullable-any-of-refs.yaml- Tests for anyOf with nullable refszod.test.tsto verify unique schema name generationImpact
✅ Fixed: Type errors when using
oneOfwith 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:
After: