10000 small fix to read method · cloudevents/sdk-python@adfee8f · GitHub
[go: up one dir, main page]

Skip to content

Commit adfee8f

Browse files
committed
small fix to read method
Signed-off-by: Tudor Plugaru <plugaru.tudor@protonmail.com>
1 parent 8aeed15 commit adfee8f

File tree

2 files changed

+44
-3
lin 10000 es changed

2 files changed

+44
-3
lines changed

src/cloudevents/core/formats/json.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from json import JSONEncoder, dumps, loads
2020
from typing import Any, Final, Pattern, Type, TypeVar, Union
2121

22-
from dateutil.parser import isoparse
22+
from dateutil.parser import isoparse # type: ignore[import-untyped]
2323

2424
from cloudevents.core.base import BaseCloudEvent
2525
from cloudevents.core.formats.base import Format
@@ -67,9 +67,9 @@ def read(self, event_klass: Type[T], data: Union[str, bytes]) -> T:
6767
if "time" in event_attributes:
6868
event_attributes["time"] = isoparse(event_attributes["time"])
6969

70-
event_data: Union[str, bytes] = event_attributes.get("data")
70+
event_data: Union[str, bytes] = event_attributes.pop("data", None)
7171
if event_data is None:
72-
event_data_base64 = event_attributes.get("data_base64")
72+
event_data_base64 = event_attributes.pop("data_base64", None)
7373
if event_data_base64 is not None:
7474
event_data = base64.b64decode(event_data_base64)
7575

@@ -93,6 +93,7 @@ def write(self, event: T) -> bytes:
9393
datacontenttype = event_dict.get(
9494
"datacontenttype", JSONFormat.CONTENT_TYPE
9595
)
96+
# Should we fail if we can't serialize data to JSON?
9697
if re.match(JSONFormat.JSON_CONTENT_TYPE_PATTERN, datacontenttype):
9798
event_dict["data"] = dumps(event_data)
9899
else:

tests/test_core/test_format/test_json.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,43 @@ def test_read_cloud_event_from_json_with_attributes_only() -> None:
175175
assert result.get_dataschema() == "http://example.com/schema"
176176
assert result.get_subject() == "test_subject"
177177
assert result.get_data() is None
178+
179+
180+
def test_read_cloud_event_from_json_with_bytes_as_data() -> None:
181+
data = '{"id": "123", "source": "source", "type": "type", "specversion": "1.0", "time": "2023-10-25T17:09:19.736166Z", "datacontenttype": "application/json", "dataschema": "http://example.com/schema", "subject": "test_subject", "data_base64": "dGVzdA=="}'.encode(
182+
"utf-8"
183+
)
184+
formatter = JSONFormat()
185+
result = formatter.read(CloudEvent, data)
186+
187+
assert result.get_id() == "123"
188+
assert result.get_source() == "source"
189+
assert result.get_type() == "type"
190+
assert result.get_specversion() == "1.0"
191+
assert result.get_time() == datetime(
192+
2023, 10, 25, 17, 9, 19, 736166, tzinfo=timezone.utc
193+
)
194+
assert result.get_datacontenttype() == "application/json"
195+
assert result.get_dataschema() == "http://example.com/schema"
196+
assert result.get_subject() == "test_subject"
197+
assert result.get_data() == b"test"
198+
199+
200+
def test_read_cloud_event_from_json_with_json_as_data() -> None:
201+
data = '{"id": "123", "source": "source", "type": "type", "specversion": "1.0", "time": "2023-10-25T17:09:19.736166Z", "datacontenttype": "application/json", "dataschema": "http://example.com/schema", "subject": "test_subject", "data": {"key": "value"}}'.encode(
202+
"utf-8"
203+
)
204+
formatter = JSONFormat()
205+
result = formatter.read(CloudEvent, data)
206+
207+
assert result.get_id() == "123"
208+
assert result.get_source() == "source"
209+
assert result.get_type() == "type"
210+
assert result.get_specversion() == "1.0"
211+
assert result.get_time() == datetime(
212+
2023, 10, 25, 17, 9, 19, 736166, tzinfo=timezone.utc
213+
)
214+
assert result.get_datacontenttype() == "application/json"
215+
assert result.get_dataschema() == "http://example.com/schema"
216+
assert result.get_subject() == "test_subject"
217+
assert result.get_data() == {"key": "value"}

0 commit comments

Comments
 (0)
0