Skip to content

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

Open
@vegardgs-ksat

Description

@vegardgs-ksat

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.

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