fix: `_json_or_string` no longer fails on malformed unicode buffers (#184)

* fix: add missing decode exception

Signed-off-by: Alexander Tkachev <sasha64sasha@gmail.com>

* fix: add optional to signature

Signed-off-by: Alexander Tkachev <sasha64sasha@gmail.com>

* refactor: better type information

Signed-off-by: Alexander Tkachev <sasha64sasha@gmail.com>

* test: json or string

Signed-off-by: Alexander Tkachev <sasha64sasha@gmail.com>

* docs: update changelog

Signed-off-by: Alexander Tkachev <sasha64sasha@gmail.com>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Signed-off-by: Alexander Tkachev <sasha64sasha@gmail.com>

* refactor: use anystr

Co-authored-by: Yurii Serhiichuk <xSAVIKx@users.noreply.github.com>
Signed-off-by: Alexander Tkachev <sasha64sasha@gmail.com>

* refactor: use anystr instead of custom type var

Signed-off-by: Alexander Tkachev <sasha64sasha@gmail.com>

* docs: _json_or_string

Signed-off-by: Alexander Tkachev <sasha64sasha@gmail.com>

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Yurii Serhiichuk <xSAVIKx@users.noreply.github.com>
This commit is contained in:
Alexander Tkachev 2022-07-24 21:49:19 +03:00 committed by GitHub
parent 0a95e63776
commit 61c8657025
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 4 deletions

View File

@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
### Fixed
- Malformed unicode buffer encoded in `base_64` json field no-longer fail CloudEvent
class construction ([#184])
### Changed
- 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
[#172]: https://github.com/cloudevents/sdk-python/pull/172
[#173]: https://github.com/cloudevents/sdk-python/pull/173
[#180]: https://github.com/cloudevents/sdk-python/pull/180
[#184]: https://github.com/cloudevents/sdk-python/pull/184

View File

@ -25,10 +25,22 @@ def default_marshaller(content: any):
return content
def _json_or_string(content: typing.Union[str, bytes]):
def _json_or_string(
content: typing.Optional[typing.AnyStr],
) -> typing.Optional[
typing.Union[
typing.Dict[typing.Any, typing.Any],
typing.List[typing.Any],
typing.AnyStr,
]
]:
"""
Given an encoded JSON string MUST return decoded JSON object.
Otherwise, MUST return the given string as-is.
"""
if content is None:
return None
try:
return json.loads(content)
except (json.JSONDecodeError, TypeError):
except (json.JSONDecodeError, TypeError, UnicodeDecodeError):
return content

View File

@ -168,8 +168,19 @@ def test_cloudevent_general_overrides():
assert len(event) == 0
def test_none_json_or_string():
assert _json_or_string(None) is None
@pytest.mark.parametrize(
"given, expected",
[
(None, None),
('{"hello": "world"}', {"hello": "world"}),
(b'{"hello": "world"}', {"hello": "world"}),
(b"Hello World", b"Hello World"),
("Hello World", "Hello World"),
(b"\x00\x00\x11Hello World", b"\x00\x00\x11Hello World"),
],
)
def test_json_or_string_match_golden_sample(given, expected):
assert _json_or_string(given) == expected
def test_get_operation_on_non_existing_attribute_must_not_raise_exception(