fix: deduplicate identical sub-schemas with same title#678
Open
uprising8664 wants to merge 5 commits into
Open
fix: deduplicate identical sub-schemas with same title#678uprising8664 wants to merge 5 commits into
uprising8664 wants to merge 5 commits into
Conversation
- Add structural hash-based deduplication for schemas with same title - Track emitted type names in generator to prevent duplicate declarations - Schemas with identical title and structure now reuse same type definition - Different structures with same title get unique numbered names (e.g. Option, Option1) - Add comprehensive test coverage for duplicate sub-schema scenarios
|
This is awesome! |
Add four test fixtures covering: - NAMED_ENUM dedup with matching title - NAMED_ENUM dedup without title (property name match) - UNNAMED_ENUM dedup with matching title - Mixed scenario (identical + non-identical enums)
Previously the dedup cache in standaloneName() only activated when
schema.title was present. Without a title, identical enum schemas on
same-named properties bypassed the cache and received fresh names
(Status, Status1, Status2...) instead of deduplicating.
Remove the if (schema.title) guard so the cache runs whenever name
is available. Change cache key from ${schema.title}::${hash} to
${name}::${hash} to cover all named schemas.
…attern declareEnums() used object-identity dedup (Set<AST>). Two distinct AST objects with the same standaloneName both emitted declarations, producing duplicates in the output. Add emittedNames: Set<string> parameter (default new Set<string>()) and check ast.standaloneName before emitting, matching the existing pattern in declareNamedInterfaces() and declareNamedTypes(). Pass emittedNames through all 5 recursive call sites (ARRAY, UNION, INTERSECTION, TUPLE, INTERFACE). Each declare function retains its own independent set.
New snapshots for four duplicate-enum fixtures, each showing a single declaration instead of duplicates. Existing real-world snapshots (FHIR, Azure, schemaStore, arrayAdditionalItems) are reduced where previously-duplicate enum and interface declarations are now correctly deduplicated. All 169 tests pass.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
This PR fixes an issue where identical sub-schemas with the same title would generate duplicate TypeScript interface declarations. The fix adds structural hash-based deduplication that tracks emitted type names and reuses them when the same schema structure appears multiple times.
Problem
Previously, when a dereferenced JSON Schema contained the same sub-schema structure multiple times (common in
oneOf,anyOf,allOfcompositions), the compiler would generate duplicate TypeScript interfaces:Example
Input Schema:
{ "title": "Example", "type": "object", "properties": { "user": { "oneOf": [ { "title": "Address", "type": "object", "properties": { "street": { "type": "string" }, "city": { "type": "string" } }, "required": ["street", "city"] }, { "title": "Contact", "type": "object", "properties": { "email": { "type": "string" } } } ] }, "billing": { "title": "Address", "type": "object", "properties": { "street": { "type": "string" }, "city": { "type": "string" } }, "required": ["street", "city"] }, "shipping": { "anyOf": [ { "title": "Address", "type": "object", "properties": { "street": { "type": "string" }, "city": { "type": "string" } }, "required": ["street", "city"] }, { "title": "Contact", "type": "object", "properties": { "phone": { "type": "string" } } } ] } } }Before (duplicate declarations):
After (single declaration):
Note: The
Addressschema appears 3 times in the input but only generates 1 interface in the output.Enum Deduplication
The same deduplication now applies to enums (both
enumdeclarations viatsEnumNamesand union types).Named Enum (
tsEnumNames)Input Schema:
{ "title": "Example", "type": "object", "properties": { "primary": { "title": "Status", "type": "string", "enum": ["active", "inactive"], "tsEnumNames": ["Active", "Inactive"] }, "secondary": { "title": "Status", "type": "string", "enum": ["active", "inactive"], "tsEnumNames": ["Active", "Inactive"] } } }Before (duplicate declarations):
After (single declaration):
Unnamed Enum (union type)
Input Schema:
{ "title": "Example", "type": "object", "properties": { "primary": { "title": "Status", "type": "string", "enum": ["active", "inactive"] }, "secondary": { "title": "Status", "type": "string", "enum": ["active", "inactive"] } } }Before:
After:
Changes
Contact,Contact1)standaloneName()dedup cache to work withoutschema.title(property name used as fallback key)declareEnums()matching the existing pattern indeclareNamedInterfaces()