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

Nested structs cause DynamicScene to fail to deserialize #10499

Closed
hankjordan opened this issue Nov 10, 2023 · 0 comments · Fixed by #10705
Closed

Nested structs cause DynamicScene to fail to deserialize #10499

hankjordan opened this issue Nov 10, 2023 · 0 comments · Fixed by #10705
Labels
A-Reflection Runtime information about types A-Scenes Serialized ECS data stored on the disk C-Bug An unexpected or incorrect behavior
Milestone

Comments

@hankjordan
Copy link
Contributor

Bevy version

0.12.0

What you did

use bevy::{
    prelude::*,
    scene::{
        serde::{
            SceneDeserializer,
            SceneSerializer,
        },
        DynamicEntity,
    },
};
use serde::{
    de::DeserializeSeed,
    Serialize,
};

#[derive(Component, Default, Reflect)]
#[reflect(Component)]
struct Parent {
    value: Option<Child>,
}

#[derive(Reflect)]
struct Child {
    value: u32,
}

fn main() {
    let mut app = App::new();

    app.add_plugins(MinimalPlugins);

    app.register_type::<Parent>()
        .register_type::<Child>()
        .register_type::<Option<Child>>();

    let world = &mut app.world;

    let registry = world.resource::<AppTypeRegistry>();

    let scene = DynamicScene {
        resources: vec![],
        entities: vec![DynamicEntity {
            entity: Entity::from_raw(0),
            components: vec![Parent { value: None }.clone_value()],
        }],
    };

    let ser = SceneSerializer::new(&scene, registry);

    let mut buf = Vec::new();

    ser.serialize(&mut serde_json::Serializer::new(&mut buf))
        .expect("Failed to serialize");

    let de = SceneDeserializer {
        type_registry: &registry.read(),
    };

    de.deserialize(&mut serde_json::Deserializer::from_slice(&buf))
        .expect("Failed to deserialize");
}

What went wrong

Failed to deserialize: Error("invalid type: string \"None\", expected reflected struct value")

Additional information

This is also triggered by built-in types like Sprite.

Related: #6891

@hankjordan hankjordan added C-Bug An unexpected or incorrect behavior A-Reflection Runtime information about types A-Scenes Serialized ECS data stored on the disk labels Nov 10, 2023
@cart cart added this to the 0.12.1 milestone Nov 21, 2023
github-merge-queue bot pushed a commit that referenced this issue Nov 23, 2023
# Objective

- Fix #10499 

## Solution

- Use `.get_represented_type_info()` module path and type ident instead
of `.reflect_*` module path and type ident when serializing the `Option`
enum

---

## Changelog

- Fix serialization bug
- Add simple test
  - Add `serde_json` dev dependency
- Add `serde` with `derive` feature dev dependency (wouldn't compile for
me without it)

---------

Co-authored-by: hank <hank@hank.co.in>
Co-authored-by: Gino Valente <49806985+MrGVSV@users.noreply.github.com>
cart pushed a commit that referenced this issue Nov 30, 2023
# Objective

- Fix #10499 

## Solution

- Use `.get_represented_type_info()` module path and type ident instead
of `.reflect_*` module path and type ident when serializing the `Option`
enum

---

## Changelog

- Fix serialization bug
- Add simple test
  - Add `serde_json` dev dependency
- Add `serde` with `derive` feature dev dependency (wouldn't compile for
me without it)

---------

Co-authored-by: hank <hank@hank.co.in>
Co-authored-by: Gino Valente <49806985+MrGVSV@users.noreply.github.com>
rdrpenguin04 pushed a commit to rdrpenguin04/bevy that referenced this issue Jan 9, 2024
# Objective

- Fix bevyengine#10499 

## Solution

- Use `.get_represented_type_info()` module path and type ident instead
of `.reflect_*` module path and type ident when serializing the `Option`
enum

---

## Changelog

- Fix serialization bug
- Add simple test
  - Add `serde_json` dev dependency
- Add `serde` with `derive` feature dev dependency (wouldn't compile for
me without it)

---------

Co-authored-by: hank <hank@hank.co.in>
Co-authored-by: Gino Valente <49806985+MrGVSV@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Reflection Runtime information about types A-Scenes Serialized ECS data stored on the disk C-Bug An unexpected or incorrect behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants