Fix: iot:UpdateThingShadow does not properly maintain state document (#4045)

Device shadow updates affect only the fields specified in the request state document.
Any field with a value of null is removed from the device's shadow.[1]

Verified behavior against a real AWS backend.

[1]: https://docs.aws.amazon.com/iot/latest/developerguide/device-shadow-rest-api.html#API_UpdateThingShadow
This commit is contained in:
Brian Pandola 2021-06-30 00:15:45 -07:00 committed by GitHub
commit 163ae322e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 7 deletions

View file

@ -109,3 +109,41 @@ def test_update():
def test_publish():
client = boto3.client("iot-data", region_name="ap-northeast-1")
client.publish(topic="test/topic", qos=1, payload=b"")
@mock_iot
@mock_iotdata
def test_delete_field_from_device_shadow():
test_thing_name = "TestThing"
iot_raw_client = boto3.client("iot", region_name="eu-central-1")
iot_raw_client.create_thing(thingName=test_thing_name)
iot = boto3.client("iot-data", region_name="eu-central-1")
iot.update_thing_shadow(
thingName=test_thing_name,
payload=json.dumps({"state": {"desired": {"state1": 1, "state2": 2}}}),
)
response = json.loads(
iot.get_thing_shadow(thingName=test_thing_name)["payload"].read()
)
assert len(response["state"]["desired"]) == 2
iot.update_thing_shadow(
thingName=test_thing_name,
payload=json.dumps({"state": {"desired": {"state1": None}}}),
)
response = json.loads(
iot.get_thing_shadow(thingName=test_thing_name)["payload"].read()
)
assert len(response["state"]["desired"]) == 1
assert "state2" in response["state"]["desired"]
iot.update_thing_shadow(
thingName=test_thing_name,
payload=json.dumps({"state": {"desired": {"state2": None}}}),
)
response = json.loads(
iot.get_thing_shadow(thingName=test_thing_name)["payload"].read()
)
assert "desired" not in response["state"]