Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with Option serialization #10705

Merged
merged 7 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/bevy_reflect/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ smol_str = { version = "0.2.0", optional = true }
ron = "0.8.0"
rmp-serde = "1.1"
bincode = "1.3"
serde_json = "1.0"
serde = { version = "1", features = ["derive"] }
hankjordan marked this conversation as resolved.
Show resolved Hide resolved

[[example]]
name = "reflect_docs"
Expand Down
59 changes: 53 additions & 6 deletions crates/bevy_reflect/src/serde/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,13 @@ impl<'a> Serialize for EnumSerializer<'a> {

match variant_type {
VariantType::Unit => {
if self.enum_value.reflect_module_path() == Some("core::option")
&& self.enum_value.reflect_type_ident() == Some("Option")
let table = self
.enum_value
.get_represented_type_info()
.map(|i| i.type_path_table());
hankjordan marked this conversation as resolved.
Show resolved Hide resolved

if table.and_then(|t| t.module_path()) == Some("core::option")
&& table.and_then(|t| t.ident()) == Some("Option")
{
serializer.serialize_none()
} else {
Expand Down Expand Up @@ -352,10 +357,13 @@ impl<'a> Serialize for EnumSerializer<'a> {
}
VariantType::Tuple if field_len == 1 => {
let field = self.enum_value.field_at(0).unwrap();
if self
let table = self
.enum_value
.reflect_type_path()
.starts_with("core::option::Option")
.get_represented_type_info()
.map(|i| i.type_path_table());
hankjordan marked this conversation as resolved.
Show resolved Hide resolved

if table.and_then(|t| t.module_path()) == Some("core::option")
&& table.and_then(|t| t.ident()) == Some("Option")
{
serializer.serialize_some(&TypedReflectSerializer::new(field, self.registry))
} else {
Expand Down Expand Up @@ -464,8 +472,8 @@ impl<'a> Serialize for ArraySerializer<'a> {

#[cfg(test)]
mod tests {
use crate as bevy_reflect;
use crate::serde::ReflectSerializer;
use crate::{self as bevy_reflect, Struct};
use crate::{Reflect, ReflectSerialize, TypeRegistry};
use bevy_utils::HashMap;
use ron::extensions::Extensions;
Expand Down Expand Up @@ -881,4 +889,43 @@ mod tests {

assert_eq!(expected, bytes);
}

#[test]
fn should_serialize_dynamic_option() {
#[derive(Default, Reflect)]
struct OtherStruct {
some: Option<SomeStruct>,
none: Option<SomeStruct>,
}

let value = OtherStruct {
some: Some(SomeStruct { foo: 999999999 }),
none: None,
};
let dynamic = value.clone_dynamic();
let reflect = dynamic.as_reflect();

let registry = get_registry();

let serializer = ReflectSerializer::new(reflect, &registry);

let mut buf = Vec::new();

let format = serde_json::ser::PrettyFormatter::with_indent(b" ");
let mut ser = serde_json::Serializer::with_formatter(&mut buf, format);

serializer.serialize(&mut ser).unwrap();

let output = std::str::from_utf8(&buf).unwrap();
let expected = r#"{
"bevy_reflect::serde::ser::tests::OtherStruct": {
"some": {
"foo": 999999999
},
"none": null
}
}"#;

assert_eq!(expected, output);
}
}
Loading