fix(python-cdk): Ensure at least one element returned by decoder (#43043)
This commit is contained in:
committed by
GitHub
parent
8f42de9c37
commit
f8dfb52af9
@@ -25,11 +25,17 @@ class JsonDecoder(Decoder):
|
||||
return False
|
||||
|
||||
def decode(self, response: requests.Response) -> Generator[Mapping[str, Any], None, None]:
|
||||
"""
|
||||
Given the response is an empty string or an emtpy list, the function will return a generator with an empty mapping.
|
||||
"""
|
||||
try:
|
||||
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 {}
|
||||
|
||||
@@ -14,13 +14,17 @@ from airbyte_protocol.models import SyncMode
|
||||
|
||||
|
||||
@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(
|
||||
|
||||
Reference in New Issue
Block a user