Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions sdk/core/azure-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## 1.15.1 (Unreleased)

### Bug Fixes

- Improved error message in the `from_dict` method of `CloudEvent` when a wrong schema is sent.

## 1.15.0 (2021-06-04)

Expand Down
10 changes: 10 additions & 0 deletions sdk/core/azure-core/azure/core/messaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ def from_dict(cls, event):
:type event: dict
:rtype: CloudEvent
"""
if not all([_ in event for _ in ("source", "type")]):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add the description of the doc or a link?

@rakshith91 rakshith91 Jun 23, 2021

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if all([_ in event for _ in (("subject", "eventType", "data", "dataVersion", "id", "eventTime"))]):
raise ValueError(
"The event does not conform to the cloud event spec." +
" Try using the EventGridEvent from azure-eventgrid library"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be more explicit:
It looks like your event is an EventGrid schema. You can parse EventGrid events using EventGridEvent.from_dict of the azure-eventgrid library

)
raise ValueError(

@lmazuel lmazuel Jun 24, 2021

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would do this whole block into a try/except around the cls at the end, pure EAFP

"The event does not conform to the cloud event spec. source and type are required."
)

kwargs = {} # type: Dict[Any, Any]
reserved_attr = [

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these keys case-sensitive?

@rakshith91 rakshith91 Jun 23, 2021

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes - making it non case sensitive would be breaking

"data",
Expand Down
34 changes: 34 additions & 0 deletions sdk/core/azure-core/tests/test_messaging_cloud_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,3 +436,37 @@ def test_cloud_custom_dict_ms_precision_is_eq_six_z_not():
assert date_obj.day == 18
assert date_obj.hour == 20
assert date_obj.microsecond == 123456

def test_eventgrid_event_schema_raises():
cloud_custom_dict = {
"id":"de0fd76c-4ef4-4dfb-ab3a-8f24a307e033",
"data":{"team": "event grid squad"},
"dataVersion": "1.0",
"subject":"Azure.Sdk.Sample",
"eventTime":"2020-08-07T02:06:08.11969Z",
"eventType":"pull request",
}
with pytest.raises(ValueError, match="The event does not conform to the cloud event spec. Try using the EventGridEvent from azure-eventgrid library"):
CloudEvent.from_dict(cloud_custom_dict)

def test_wrong_schema_raises_no_source():
cloud_custom_dict = {
"id":"de0fd76c-4ef4-4dfb-ab3a-8f24a307e033",
"data":{"team": "event grid squad"},
"type":"Azure.Sdk.Sample",
"time":"2020-08-07T02:06:08.11969Z",
"specversion":"1.0",
}
with pytest.raises(ValueError, match="The event does not conform to the cloud event spec. source and type are required."):
CloudEvent.from_dict(cloud_custom_dict)

def test_wrong_schema_raises_no_type():
cloud_custom_dict = {
"id":"de0fd76c-4ef4-4dfb-ab3a-8f24a307e033",
"data":{"team": "event grid squad"},
"source":"Azure/Sdk/Sample",
"time":"2020-08-07T02:06:08.11969Z",
"specversion":"1.0",
}
with pytest.raises(ValueError, match="The event does not conform to the cloud event spec. source and type are required."):
CloudEvent.from_dict(cloud_custom_dict)