From 27954cb901c422c60b35b1fd360ae3ed87c810d7 Mon Sep 17 00:00:00 2001 From: Danil Date: Thu, 28 Mar 2024 16:49:24 +0100 Subject: [PATCH] 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)