Skip to content

Commit

Permalink
fix!: Ensure YAML/JSON loading uses UTF-8 instead of default encoding.
Browse files Browse the repository at this point in the history
Signed-off-by: Karthik Bekal Pattathana <[email protected]>
  • Loading branch information
karthikbekalp committed Dec 19, 2024
1 parent 509a868 commit 9dc30f9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/openjd/adaptor_runtime/_entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,14 +590,14 @@ def _load_data(data: str) -> dict:

def _load_yaml_json(data: str) -> Any:
"""
Loads a YAML/JSON file/string.
Loads a YAML/JSON file/string using the UTF-8 encoding.
Note that yaml.safe_load() is capable of loading JSON documents.
"""
loaded_yaml = None
if data.startswith("file://"):
filepath = data[len("file://") :]
with open(filepath) as yaml_file:
with open(filepath, encoding="utf-8") as yaml_file:
loaded_yaml = yaml.safe_load(yaml_file)
else:
loaded_yaml = yaml.safe_load(data)
Expand Down
6 changes: 3 additions & 3 deletions test/openjd/adaptor_runtime/unit/test_entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -860,12 +860,12 @@ def test_accepts_file(self, input: str, expected: dict):

# WHEN
open_mock: MagicMock
with patch.object(runtime_entrypoint, "open", mock_open(read_data=input)) as open_mock:
with patch.object(runtime_entrypoint, "open", mock_open(read_data=input), "encoding=utf8") as open_mock:
output = _load_data(file_uri)

# THEN
assert output == expected
open_mock.assert_called_once_with(filepath)
open_mock.assert_called_once_with(filepath, encoding="utf-8")

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

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

def test_raises_when_parsing_fails(self, caplog: pytest.LogCaptureFixture):
Expand Down

0 comments on commit 9dc30f9

Please sign in to comment.