From 353558737f3ef73e93164c596ff920d4344f30a3 Mon Sep 17 00:00:00 2001 From: Jean-Gabriel Gill-Couture Date: Mon, 14 Oct 2024 15:03:34 -0400 Subject: [PATCH] wip: Strict deserialization returns error on any unknown xml field or attribute --- yaserde/tests/deserializer.rs | 17 +++++++++++++++++ yaserde_derive/src/de/expand_struct.rs | 3 +++ 2 files changed, 20 insertions(+) diff --git a/yaserde/tests/deserializer.rs b/yaserde/tests/deserializer.rs index de218c6..fbdc88f 100644 --- a/yaserde/tests/deserializer.rs +++ b/yaserde/tests/deserializer.rs @@ -1117,3 +1117,20 @@ fn de_nested_macro_rules() { float_attrs!(f32); } + +#[test] +fn de_strict() { + init(); + + #[derive(PartialEq, Debug, YaDeserialize)] + pub struct Struct { + id: i32, + } + let xml_content = r#" + + 123 + + "#; + let load: Result = from_str(xml_content); + assert!(load.is_err()); +} diff --git a/yaserde_derive/src/de/expand_struct.rs b/yaserde_derive/src/de/expand_struct.rs index fca7486..90fddbe 100644 --- a/yaserde_derive/src/de/expand_struct.rs +++ b/yaserde_derive/src/de/expand_struct.rs @@ -431,6 +431,9 @@ pub fn parse( match (namespace.as_str(), name.local_name.as_str()) { #call_visitors _ => { + ::yaserde::__derive_trace!("SKIPPINGSKIPPING Skipping element {:?}", name.local_name); + return Err(format!("Found unauthorized element {}", name.local_name)); + let event = reader.next_event()?; #write_unused