Skip to content

Commit 61c8657

Browse files
sasha-tkachevpre-commit-ci[bot]xSAVIKx
authored
fix: _json_or_string no longer fails on malformed unicode buffers (#184)
* fix: add missing decode exception Signed-off-by: Alexander Tkachev <[email protected]> * fix: add optional to signature Signed-off-by: Alexander Tkachev <[email protected]> * refactor: better type information Signed-off-by: Alexander Tkachev <[email protected]> * test: json or string Signed-off-by: Alexander Tkachev <[email protected]> * docs: update changelog Signed-off-by: Alexander Tkachev <[email protected]> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Signed-off-by: Alexander Tkachev <[email protected]> * refactor: use anystr Co-authored-by: Yurii Serhiichuk <[email protected]> Signed-off-by: Alexander Tkachev <[email protected]> * refactor: use anystr instead of custom type var Signed-off-by: Alexander Tkachev <[email protected]> * docs: _json_or_string Signed-off-by: Alexander Tkachev <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Yurii Serhiichuk <[email protected]>
1 parent 0a95e63 commit 61c8657

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Fixed
9+
- Malformed unicode buffer encoded in `base_64` json field no-longer fail CloudEvent
10+
class construction ([#184])
811

912
### Changed
1013
- Default branch changed from `master` to `main` ([#180])
@@ -169,3 +172,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
169172
[#172]: https://github.com/cloudevents/sdk-python/pull/172
170173
[#173]: https://github.com/cloudevents/sdk-python/pull/173
171174
[#180]: https://github.com/cloudevents/sdk-python/pull/180
175+
[#184]: https://github.com/cloudevents/sdk-python/pull/184

cloudevents/http/util.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,22 @@ def default_marshaller(content: any):
2525
return content
2626

2727

28-
def _json_or_string(content: typing.Union[str, bytes]):
28+
def _json_or_string(
29+
content: typing.Optional[typing.AnyStr],
30+
) -> typing.Optional[
31+
typing.Union[
32+
typing.Dict[typing.Any, typing.Any],
33+
typing.List[typing.Any],
34+
typing.AnyStr,
35+
]
36+
]:
37+
"""
38+
Given an encoded JSON string MUST return decoded JSON object.
39+
Otherwise, MUST return the given string as-is.
40+
"""
2941
if content is None:
3042
return None
3143
try:
3244
return json.loads(content)
33-
except (json.JSONDecodeError, TypeError):
45+
except (json.JSONDecodeError, TypeError, UnicodeDecodeError):
3446
return content

cloudevents/tests/test_http_cloudevent.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,19 @@ def test_cloudevent_general_overrides():
168168
assert len(event) == 0
169169

170170

171-
def test_none_json_or_string():
172-
assert _json_or_string(None) is None
171+
@pytest.mark.parametrize(
172+
"given, expected",
173+
[
174+
(None, None),
175+
('{"hello": "world"}', {"hello": "world"}),
176+
(b'{"hello": "world"}', {"hello": "world"}),
177+
(b"Hello World", b"Hello World"),
178+
("Hello World", "Hello World"),
179+
(b"\x00\x00\x11Hello World", b"\x00\x00\x11Hello World"),
180+
],
181+
)
182+
def test_json_or_string_match_golden_sample(given, expected):
183+
assert _json_or_string(given) == expected
173184

174185

175186
def test_get_operation_on_non_existing_attribute_must_not_raise_exception(

0 commit comments

Comments
 (0)