Skip to content

TypeRegistrationDeserializer: allow fallback to short type paths #18885

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all 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
20 changes: 14 additions & 6 deletions crates/bevy_reflect/src/serde/de/registrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ use serde::de::{DeserializeSeed, Error, Visitor};

/// A deserializer for type registrations.
///
/// This will return a [`&TypeRegistration`] corresponding to the given type.
/// This deserializer expects a string containing the _full_ [type path] of the
/// type to find the `TypeRegistration` of.
/// The string provided may be **either** of the following:
/// * the *full* [type path] (`"my_crate::module::MyType"`), **or**
/// * the *short* type path returned by [`TypePath::short_type_path`]
/// (`"MyType"`).
///
/// For backward‑compatibility the deserializer first attempts a lookup with the
/// full type path; if no registration is found it falls back to the short type
/// path.
Comment on lines +12 to +14
Copy link
Member

Choose a reason for hiding this comment

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

This isn't just a backward compatible thing. We always want to prioritize full type paths since short paths are more likely to conflict. So I would maybe change the wording a bit here to indicate the intended priority.

///
/// [`&TypeRegistration`]: TypeRegistration
/// [type path]: crate::TypePath::type_path
Expand Down Expand Up @@ -40,9 +45,12 @@ impl<'a, 'de> DeserializeSeed<'de> for TypeRegistrationDeserializer<'a> {
where
E: Error,
{
self.0.get_with_type_path(type_path).ok_or_else(|| {
make_custom_error(format_args!("no registration found for `{type_path}`"))
})
self.0
.get_with_type_path(type_path)
.or_else(|| self.0.get_with_short_type_path(type_path))
.ok_or_else(|| {
make_custom_error(format_args!("no registration found for `{type_path}`"))
})
}
}

Expand Down
Loading