Repository navigation
codegen: reject a JSON field given twice in a message with a oneof - #845
hamza-paracha wants to merge 1 commit into
Conversation
|
I have read the CLA Document and I hereby sign the CLA 1 out of 2 committers have signed the CLA. |
A message with a oneof (or an extension range) gets a hand-written
Deserialize impl. Its arms for regular fields overwrote the local on a
repeated key, so {"id": "1", "id": "2"} decoded as id = 2 and a JSON
name followed by the proto name kept the second value. The derived impl
and DynamicMessage::from_json already reject both with a duplicate field
error, as the proto3 JSON spec requires. Each arm now returns serde's
duplicate_field error when its field was already read.
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
3f686ed to
05dd71c
Compare
|
I have read the CLA Document and I hereby sign the CLA |
|
This fix was written with Devin, an AI coding agent, and its co-author trailer is on the commit. I've signed the CLA; a bot can't. Could a maintainer add |
|
Conformance evidence: I ran |
Summary
A message with a
oneof(or an extension range) gets a hand-writtenDeserializeimpl fromgenerate_custom_deserialize. Its arms for regular, non-oneof fields overwrote the field's local on every key, so a field given twice kept the last value:The other two JSON decoders reject both inputs: the serde-derived impl used for a message without a
oneof(duplicate field), andDynamicMessage::from_json(#138). The proto3 JSON spec and C++/Go reject it too, and the conformance suite'sRecommended.*.JsonInput.FieldNameDuplicate*tests expect a parse failure. Duplicateoneofmembers were already rejected (multiple oneof fields set for ...), so only regular fields had the gap.TestAllTypesProto3itself has aoneof, so the generated conformance binary accepts these inputs whileBUFFA_VIA_REFLECT=1rejects them.Each regular-field arm now checks its local first:
That is the same error the derive path gives, and it covers the JSON name, the proto name, or one of each, as well as an explicit
nullfollowed by a value (the derive path treatsnullas present too). Repeated and map fields, message fields andgoogle.protobuf.Valuefields go through the same arm.buffa-descriptor/src/generated/google.protobuf.descriptor.rsis regenerated (its messages with extension ranges use the custom impl). The WKT and option-type outputs come out unchanged.This changes behavior for input that repeats a key, which used to decode and now fails. The changelog fragment is under Fixed.
Testing
buffa-test:test_json_mixed_oneof_rejects_duplicate_fieldscovers a scalar, both name forms, a repeated field, a map, a message, aValueandnull-then-value. It fails onmainand passes with this change.buffa-codegen:test_json_custom_deserialize_rejects_duplicate_regular_fieldschec