Repository navigation
Make sury-ppx read and write JSON the way Serde does - #491
Closed
rolandpeelen wants to merge 1 commit into
Closed
rolandpeelen wants to merge 1 commit into
rolandpeelen wants to merge 1 commit into
Conversation
The ppx now generates Serde's default representation:
- option<'a> reads and writes None as null; a record field of type
option<_>, or one marked ?, also reads a missing key as None
- unit is null
- a constructor with a payload is externally tagged: {"Ctor": a},
{"Ctor": [a, b]}, {"Ctor": {"x": a}}, one key and nothing else
- a polymorphic variant with a payload is {"tag": a}
- an @tag constructor without a payload is {"kind": "Ctor"}
- @as on a constructor names its key
packages/serde-compat mirrors every @Schema type in the ppx tests as
the Rust type Serde reads the same JSON into, and writes Serde's
verdicts to fixtures.json. Each e2e test file holds its schemas to them
with assertSerdeCompatible, in both directions. `cargo run -- check`
runs in CI and fails on a stale fixtures.json or an unmirrored type.
The README in packages/serde-compat lists the differences that remain
and the Serde features the ppx doesn't cover yet.
|
Caution Review failedThe pull request is closed.
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. CodeRabbit Verify
|
Author
|
Never meant to open this - sorry! |
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.
What changes for you
The ppx now generates schemas that read and write JSON the way Serde does for the matching Rust type, in its default mode. A ReScript app and a Rust service can share their types.
option<'a>Option<A>undefinedforNonenullforNone; a record field also reads a missing key asNoneunit()undefinednullCtor('a)Ctor(A){"TAG": "Ctor", "_0": a}{"Ctor": a}Ctor('a, 'b, 'c)Ctor(A, B, C){"TAG": "Ctor", "_0": a, "_1": b, "_2": c}{"Ctor": [a, b, c]}Ctor({x: 'a})Ctor { x: A }{"TAG": "Ctor", "x": a}{"Ctor": {"x": a}}#tag('a)tag(A){"NAME": "tag", "VAL": a}{"tag": a}@tag("kind")constructor without payload#[serde(tag = "kind")]unit variant"Ctor"{"kind": "Ctor"}@ason a constructor names its key, like#[serde(rename)]. A non-string@ason a constructor with a payload is a compile error.@s.defaultstill fills in only a missing value and rejectsnull, like#[serde(default)].@s.nullon a?field makes the key required.@s.nullablewritesNoneas a missing key, like#[serde(default, skip_serializing_if = "Option::is_none")].The
sury-ppxREADME gains a "Serde compatibility" section with the type and attribute mapping.Impact
option,unit, constructor with a payload and polymorphic variant with a payload changes its JSON. To keep an old shape, describe it with@s.matches, for example@s.matches(S.option(S.string)) option<string>. The README says so.packages/sury/srcis untouched. The change is the ppx's codegen (Structure.ml), its tests and docs, and a new Rust crate.option<address>field inPpx_Protobuf_test.resround-trips throughS.protobufwith the new option shape.How it's held to Serde
packages/serde-compatis a Rust crate that mirrors every@schematype inpackages/e2e/src/ppxas the type Serde reads the same JSON into: 149 types, 527 cases.cargo runwrites Serde's verdicts tofixtures.json:Each e2e test file then holds its schemas to those verdicts:
.acceptsand.rejectsassert Serde agrees, so a mirror can't record a verdict it didn't mean..divergescase fails the suite once Sury starts agreeing, so a closed gap can't linger.cargo run -- checkis a new CI job and apnpm verifystep. It fails on a stalefixtures.json, or on a@schematype with no mirror orskip.CLAUDE.mdandCONTRIBUTING.mdstate the rule: a new ppx test type gets a mirror.On main's ppx, 44 of the 149 mirrored types fail this check. On this branch all pass.
What isn't Serde yet
packages/serde-compat/README.mdlists it, with a todo list for full compatibility. In short:.divergescases):{"Variant": null}and a struct from an array of its fields in order.1.0for an integer and a repeated key;JSON.parseerases both before Sury sees them.-0.0comes out as0.@s.nullablewritesundefined.@unboxedconstructor without a payload reads its name, where Serde's untagged unit variant isnull.@tagconstructor whose payload isn't an inline record keeps it under_0. Serde refusestagon a tuple variant at compile time.S.url,S.date,S.blob,S.formData,S.urlSearchParamshold JS instances, plus three@s.matchesschemas with no JSON form. Eachskipcarries its reason.rename_all, flattening an@tagnewtype's struct next to the tag, adjacent tagging (tag+content),alias,other,skip*, containerdefault, integer widths beyondi32/u16/2^53, non-string map keys, tuple and unit structs.Verification
cargo run --locked -- checkup to date;cargo clippyandcargo fmt --checkclean.pnpm lint:deadcodeclean.Foo(int, int, string)was checked against serde_json directly:{"Foo":[1,2,"a"]}both ways,_0,_1,_2landing on positions 0, 1, 2, and the same refusals for short, long and map-shaped payloads.pnpm verify(it touches nothing underpackages/sury), and the CI ppx build on Linux.Follow-up
The fixtures compare JSON in with JSON out, so they can't catch two same-typed positional slots being swapped the same way in both directions. The permanent tests only have multi-argument constructors whose slots differ in type, where a swap fails the type check. A
Foo(int, int, string)case with direct value assertions would close that.