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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
kind: Fixed
body: |-
**`DynamicMessage::from_json_ignoring_unknown` ignores an unknown enum name**. With `DynamicMessageSeed::ignore_unknown_fields(true)`, an enum name the enum does not declare was still a parse error, so `{"color": "PURPLE"}` failed where generated code with `JsonParseOptions::ignore_unknown_enum_values` and protobuf C++ with `ignore_unknown_fields` both accept it. The name is now ignored: a singular field is left unset, and the element of a repeated field or the entry of a map is dropped. Strict parsing is unchanged, and an unknown number for a closed enum is still an error. This passes the ten `Recommended.*.JsonInput.IgnoreUnknownEnumStringValue*` conformance tests under reflection.
time: 2026-11-20T21:00:00+00:00
112 changes: 94 additions & 18 deletions buffa-descriptor/src/reflect/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,11 @@ impl DynamicMessageSeed {
/// The setting propagates to nested messages, repeated elements, and map
/// values. See [`DynamicMessage::from_json_ignoring_unknown`].
///
/// It also covers enum names the enum does not declare, as in protobuf
/// C++ and Java: an unknown name leaves a singular field unset and drops
/// the element of a repeated field or the entry of a map. An unknown
/// number for a closed enum is still an error.
///
/// An unknown field inside a `google.protobuf.Any` payload is buffered
/// with the rest of the payload before it is discarded, and the buffer
/// counts toward the
Expand Down Expand Up @@ -984,7 +989,12 @@ impl<'de> DeserializeSeed<'de> for SingularSeed<'_> {
fn deserialize<D: Deserializer<'de>>(self, d: D) -> Result<Self::Value, D::Error> {
match self.kind {
SingularKind::Scalar(sc) => deserialize_optional_scalar(sc, d),
SingularKind::Enum(eidx) => deserialize_enum(self.pool, eidx, self.enum_type, d),
SingularKind::Enum(eidx) => Ok(
match deserialize_enum(self.pool, eidx, self.enum_type, self.flags, d)? {
EnumJson::Value(v) => Some(v),
EnumJson::Null | EnumJson::Unknown => None,
},
),
SingularKind::Message(midx) => {
// `google.protobuf.Value` treats JSON `null` as a present
// `null_value` member, not "unset" — dispatch straight to the
Expand Down Expand Up @@ -1206,29 +1216,45 @@ fn scalar_from_str<E: de::Error>(sc: ScalarType, v: &str) -> Result<Value, E> {
})
}

/// The outcome of reading one JSON enum value.
enum EnumJson {
Value(Value),
/// JSON `null` for an enum other than `google.protobuf.NullValue`.
Null,
/// A name the enum does not declare, read with
/// [`ParseFlags::ignore_unknown`] set: the field is left unset, or the
/// element or map entry dropped, as on the generated path.
Unknown,
}

fn deserialize_enum<'de, D: Deserializer<'de>>(
pool: &Arc<DescriptorPool>,
eidx: EnumIndex,
enum_type: Option<EnumType>,
flags: ParseFlags,
d: D,
) -> Result<Option<Value>, D::Error> {
) -> Result<EnumJson, D::Error> {
struct EnumVisitor<'a> {
pool: &'a Arc<DescriptorPool>,
eidx: EnumIndex,
enum_type: Option<EnumType>,
ignore_unknown: bool,
}
impl<'de> Visitor<'de> for EnumVisitor<'_> {
type Value = Option<Value>;
type Value = EnumJson;
fn expecting(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, "an enum string, number, or null")
}
fn visit_str<E: de::Error>(self, v: &str) -> Result<Self::Value, E> {
let ed = self.pool.enumeration(self.eidx);
ed.value_by_name(v)
.map(|ev| Some(Value::EnumNumber(ev.number)))
.ok_or_else(|| {
de::Error::invalid_value(de::Unexpected::Str(v), &"a known enum variant name")
})
match ed.value_by_name(v) {
Some(ev) => Ok(EnumJson::Value(Value::EnumNumber(ev.number))),
None if self.ignore_unknown => Ok(EnumJson::Unknown),
None => Err(de::Error::invalid_value(
de::Unexpected::Str(v),
&"a known enum variant name",
)),
}
}
fn visit_i64<E: de::Error>(self, v: i64) -> Result<Self::Value, E> {
let n = i32::try_from(v).map_err(de::Error::custom)?;
Expand All @@ -1237,7 +1263,7 @@ fn deserialize_enum<'de, D: Deserializer<'de>>(
if self.enum_type.unwrap_or(ed.enum_type) == EnumType::Closed && ed.value(n).is_none() {
return Err(de::Error::custom("unknown closed enum value"));
}
Ok(Some(Value::EnumNumber(n)))
Ok(EnumJson::Value(Value::EnumNumber(n)))
}
fn visit_u64<E: de::Error>(self, v: u64) -> Result<Self::Value, E> {
self.visit_i64(i64::try_from(v).map_err(de::Error::custom)?)
Expand All @@ -1247,9 +1273,9 @@ fn deserialize_enum<'de, D: Deserializer<'de>>(
// enums treat null as unset.
let ed = self.pool.enumeration(self.eidx);
if ed.full_name == "google.protobuf.NullValue" {
Ok(Some(Value::EnumNumber(0)))
Ok(EnumJson::Value(Value::EnumNumber(0)))
} else {
Ok(None)
Ok(EnumJson::Null)
}
}
fn visit_none<E: de::Error>(self) -> Result<Self::Value, E> {
Expand All @@ -1264,9 +1290,26 @@ fn deserialize_enum<'de, D: Deserializer<'de>>(
pool,
eidx,
enum_type,
ignore_unknown: flags.ignore_unknown,
})
}

/// [`DeserializeSeed`] for one element or map value of an enum field.
struct EnumSeed<'a> {
pool: &'a Arc<DescriptorPool>,
eidx: EnumIndex,
enum_type: Option<EnumType>,
flags: ParseFlags,
}

impl<'de> DeserializeSeed<'de> for EnumSeed<'_> {
type Value = EnumJson;

fn deserialize<D: Deserializer<'de>>(self, d: D) -> Result<Self::Value, D::Error> {
deserialize_enum(self.pool, self.eidx, self.enum_type, self.flags, d)
}
}

struct ListVisitor<'a> {
pool: &'a Arc<DescriptorPool>,
kind: SingularKind,
Expand All @@ -1285,6 +1328,26 @@ impl<'de> Visitor<'de> for ListVisitor<'_> {
}
fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
let mut out = Vec::new();
if let SingularKind::Enum(eidx) = self.kind {
let seed = || EnumSeed {
pool: self.pool,
eidx,
enum_type: self.enum_type,
flags: self.flags,
};
while let Some(v) = seq.next_element_seed(seed())? {
let v = match v {
EnumJson::Value(v) => v,
EnumJson::Unknown => continue,
EnumJson::Null => {
return Err(de::Error::custom("null element in repeated field"));
}
};
charge(self.budget, core::mem::size_of::<Value>())?;
out.push(v);
}
return Ok(Some(Value::List(out)));
}
while let Some(v) = seq.next_element_seed(SingularSeed {
pool: self.pool,
kind: self.kind,
Expand Down Expand Up @@ -1327,13 +1390,26 @@ impl<'de> Visitor<'de> for MapFieldVisitor<'_> {
let mut out: Vec<(MapKey, Value)> = Vec::new();
while let Some(key) = map.next_key::<String>()? {
let k = parse_map_key(self.key, &key).map_err(de::Error::custom)?;
let v = map.next_value_seed(SingularSeed {
pool: self.pool,
kind: self.value,
enum_type: self.enum_type,
flags: self.flags,
budget: self.budget,
})?;
let v = if let SingularKind::Enum(eidx) = self.value {
match map.next_value_seed(EnumSeed {
pool: self.pool,
eidx,
enum_type: self.enum_type,
flags: self.flags,
})? {
EnumJson::Value(v) => Some(v),
EnumJson::Unknown => continue,
EnumJson::Null => None,
}
} else {
map.next_value_seed(SingularSeed {
pool: self.pool,
kind: self.value,
enum_type: self.enum_type,
flags: self.flags,
budget: self.budget,
})?
};
let v = v.ok_or_else(|| de::Error::custom("null value in map field"))?;
charge(
self.budget,
Expand Down
31 changes: 31 additions & 0 deletions buffa-descriptor/tests/json_e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,37 @@ fn json_unknown_enum_name_error_names_the_value() {
);
}

#[test]
fn json_lenient_mode_ignores_unknown_enum_names() {
let p = pool();
let idx = p.message_index("reflect.test.Containers").unwrap();
// `color` is field 6, `colors` field 7.
let input = r#"{"color": "PURPLE", "colors": ["RED", "PURPLE", "BLUE"]}"#;
assert!(DynamicMessage::from_json(Arc::clone(&p), idx, input).is_err());
let m = DynamicMessage::from_json_ignoring_unknown(Arc::clone(&p), idx, input)
.expect("lenient parse succeeds");
assert_eq!(
m.field_by_number(6),
None,
"unknown name leaves the field unset"
);
assert_eq!(
m.field_by_number(7),
Some(&Value::List(vec![
Value::EnumNumber(1),
Value::EnumNumber(3)
])),
"unknown element is dropped"
);
// Null elements are still rejected in lenient mode.
assert!(DynamicMessage::from_json_ignoring_unknown(
Arc::clone(&p),
idx,
r#"{"colors": ["RED", null]}"#
)
.is_err());
}

#[test]
fn generated_enum_unknown_name_error_names_the_value() {
use buffa_descriptor::generated::descriptor::Edition;
Expand Down
6 changes: 4 additions & 2 deletions buffa/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,10 @@
//! modes. For open enums, it is read as the enum's default value.
//!
//! The option applies to generated message types. JSON parsing of a
//! `DynamicMessage` in `buffa-descriptor` does not read the option, and an
//! unknown enum name is a parse error there.
//! `DynamicMessage` in `buffa-descriptor` does not read the option: there an
//! unknown enum name is a parse error, unless the parse ignores unknown
//! fields (`DynamicMessageSeed::ignore_unknown_fields`), which ignores it the
//! same way.

/// Options controlling protobuf JSON parsing behavior.
///
Expand Down
Loading