-
-
Notifications
You must be signed in to change notification settings - Fork 170
Deserialize empty file to struct containing no required fields #86
Comments
Thanks! I changed the title because I was not able to reproduce the panic -- probably the panic is coming from an I would welcome a PR to support deserializing an empty YAML input to a struct containing no required fields. |
A minimal test case:
Should result in a YAML In my case, the file isn't literally empty, but contains only comments. I tried to coerce the value by doing |
I'd like to write a PR for this, but I'm not familiar with serde internals. I added some tests to get started. In the case of a completely empty document (no --- document start) the event loader will be totally empty. Looking at the code it looks like src/de.rs:1039 is the cause for failure for a completely empty document. But I'm not sure how to detect whether or not the type we're deserializing |
So I've looked into this too and I have a bit more information on the matter. The The deserializer called first for an empty file is Since this is an interruption in an active map, that means that At this point it's possible to just call And this is where I don't quite understand what's going on. Next the Obviously the other changes also change @dtolnay are you aware of which are of code this change is likely required to touch? I've seen that |
It would be helpful for us if at least the error raised when an empty document is detected (if I'm interpreting this code correctly) was a different error type and distinguishable from other unexpected EOF cases. |
This is fixed in 0.9. use serde::Deserialize;
#[derive(Deserialize, Debug)]
struct Struct {
optional: Option<String>,
}
fn main() -> Result<(), serde_yaml::Error> {
let value: Struct = serde_yaml::from_str("")?;
println!("{:#?}", value);
Ok(())
} Struct {
optional: None,
} |
serde-yaml resolves dtolnay/serde-yaml#86 in 0.9.x. This patch bumps it to the latest version to clean up legacy codes.
serde-yaml resolves dtolnay/serde-yaml#86 in 0.9.x. This patch bumps it to the latest version to clean up legacy codes.
... even though there are no non-optional fields in the struct.
Or any fields at all, for that matter.
Attempting to deserialize an empty file (or a file containing only newlines) produces the following error:
IMO deserializing an empty file should succeed as long as there are no required fields in the struct.
(I'm using serde_yaml 0.7.3)
The text was updated successfully, but these errors were encountered: