Skip to content

fix(arrow-schema): validate UnionFields when deserializing - #11638

Merged
Jefffrey merged 2 commits into
apache:mainfrom
ump45nose:fix/union-fields-serde-validate
Oct 18, 2026
Merged

Jefffrey merged 2 commits into
apache:mainfrom
ump45nose:fix/union-fields-serde-validate

Conversation

@ump45nose

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

UnionFields derived Deserialize, so deserializing could build a value with duplicate or negative type ids, skipping the checks that #11533 added to the other constructors.

What changes are included in this PR?

Serialize is still derived (transparent), but Deserialize is now implemented by hand: it reads the same sequence of (type_id, field) pairs and builds the value with UnionFields::try_new, returning a serde error if validation fails. The wire format doesn't change, so anything serialized before still deserializes as long as it was valid.

I kept serde rather than removing it, since DataType (and therefore Field and Schema) derive serde and need UnionFields to support it.

Are these changes tested?

Yes, two new tests in fields.rs (behind the serde feature, using postcard like the existing Field serde tests): a round trip of a valid UnionFields, and deserializing input with a duplicate type id and with a negative type id, both of which now fail. The second test fails without this change.

Are there any significant user-facing changes?

Deserializing an invalid UnionFields (or a DataType::Union containing one) now returns an error instead of succeeding.

AI disclosure: I used an AI coding assistant to write the change, the tests and this description, and I reviewed all of it.

@github-actions github-actions Bot added arrow-core Core crates related to representing arrow data bug Squash those bugs labels Oct 17, 2026
Comment thread arrow-schema/src/fields.rs Outdated
let negative: Vec<(i8, FieldRef)> = vec![(0, Arc::clone(&field)), (-1, field)];
let serialized = postcard::to_stdvec(&negative).unwrap();
let err = postcard::from_bytes::<UnionFields>(&serialized).unwrap_err();
assert!(matches!(err, postcard::Error::SerdeDeCustom), "{err:?}");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible to assert some of the error message too?

Comment thread arrow-schema/src/fields.rs Outdated
Comment on lines +323 to +324
/// Deserializes the same representation as `Serialize`, but goes through
/// [`UnionFields::try_new`] so the invariants above are enforced.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Deserializes the same representation as `Serialize`, but goes through
/// [`UnionFields::try_new`] so the invariants above are enforced.
// Manual implementation to enforce invariants per docstring above

@Jefffrey
Jefffrey enabled auto-merge October 18, 2026 02:16
@Jefffrey
Jefffrey added this pull request to the merge queue Oct 18, 2026
Merged via the queue into apache:main with commit a2b405f Oct 18, 2026
35 checks passed
alamb added a commit that referenced this pull request Oct 20, 2026
…ializing (#11672)

- Backport #11638
- For inclusion in #11664

Cherry-pick of
a2b405f

---------

Co-authored-by: ump45nose <[email protected]>
Co-authored-by: ump45nose <[email protected]>
Co-authored-by: Andrew Lamb <[email protected]>
alamb added a commit that referenced this pull request Oct 22, 2026
…ializing (#11760)

- Backport #11638
- For inclusion in #11665

Cherry-pick of
a2b405f

Co-authored-by: ump45nose <[email protected]>
Co-authored-by: ump45nose <[email protected]>
Co-authored-by: Andrew Lamb <[email protected]>
alamb pushed a commit that referenced this pull request Oct 22, 2026
…ializing (#11813)

- Backport #11638
- For inclusion in #11666

Cherry-pick of
a2b405f

Co-authored-by: ump45nose <[email protected]>
Co-authored-by: ump45nose <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arrow-core Core crates related to representing arrow data bug Squash those bugs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UnionFields serde allows bypass of validation

2 participants