Repository navigation
THRIFT-735: Reject unset unions in required-field validation - #4125
slachiewicz wants to merge 1 commit into
Conversation
4c640db to
79556a5
Compare
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are recorded for the required-union validation change.
No open issues 🎉
What changed in this PR
Updates Java code generation so required union fields fail validation when the union object exists but has no member set. This also covers typedef aliases while preserving validation behavior for optional unions.
Changes:
- Adds
isSet()checks for required union fields in generated Java validators. - Adds five regression tests covering populated, missing, unset, aliased, and optional unions.
- Registers the new Thrift test schema with Gradle.
| File | Description |
|---|---|
lib/java/src/test/resources/JavaRequiredUnion.thrift |
Defines required, aliased, and optional union test fields. |
lib/java/src/test/java/org/apache/thrift/TestRequiredUnionValidation.java |
Tests required and optional union validation behavior. |
lib/java/gradle/generateTestThrift.gradle |
Registers the new schema for Java test generation. |
compiler/cpp/src/thrift/generate/t_java_generator.cc |
Generates checks that reject unset required unions. |
Review effort: Balanced
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. Two suggestions, below the bar for an issue but verified by reading:
thrift/lib/java/gradle/generateTestThrift.gradle Lines 93 to 95 in 79556a5
thrift/compiler/cpp/src/thrift/generate/t_java_generator.cc Lines 2232 to 2246 in 79556a5 🤖 Generated with Claude Code |
Client: java validate() only null-checked a required field, so a union that was read with no recognized member (for example an enum value removed from the IDL) passed. The generated check now also requires TUnion.isSet(), for direct and typedef'd union fields in both the default and beans styles. Optional and non-union fields are unchanged. Tested with TestRequiredUnionValidation, which fails on the old compiler. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
79556a5 to
b732413
Compare
Jira: THRIFT-735
A required union field passed
validate()when the union object was present but had no field set, for example after an unknown member was skipped on read. The Java generator now also checksisSet()on required fields whose true type is a union, so such a struct fails withTProtocolExceptionlike an unset required field. Writing an unset union was already refused (TUnion.write()); only validation was missing.This makes
validate()stricter for that one case, so it is worth a line in the release notes.Verified:
gradle test --tests '*TestRequiredUnionValidation'→ master compiler 2 of 5 fail, this branch 5 of 5 pass; TestTUnion, TestTDeserializer and the recursion-limit tests pass.