Skip to content

Reject two non-null members of one oneof when reading JSON - #3824

Open
oldergod wants to merge 1 commit into
masterfrom
bquenaudon.2026-11-24.json-oneof-duplicates
Open

oldergod wants to merge 1 commit into
masterfrom
bquenaudon.2026-11-24.json-oneof-duplicates

Conversation

@oldergod

Copy link
Copy Markdown
Member

Wire 8.0.0-alpha01 keeps the last member when JSON sets two members of one oneof.
Wire 7 rejected this input for generated Java and for generated Kotlin with flat oneofs, like protobuf-java does.
The change came from #3781, which made the shared field bindings keep the last member.
That rule is right for binary decoding, but Moshi and Gson use the same bindings.
FieldOrOneOfBinding.setFromJson now assigns a oneof member without clearing the other members.
build() then throws the same IllegalArgumentException as in Wire 7, with the same message.
The Moshi and Gson adapters call it.
Binary decoding does not change, and the last member on the wire still wins.
Boxed and sealed oneofs keep the last member in JSON, as they did in Wire 7.
A null member next to a value is skipped, so the value stays, in either order.
The Swift runtime does not change.

Release note: Moshi and Gson fail again when JSON sets two non-null members of one oneof in generated Java or flat generated Kotlin code, as in Wire 7.

Wire 7 failed when JSON set two members of one oneof for generated Java and for generated Kotlin with flat oneofs.
Wire 8.0.0-alpha01 keeps the last member, because FieldBinding now calls the oneof builder method for every decoder.
That rule is right for binary decoding, but Moshi and Gson share the bindings.
Add FieldOrOneOfBinding.setFromJson, which assigns the member without clearing the others, so build() fails as before.
Binary decoding keeps the last member.
Boxed and sealed oneofs keep the last member in JSON, as in Wire 7.
A null member next to a value is skipped and keeps the value.

This branch has not been deployed

No deployments
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