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

Category::Syntax error returned on valid JSON when deserializing incompatible type into enum value #1224

Open
vegardgs-ksat opened this issue Dec 13, 2024 · 1 comment

Comments

@vegardgs-ksat
Copy link

I encountered the following situation:

Problem

#[derive(Debug, Deserialize, Serialize)]
pub struct ObjectWithAdt {
    pub kind: TypedVariant,
}

#[derive(Debug, Deserialize, Serialize)]
pub enum TypedVariant {
    A,
    B,
}

let payload = json!({
    "kind": 1,
});

let serialized = serde_json::to_string(&payload).unwrap();
let err = serde_json::from_str::<ObjectWithAdt>(&serialized).unwrap_err();
dbg!(&err);
assert_eq!(err.classify(), Category::Data);

Encountered the following error:

src/main.rs:55:5] &err = Error("expected value", line: 1, column: 9)
thread 'main' panicked at src/main.rs:56:5:
assertion `left == right` failed
  left: Syntax
 right: Data

Expectation

I expected something akin to this output:

src/main.rs:55:5] &err = Error("invalid type: integer `1`",  line: 1, column: 9)

I am sympathetic to the fact that which type to expect may be hard to capture as part of the error reporting. However, it comes across as incorrect that the error scenario is classified into Catalog::Syntax - the payload is clearly valid JSON.

See separate playground link for multiple permutations, including the failing scenario described herein.

@vegardgs-ksat
Copy link
Author

I just found this open PR which improves the error messages for this scenario - I assume this also changes the Category into the correct one as well

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