Skip to content

defer a self-naming untagged newtype member behind z.lazy, where the eager read cycled - #231

Merged
eburgos merged 1 commit into
masterfrom
lazy-untagged-tuple-single-self-reference
Aug 11, 2026
Merged

defer a self-naming untagged newtype member behind z.lazy, where the eager read cycled#231
eburgos merged 1 commit into
masterfrom
lazy-untagged-tuple-single-self-reference

Conversation

@eburgos

@eburgos eburgos commented Aug 11, 2026

Copy link
Copy Markdown
Owner

render_untagged_tuple_single took self_type_name and discarded it, so a newtype member naming the union itself was written straight into the const that declares that name. A self-recursive untagged union — the plain-JSON value shape, B(bool) | L(Vec<Self>) | M(HashMap<String, Self>) | N(f64) | S(String) — therefore emitted

const V$RawSchema = z.union([z.boolean(), z.array(V$Schema), z.record(z.string(), V$Schema), z.number(), z.string()]);
export const V$Schema: ZodType<V> = V$RawSchema;

which reads V$Schema inside its own initializer. Importing the generated bundle throws ReferenceError: Cannot access 'V$Schema' before initialization before a single value is ever parsed.

The fix

The sibling render_untagged_named already defers such a member, with a get key() { … } getter hung off the key its struct variant spells. A newtype variant carries no key to hang one off, so the deferral is the thunk the rest of the crate already answers the same problem with: the member's zod slot is wrapped by deferred_zod_operandz.lazy(() => …) — whenever the member's type names the union itself, and left eager whenever it does not, so the indirection is paid only where the cycle is.

const V$RawSchema = z.union([z.boolean(), z.lazy(() => z.array(V$Schema)), z.lazy(() => z.record(z.string(), V$Schema)), z.number(), z.string()]);

TypeScript and the JSON document do not move: a TypeScript type names itself wherever it likes, and JSON Schema defers through $ref. The externally tagged form was never affected either — its recursion already sat inside a z.strictObject({ get … }), which is exactly the deferral a bare union member had no place to write. The #[cfg(not(feature = "zod"))] branch keeps discarding self_type_name, the way the named renderer's already does.

Coverage

A new JsonValueUnion fixture in tests/untagged_tests/tests.rs — the plain-JSON value shape — with four tests asserting:

  • both recursive members render behind z.lazy (the array and the record);
  • the three scalar members stay eager, with no z.lazy(() => z.string()) and friends anywhere;
  • the TypeScript definition is the flat union and carries no trace of the thunk;
  • a nested value ({"items":["a",true,1.5]}) round-trips through serde.

The two zod assertions were confirmed to fail with the deferral disabled and pass with it on.

Gates

just fmt, just check, just lint-all across all 68 toggles, and just test across the full feature powerset — all green. No suppressions.

…eager read cycled

`render_untagged_tuple_single` took `self_type_name` and discarded it, so a
newtype member naming the union itself was written straight into the `const`
that declares that name. A self-recursive untagged union — the plain-JSON value
shape, `B(bool) | L(Vec<Self>) | M(HashMap<String, Self>) | N(f64) | S(String)`
— therefore emitted `const V$RawSchema = z.union([…, z.array(V$Schema), …])`,
which reads `V$Schema` inside its own initializer: importing the bundle throws
`ReferenceError: Cannot access 'V$Schema' before initialization` before a single
value is ever parsed.

The sibling `render_untagged_named` already defers such a member, with a
`get key() { … }` getter hung off the key its struct variant spells. A newtype
variant carries no key to hang one off, so the deferral is the thunk the rest of
the crate already answers the same problem with: the member's zod slot is
wrapped by `deferred_zod_operand` — `z.lazy(() => …)` — whenever the member's
type names the union itself, and left eager whenever it does not, so the
indirection is paid only where the cycle is.

TypeScript and the JSON document do not move: a TypeScript type names itself
wherever it likes, and JSON Schema defers through `$ref`. The externally tagged
form was never affected either — its recursion already sat inside a
`z.strictObject({ get … })`, which is exactly the deferral a bare union member
had no place to write.

The regression fixture is that plain-JSON union, asserting both recursive
members render behind `z.lazy` — the array and the record — that the three
scalar members stay eager, that the TypeScript definition carries no trace of
the thunk, and that a nested value round-trips through serde.

just fmt, just check, just lint-all across all 68 toggles, and the test
powerset across every combination — all green.
@eburgos
eburgos merged commit 838510d into master Aug 11, 2026
1 check passed
@eburgos
eburgos deleted the lazy-untagged-tuple-single-self-reference branch August 11, 2026 04:02
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.

1 participant