Skip to content

Commit

Permalink
Treat Setting value with empty array string as empty array
Browse files Browse the repository at this point in the history
Signed-off-by: Jeongmin Yu <machenity@gmail.com>
  • Loading branch information
machenity committed Oct 14, 2023
1 parent 1447d75 commit 35515a7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2337,11 +2337,17 @@ public static <T> Setting<List<T>> listSetting(
if (defaultStringValue.apply(Settings.EMPTY) == null) {
throw new IllegalArgumentException("default value function must not return null");
}
Function<String, List<T>> parser = (s) -> parseableStringToList(s).stream().map(singleValueParser).collect(Collectors.toList());
Function<String, List<T>> parser = (s) -> isParsedAsEmptyArray(s)
? Collections.emptyList()
: parseableStringToList(s).stream().map(singleValueParser).collect(Collectors.toList());

return new ListSetting<>(key, fallbackSetting, defaultStringValue, parser, validator, properties);
}

private static boolean isParsedAsEmptyArray(String parsableString) {
return parsableString.replaceAll("\\s", "").contentEquals("[\"[]\"]");
}

private static List<String> parseableStringToList(String parsableString) {
// fromXContent doesn't use named xcontent or deprecation.
try (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,10 @@ public void testUnknownNodeRoleOnly() {
assertEquals(testRole, nodeRoles.get(0).roleName());
assertEquals(testRole, nodeRoles.get(0).roleNameAbbreviation());
}

public void testNodeRoleOfEmptyArrayNameShouldBeIgnored() {
String testRole = "[ ]";
Settings roleSettings = Settings.builder().put(NodeRoleSettings.NODE_ROLES_SETTING.getKey(), testRole).build();
assertEquals(Collections.emptyList(), NodeRoleSettings.NODE_ROLES_SETTING.get(roleSettings));
}
}

0 comments on commit 35515a7

Please sign in to comment.