Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix(client): ensure encoded query params are never duplicated#2219

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
max-wittig merged 2 commits intomainfromfix/no-duplicate-params
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes fromall 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
9 changes: 7 additions & 2 deletionsgitlab/client.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -20,6 +20,7 @@
import re
import time
from typing import Any, cast, Dict, List, Optional, Tuple, TYPE_CHECKING, Union
from urllib import parse

import requests
import requests.utils
Expand DownExpand Up@@ -677,11 +678,15 @@ def http_request(
GitlabHttpError: When the return code is not 2xx
"""
query_data = query_data or {}
url = self._build_url(path)
raw_url = self._build_url(path)

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

url = raw_url.replace(parsed.query, "").strip("?")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Why not useparse.urlunparse?

url = parse.urlunparse(parsed._replace(query=""))

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I saw that on stackoverflow as well but it uses a private method (_replace) so didn't want to touch that 😁

JohnVillalovos reacted with laugh emoji
Copy link
Member

@JohnVillalovosJohnVillalovosAug 4, 2022
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Oh it is documented in the official docs:

https://docs.python.org/3/library/urllib.parse.html

As is the case with all named tuples, the subclass has a few additional methods and attributes that are particularly useful. One such method is _replace(). The _replace() method will return a new ParseResult object replacing specified fields with new values.


# Deal with kwargs: by default a user uses kwargs to send data to the
# gitlab server, but this generates problems (python keyword conflicts
# and python-gitlab/gitlab conflicts).
Expand Down
18 changes: 18 additions & 0 deletionstests/unit/test_gitlab_http_methods.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -36,6 +36,24 @@ def test_http_request(gl):
assert responses.assert_call_count(url, 1) is True


@responses.activate
def test_http_request_with_url_encoded_kwargs_does_not_duplicate_params(gl):
url = "http://localhost/api/v4/projects?topics%5B%5D=python"
responses.add(
method=responses.GET,
url=url,
json=[{"name": "project1"}],
status=200,
match=[responses.matchers.query_param_matcher({"topics[]": "python"})],
)

kwargs = {"topics[]": "python"}
http_r = gl.http_request("get", "/projects?topics%5B%5D=python", **kwargs)
http_r.json()
assert http_r.status_code == 200
assert responses.assert_call_count(url, 1)


@responses.activate
def test_http_request_404(gl):
url = "http://localhost/api/v4/not_there"
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp