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

Deserialization for enums with #[serde(flatten)] is not working #39

Open
josecelano opened this issue Oct 9, 2023 · 1 comment
Open
Labels

Comments

@josecelano
Copy link
Collaborator

josecelano commented Oct 9, 2023

I think these two failing tests could be the same problem because they both use #[serde(flatten)] for an enum field.

  • cargo test ser_de_flattened_enum -- --nocapture
  • cargo test ser_de_flattened_adjacently_tagged_enum -- --nocapture

Failing test 1

$ cargo test ser_de_flattened_enum -- --nocapture
    Finished test [unoptimized + debuginfo] target(s) in 0.00s
     Running unittests src/lib.rs (target/debug/deps/torrust_serde_bencode-61b7eea2ea5618bf)

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

     Running tests/tests.rs (target/debug/deps/tests-b55451bec73c8180)

running 1 test
bytes: "d12:message_type8:Responsee"
bytes: "d12:message_type8:Responsee"
thread 'ser_de_flattened_enum' panicked at tests/tests.rs:34:41:
called `Result::unwrap()` on an `Err` value: InvalidType("Invalid Type: byte array (expected: `string or map`)")
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
test ser_de_flattened_enum ... FAILED

failures:

failures:
    ser_de_flattened_enum

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 42 filtered out; finished in 0.00s

error: test failed, to rerun pass `--test tests`

Failing test 2

$ cargo test ser_de_flattened_adjacently_tagged_enum -- --nocapture
    Finished test [unoptimized + debuginfo] target(s) in 0.00s
     Running unittests src/lib.rs (target/debug/deps/torrust_serde_bencode-61b7eea2ea5618bf)

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

     Running tests/tests.rs (target/debug/deps/tests-b55451bec73c8180)

running 1 test
bytes: "d7:contentd5:tokeni456ee2:idi123e4:type7:Requeste"
thread 'ser_de_flattened_adjacently_tagged_enum' panicked at tests/tests.rs:34:41:
called `Result::unwrap()` on an `Err` value: InvalidType("Invalid Type: byte array (expected: `string or map`)")
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
test ser_de_flattened_adjacently_tagged_enum ... FAILED

failures:

failures:
    ser_de_flattened_adjacently_tagged_enum

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 42 filtered out; finished in 0.00s

error: test failed, to rerun pass `--test tests`

Related commits

@ngoquang2708
Copy link

Using "tag" and "content" at the same time on the enum have the same issue:

#[derive(Deserialize, Serialize)]
struct A {
    #[serde(flatten)]
    b: B,
}

#[derive(Deserialize, Serialize)]
#[serde(tag = "a", content = "b")]
enum B {
    C,
}

let a = A { b: B::C };
let s = serde_bencode::to_string(&a).unwrap();
let _: A = serde_bencode::from_bytes(s.as_bytes()).unwrap();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants