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

Recursive paths are not correctly detected #6

Closed
SOF3 opened this issue Jun 6, 2020 · 3 comments
Closed

Recursive paths are not correctly detected #6

SOF3 opened this issue Jun 6, 2020 · 3 comments

Comments

@SOF3
Copy link

SOF3 commented Jun 6, 2020

use std::collections::HashMap;

use serde::*;

#[derive(Debug, Deserialize, Clone)]
pub struct Schema {
    #[serde(flatten)]
    typed: TypedSchema,
    description: Option<String>,
}

#[derive(Deserialize, Debug, Clone)]
#[serde(tag = "type")]
pub enum TypedSchema {
    Object(ObjectSchema),
    Array(ArraySchema),
}

#[derive(Deserialize, Clone, Debug)]
pub struct ObjectSchema {
    properties: HashMap<String, Schema>,
}

#[derive(Deserialize, Clone, Debug)]
pub struct ArraySchema {
    items: Box<Schema>,
}

fn main() {
    let r: Result<Schema, _> = dbg!(serde_path_to_error::deserialize(&mut serde_json::Deserializer::from_str(json())));
    if let Err(err) = r {
        println!("path: {}", err.path());
    }
}

fn json() -> &'static str{
    r#"
{
    "type": "Object",
    "properties": {
      "subkeys": {
          "properties": {
            "emails": { "type": "Array", "items": {} },
            "subkeys": { "type": "Array", "items": {} }
        }
      }
    }
}
    "#
}
[src/main.rs:30] serde_path_to_error::deserialize(&mut serde_json::Deserializer::from_str(json())) = Err(
    Error {
        path: Path {
            segments: [],
        },
        original: Error("missing field `type`", line: 12, column: 1),
    },
)
path: .

The errors are at emails and subkeys having empty objects, but the path was reported to be at ..

@dtolnay
Copy link
Owner

dtolnay commented Jun 6, 2020

Would you be able to minimize this to the smallest data structure that reproduces the problem? There is a lot going on in that code (attributes and types and so forth) and probably not all of it is related to this issue.

@SOF3
Copy link
Author

SOF3 commented Jun 23, 2020

It appears that this issue might be caused by an unnoticed upstream bug in serde or serde_json.

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=c68d1d013cbc59fbee638653af809ab2

The line and column reported by serde_json is also incorrect, pointing to EOF rather than the actual problematic line. However I am not sure if it is caused by serde or serde_json.

@dtolnay
Copy link
Owner

dtolnay commented Sep 12, 2021

I'll go ahead and close this in the absence of a repro involving serde_path_to_error.

@dtolnay dtolnay closed this as completed Sep 12, 2021
Repository owner locked and limited conversation to collaborators Mar 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants