From 608b8065343b5427e1bcc649d00feb77543aec7e Mon Sep 17 00:00:00 2001 From: Danil Date: Thu, 28 Mar 2024 16:32:08 +0100 Subject: [PATCH 1/2] feat(protobuf): Allow unknown fields during serialization Signed-off-by: Danil --- node/libs/protobuf/src/serde.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/node/libs/protobuf/src/serde.rs b/node/libs/protobuf/src/serde.rs index 03532551..03fb1a54 100644 --- a/node/libs/protobuf/src/serde.rs +++ b/node/libs/protobuf/src/serde.rs @@ -42,7 +42,8 @@ pub fn deserialize_proto<'de, T: ReflectMessage + Default, D: serde::Deserialize d: D, ) -> Result { let mut p = T::default(); - let msg = prost_reflect::DynamicMessage::deserialize(p.descriptor(), d)?; + let options = prost_reflect::DeserializeOptions::new().deny_unknown_fields(false); + let msg = prost_reflect::DynamicMessage::deserialize_with_options(p.descriptor(), d, &options)?; p.merge(msg.encode_to_vec().as_slice()).unwrap(); Ok(p) } From 27954cb901c422c60b35b1fd360ae3ed87c810d7 Mon Sep 17 00:00:00 2001 From: Danil Date: Thu, 28 Mar 2024 16:49:24 +0100 Subject: [PATCH 2/2] Introduce additional method that keeps the default behaviour the same Signed-off-by: Danil --- node/libs/protobuf/src/serde.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/node/libs/protobuf/src/serde.rs b/node/libs/protobuf/src/serde.rs index 03fb1a54..1a2308fc 100644 --- a/node/libs/protobuf/src/serde.rs +++ b/node/libs/protobuf/src/serde.rs @@ -37,12 +37,24 @@ pub fn serialize(x: &T, s: S) -> Result>( d: D, +) -> Result { + 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 { let mut p = T::default(); - let options = prost_reflect::DeserializeOptions::new().deny_unknown_fields(false); + 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)