diff --git a/.changes/next-release/bugfix-AWSSDKforJavav2-5155891.json b/.changes/next-release/bugfix-AWSSDKforJavav2-5155891.json new file mode 100644 index 00000000000..8ffdaaaa071 --- /dev/null +++ b/.changes/next-release/bugfix-AWSSDKforJavav2-5155891.json @@ -0,0 +1,6 @@ +{ + "type": "bugfix", + "category": "AWS SDK for Java v2", + "contributor": "", + "description": "DynamoDB Enhanced - Attribute name cleaner now uses a regex pattern instead of a set of defined characters." +} diff --git a/services-custom/dynamodb-enhanced/src/main/java/software/amazon/awssdk/enhanced/dynamodb/internal/EnhancedClientUtils.java b/services-custom/dynamodb-enhanced/src/main/java/software/amazon/awssdk/enhanced/dynamodb/internal/EnhancedClientUtils.java index 61d750e98a7..66db23f07ae 100644 --- a/services-custom/dynamodb-enhanced/src/main/java/software/amazon/awssdk/enhanced/dynamodb/internal/EnhancedClientUtils.java +++ b/services-custom/dynamodb-enhanced/src/main/java/software/amazon/awssdk/enhanced/dynamodb/internal/EnhancedClientUtils.java @@ -21,12 +21,10 @@ import java.util.List; import java.util.Map; import java.util.Optional; -import java.util.Set; import java.util.function.Function; import java.util.function.Supplier; import java.util.regex.Pattern; import java.util.stream.Collectors; -import java.util.stream.Stream; import software.amazon.awssdk.annotations.SdkInternalApi; import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClientExtension; import software.amazon.awssdk.enhanced.dynamodb.Key; @@ -40,9 +38,7 @@ @SdkInternalApi public final class EnhancedClientUtils { - private static final Set SPECIAL_CHARACTERS = Stream.of( - '*', '.', '-', '#', '+', ':', '/', '(', ')', ' ', - '&', '<', '>', '?', '=', '!', '@', '%', '$', '|').collect(Collectors.toSet()); + private static final Pattern SPECIAL_CHARACTERS_PATTERN = Pattern.compile("\\W"); private static final Pattern NESTED_OBJECT_PATTERN = Pattern.compile(NESTED_OBJECT_UPDATE); private EnhancedClientUtils() { @@ -57,18 +53,7 @@ private EnhancedClientUtils() { * @return A key that has all these characters scrubbed and overwritten with an underscore. */ public static String cleanAttributeName(String key) { - boolean somethingChanged = false; - - char[] chars = key.toCharArray(); - - for (int i = 0; i < chars.length; ++i) { - if (SPECIAL_CHARACTERS.contains(chars[i])) { - chars[i] = '_'; - somethingChanged = true; - } - } - - return somethingChanged ? new String(chars) : key; + return SPECIAL_CHARACTERS_PATTERN.matcher(key).replaceAll("_"); } private static boolean isNestedAttribute(String key) { diff --git a/services-custom/dynamodb-enhanced/src/test/java/software/amazon/awssdk/enhanced/dynamodb/internal/EnhancedClientUtilsTest.java b/services-custom/dynamodb-enhanced/src/test/java/software/amazon/awssdk/enhanced/dynamodb/internal/EnhancedClientUtilsTest.java index 6e3bbdbdc9a..9ec86f844b4 100644 --- a/services-custom/dynamodb-enhanced/src/test/java/software/amazon/awssdk/enhanced/dynamodb/internal/EnhancedClientUtilsTest.java +++ b/services-custom/dynamodb-enhanced/src/test/java/software/amazon/awssdk/enhanced/dynamodb/internal/EnhancedClientUtilsTest.java @@ -60,8 +60,8 @@ public void createKeyFromMap_partitionAndSort() { @Test public void cleanAttributeName_cleansSpecialCharacters() { - String result = EnhancedClientUtils.cleanAttributeName("a*b.c-d:e#f+g:h/i(j)k&ln?o=p!q@r%s$t|u"); + String result = EnhancedClientUtils.cleanAttributeName("a*b.c-d:e#f+g:h/i(j)k&ln?o=p!q@r%s$t|u~v[w]x"); - assertThat(result).isEqualTo("a_b_c_d_e_f_g_h_i_j_k_l_m_n_o_p_q_r_s_t_u"); + assertThat(result).isEqualTo("a_b_c_d_e_f_g_h_i_j_k_l_m_n_o_p_q_r_s_t_u_v_w_x"); } } \ No newline at end of file