From 8e5f860633025d018c6b00001aa7aa78c8a066c1 Mon Sep 17 00:00:00 2001 From: Mike Moore Date: Sat, 7 May 2022 05:24:02 +0100 Subject: [PATCH 1/3] Handle when attributes not present --- src/functions_framework/event_conversion.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/functions_framework/event_conversion.py b/src/functions_framework/event_conversion.py index 28cf2a1b..3597e371 100644 --- a/src/functions_framework/event_conversion.py +++ b/src/functions_framework/event_conversion.py @@ -317,7 +317,7 @@ def marshal_background_event_data(request): "data": { "@type": _PUBSUB_MESSAGE_TYPE, "data": request_data["message"]["data"], - "attributes": request_data["message"]["attributes"], + "attributes": request_data["message"].get("attributes",{}), }, } except (AttributeError, KeyError, TypeError): From 7b0b409bcf6623c61acb2b3aaab8b51e3f2082b2 Mon Sep 17 00:00:00 2001 From: Mike Moore Date: Sat, 7 May 2022 06:21:07 +0100 Subject: [PATCH 2/3] Handle when attributes not present --- src/functions_framework/event_conversion.py | 2 +- tests/test_convert.py | 47 +++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/functions_framework/event_conversion.py b/src/functions_framework/event_conversion.py index 3597e371..06e5a812 100644 --- a/src/functions_framework/event_conversion.py +++ b/src/functions_framework/event_conversion.py @@ -317,7 +317,7 @@ def marshal_background_event_data(request): "data": { "@type": _PUBSUB_MESSAGE_TYPE, "data": request_data["message"]["data"], - "attributes": request_data["message"].get("attributes",{}), + "attributes": request_data["message"].get("attributes", {}), }, } except (AttributeError, KeyError, TypeError): diff --git a/tests/test_convert.py b/tests/test_convert.py index 0d41d5ed..8a4a26c8 100644 --- a/tests/test_convert.py +++ b/tests/test_convert.py @@ -100,6 +100,14 @@ def raw_pubsub_request(): } +@pytest.fixture +def raw_pubsub_request_noattributes(): + return { + "subscription": "projects/sample-project/subscriptions/gcf-test-sub", + "message": {"data": "eyJmb28iOiJiYXIifQ==", "messageId": "1215011316659232"}, + } + + @pytest.fixture def marshalled_pubsub_request(): return { @@ -121,6 +129,27 @@ def marshalled_pubsub_request(): } +@pytest.fixture +def marshalled_pubsub_request_noattr(): + return { + "data": { + "@type": "type.googleapis.com/google.pubsub.v1.PubsubMessage", + "data": "eyJmb28iOiJiYXIifQ==", + "attributes": {}, + }, + "context": { + "eventId": "1215011316659232", + "eventType": "google.pubsub.topic.publish", + "resource": { + "name": "projects/sample-project/topics/gcf-test", + "service": "pubsub.googleapis.com", + "type": "type.googleapis.com/google.pubsub.v1.PubsubMessage", + }, + "timestamp": "2021-04-17T07:21:18.249Z", + }, + } + + @pytest.fixture def raw_pubsub_cloud_event_output(marshalled_pubsub_request): event = PUBSUB_CLOUD_EVENT.copy() @@ -343,6 +372,24 @@ def test_marshal_background_event_data_without_topic_in_path( assert payload == marshalled_pubsub_request +def test_marshal_background_event_data_without_topic_in_path_no_attr( + raw_pubsub_request_noattributes, marshalled_pubsub_request +): + req = flask.Request.from_values( + json=raw_pubsub_request_noattributes, path="/myfunc/" + ) + payload = event_conversion.marshal_background_event_data(req) + + # Remove timestamps as they get generates on the fly + del marshalled_pubsub_request["context"]["timestamp"] + del payload["context"]["timestamp"] + + # Resource name is set to empty string when it cannot be parsed from the request path + marshalled_pubsub_request["context"]["resource"]["name"] = "" + + assert payload == marshalled_pubsub_request + + def test_marshal_background_event_data_with_topic_path( raw_pubsub_request, marshalled_pubsub_request ): From 725e169aeb4a4bc1267100d661be805355de59b0 Mon Sep 17 00:00:00 2001 From: Mike Moore Date: Wed, 11 May 2022 07:32:09 +0100 Subject: [PATCH 3/3] fix(unit tests): fixed bug if using incorrect test result in unit tests for no attr test --- tests/test_convert.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_convert.py b/tests/test_convert.py index 8a4a26c8..592681e7 100644 --- a/tests/test_convert.py +++ b/tests/test_convert.py @@ -373,7 +373,7 @@ def test_marshal_background_event_data_without_topic_in_path( def test_marshal_background_event_data_without_topic_in_path_no_attr( - raw_pubsub_request_noattributes, marshalled_pubsub_request + raw_pubsub_request_noattributes, marshalled_pubsub_request_noattr ): req = flask.Request.from_values( json=raw_pubsub_request_noattributes, path="/myfunc/" @@ -381,13 +381,13 @@ def test_marshal_background_event_data_without_topic_in_path_no_attr( payload = event_conversion.marshal_background_event_data(req) # Remove timestamps as they get generates on the fly - del marshalled_pubsub_request["context"]["timestamp"] + del marshalled_pubsub_request_noattr["context"]["timestamp"] del payload["context"]["timestamp"] # Resource name is set to empty string when it cannot be parsed from the request path - marshalled_pubsub_request["context"]["resource"]["name"] = "" + marshalled_pubsub_request_noattr["context"]["resource"]["name"] = "" - assert payload == marshalled_pubsub_request + assert payload == marshalled_pubsub_request_noattr def test_marshal_background_event_data_with_topic_path(