From 014440103eb5ac59c441b3fa517fc6d1ad996b8e Mon Sep 17 00:00:00 2001 From: Ludovic Rivallain Date: Wed, 15 Mar 2023 17:50:16 +0100 Subject: [PATCH] Fix: empty payload (#1105) Closes #1081 Signed-off-by: Ludovic Rivallain --- .../iot/device/iothub/pipeline/pipeline_stages_iothub_http.py | 4 ++-- .../iot/device/iothub/pipeline/pipeline_stages_iothub_mqtt.py | 4 ++-- .../unit/iothub/pipeline/test_pipeline_stages_iothub_mqtt.py | 4 +++- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/azure-iot-device/azure/iot/device/iothub/pipeline/pipeline_stages_iothub_http.py b/azure-iot-device/azure/iot/device/iothub/pipeline/pipeline_stages_iothub_http.py index 01ecf4b2f..ea3436a5b 100644 --- a/azure-iot-device/azure/iot/device/iothub/pipeline/pipeline_stages_iothub_http.py +++ b/azure-iot-device/azure/iot/device/iothub/pipeline/pipeline_stages_iothub_http.py @@ -77,7 +77,7 @@ def on_request_response(op, error): ) error = map_http_error(error=error, http_op=op) if not error: - op_waiting_for_response.method_response = json.loads(op.response_body) + op_waiting_for_response.method_response = json.loads(op.response_body or 'null') op_waiting_for_response.complete(error=error) self.send_op_down( @@ -122,7 +122,7 @@ def on_request_response(op, error): ) error = map_http_error(error=error, http_op=op) if not error: - op_waiting_for_response.storage_info = json.loads(op.response_body) + op_waiting_for_response.storage_info = json.loads(op.response_body or 'null') op_waiting_for_response.complete(error=error) self.send_op_down( diff --git a/azure-iot-device/azure/iot/device/iothub/pipeline/pipeline_stages_iothub_mqtt.py b/azure-iot-device/azure/iot/device/iothub/pipeline/pipeline_stages_iothub_mqtt.py index 128d2c04d..7b9d3dea4 100644 --- a/azure-iot-device/azure/iot/device/iothub/pipeline/pipeline_stages_iothub_mqtt.py +++ b/azure-iot-device/azure/iot/device/iothub/pipeline/pipeline_stages_iothub_mqtt.py @@ -203,7 +203,7 @@ def _handle_pipeline_event(self, event): method_received = MethodRequest( request_id=request_id, name=method_name, - payload=json.loads(event.payload.decode("utf-8")), + payload=json.loads(event.payload.decode("utf-8") or 'null'), ) self.send_event_up(pipeline_events_iothub.MethodRequestEvent(method_received)) @@ -219,7 +219,7 @@ def _handle_pipeline_event(self, event): elif mqtt_topic_iothub.is_twin_desired_property_patch_topic(topic): self.send_event_up( pipeline_events_iothub.TwinDesiredPropertiesPatchEvent( - patch=json.loads(event.payload.decode("utf-8")) + patch=json.loads(event.payload.decode("utf-8") or 'null') ) ) diff --git a/tests/unit/iothub/pipeline/test_pipeline_stages_iothub_mqtt.py b/tests/unit/iothub/pipeline/test_pipeline_stages_iothub_mqtt.py index b9e116f53..4d049cd3b 100644 --- a/tests/unit/iothub/pipeline/test_pipeline_stages_iothub_mqtt.py +++ b/tests/unit/iothub/pipeline/test_pipeline_stages_iothub_mqtt.py @@ -892,7 +892,7 @@ def test_method_request(self, event, stage, method_name, rid): assert new_event.method_request.name == method_name assert new_event.method_request.request_id == rid # This is expanded on in in the next test - assert new_event.method_request.payload == json.loads(event.payload.decode("utf-8")) + assert new_event.method_request.payload == json.loads(event.payload.decode("utf-8") or 'null') @pytest.mark.it( "Derives the MethodRequestEvent's payload by converting the original event's payload from bytes into a JSON object" @@ -904,6 +904,7 @@ def test_method_request(self, event, stage, method_name, rid): pytest.param(b'"payload"', "payload", id="String JSON"), pytest.param(b"1234", 1234, id="Int JSON"), pytest.param(b"null", None, id="None JSON"), + pytest.param(b"", None, id="Empty JSON"), ], ) def test_json_payload(self, event, stage, original_payload, derived_payload): @@ -975,6 +976,7 @@ def event(self): pytest.param(b'"payload"', "payload", id="String JSON"), pytest.param(b"1234", 1234, id="Int JSON"), pytest.param(b"null", None, id="None JSON"), + pytest.param(b"", None, id="Empty JSON"), ], ) def test_twin_patch_event(self, event, stage, original_payload, derived_payload):