Skip to content

Untagged enum does not play nice with u128 #1682

@WiSaGaN

Description

@WiSaGaN

I would expect both tests pass, but instead, only test64 passed, while test128 failed with

data did not match any variant of untagged enum E128

Maybe related to serde-rs/json#559

use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize)]
#[derive(Debug, PartialEq)]
pub struct S64 {
    pub f: u64,
}

#[derive(Deserialize, Serialize)]
#[serde(untagged)]
#[derive(Debug, PartialEq)]
pub enum E64 {
    S(S64),
}

#[derive(Deserialize, Serialize)]
#[derive(Debug, PartialEq)]
pub struct S128 {
    pub f: u128,
}

#[derive(Deserialize, Serialize)]
#[serde(untagged)]
#[derive(Debug, PartialEq)]
pub enum E128 {
    S(S128),
}


#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test64() {
        let incoming_str = r#"{"f":1}"#;
        let expected_incoming = E64::S(S64 { f: 1 });
        let incoming = serde_json::from_str(&incoming_str).unwrap();
        assert_eq!(expected_incoming, incoming);
    }

    #[test]
    fn test128() {
        let incoming_str = r#"{"f":1}"#;
        let expected_incoming = E128::S(S128 { f: 1 });
        let incoming = serde_json::from_str(&incoming_str).unwrap();
        assert_eq!(expected_incoming, incoming);
    }
}

playground

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions