Skip to content

Commit 591edc4

Browse files
committed
serialize processor unit tests
1 parent c81cbb1 commit 591edc4

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-6
lines changed

crates/bevy_reflect/src/serde/ser/mod.rs

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ mod tests {
2121
PartialReflect, Reflect, ReflectSerialize, Struct, TypeRegistry,
2222
};
2323
use bevy_utils::{HashMap, HashSet};
24+
use core::any::TypeId;
2425
use core::{f32::consts::PI, ops::RangeInclusive};
2526
use ron::{extensions::Extensions, ser::PrettyConfig};
2627
use serde::{Serialize, Serializer};
27-
use std::any::TypeId;
2828

2929
#[derive(Reflect, Debug, PartialEq)]
3030
struct MyStruct {
@@ -511,8 +511,8 @@ mod tests {
511511
let mut registry = TypeRegistry::new();
512512
registry.register::<Foo>();
513513

514-
let mut processor = FooProcessor;
515-
let serializer = ReflectSerializer::with_processor(&value, &registry, &mut processor);
514+
let processor = FooProcessor;
515+
let serializer = ReflectSerializer::with_processor(&value, &registry, &processor);
516516
let output = ron::ser::to_string_pretty(&serializer, PrettyConfig::default()).unwrap();
517517
let expected = r#"{
518518
"bevy_reflect::serde::ser::tests::Foo": (
@@ -573,8 +573,8 @@ mod tests {
573573
registry.register::<Foo>();
574574
registry.register::<SubFoo>();
575575

576-
let mut processor = FooProcessor;
577-
let serializer = ReflectSerializer::with_processor(&value, &registry, &mut processor);
576+
let processor = FooProcessor;
577+
let serializer = ReflectSerializer::with_processor(&value, &registry, &processor);
578578
let output = ron::ser::to_string_pretty(&serializer, PrettyConfig::default()).unwrap();
579579
let expected = r#"{
580580
"bevy_reflect::serde::ser::tests::Foo": (
@@ -586,6 +586,53 @@ mod tests {
586586
assert_eq!(expected, output);
587587
}
588588

589+
#[test]
590+
fn should_propagate_processor_serialize_error() {
591+
struct ErroringProcessor;
592+
593+
impl ReflectSerializerProcessor for ErroringProcessor {
594+
fn try_serialize<S>(
595+
&self,
596+
value: &dyn PartialReflect,
597+
_: &TypeRegistry,
598+
serializer: S,
599+
) -> Result<Result<S::Ok, S>, S::Error>
600+
where
601+
S: Serializer,
602+
{
603+
let Some(value) = value.try_as_reflect() else {
604+
return Ok(Err(serializer));
605+
};
606+
607+
let type_id = value.reflect_type_info().type_id();
608+
if type_id == TypeId::of::<i32>() {
609+
Err(serde::ser::Error::custom("my custom serialize error"))
610+
} else {
611+
Ok(Err(serializer))
612+
}
613+
}
614+
}
615+
616+
let value = 123_i32;
617+
618+
let registry = TypeRegistry::new();
619+
620+
let processor = ErroringProcessor;
621+
let serializer = ReflectSerializer::with_processor(&value, &registry, &processor);
622+
let error = ron::ser::to_string_pretty(&serializer, PrettyConfig::default()).unwrap_err();
623+
624+
#[cfg(feature = "debug_stack")]
625+
assert_eq!(
626+
error,
627+
ron::Error::Message("my custom deserialize error (stack: `i32`)".to_string())
628+
);
629+
#[cfg(not(feature = "debug_stack"))]
630+
assert_eq!(
631+
error,
632+
ron::Error::Message("my custom deserialize error".to_string())
633+
);
634+
}
635+
589636
#[cfg(feature = "functions")]
590637
mod functions {
591638
use super::*;

crates/bevy_reflect/src/serde/ser/serializer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ impl<P: ReflectSerializerProcessor> Serialize for TypedReflectSerializer<'_, P>
235235

236236
// First, check if our processor wants to serialize this type
237237
// This takes priority over any other serialization operations
238-
let serializer = if let Some(processor) = self.processor.as_deref() {
238+
let serializer = if let Some(processor) = self.processor {
239239
match processor.try_serialize(self.value, self.registry, serializer) {
240240
Ok(Ok(value)) => {
241241
return Ok(value);

0 commit comments

Comments
 (0)