fix(iot): honour attributePayload.merge, thingTypeName and expectedVersion on things (#6657)

* fix(iot): honour attributePayload.merge, thingTypeName and expectedVersion on things

UpdateThing replaced the attributes with whatever the request carried, so a
merge update dropped the stored attributes and a type-only update wiped them.
It now merges when attributePayload.merge is true, removes an attribute given
an empty value, leaves the attributes alone when the request has none, sets or
removes the thing type, and rejects a missing or deprecated type and a
removeThingType that also names a type. DeleteThing now honours
expectedVersion, and a stale version reports AWS's message.

Closes floci-io/floci#6655

* fix(iot): serialize UpdateThing and DeleteThing, reject a malformed thingTypeName

A merge is a read-modify-write and expectedVersion is a compare-and-set,
so both now run under one thing write lock. An empty or malformed
thingTypeName on UpdateThing is an InvalidRequestException, as in AWS,
instead of being treated as absent.

* test(iot): race versioned thing updates and deletes, rerun the SDK test on AWS

A race test holds the thing write lock to one winner per expected
version. On AWS the SDK test borrows an active test thing type, as
fleetIndexSearch does, since a deprecated type cannot be deleted for
five minutes and a rerun would fail.

* fix(iot): answer a malformed thingTypeName with AWS's validation message

UpdateThing now builds the refusal with SmithyValidationErrors, listing
the length violation before the pattern one, as measured on AWS.
This commit is contained in:
Mario Toffia authored and GitHub committed 2026-10-26 18:58:11 -04:00
1 parent 1b7bb80f36
commit c6505c9144
6 files changed
+546 -34

No files matched your search

@@ -1020,6 +1020,80 @@ class IotTest {
iot.deleteThingType(DeleteThingTypeRequest.builder().thingTypeName(thingType).build());
}
@Test
void thingUpdateHonoursMergeThingTypeAndExpectedVersion() {
// AWS keeps a deprecated thing type for five minutes before it can be deleted, so on AWS the
// test borrows a test thing type the account already has, as fleetIndexSearch does.
String thingType = TestFixtures.isRealAws() ? existingTestThingType() : "java-iot-update-type";
assertThat(thingType)
.as("on AWS the account needs an active thing type whose name contains \"test\"")
.isNotNull();
String thingName = "java-iot-update-thing";
try {
iot.deleteThing(DeleteThingRequest.builder().thingName(thingName).build());
} catch (Exception ignored) {
// Nothing is left over from an earlier run.
}
if (!TestFixtures.isRealAws()) {
deleteThingTypeQuietly(thingType);
iot.createThingType(CreateThingTypeRequest.builder().thingTypeName(thingType).build());
}
iot.createThing(CreateThingRequest.builder()
.thingName(thingName)
.attributePayload(AttributePayload.builder().attributes(Map.of("env", "java", "owner", "iot")).build())
.build());
iot.updateThing(UpdateThingRequest.builder()
.thingName(thingName)
.attributePayload(AttributePayload.builder()
.attributes(Map.of("fw", "1.2", "owner", ""))
.merge(true)
.build())
.build());
DescribeThingResponse merged = iot.describeThing(DescribeThingRequest.builder().thingName(thingName).build());
assertThat(merged.attributes()).isEqualTo(Map.of("env", "java", "fw", "1.2"));
iot.updateThing(UpdateThingRequest.builder().thingName(thingName).thingTypeName(thingType).build());
DescribeThingResponse typed = iot.describeThing(DescribeThingRequest.builder().thingName(thingName).build());
assertThat(typed.thingTypeName()).isEqualTo(thingType);
assertThat(typed.attributes()).isEqualTo(Map.of("env", "java", "fw", "1.2"));
iot.updateThing(UpdateThingRequest.builder().thingName(thingName).removeThingType(true).build());
DescribeThingResponse untyped = iot.describeThing(DescribeThingRequest.builder().thingName(thingName).build());
assertThat(untyped.thingTypeName()).isNull();
assertThatThrownBy(() -> iot.updateThing(UpdateThingRequest.builder()
.thingName(thingName)
.thingTypeName(thingType)
.removeThingType(true)
.build()))
.isInstanceOf(InvalidRequestException.class);
long version = untyped.version();
assertThatThrownBy(() -> iot.deleteThing(DeleteThingRequest.builder()
.thingName(thingName)
.expectedVersion(version - 1)
.build()))
.isInstanceOf(VersionConflictException.class);
assertThat(iot.describeThing(DescribeThingRequest.builder().thingName(thingName).build()).version()).isEqualTo(version);
iot.deleteThing(DeleteThingRequest.builder().thingName(thingName).expectedVersion(version).build());
assertThatThrownBy(() -> iot.describeThing(DescribeThingRequest.builder().thingName(thingName).build()))
.isInstanceOf(ResourceNotFoundException.class);
if (!TestFixtures.isRealAws()) {
deleteThingTypeQuietly(thingType);
}
}
private void deleteThingTypeQuietly(String thingType) {
try {
iot.deprecateThingType(DeprecateThingTypeRequest.builder().thingTypeName(thingType).build());
iot.deleteThingType(DeleteThingTypeRequest.builder().thingTypeName(thingType).build());
} catch (Exception ignored) {
// Absent: nothing is left over from an earlier run.
}
}
@Test
void mqttConnectPublishSubscribe() throws Exception {
String topic = "devices/java-iot-mqtt/events";