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
Keep aliases always sorted and include aliases in expecting message for field/variant_identifier #2458
Conversation
(review this commit with "ignore whitespace changes" option on)
failures (2): field_identifier::unknown variant_identifier::unknown
Fixes tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! I agree that the current order of names in the error message is probably the worst possible.
#[derive(serde::Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct Thing {
#[serde(alias = "z", alias = "x")]
pub y: u8,
}
fn main() {
let j = r#" {"w":null} "#;
println!("{}", serde_json::from_str::<Thing>(j).unwrap_err());
}
unknown field `w`, expected one of `x`, `z`, `y` at line 1 column 5
If I understand correctly, your PR changes that to "expected one of x, y, z".
I think my preference would be to respect the order given by the programmer, by putting first the name they chose to elevate uniquely as the real name of the field in Rust, and following it with the other names they wrote, in the order written. "expected one of y, z, x". What do you think?
If some other order would be especially desired in the context of a particular application, they can always include the real field name in the appropriate spot in the explicit list of aliases (I believe this is already allowed today). serde(alias = "z", alias = "x", alias = "y")
Yes.
That easily could get out of control. What order we should expect in that case? #[derive(serde::Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct Thing {
pub w: u8,
#[serde(alias = "z", alias = "x")]
pub y: u8,
#[serde(alias = "same", alias = "other", alias = "same")]
pub same: u8,
}
It seems that the sorted list of all fields is a better choice, IMO. While you can reorder fields in your struct to create a some sorted list, it
My implementation also does not keep totally sorted list, but that can be fixed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense now — thanks!
This PR fixes two problems:
BTreeSet
, but after adding a main name to the sorted list of aliases it is not sorted anymore#[serde(field_identifier)]
and#[serde(variant_identifier)]
does not include aliases in their default expecting message.I've updated tests for identifiers and fix both problems. I put them into one PR because the first problem affects the newly added test for aliases for the second problem.
The last 4 commits are small refactoring that slightly reduces number of clones and optimizes generation.