Skip to content

fix(python-cdk): Ensure at least one element returned by decoder #43043

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

Merged
merged 3 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ def decode(self, response: requests.Response) -> Generator[Mapping[str, Any], No
body_json = response.json()
if not isinstance(body_json, list):
body_json = [body_json]
yield from body_json
if len(body_json) == 0:
yield {}
else:
yield from body_json
except requests.exceptions.JSONDecodeError:
logger.warning(f"Response cannot be parsed into json: {response.status_code=}, {response.text=}")
yield {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@


@pytest.mark.parametrize(
"response_body, expected_json",
[("", [{}]), ('{"healthcheck": {"status": "ok"}}', [{"healthcheck": {"status": "ok"}}])],
"response_body, first_element",
[
("", {}),
("[]", {}),
('{"healthcheck": {"status": "ok"}}', {"healthcheck": {"status": "ok"}})
],
)
def test_json_decoder(requests_mock, response_body, expected_json):
def test_json_decoder(requests_mock, response_body, first_element):
requests_mock.register_uri("GET", "https://airbyte.io/", text=response_body)
response = requests.get("https://airbyte.io/")
assert list(JsonDecoder(parameters={}).decode(response)) == expected_json
assert next(JsonDecoder(parameters={}).decode(response)) == first_element


@pytest.mark.parametrize(
Expand Down
Loading