-
-
Notifications
You must be signed in to change notification settings - Fork 791
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
Field aliases do not work in combination with flatten
#1504
Comments
I just got bitten by this too. |
This is a bug -- I would accept a PR to fix this. |
I'll try to give it a shot, but first time poking at serde_derive, so no promises :) |
The issue is that FlatStructAccess has no access to the aliases of the struct it's deserializing. Ideally we'd try to serialize the key rather than checking whether we're going to use it before-hand, then actually take the value out, but that happens to be tricky with the current seed API. So we need to somehow get the aliased names back to FlatStructAccess. Introduce a serialize_struct-like API that takes them in a backwards-compatible way. For parallelism, and since we also support aliases on enum variants, also extend the struct_variant API in a similar way. I'm open to better ways to fix it, but I can't think of any other that isn't a breaking change... Fixes serde-rs#1504.
The issue is that FlatStructAccess has no access to the aliases of the struct it's deserializing. Ideally we'd try to serialize the key rather than checking whether we're going to use it before-hand, then actually take the value out, but that happens to be tricky with the current seed API. So we need to somehow get the aliased names back to FlatStructAccess. Introduce a serialize_struct-like API that takes them in a backwards-compatible way. For parallelism, and since we also support aliases on enum variants, also extend the struct_variant API in a similar way. I'm open to better ways to fix it, but I can't think of any other that isn't a breaking change... Fixes serde-rs#1504.
This is gonna be useful to parse aggregate data. Patch with serde-rs/serde#1519 to work-around / fix serde-rs/serde#1504. That bound is needed due to deserialize_with, it's kind-of unfortunate that it cannot be inferred.
Yeah, I just got bit by this too. use serde::Deserialize;
use serde_json::from_str;
#[derive(Debug, Deserialize)]
struct Value {
#[serde(flatten)]
data: Ser,
}
#[derive(Debug, Deserialize)]
enum Ser {
#[serde(rename = "var", alias = "def")]
Variant(u8)
}
fn main() {
let input = r#"{ "def": 0 }"#;
println!("{:?}", from_str::<Ser>(input).unwrap());
println!("{:?}", from_str::<Value>(input).unwrap());
} |
After hunting around, I found the issue. We pass an array of valid variants directly into the I'm open to ideas. EDIT: I forgot that structs have this issue as well. We could add the aliased names to the arrays, and then internally, when it actually goes to deserialise it, sort out which one to use, etc, like normal. |
Serde fails to deserialize aliased fields in flattened structs: serde-rs/serde#1504
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
But what if... I were to rename the "name" field to "username" so that I can use it for Top10s and disguise it as "name" using an alias? Delightfully devilish, Seymour... Hacky fix to get around serde-rs/serde#1504. Alias and name should be swapped once the issue is fixed. Also I forgot to make a field public for some reason.
The issue is that FlatStructAccess has no access to the aliases of the struct it's deserializing. Ideally we'd try to serialize the key rather than checking whether we're going to use it before-hand, then actually take the value out, but that happens to be tricky with the current seed API. So we need to somehow get the aliased names back to FlatStructAccess. Introduce a serialize_struct-like API that takes them in a backwards-compatible way. For parallelism, and since we also support aliases on enum variants, also extend the struct_variant API in a similar way. I'm open to better ways to fix it, but I can't think of any other that isn't a breaking change... Fixes serde-rs#1504.
This comment has been minimized.
This comment has been minimized.
The issue is that FlatStructAccess has no access to the aliases of the struct it's deserializing. Ideally we'd try to serialize the key rather than checking whether we're going to use it before-hand, then actually take the value out, but that happens to be tricky with the current seed API. So we need to somehow get the aliased names back to FlatStructAccess. Introduce a serialize_struct-like API that takes them in a backwards-compatible way. For parallelism, and since we also support aliases on enum variants, also extend the struct_variant API in a similar way. I'm open to better ways to fix it, but I can't think of any other that isn't a breaking change... Fixes serde-rs#1504.
Field aliases as introduced in #1458 do not work when deserializing "flattened" structures.
Expected behavior
Field aliases should work when deserializing "flattened" structures.
Observed behavior
Field aliases do not work when deserializing "flattened" structures.
How to reproduce
The following code can be used to reproduce the problem, also available as Rust Playground snippet here.
The text was updated successfully, but these errors were encountered: