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

RESTClient: fix duplicate params in URL in JSONResponsePaginator #1515

Merged
Merged
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
4 changes: 4 additions & 0 deletions dlt/sources/helpers/rest_client/paginators.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,10 @@ def update_request(self, request: Request) -> None:

request.url = self._next_reference

# Clear the query parameters from the previous request otherwise they
# will be appended to the next URL in Session.prepare_request
request.params = None


class HeaderLinkPaginator(BaseNextUrlPaginator):
"""A paginator that uses the 'Link' header in HTTP responses
Expand Down
25 changes: 25 additions & 0 deletions tests/sources/helpers/rest_client/test_paginators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest

from requests.models import Response, Request
from requests import Session

from dlt.sources.helpers.rest_client.paginators import (
SinglePagePaginator,
Expand Down Expand Up @@ -157,6 +158,30 @@ def test_update_request(self, test_case):
paginator.update_request(request)
assert request.url == test_case["expected"]

def test_no_duplicate_params_on_update_request(self):
paginator = JSONResponsePaginator()

request = Request(
method="GET",
url="http://example.com/api/resource",
params={"param1": "value1"},
)

session = Session()

response = Mock(Response, json=lambda: {"next": "/api/resource?page=2&param1=value1"})
paginator.update_state(response)
paginator.update_request(request)

assert request.url == "http://example.com/api/resource?page=2&param1=value1"

# RESTClient._send_request() calls Session.prepare_request() which
# updates the URL with the query parameters from the request object.
prepared_request = session.prepare_request(request)

# The next request should just use the "next" URL without any duplicate parameters.
assert prepared_request.url == "http://example.com/api/resource?page=2&param1=value1"


class TestSinglePagePaginator:
def test_update_state(self):
Expand Down
Loading