Skip to content

Commit f8dfb52

Browse files
authored
fix(python-cdk): Ensure at least one element returned by decoder (#43043)
1 parent 8f42de9 commit f8dfb52

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

airbyte-cdk/python/airbyte_cdk/sources/declarative/decoders/json_decoder.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,17 @@ def is_stream_response(self) -> bool:
2525
return False
2626

2727
def decode(self, response: requests.Response) -> Generator[Mapping[str, Any], None, None]:
28+
"""
29+
Given the response is an empty string or an emtpy list, the function will return a generator with an empty mapping.
30+
"""
2831
try:
2932
body_json = response.json()
3033
if not isinstance(body_json, list):
3134
body_json = [body_json]
32-
yield from body_json
35+
if len(body_json) == 0:
36+
yield {}
37+
else:
38+
yield from body_json
3339
except requests.exceptions.JSONDecodeError:
3440
logger.warning(f"Response cannot be parsed into json: {response.status_code=}, {response.text=}")
3541
yield {}

airbyte-cdk/python/unit_tests/sources/declarative/decoders/test_json_decoder.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@
1414

1515

1616
@pytest.mark.parametrize(
17-
"response_body, expected_json",
18-
[("", [{}]), ('{"healthcheck": {"status": "ok"}}', [{"healthcheck": {"status": "ok"}}])],
17+
"response_body, first_element",
18+
[
19+
("", {}),
20+
("[]", {}),
21+
('{"healthcheck": {"status": "ok"}}', {"healthcheck": {"status": "ok"}})
22+
],
1923
)
20-
def test_json_decoder(requests_mock, response_body, expected_json):
24+
def test_json_decoder(requests_mock, response_body, first_element):
2125
requests_mock.register_uri("GET", "https://airbyte.io/", text=response_body)
2226
response = requests.get("https://airbyte.io/")
23-
assert list(JsonDecoder(parameters={}).decode(response)) == expected_json
27+
assert next(JsonDecoder(parameters={}).decode(response)) == first_element
2428

2529

2630
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)