Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .changes/unreleased/fixed-20261120-json-duplicate-fields.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
kind: Fixed
body: |-
**Generated JSON decoding rejects a field given twice in a message that has a `oneof`**. A message with a `oneof` gets a hand-written `Deserialize` impl, which kept the last value of a repeated key, so `{"id": "1", "id": "2"}` decoded as `id = 2` and `{"fooBar": 1, "foo_bar": 2}` (the JSON name, then the proto name) as `foo_bar = 2`. It now returns serde's `duplicate field` error, as the derived impl on a message without a `oneof` and `DynamicMessage::from_json` already did, and as the proto3 JSON spec and the `Recommended.*.JsonInput.FieldNameDuplicate*` conformance tests require. Duplicate members of a `oneof` were already rejected.
time: 2026-11-20T03:30:00+00:00
15 changes: 12 additions & 3 deletions buffa-codegen/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1437,15 +1437,24 @@ fn custom_deser_regular_field(
};

// Match arm accepting both json_name and proto_name. `accepted_keys`
// lists the same two names.
// lists the same two names. A field given twice, under either name, is
// an error, as on the derive path.
let body = quote! {
if #var_ident.is_some() {
return ::core::result::Result::Err(
<__A::Error as ::serde::de::Error>::duplicate_field(#json_name),
);
}
#var_ident = Some(#deser_expr);
};
let (arm, accepted_keys) = if json_name != field_name {
(
quote! { #json_name | #field_name => { #var_ident = Some(#deser_expr); } },
quote! { #json_name | #field_name => { #body } },
vec![json_name.to_string(), field_name.to_string()],
)
} else {
(
quote! { #json_name => { #var_ident = Some(#deser_expr); } },
quote! { #json_name => { #body } },
vec![json_name.to_string()],
)
};
Expand Down
47 changes: 47 additions & 0 deletions buffa-codegen/src/tests/json_codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,3 +673,50 @@ fn message_named_result_does_not_shadow_std_result_in_serde() {
"serde Deserialize should use ::core::result::Result.\nGenerated code:\n{content}"
);
}

#[test]
fn test_json_custom_deserialize_rejects_duplicate_regular_fields() {
let mut file = proto3_file("dup_fields.proto");
file.message_type.push(DescriptorProto {
name: Some("WithOneof".to_string()),
field: vec![
FieldDescriptorProto {
name: Some("foo_bar".to_string()),
number: Some(1),
label: Some(Label::LABEL_OPTIONAL),
r#type: Some(Type::TYPE_INT32),
json_name: Some("fooBar".to_string()),
..Default::default()
},
FieldDescriptorProto {
name: Some("name".to_string()),
number: Some(2),
label: Some(Label::LABEL_OPTIONAL),
r#type: Some(Type::TYPE_STRING),
oneof_index: Some(0),
json_name: Some("name".to_string()),
..Default::default()
},
],
oneof_decl: vec![OneofDescriptorProto {
name: Some("kind".to_string()),
..Default::default()
}],
..Default::default()
});

let files = generate(&[file], &["dup_fields.proto".to_string()], &json_config())
.expect("should generate");
let content = &joined(&files);

// Both names of the regular field share one arm, guarded by the local.
assert!(
content.contains(r#""fooBar" | "foo_bar" =>"#),
"regular field arm must accept both names: {content}"
);
assert!(
content.contains("__f_foo_bar.is_some()")
&& content.contains("::serde::de::Error>::duplicate_field("),
"regular field arm must reject a repeated key: {content}"
);
}
Loading
Loading