Skip to content

Commit

Permalink
Merge pull request #204 from snyk-labs/fix-limit-duplication
Browse files Browse the repository at this point in the history
Fix issue with limit duplication leading to errors
  • Loading branch information
lili2311 authored Oct 11, 2023
2 parents dc75bc9 + c415757 commit 9ec5552
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
6 changes: 6 additions & 0 deletions snyk/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import urllib.parse
from typing import Any, List, Optional
from urllib.parse import parse_qs, urlparse

import requests
from retry.api import retry_call
Expand Down Expand Up @@ -162,6 +163,11 @@ def get(
if isinstance(v, bool):
params[k] = str(v).lower()

# the limit is returned in the url, and if two limits are passed
# the API interprets as an array and throws an error
if "limit" in parse_qs(urlparse(path).query):
params.pop("limit", None)

debug_url = f"{url}&{urllib.parse.urlencode(params)}"
fkwargs = {"headers": self.api_headers, "params": params}
else:
Expand Down
4 changes: 1 addition & 3 deletions snyk/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,7 @@ def _rest_to_v1_response_format(self, project):

def _query(self, tags: List[Dict[str, str]] = [], next_url: str = None):
projects = []
params: dict = {
"limit": 100,
}
params: dict = {"limit": 100}
if self.instance:
path = "/orgs/%s/projects" % self.instance.id if not next_url else next_url

Expand Down
7 changes: 7 additions & 0 deletions snyk/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,3 +322,10 @@ def test_get_rest_pages(
data = rest_client.get_rest_pages(f"orgs/{V3_ORG}/targets", t_params)

assert len(data) == 30

def test_rest_limit_deduplication(self, requests_mock, rest_client):
requests_mock.get(
f"{REST_URL}/orgs/{REST_ORG}/projects?limit=100&version={REST_VERSION}"
)
params = {"limit": 10}
rest_client.get(f"orgs/{REST_ORG}/projects?limit=100", params)

0 comments on commit 9ec5552

Please sign in to comment.