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

Flatten Option permits deserialization without Token::Some in the token stream, but non-flatten not #3

Open
Mingun opened this issue Oct 15, 2020 · 0 comments

Comments

@Mingun
Copy link
Contributor

Mingun commented Oct 15, 2020

I found that test in the sources:
https://github.com/serde-rs/serde/blob/e6f086d85edfa3bde3f4486142301962ec5f1c8c/test_suite/tests/test_annotations.rs#L1824-L1925

Can anybody explain, why deserialize tokens don't match the serialized one (Token::Some is missed)? For me that seems as an error, because non-flatten option can't be deserialized in the same manner:

use serde::Deserialize;
use serde_test::{assert_de_tokens, Token};

#[derive(Debug, PartialEq, Deserialize)]
struct Outer {
  #[serde(flatten)]
  inner: Inner,
}

#[derive(Debug, PartialEq, Deserialize)]
struct Inner {
  option: Option<u64>,
}

/// Failed with
/// tokens failed to deserialize: invalid type: integer `2`, expected option
#[test]
fn ordinal() {
  assert_de_tokens(
    &Inner {
      option: Some(2),
    },
    &[
      Token::Map { len: None },
      Token::Str("option"),
      Token::U64(2),
      Token::MapEnd,
    ],
  );
}
/// Success (!?)
#[test]
fn flatten() {
  assert_de_tokens(
    &Outer {
      inner: Inner {
        option: Some(2),
      },
    },
    &[
      Token::Map { len: None },
      Token::Str("option"),
      Token::U64(2),
      Token::MapEnd,
    ],
  );
}
@dtolnay dtolnay transferred this issue from serde-rs/serde Jul 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant