Skip to content

JSON Schema generation fails for discriminated unions in recursive scopes #1546

Closed

Description

@seatedro

Report a bug

toJsonSchema() throws "Unexpected JSON Schema input for structure: {}" when generating JSON schema for a recursive scope containing a discriminated union where the union members have additional fields beyond the discriminator.

Workaround is to define the union outside the scope.

🔎 Search Terms

  • toJSONSchema
  • discriminated unions
  • recursive types
  • cyclic types

🧩 Context

  • ArkType version: 2.1.26
  • TypeScript version (5.1+): 5.8.3
  • Other context you think may be relevant (JS flavor, OS, etc.): - Runtimes: Node.js v22.19.0, Bun 1.3.0

🧑‍💻 Repro

Playground Link

import { scope } from "arktype";

const s = scope({
  TypeA: { 
    kind: "'a'",
    value: "number"  // Additional field beyond discriminator
  },
  TypeB: { 
    kind: "'b'",
    value: "number"  // Additional field beyond discriminator
  },
  Union: "TypeA | TypeB",
  Node: {
    source: "SearchNode",  // Recursive reference
    data: "Union",
  },
  SearchNode: "Node",
});

const out = s.type("SearchNode").toJsonSchema(); // ❌ Throws error

Activity

  1. rynvelt commented on Nov 15, 2025

    @rynvelt

    Encountered the same issue but the workaround does not work for me.

    My finding is: toJsonSchema() fails when combining a recursive schema with a discriminated union where any variant has 2 or more properties:

    🧑‍💻 Repro

     import { scope, type } from "arktype";
    
      const recursive = scope({
        Node: { value: "string", "children?": "Node[]" }
      }).export().Node;
    
      // ✅ WORKS: Both variants have exactly 1 property
      console.log("Test 1 (1 property per variant):");
      const union1 = type.or(
        type({ kind: "'a'" }),
        type({ kind: "'b'" })
      );
    
      try {
        type({ recursive, union: union1 }).toJsonSchema();
        console.log("✅ PASS");
      } catch (e) {
        console.log("❌ FAIL:", e.message);
      }
    
      // ❌ FAILS: Variants have 2+ properties
      console.log("\nTest 2 (2 properties per variant):");
      const union2 = type.or(
        type({ kind: "'a'", value: "string" }),
        type({ kind: "'b'", value: "number" })
      );
    
      try {
        type({ recursive, union: union2 }).toJsonSchema();
        console.log("✅ PASS");
      } catch (e) {
        console.log("❌ FAIL:", e.message);
      }

    Output:

    Test 1 (1 property per variant):
    ✅ PASS
    
    Test 2 (2 properties per variant):
    ❌ FAIL: Unexpected JSON Schema input for structure: {}

    🧩 Context

    • ArkType version: 2.1.25
    • TypeScript version (5.1+): 5.9.2
    • Other context you think may be relevant (JS flavor, OS, etc.): - Bun 1.3.0

    I noticed that this does not happen for unions with two or more fields if there is no discriminator field in this union.

  2. added 2 commits that reference this issue on Oct 7, 2026
    f57ad99
    5521dd6
  3. added a commit that references this issue on Oct 20, 2026
    2503852
  4. moved this from To do to Done (merged or closed) in arktypeioon Oct 20, 2026
  5. added a commit that references this issue on Oct 26, 2026
    fc06904
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

  • Status
    Done (merged or closed)

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions