Skip to content

Commit a820fd4

Browse files
MikeMoore63anniefu
andauthored
fix: for issue #170 gracefully handle pubsub messages without attributes in them (#187)
* Handle when attributes not present Co-authored-by: Annie Fu <16651409+anniefu@users.noreply.github.com>
1 parent f2285f9 commit a820fd4

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/functions_framework/event_conversion.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ def marshal_background_event_data(request):
317317
"data": {
318318
"@type": _PUBSUB_MESSAGE_TYPE,
319319
"data": request_data["message"]["data"],
320-
"attributes": request_data["message"]["attributes"],
320+
"attributes": request_data["message"].get("attributes", {}),
321321
},
322322
}
323323
except (AttributeError, KeyError, TypeError):

tests/test_convert.py

+47
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ def raw_pubsub_request():
100100
}
101101

102102

103+
@pytest.fixture
104+
def raw_pubsub_request_noattributes():
105+
return {
106+
"subscription": "projects/sample-project/subscriptions/gcf-test-sub",
107+
"message": {"data": "eyJmb28iOiJiYXIifQ==", "messageId": "1215011316659232"},
108+
}
109+
110+
103111
@pytest.fixture
104112
def marshalled_pubsub_request():
105113
return {
@@ -121,6 +129,27 @@ def marshalled_pubsub_request():
121129
}
122130

123131

132+
@pytest.fixture
133+
def marshalled_pubsub_request_noattr():
134+
return {
135+
"data": {
136+
"@type": "type.googleapis.com/google.pubsub.v1.PubsubMessage",
137+
"data": "eyJmb28iOiJiYXIifQ==",
138+
"attributes": {},
139+
},
140+
"context": {
141+
"eventId": "1215011316659232",
142+
"eventType": "google.pubsub.topic.publish",
143+
"resource": {
144+
"name": "projects/sample-project/topics/gcf-test",
145+
"service": "pubsub.googleapis.com",
146+
"type": "type.googleapis.com/google.pubsub.v1.PubsubMessage",
147+
},
148+
"timestamp": "2021-04-17T07:21:18.249Z",
149+
},
150+
}
151+
152+
124153
@pytest.fixture
125154
def raw_pubsub_cloud_event_output(marshalled_pubsub_request):
126155
event = PUBSUB_CLOUD_EVENT.copy()
@@ -343,6 +372,24 @@ def test_marshal_background_event_data_without_topic_in_path(
343372
assert payload == marshalled_pubsub_request
344373

345374

375+
def test_marshal_background_event_data_without_topic_in_path_no_attr(
376+
raw_pubsub_request_noattributes, marshalled_pubsub_request_noattr
377+
):
378+
req = flask.Request.from_values(
379+
json=raw_pubsub_request_noattributes, path="/myfunc/"
380+
)
381+
payload = event_conversion.marshal_background_event_data(req)
382+
383+
# Remove timestamps as they get generates on the fly
384+
del marshalled_pubsub_request_noattr["context"]["timestamp"]
385+
del payload["context"]["timestamp"]
386+
387+
# Resource name is set to empty string when it cannot be parsed from the request path
388+
marshalled_pubsub_request_noattr["context"]["resource"]["name"] = ""
389+
390+
assert payload == marshalled_pubsub_request_noattr
391+
392+
346393
def test_marshal_background_event_data_with_topic_path(
347394
raw_pubsub_request, marshalled_pubsub_request
348395
):

0 commit comments

Comments
 (0)