Fix invalid Rust emitted for defaults on types with flattened properties#1039
Open
danieleades wants to merge 1 commit into
Open
Fix invalid Rust emitted for defaults on types with flattened properties#1039danieleades wants to merge 1 commit into
danieleades wants to merge 1 commit into
Conversation
…tput When generating a Rust value expression for a type-level `default` on a struct that has a `#[serde(flatten)]` property, the flattened property's name was interpolated as a string literal instead of an identifier, producing invalid Rust (e.g. `"extra": ...` inside a struct literal) that fails to compile and cannot be formatted by rustfmt. Use format_ident! for the flattened field, matching the sibling non-flattened path just above it. Added tests/schemas/flatten-default.json as a regression test, exercised by the existing schemas test harness (compiled via trybuild).
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.
Problem
When generating a Rust value expression for a type-level
defaulton a struct that has a#[serde(flatten)]property (e.g. an object withadditionalPropertiesalongside declared properties), the code intypify-impl/src/value.rsinterpolated the flattened property's name as a string literal instead of an identifier:Since
nameis aString,quote!renders it as a string literal ("extra": ...) rather than a field name, producing invalid Rust inside the struct literal. This causes rustfmt/compilation to fail for any generated default value on a struct with a flattened property.Fix
Build a proper identifier for the flattened field, matching the sibling non-flattened path a few lines above (
format_ident!("{}", &prop.name)).Testing
Added
typify/tests/schemas/flatten-default.json, a minimal schema with a struct-typed property carrying adefaultwhere the struct has both declared properties andadditionalProperties(flattened into a map). This is picked up automatically by the existingtest_schemasharness, which regenerates and rustfmts the output and then compiles it via trybuild.error: expected identifier, found "extra"(rustfmt).cargo test --workspace --lockedpasses, including trybuild compilation of the new golden file.cargo fmt --all -- --checkis clean.