Skip to content

Commit

Permalink
util/request_helper: Fix query_with_params() behavior for duplicate…
Browse files Browse the repository at this point in the history
… keys

Our search endpoint supports having multiple `ids[]` query parameters defined, but the way we generate our paginations fields only considered the last such query parameter until now. This commit fixes the code to include all query parameters instead.
  • Loading branch information
Turbo87 committed Dec 12, 2024
1 parent 0044c42 commit 160ee9d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/tests/routes/crates/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,11 +673,11 @@ async fn multiple_ids() -> anyhow::Result<()> {
}

let response = anon.search(&format!("{query}&per_page=1&page=2")).await;
assert_snapshot!(response.meta.prev_page.unwrap(), @"?ids%5B%5D=unknown&per_page=1&page=1");
assert_snapshot!(response.meta.next_page.unwrap(), @"?ids%5B%5D=unknown&per_page=1&page=3");
assert_snapshot!(response.meta.prev_page.unwrap(), @"?ids%5B%5D=foo&ids%5B%5D=bar&ids%5B%5D=baz&ids%5B%5D=baz&ids%5B%5D=unknown&per_page=1&page=1");
assert_snapshot!(response.meta.next_page.unwrap(), @"?ids%5B%5D=foo&ids%5B%5D=bar&ids%5B%5D=baz&ids%5B%5D=baz&ids%5B%5D=unknown&per_page=1&page=3");

let response = anon.search(&format!("{query}&per_page=1")).await;
assert_snapshot!(response.meta.next_page.unwrap(), @"?ids%5B%5D=unknown&per_page=1&seek=Mg");
assert_snapshot!(response.meta.next_page.unwrap(), @"?ids%5B%5D=foo&ids%5B%5D=bar&ids%5B%5D=baz&ids%5B%5D=baz&ids%5B%5D=unknown&per_page=1&seek=Mg");

Ok(())
}
Expand Down
9 changes: 8 additions & 1 deletion src/util/request_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,15 @@ impl<T: RequestPartsExt> RequestUtils for T {
}

fn query_with_params(&self, new_params: IndexMap<String, String>) -> String {
let mut params = self.query();
let query_bytes = self.uri().query().unwrap_or("").as_bytes();

let mut params = url::form_urlencoded::parse(query_bytes)
.into_owned()
.filter(|(key, _)| !new_params.contains_key(key))
.collect::<Vec<_>>();

params.extend(new_params);

let query_string = url::form_urlencoded::Serializer::new(String::new())
.extend_pairs(params)
.finish();
Expand Down

0 comments on commit 160ee9d

Please sign in to comment.