Skip to content

Commit

Permalink
feat(protobuf): Allow unknown fields during serialization (#83)
Browse files Browse the repository at this point in the history
## What ❔

Allowing to use unknown fields during deserialization

## Why ❔

It allows us to parse config schemas even if the config value is unknown

---------

Signed-off-by: Danil <[email protected]>
  • Loading branch information
Deniallugo authored Mar 28, 2024
1 parent ce92795 commit 8833a9b
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions node/libs/protobuf/src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,25 @@ pub fn serialize<T: ProtoFmt, S: serde::Serializer>(x: &T, s: S) -> Result<S::Ok
serialize_proto(&x.build(), s)
}

/// Implementation of serde::Deserialize for arbitrary ReflectMessage.
/// Implementation of serde::Deserialize for arbitrary ReflectMessage denying unknown fields
pub fn deserialize_proto<'de, T: ReflectMessage + Default, D: serde::Deserializer<'de>>(
d: D,
) -> Result<T, D::Error> {
deserialize_proto_with_options(d, true)
}

/// Implementation of serde::Deserialize for arbitrary ReflectMessage with deny_unknown_fields option
pub fn deserialize_proto_with_options<
'de,
T: ReflectMessage + Default,
D: serde::Deserializer<'de>,
>(
d: D,
deny_unknown_fields: bool,
) -> Result<T, D::Error> {
let mut p = T::default();
let msg = prost_reflect::DynamicMessage::deserialize(p.descriptor(), d)?;
let options = prost_reflect::DeserializeOptions::new().deny_unknown_fields(deny_unknown_fields);
let msg = prost_reflect::DynamicMessage::deserialize_with_options(p.descriptor(), d, &options)?;
p.merge(msg.encode_to_vec().as_slice()).unwrap();
Ok(p)
}
Expand Down

0 comments on commit 8833a9b

Please sign in to comment.