Skip to content

fix: deduplicate identical sub-schemas with same title#678

Open
uprising8664 wants to merge 5 commits into
bcherny:masterfrom
uprising8664:master
Open

fix: deduplicate identical sub-schemas with same title#678
uprising8664 wants to merge 5 commits into
bcherny:masterfrom
uprising8664:master

Conversation

@uprising8664

@uprising8664 uprising8664 commented Jan 7, 2026

Copy link
Copy Markdown

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, allOf compositions), 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):

export interface Address {
  street: string;
  city: string;
}
export interface Address1 {
  street: string;
  city: string;
}
export interface Address2 {
  street: string;
  city: string;
}

After (single declaration):

export interface Example {
  user?: Address | Contact;
  billing?: Address;
  shipping?: Address | Contact1;
  [k: string]: unknown;
}
export interface Address {
  street: string;
  city: string;
  [k: string]: unknown;
}
export interface Contact {
  email?: string;
  [k: string]: unknown;
}
export interface Contact1 {
  phone?: string;
  [k: string]: unknown;
}

Note: The Address schema appears 3 times in the input but only generates 1 interface in the output.

Enum Deduplication

The same deduplication now applies to enums (both enum declarations via tsEnumNames and 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):

export enum Status {
  Active = "active",
  Inactive = "inactive",
}
export enum Status1 {
  Active = "active",
  Inactive = "inactive",
}

After (single declaration):

export enum Status {
  Active = "active",
  Inactive = "inactive",
}

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:

export type Status = "active" | "inactive";
export type Status1 = "active" | "inactive";

After:

export type Status = "active" | "inactive";

Changes

  • Add structural hashing to identify identical schemas
  • Track emitted type names in generator to prevent duplicates
  • Schemas with identical title and structure now reuse the same type definition
  • Different structures with the same title still get unique numbered names (e.g., Contact, Contact1)
  • Extend standaloneName() dedup cache to work without schema.title (property name used as fallback key)
  • Add name-based deduplication to declareEnums() matching the existing pattern in declareNamedInterfaces()
  • Add test coverage for duplicate sub-schema and enum scenarios

- 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
@evert

evert commented Mar 9, 2026

Copy link
Copy Markdown

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.
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.

2 participants