Skip to content

Commit d80da46

Browse files
committed
fix!: Ensure YAML/JSON loading uses UTF-8 instead of default encoding.
Signed-off-by: Karthik Bekal Pattathana <133984042+karthikbekalp@users.noreply.github.com>
1 parent 509a868 commit d80da46

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

src/openjd/adaptor_runtime/_entrypoint.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -590,14 +590,14 @@ def _load_data(data: str) -> dict:
590590

591591
def _load_yaml_json(data: str) -> Any:
592592
"""
593-
Loads a YAML/JSON file/string.
593+
Loads a YAML/JSON file/string using the UTF-8 encoding.
594594
595595
Note that yaml.safe_load() is capable of loading JSON documents.
596596
"""
597597
loaded_yaml = None
598598
if data.startswith("file://"):
599599
filepath = data[len("file://") :]
600-
with open(filepath) as yaml_file:
600+
with open(filepath, encoding="utf-8") as yaml_file:
601601
loaded_yaml = yaml.safe_load(yaml_file)
602602
else:
603603
loaded_yaml = yaml.safe_load(data)

test/openjd/adaptor_runtime/unit/test_entrypoint.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -860,12 +860,14 @@ def test_accepts_file(self, input: str, expected: dict):
860860

861861
# WHEN
862862
open_mock: MagicMock
863-
with patch.object(runtime_entrypoint, "open", mock_open(read_data=input)) as open_mock:
863+
with patch.object(
864+
runtime_entrypoint, "open", mock_open(read_data=input), "encoding=utf8"
865+
) as open_mock:
864866
output = _load_data(file_uri)
865867

866868
# THEN
867869
assert output == expected
868-
open_mock.assert_called_once_with(filepath)
870+
open_mock.assert_called_once_with(filepath, encoding="utf-8")
869871

870872
@patch.object(runtime_entrypoint, "open")
871873
def test_raises_on_os_error(self, mock_open: MagicMock, caplog: pytest.LogCaptureFixture):
@@ -880,7 +882,7 @@ def test_raises_on_os_error(self, mock_open: MagicMock, caplog: pytest.LogCaptur
880882

881883
# THEN
882884
assert raised_err.value is mock_open.side_effect
883-
mock_open.assert_called_once_with(filepath)
885+
mock_open.assert_called_once_with(filepath, encoding="utf-8")
884886
assert "Failed to open data file: " in caplog.text
885887

886888
def test_raises_when_parsing_fails(self, caplog: pytest.LogCaptureFixture):

0 commit comments

Comments
 (0)