Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DynamoDB Enhanced - Change to use regex pattern for the attribute name cleaner #5677

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/next-release/bugfix-AWSSDKforJavav2-5155891.json
Original file line number Diff line number Diff line change
@@ -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."
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -40,9 +38,7 @@

@SdkInternalApi
public final class EnhancedClientUtils {
private static final Set<Character> 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() {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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&l<m>n?o=p!q@r%s$t|u");
String result = EnhancedClientUtils.cleanAttributeName("a*b.c-d:e#f+g:h/i(j)k&l<m>n?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");
}
}
Loading