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:
parent
0a95e63776
commit
61c8657025
|
@ -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).
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
### Fixed
|
||||||
|
- Malformed unicode buffer encoded in `base_64` json field no-longer fail CloudEvent
|
||||||
|
class construction ([#184])
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
- Default branch changed from `master` to `main` ([#180])
|
- 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
|
[#172]: https://github.com/cloudevents/sdk-python/pull/172
|
||||||
[#173]: https://github.com/cloudevents/sdk-python/pull/173
|
[#173]: https://github.com/cloudevents/sdk-python/pull/173
|
||||||
[#180]: https://github.com/cloudevents/sdk-python/pull/180
|
[#180]: https://github.com/cloudevents/sdk-python/pull/180
|
||||||
|
[#184]: https://github.com/cloudevents/sdk-python/pull/184
|
||||||
|
|
|
@ -25,10 +25,22 @@ def default_marshaller(content: any):
|
||||||
return content
|
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:
|
if content is None:
|
||||||
return None
|
return None
|
||||||
try:
|
try:
|
||||||
return json.loads(content)
|
return json.loads(content)
|
||||||
except (json.JSONDecodeError, TypeError):
|
except (json.JSONDecodeError, TypeError, UnicodeDecodeError):
|
||||||
return content
|
return content
|
||||||
|
|
|
@ -168,8 +168,19 @@ def test_cloudevent_general_overrides():
|
||||||
assert len(event) == 0
|
assert len(event) == 0
|
||||||
|
|
||||||
|
|
||||||
def test_none_json_or_string():
|
@pytest.mark.parametrize(
|
||||||
assert _json_or_string(None) is None
|
"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(
|
def test_get_operation_on_non_existing_attribute_must_not_raise_exception(
|
||||||
|
|
Loading…
Reference in New Issue