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

Allow map keys starting with a digit #113

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/de/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,17 @@ impl<'a> Parser<'a> {
// First character is an integer, attempt to parse it as an integer key
b'0'..=b'9' => {
let key = self.parse_key(b']', true)?;
let key = key.parse().map_err(Error::from)?;
self.parse_ord_seq_value(key, node)?;
match key.parse() {
Ok(key) => {
self.parse_ord_seq_value(key, node)?;
}
Err(parse_int_error) => {
// key was not an integer, try to parse it as a map key instead
self.parse_map_value(key, node)
// return original error instead to provide better error messages for seqs
.map_err(|_| parse_int_error)?;
}
}
return Ok(true);
}
// Key is "[a..=" so parse up to the closing "]"
Expand Down
37 changes: 37 additions & 0 deletions tests/test_deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,43 @@ fn deserialize_map_with_int_keys() {
.expect_err("should error with repeated key");
}

#[test]
fn deserialize_map_with_uuid_keys() {
#[derive(Debug, Deserialize)]
struct Mapping {
mapping: HashMap<String, u64>,
}

let test: Mapping = serde_qs::from_str(
"mapping[5b53d2c1-3745-47e3-b421-76c05c5c7523]=1&mapping[00000000-0000-0000-0000-000000000000]=2&mapping[a4b2e25c-e80c-4e2a-958c-35f2f5151f46]=3&mapping[ffffffff-ffff-ffff-ffff-ffffffffffff]=4"
).unwrap();

assert_eq!(
test.mapping
.get("5b53d2c1-3745-47e3-b421-76c05c5c7523")
.cloned(),
Some(1)
);
assert_eq!(
test.mapping
.get("00000000-0000-0000-0000-000000000000")
.cloned(),
Some(2)
);
assert_eq!(
test.mapping
.get("a4b2e25c-e80c-4e2a-958c-35f2f5151f46")
.cloned(),
Some(3)
);
assert_eq!(
test.mapping
.get("ffffffff-ffff-ffff-ffff-ffffffffffff")
.cloned(),
Some(4)
);
}

#[test]
fn deserialize_unit_types() {
// allow these clippy lints cause I like how explicit the test is
Expand Down