diff --git a/services-custom/dynamodb-enhanced/src/main/java/software/amazon/awssdk/enhanced/dynamodb/extensions/AutoGeneratedUuidExtension.java b/services-custom/dynamodb-enhanced/src/main/java/software/amazon/awssdk/enhanced/dynamodb/extensions/AutoGeneratedUuidExtension.java index d92db8c60bbd..520d6ff016a9 100644 --- a/services-custom/dynamodb-enhanced/src/main/java/software/amazon/awssdk/enhanced/dynamodb/extensions/AutoGeneratedUuidExtension.java +++ b/services-custom/dynamodb-enhanced/src/main/java/software/amazon/awssdk/enhanced/dynamodb/extensions/AutoGeneratedUuidExtension.java @@ -116,8 +116,21 @@ public WriteModification beforeWrite(DynamoDbExtensionContext.BeforeWrite contex .build(); } + /** + * Inserts a new UUID into the given map of attributes if the specified attribute key is absent or has an empty value. + * + * This method checks whether the attribute with the specified key is already present in the `itemToTransform` map + * and if its value is a non-empty string. If the attribute exists and has a value, the method does nothing. + * Otherwise, it generates a new UUID using {@link UUID#randomUUID()} and sets it as the value for the given key. + * + * @param itemToTransform The map containing attributes of the item being transformed. + * @param key The attribute key that should be checked and potentially updated with a new UUID. + */ private void insertUuidInItemToTransform(Map itemToTransform, String key) { + if (itemToTransform.containsKey(key) && itemToTransform.get(key).s() != null && !itemToTransform.get(key).s().isEmpty()) { + return; + } itemToTransform.put(key, AttributeValue.builder().s(UUID.randomUUID().toString()).build()); } diff --git a/services-custom/dynamodb-enhanced/src/test/java/software/amazon/awssdk/enhanced/dynamodb/extensions/AutoGeneratedUuidExtensionTest.java b/services-custom/dynamodb-enhanced/src/test/java/software/amazon/awssdk/enhanced/dynamodb/extensions/AutoGeneratedUuidExtensionTest.java index cc69f503d50f..8d5cb24c9cd4 100644 --- a/services-custom/dynamodb-enhanced/src/test/java/software/amazon/awssdk/enhanced/dynamodb/extensions/AutoGeneratedUuidExtensionTest.java +++ b/services-custom/dynamodb-enhanced/src/test/java/software/amazon/awssdk/enhanced/dynamodb/extensions/AutoGeneratedUuidExtensionTest.java @@ -45,7 +45,7 @@ public class AutoGeneratedUuidExtensionTest { private static final OperationContext PRIMARY_CONTEXT = DefaultOperationContext.create(TABLE_NAME, TableMetadata.primaryIndexName()); - private final AutoGeneratedUuidExtension atomicCounterExtension = AutoGeneratedUuidExtension.create(); + private final AutoGeneratedUuidExtension autoGeneratedUuidExtension = AutoGeneratedUuidExtension.create(); private static final StaticTableSchema ITEM_WITH_UUID_MAPPER = @@ -54,7 +54,9 @@ public class AutoGeneratedUuidExtensionTest { .addAttribute(String.class, a -> a.name("id") .getter(ItemWithUuid::getId) .setter(ItemWithUuid::setId) - .addTag(primaryPartitionKey())) + .addTag(primaryPartitionKey()) + .addTag(AutoGeneratedUuidExtension.AttributeTags.autoGeneratedUuidAttribute()) + ) .addAttribute(String.class, a -> a.name("uuidAttribute") .getter(ItemWithUuid::getUuidAttribute) .setter(ItemWithUuid::setUuidAttribute) @@ -62,9 +64,33 @@ public class AutoGeneratedUuidExtensionTest { ) .addAttribute(String.class, a -> a.name("simpleString") .getter(ItemWithUuid::getSimpleString) - .setter(ItemWithUuid::setSimpleString)) + .setter(ItemWithUuid::setSimpleString) + ) .build(); + @Test + public void beforeWrite_updateItemOperation_hasUuidInItem_Pk_doesNotCreateUpdateExpressionAndFilters() { + ItemWithUuid SimpleItem = new ItemWithUuid(); + SimpleItem.setId(RECORD_ID); + + Map items = ITEM_WITH_UUID_MAPPER.itemToMap(SimpleItem, true); + assertThat(items).hasSize(1); + + WriteModification result = + autoGeneratedUuidExtension.beforeWrite(DefaultDynamoDbExtensionContext.builder() + .items(items) + .tableMetadata(ITEM_WITH_UUID_MAPPER.tableMetadata()) + .operationName(OperationName.UPDATE_ITEM) + .operationContext(PRIMARY_CONTEXT).build()); + + Map transformedItem = result.transformedItem(); + assertThat(transformedItem).isNotNull().hasSize(2); + assertThat(transformedItem).containsEntry("id", AttributeValue.fromS(RECORD_ID)); + isValidUuid(transformedItem.get("uuidAttribute").s()); + assertThat(result.updateExpression()).isNull(); + + } + @Test public void beforeWrite_updateItemOperation_hasUuidInItem_doesNotCreateUpdateExpressionAndFilters() { ItemWithUuid SimpleItem = new ItemWithUuid(); @@ -76,7 +102,7 @@ public void beforeWrite_updateItemOperation_hasUuidInItem_doesNotCreateUpdateExp assertThat(items).hasSize(2); WriteModification result = - atomicCounterExtension.beforeWrite(DefaultDynamoDbExtensionContext.builder() + autoGeneratedUuidExtension.beforeWrite(DefaultDynamoDbExtensionContext.builder() .items(items) .tableMetadata(ITEM_WITH_UUID_MAPPER.tableMetadata()) .operationName(OperationName.UPDATE_ITEM) @@ -99,7 +125,7 @@ public void beforeWrite_updateItemOperation_hasNoUuidInItem_doesNotCreatesUpdate assertThat(items).hasSize(1); WriteModification result = - atomicCounterExtension.beforeWrite(DefaultDynamoDbExtensionContext.builder() + autoGeneratedUuidExtension.beforeWrite(DefaultDynamoDbExtensionContext.builder() .items(items) .tableMetadata(ITEM_WITH_UUID_MAPPER.tableMetadata()) .operationName(OperationName.UPDATE_ITEM) @@ -121,7 +147,7 @@ public void beforeWrite_updateItemOperation_UuidNotPresent_newUuidCreated() { assertThat(items).hasSize(1); WriteModification result = - atomicCounterExtension.beforeWrite(DefaultDynamoDbExtensionContext.builder() + autoGeneratedUuidExtension.beforeWrite(DefaultDynamoDbExtensionContext.builder() .items(items) .tableMetadata(ITEM_WITH_UUID_MAPPER.tableMetadata()) .operationName(OperationName.UPDATE_ITEM)