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

fix: utf-16 json encoder support #28486

Merged
merged 1 commit into from
May 15, 2024
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
21 changes: 15 additions & 6 deletions superset/sqllab/execution_context_convertor.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,22 @@

def serialize_payload(self) -> str:
if self._exc_status == SqlJsonExecutionStatus.HAS_RESULTS:
return json.dumps(
apply_display_max_row_configuration_if_require(
self.payload, self._max_row_in_display_configuration
),
default=utils.pessimistic_json_iso_dttm_ser,
ignore_nan=True,
sql_results = apply_display_max_row_configuration_if_require(
self.payload, self._max_row_in_display_configuration
)
try:
return json.dumps(
sql_results,
default=utils.pessimistic_json_iso_dttm_ser,
ignore_nan=True,
)
except UnicodeDecodeError:
return json.dumps(

Check warning on line 65 in superset/sqllab/execution_context_convertor.py

View check run for this annotation

Codecov / codecov/patch

superset/sqllab/execution_context_convertor.py#L64-L65

Added lines #L64 - L65 were not covered by tests
sql_results,
default=utils.pessimistic_json_iso_dttm_ser,
ensure_ascii=False,
ignore_nan=True,
)

return json.dumps(
{"query": self.payload}, default=utils.json_int_dttm_ser, ignore_nan=True
Expand Down
5 changes: 4 additions & 1 deletion superset/utils/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,10 @@ def base_json_conv(obj: Any) -> Any:
try:
return obj.decode("utf-8")
except Exception: # pylint: disable=broad-except
return "[bytes]"
try:
return obj.decode("utf-16")
except Exception: # pylint: disable=broad-except
return "[bytes]"

raise TypeError(f"Unserializable object {obj} of type {type(obj)}")

Expand Down
3 changes: 2 additions & 1 deletion tests/integration_tests/utils_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ def test_base_json_conv(self):
assert isinstance(base_json_conv(time()), str)
assert isinstance(base_json_conv(timedelta(0)), str)
assert isinstance(base_json_conv(b""), str)
assert base_json_conv(bytes("", encoding="utf-16")) == "[bytes]"
assert isinstance(base_json_conv(b"\xff\xfe"), str)
assert base_json_conv(b"\xff") == "[bytes]"

with pytest.raises(TypeError):
base_json_conv(np.datetime64())
Expand Down
10 changes: 10 additions & 0 deletions tests/unit_tests/utils/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,3 +437,13 @@ def test_pessimistic_json_iso_dttm_ser_nonutf8():
json_str = json.dumps(data, default=pessimistic_json_iso_dttm_ser)
reloaded_data = json.loads(json_str)
assert reloaded_data["INVALID_UTF8_BYTES"] == "[bytes]"


def test_pessimistic_json_iso_dttm_ser_utf16():
data = {
"VALID_UTF16_BYTES": b"\xff\xfeS0\x930k0a0o0\x16NLu",
}
assert isinstance(data["VALID_UTF16_BYTES"], bytes)
json_str = json.dumps(data, default=pessimistic_json_iso_dttm_ser)
reloaded_data = json.loads(json_str)
assert reloaded_data["VALID_UTF16_BYTES"] == "こんにちは世界"
Loading