Skip to content

Commit 0f4d16a

Browse files
authored
Don't ignore additional entries in UntypedReflectDeserializerVisitor (#7112)
# Objective Currently when `UntypedReflectDeserializerVisitor` deserializes a `Box<dyn Reflect>` it only considers the first entry of the map, silently ignoring any additional entries. For example the following RON data: ```json { "f32": 1.23, "u32": 1, } ``` is successfully deserialized as a `f32`, completly ignoring the `"u32": 1` part. ## Solution `UntypedReflectDeserializerVisitor` was changed to check if any other key could be deserialized, and in that case returns an error. --- ## Changelog `UntypedReflectDeserializer` now errors on malformed inputs instead of silently disgarding additional data. ## Migration Guide If you were deserializing `Box<dyn Reflect>` values with multiple entries (i.e. entries other than `"type": { /* fields */ }`) you should remove them or deserialization will fail.
1 parent 8fa94a0 commit 0f4d16a

File tree

1 file changed

+9
-2
lines changed
  • crates/bevy_reflect/src/serde

1 file changed

+9
-2
lines changed

crates/bevy_reflect/src/serde/de.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use crate::{
88
};
99
use erased_serde::Deserializer;
1010
use serde::de::{
11-
self, DeserializeSeed, EnumAccess, Error, MapAccess, SeqAccess, VariantAccess, Visitor,
11+
self, DeserializeSeed, EnumAccess, Error, IgnoredAny, MapAccess, SeqAccess, VariantAccess,
12+
Visitor,
1213
};
1314
use serde::Deserialize;
1415
use std::any::TypeId;
@@ -322,11 +323,17 @@ impl<'a, 'de> Visitor<'de> for UntypedReflectDeserializerVisitor<'a> {
322323
{
323324
let registration = map
324325
.next_key_seed(TypeRegistrationDeserializer::new(self.registry))?
325-
.ok_or_else(|| Error::invalid_length(0, &"at least one entry"))?;
326+
.ok_or_else(|| Error::invalid_length(0, &"a single entry"))?;
327+
326328
let value = map.next_value_seed(TypedReflectDeserializer {
327329
registration,
328330
registry: self.registry,
329331
})?;
332+
333+
if map.next_key::<IgnoredAny>()?.is_some() {
334+
return Err(Error::invalid_length(2, &"a single entry"));
335+
}
336+
330337
Ok(value)
331338
}
332339
}

0 commit comments

Comments
 (0)