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: deserialize DynamicEnum using index #12464

Merged
merged 9 commits into from
Mar 14, 2024
Merged

Fix: deserialize DynamicEnum using index #12464

merged 9 commits into from
Mar 14, 2024

Conversation

mrchantey
Copy link
Contributor

@mrchantey mrchantey commented Mar 13, 2024

Objective

Solution

  • Change dynamic_enum.set_variant to dynamic_enum.set_variant_with_index in EnumVisitor

@alice-i-cecile alice-i-cecile requested a review from MrGVSV March 13, 2024 22:32
@alice-i-cecile alice-i-cecile added C-Bug An unexpected or incorrect behavior A-Reflection Runtime information about types A-Scenes Serialized ECS data stored on the disk labels Mar 13, 2024
@MrGVSV
Copy link
Member

MrGVSV commented Mar 13, 2024

I think I found the fix if you want to incorporate it in this PR:

The problem is here:

dynamic_enum.set_variant(variant_info.name(), value);

We need to change it to:

let variant_name = variant_info.name();
let variant_index = self
  .enum_info
  .index_of(variant_name)
  .expect("variant should exist");
dynamic_enum.set_variant_with_index(variant_index, variant_name, value);

Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great start! Can you add a matching test in bevy_reflect to check the serialization there? Presumably that's where the actual bug is: the scene machinery is likely redundant.

@mrchantey mrchantey changed the title Test for DynamicScene reserialization Fix: deserialize DynamicEnum using index Mar 13, 2024
@mrchantey
Copy link
Contributor Author

Awesome I added the bevy_reflect test, should I remove the bevy_scene test?

@MrGVSV
Copy link
Member

MrGVSV commented Mar 13, 2024

Awesome I added the bevy_reflect test, should I remove the bevy_scene test?

Yeah I think you can remove it. It would be nice to have a re-serialization test but I think the reflection test sorta makes it redundant. Maybe if there's certain behavior we want to verify re-serializes correctly, we add the test then.

But yeah as for now, I don't think it's super necessary.

@mrchantey
Copy link
Contributor Author

mrchantey commented Mar 13, 2024

Okay, refactored enum_should_reserialize into should_reserialize for MyStruct, now getting a new error:

---- serde::de::tests::should_reserialize stdout ----
thread 'serde::de::tests::should_reserialize' panicked at crates\bevy_reflect\src\serde\de.rs:1449:74:
called `Result::unwrap()` on an `Err` value: Message("Unexpected missing field `renamed` in `CustomDeserialize`")

Did we catch another bug? commenting out #[serde(rename = "renamed")] fixes the new test.

btw i also deduplicated the three identical MyStruct instantiations instead of copy-pasting a new one, let me know if that should be reverted.

@MrGVSV
Copy link
Member

MrGVSV commented Mar 14, 2024

Okay, refactored enum_should_reserialize into should_reserialize for MyStruct, now getting a new error:


---- serde::de::tests::should_reserialize stdout ----

thread 'serde::de::tests::should_reserialize' panicked at crates\bevy_reflect\src\serde\de.rs:1449:74:

called `Result::unwrap()` on an `Err` value: Message("Unexpected missing field `renamed` in `CustomDeserialize`")

Did we catch another bug? commenting out #[serde(rename = "renamed")] fixes the new test.

No I think this is expected behavior. We're serializing the Rust name but saying we expect the custom name (for a user the fix would be to register ReflectSerialize as well, otherwise they're declaring a one-way format).

Maybe let's try changing rename to alias? That should allow both to be used.

@mrchantey
Copy link
Contributor Author

Great yep using alias did the trick, I can confirm the new test fails without set_variant_with_index.

@alice-i-cecile
Copy link
Member

Great stuff: thanks for the contribution!

@alice-i-cecile alice-i-cecile added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Mar 14, 2024
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Mar 14, 2024
Merged via the queue into bevyengine:main with commit d3e4432 Mar 14, 2024
26 checks passed
@MrGVSV MrGVSV added this to the 0.13.1 milestone Mar 14, 2024
mockersf pushed a commit that referenced this pull request Mar 15, 2024
# Objective

- Addresses #12462
- When we serialize an enum, deserialize it, then reserialize it, the
correct variant should be selected.

## Solution

- Change `dynamic_enum.set_variant` to
`dynamic_enum.set_variant_with_index` in `EnumVisitor`
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 S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants