Skip to content

Commit d263f57

Browse files
authored
Merge pull request #2219 from python-gitlab/fix/no-duplicate-params
fix(client): ensure encoded query params are never duplicated
2 parents 2ebfc70 + 1398426 commit d263f57

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

gitlab/client.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import re
2121
import time
2222
from typing import Any, cast, Dict, List, Optional, Tuple, TYPE_CHECKING, Union
23+
from urllib import parse
2324

2425
import requests
2526
import requests.utils
@@ -677,11 +678,15 @@ def http_request(
677678
GitlabHttpError: When the return code is not 2xx
678679
"""
679680
query_data = query_data or {}
680-
url = self._build_url(path)
681+
raw_url = self._build_url(path)
681682

682-
params: Dict[str, Any] = {}
683+
# parse user-provided URL params to ensure we don't add our own duplicates
684+
parsed = parse.urlparse(raw_url)
685+
params = parse.parse_qs(parsed.query)
683686
utils.copy_dict(src=query_data, dest=params)
684687

688+
url = raw_url.replace(parsed.query, "").strip("?")
689+
685690
# Deal with kwargs: by default a user uses kwargs to send data to the
686691
# gitlab server, but this generates problems (python keyword conflicts
687692
# and python-gitlab/gitlab conflicts).

tests/unit/test_gitlab_http_methods.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,24 @@ def test_http_request(gl):
3636
assert responses.assert_call_count(url, 1) is True
3737

3838

39+
@responses.activate
40+
def test_http_request_with_url_encoded_kwargs_does_not_duplicate_params(gl):
41+
url = "http://localhost/api/v4/projects?topics%5B%5D=python"
42+
responses.add(
43+
method=responses.GET,
44+
url=url,
45+
json=[{"name": "project1"}],
46+
status=200,
47+
match=[responses.matchers.query_param_matcher({"topics[]": "python"})],
48+
)
49+
50+
kwargs = {"topics[]": "python"}
51+
http_r = gl.http_request("get", "/projects?topics%5B%5D=python", **kwargs)
52+
http_r.json()
53+
assert http_r.status_code == 200
54+
assert responses.assert_call_count(url, 1)
55+
56+
3957
@responses.activate
4058
def test_http_request_404(gl):
4159
url = "http://localhost/api/v4/not_there"

0 commit comments

Comments
 (0)