Repository navigation
Support #[serde(transparent)] structs by generating type aliases - #296
Open
xiao115255 wants to merge 1 commit into
Open
xiao115255 wants to merge 1 commit into
xiao115255 wants to merge 1 commit into
Conversation
A struct annotated with #[serde(transparent)] serializes as the value of its single non-skipped field, but typeshare still generated a wrapper struct for it, producing definitions that do not round-trip with the serialized data. Parse such structs as a type alias to the non-skipped field's type, mirroring what serde actually produces. This reuses the existing alias path taken by single-field tuple structs. Fixes 1Password#26 Co-Authored-By: Claude Code <noreply@anthropic.com>
This branch has not been deployed
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 & Why
A struct annotated with #[serde(transparent)] serializes as the value of its single non-skipped field, but typeshare still generated a wrapper struct for it. The generated definitions therefore did not match the actual serialized shape (per the examples in #26):
Before:
After:
Changes:
core/src/parser.rs: when parsing a named struct with#[serde(transparent)]and exactly one non-skipped field, emit aRustItem::Aliasto that field's type instead of a wrapper struct. This reuses the existing alias path already taken by single-field tuple structs, so all language backends get the correct behavior. Structs whose transparent field is overridden via#[typeshare(serialized_as)]are handled as well; structs that do not have exactly one non-skipped field fall back to the previous behavior (serde itself rejects those at compile time).core/data/tests/can_handle_serde_transparent/: new snapshot test covering both a single-field transparent struct and a transparent struct with an additional#[serde(skip)]field, across kotlin, swift, typescript, scala, go, and python.docs/src/usage/annotations.md: document the supported annotation.Fixes #26
How Tested
cargo test --workspace: all 315 snapshot tests plus unit/doc tests pass (full suite, not just the new tests) — no regressions.cargo fmt --all -- --check: clean.cargo clippy --workspace --all-targets: no new warnings (37 pre-existing before and after).cargo run -p typeshare-cli -- --lang=typescript --output-file=out.ts sample.rs) and confirmed it now emitsexport type Players = Player[];.AI Disclosure
This PR was prepared with AI assistance (Claude Code), reviewed and submitted by @xiao115255.