ReflectDeserializer does not error when deserializing incomplete enum value
- Dominant language
- Rust
- Stars
- 48.2k
- Forks
- 4.8k
- Avg merge
- 3d 16h
- Merged PRs (30d)
- 171
Description
## Bevy version
v0.13.0
## What you did
```rust
#[derive(Reflect)]
enum E {
A,
B{ a: usize, b: usize },
}
```
1. Initialized an enum to `E::A`.
2. Created a reflected `E::B` with only one field by deserializing it from JSON using `TypedReflectDeserializer`.
3. Applied the incomplete reflected `E::B` to the enum with value `E::A`, using `Reflect::apply`.
## What went wrong
[This panic](https://github.com/bevyengine/bevy/blob/512b7463a326d0048f16c2956d502395c5cb3cba/crates/bevy_reflect/bevy_reflect_derive/src/enum_utility.rs#L61) triggered. Basically, the reflect machinery cannot apply an incomplete enum variant to a different enum variant.
This is a bug because I expect the deserialize-from-JSON step to catch this kind of error so you never hit the panic. Note that, in contrast to enums, if you reflect-deserialize a struct with missing fields, then a serialization error will occur.
## Additional information
You can avoid this panic by using `FromReflect::from_reflect` instead of `Reflect::apply`, which will fail without panicking.
If you don't want `FromReflect::from_reflect` to fail when a field is missing, use `#[reflect(default)]` on potentially-missing fields.
Contributor guide
Research direction
Start in crates/bevy_reflect/bevy_reflect_derive/src/enum_utility.rs at the linked panic, then trace how TypedReflectDeserializer handles enum variants with missing fields. Compare this behavior with struct deserialization and the FromReflect path. Done means incomplete enum values are rejected during deserialization rather than reaching Reflect::apply and panicking.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- game-dev
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 52/100