defer a self-naming untagged newtype member behind z.lazy, where the eager read cycled - #231
Merged
Merged
Conversation
…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.
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.
render_untagged_tuple_singletookself_type_nameand discarded it, so a newtype member naming the union itself was written straight into theconstthat 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 emittedwhich reads
V$Schemainside its own initializer. Importing the generated bundle throwsReferenceError: Cannot access 'V$Schema' before initializationbefore a single value is ever parsed.The fix
The sibling
render_untagged_namedalready defers such a member, with aget 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 bydeferred_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 az.strictObject({ get … }), which is exactly the deferral a bare union member had no place to write. The#[cfg(not(feature = "zod"))]branch keeps discardingself_type_name, the way the named renderer's already does.Coverage
A new
JsonValueUnionfixture intests/untagged_tests/tests.rs— the plain-JSON value shape — with four tests asserting:z.lazy(the array and the record);z.lazy(() => z.string())and friends anywhere;{"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-allacross all 68 toggles, andjust testacross the full feature powerset — all green. No suppressions.