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

#[serde(other)] not working as expected on enum tuple-like variants #359

Open
b-zee opened this issue Jan 28, 2025 · 0 comments
Open

#[serde(other)] not working as expected on enum tuple-like variants #359

b-zee opened this issue Jan 28, 2025 · 0 comments

Comments

@b-zee
Copy link

b-zee commented Jan 28, 2025

I have a use case where I want to have the possibility to introduce a breaking change to a data structure. If my app currently only supports version 0, it will detect when a version 1 is used. I have written the following code, but I get an error instead of matching on the #[serde(other)] variant. (Inspiration taken from https://stackoverflow.com/a/70380491)

#[derive(serde::Serialize, serde::Deserialize)]
enum A {
    V0(String),
    #[serde(other)]
    Unsupported,
}

#[derive(serde::Serialize, serde::Deserialize)]
enum B {
    V0(String),
    V1(usize),
    #[serde(other)]
    Unsupported,
}

#[test]
fn test() {
    let b_v1 = rmp_serde::to_vec(&B::V1(1)).unwrap();

    // Yields `Err(rmp_serde::decode::Error::TypeMismatch(rmp::Marker::FixPos(1)))`
    let a_unsupported = rmp_serde::from_slice::<A>(b_v1.as_slice());
    // But instead, I would expect this to pass (`Ok(A::Unsupported)`):
    assert!(matches!(a_unsupported, Ok(A::Unsupported)));
}

The above code does work when I change the A and B enums to be unit-only:

 #[derive(serde::Serialize, serde::Deserialize)]
 enum A {
-    V0(String),
+    V0,
     #[serde(other)]
     Unsupported,
 }
 
 #[derive(serde::Serialize, serde::Deserialize)]
 enum B {
-    V0(String),
-    V1(usize),
+    V0,
+    V1,
     #[serde(other)]
     Unsupported,
 }
 
 #[test]
 fn test() {
-    let b_v1 = rmp_serde::to_vec(&B::V1(1)).unwrap();
+    let b_v1 = rmp_serde::to_vec(&B::V1).unwrap();
 
     // Yields `Err(rmp_serde::decode::Error::TypeMismatch(rmp::Marker::FixPos(1)))`
     let a_unsupported = rmp_serde::from_slice::<A>(b_v1.as_slice());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant