Repository navigation
fix(gts-macros): read optional and renamed fields of derived structs - #163
Conversation
The GtsDeserialize impl generated for a derived struct (base = Parent) matched member names against the Rust field idents and ignored a derived struct with a renamed field did not deserialize what it serialized: the wire name was ignored and reported as a missing field. Read each member under its serialized name. When that name differs from the key matched before, accept the old key as an alias, so input that deserialized until now still does. Signed-off-by: MikeFalcon77 <3686170+MikeFalcon77@users.noreply.github.com>
The GtsDeserialize impl generated for a derived struct treated every absent member as missing, except an Option<P> generic slot. An ordinary Option<T> field, or a field with #[serde(default)], was therefore required on input, although the generated schema leaves it out of `required` and the README documents it as optional. Adding such a field to a derived type made old instances unreadable. Read an absent member as serde derive does for base structs: a field with #[serde(default)] or #[serde(default = "path")] takes that default, an Option<T> field is None, and any other field is missing. Schemas and serialization are unchanged. Document the serde field rules that apply to derived structs and add tests covering defaults, null, renamed members, round-trip and schema agreement. Signed-off-by: MikeFalcon77 <3686170+MikeFalcon77@users.noreply.github.com>
get_serde_rename matched on the attribute's token string, so a rename followed by another option was misread: `rename = "kind", default = "f"` produced the name `kind", default = "f`, and `rename = "kind", default` lost the rename. With absent members now defaulted, that turned into a silently defaulted field. Parse #[serde(...)] options as syn metas, shared by the rename, default and option-name helpers. The derived-struct visitor kept members in locals named after the fields, which shadowed a default function of the same name and the visitor's own `map`. Use prefixed locals and build the struct from the finished values directly. The legacy key now follows serde's snake_case rule exactly, ASCII lowercasing included. Document which serde rules derived structs apply and test the cases above, the alias/wire-name duplicate and a generic middle type. Signed-off-by: MikeFalcon77 <3686170+MikeFalcon77@users.noreply.github.com>
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars.
|
code-rankerBuilt on a fork. View full report ↗ rust
|
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
| } else { | ||
| quote! { | ||
| let #ident = #ident.ok_or_else(|| serde::de::Error::missing_field(#name))?; | ||
| quote! { #[serde(rename = #name, alias = #legacy)] #ident } |
There was a problem hiding this comment.
With b renamed to c and then a renamed to b, reading our own {"c":"first","b":"second"} fails with a duplicate-field error because the first field claims b as an alias. Please skip legacy aliases that match another field’s wire name.
There was a problem hiding this comment.
Fixed. A legacy alias is no longer emitted when another field is written under that key. swapped_renames_read_back_in_declaration_order covers your order (b -> c before a -> b) with a round-trip.
| quote! { #ident: #local.unwrap_or_default() } | ||
| } | ||
| Some(FieldDefault::Path(path)) => { | ||
| quote! { #ident: #local.unwrap_or_else(#path) } |
There was a problem hiding this comment.
This breaks #[serde(default = "Self::default_level")] because Self here refers to StructVisitor. Please resolve the default path in the original struct’s context and add a regression test.
There was a problem hiding this comment.
Fixed. Self in a default = "..." path is replaced with the struct type (as serde derive does), including generic structs. tests are updated
A legacy alias of a derived field could take another field's wire name: with `b` renamed to `c` declared before `a` renamed to `b`, the alias `b` of the first field matched before the second field's name, so the struct's own serialized form failed with a duplicate field. Skip a legacy alias that another field is written under. A `default = "Self::f"` path was called from the generated visitor, where `Self` is the visitor, and did not compile. Replace `Self` in default paths with the struct type, as serde derive does. Signed-off-by: MikeFalcon77 <3686170+MikeFalcon77@users.noreply.github.com>
Description
Derived structs (
#[struct_to_gts_schema(base = Parent, ...)]) are read through a generatedGtsDeserializeimpl. That impl disagreed with the generated schema and with serde derive:Option<P>generic slot was amissing_fielderror, although the schema leavesOption<T>and#[serde(default)]fields out ofrequiredand the README documents them as optional. Adding an optional field to a derived type therefore made existing instances unreadable, so consumers had to bump the type's major version.#[serde(rename = "wire")]reportedmissing field `wire`even whenwirewas present, andGtsSerializeoutput did not read back.renamefollowed by another option was misparsed.get_serde_renamematched the raw token string:rename = "kind", default = "f"yielded the namekind", default = "f, andrename = "kind", defaultlost the rename.Changes (in
gts-macros):#[serde(default)]→Default::default(),#[serde(default = "path")]→path(),Option<T>→None, anything else → missing field. Schemas and serialization are unchanged.#[serde(...)]field options assyn::Meta(shared by the rename, default and option-name helpers), and keep the visitor's members in prefixed locals so a field cannot shadow its default function or the visitor'smap.Behavior notes
arenamed to"b"and fieldbrenamed to"c", the input{"a": …, "b": …}used to read and now fails with a duplicate-field error, while{"b": …, "c": …}(the documented wire form) now reads. This follows from the wire name taking precedence.get_serde_renameis used for base structs (type-field check, recorded generic field name). A base field written#[serde(rename = "x", default)]is now recognized under"x"instead of its Rust name. This is a correction of the same parsing bug.Optionwritten through a type alias is not recognized, andskip_serializing_if,skip,rename(serialize = …, deserialize = …)and containerrename_allare not applied to derived structs. The derived serializer still writesnullforNone.Type of Change
Testing
New
gts-macros/tests/derived_optional_fields_tests.rs(16 tests): absent,nulland present optional/default fields, missing required fields, renamed members and their legacy alias, alias + wire name as a duplicate, round-trip throughGtsSerialize, schema/deserializer agreement onrequired, rename followed bydefault = "...", a default function named like its field, a field namedmap, and a generic middle type in a chain. 7 of the first 9 tests fail onmain; themapcase does not compile there.cargo test --workspace cargo fmt --all -- --check cargo clippy --workspace --all-targets --all-features -- -D warningsChecklist
cargo test)cargo fmt)cargo clippy)