Skip to content

ReflectDeserializer does not error when deserializing incomplete enum value #12357

Open
@UkoeHB

Description

@UkoeHB

Bevy version

v0.13.0

What you did

#[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 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ReflectionRuntime information about typesC-BugAn unexpected or incorrect behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions