Skip to content

Reject externally tagged variants with more than one field - #720

Open
ciprianf-flexion wants to merge 1 commit into
getml:mainfrom
ciprianf-flexion:fix/field-variant-reject-multiple-fields
Open

Reject externally tagged variants with more than one field#720
ciprianf-flexion wants to merge 1 commit into
getml:mainfrom
ciprianf-flexion:fix/field-variant-reject-multiple-fields

Conversation

@ciprianf-flexion

Copy link
Copy Markdown

Reading an rfl::Variant<rfl::Field<...>, ...> from an object with several keys silently keeps the last recognized tag. Worse, an unknown key followed by a valid tag overwrites the "could not match field" error, so {"triangle": {...}, "circle": {...}} parses successfully as a circle.

Serde, which this format models (per variants_and_tagged_unions.md), represents an externally tagged enum as a single-key map and rejects anything else, see serde.rs/enum-representations.html.

This change makes the reader return an error whenever a second field is encountered, mirroring the existing "expected the object to have exactly one field, but found none" error.

Added tests for both cases in tests/json/test_field_variant_multiple_fields.cpp.

Reading an rfl::Variant<rfl::Field<...>, ...> from an object with
several keys silently kept the last recognized tag, and an unknown key
followed by a valid tag overwrote the error. Serde, which this format
models, rejects such objects. The reader now returns an error whenever
a second field is encountered, mirroring the existing 'found none'
error.
@ciprianf-flexion
ciprianf-flexion marked this pull request as ready for review August 28, 2026 09:55
@ciprianf-flexion

Copy link
Copy Markdown
Author

@liuzicheng1987 I cannot add you explicitly as a reviewer, but would you mind taking a look? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant