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

Bad error location with flatten tagged enum #2581

Closed
fixcik opened this issue Aug 20, 2023 · 1 comment
Closed

Bad error location with flatten tagged enum #2581

fixcik opened this issue Aug 20, 2023 · 1 comment

Comments

@fixcik
Copy link

fixcik commented Aug 20, 2023

Hi! I attempted to deserialize a structure with a flattened tagged enum. However, when providing incorrect input data, the error's location is not properly indicated.

Code sample:

use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
struct Task {
    id: String,
    #[serde(flatten)]
    task_type: TaskType,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "type", content = "params", rename_all = "snake_case")]
enum TaskType {
    Complex(ComplexTaskParams),
}

#[derive(Debug, Serialize, Deserialize)]
struct ComplexTaskParams {
    param_one: String,
    param_two: String,
}

fn main() {
    let yaml = r#"
        id: 1
        type: complex
        params:
            param_one: value_one
            param_two: 123
    "#;

    let task: Task = serde_yaml::from_str(yaml).unwrap();
    print!("{:?}", task);
}

Expected error location of param_two - line: 5, but it's locate start position of Task:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: 
Error("invalid type: integer `123`, expected a string", line: 2, column: 9)', src/main.rs:31:49
@jonasbb
Copy link
Contributor

jonasbb commented Aug 20, 2023

This is a duplicate of #1183. With flatten the data gets deserialized into an intermediate buffer, advancing the location in the yaml file, but then fails to deserialize the buffer data into the struct.

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

3 participants