Allow nested square brackets in map key when binding

Fixes gh-3202
pull/8534/merge
Madhura Bhave 8 years ago
parent e8170cf00d
commit 233ef67a04

@ -513,18 +513,25 @@ public final class ConfigurationPropertyName
int start = 0; int start = 0;
boolean indexed = false; boolean indexed = false;
int length = name.length(); int length = name.length();
int openBracketCount = 0;
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
char ch = name.charAt(i); char ch = name.charAt(i);
if (indexed && ch == ']') { if (ch == ']') {
openBracketCount--;
if (openBracketCount == 0) {
processElement(processor, name, start, i + 1, indexed); processElement(processor, name, start, i + 1, indexed);
start = i + 1; start = i + 1;
indexed = false; indexed = false;
} }
else if (!indexed && ch == '[') { }
else if (ch == '[') {
openBracketCount++;
if (!indexed) {
processElement(processor, name, start, i, indexed); processElement(processor, name, start, i, indexed);
start = i; start = i;
indexed = true; indexed = true;
} }
}
else if (!indexed && ch == separator) { else if (!indexed && ch == separator) {
processElement(processor, name, start, i, indexed); processElement(processor, name, start, i, indexed);
start = i + 1; start = i + 1;

@ -503,6 +503,15 @@ public class MapBinderTests {
assertThat(map.get(0)).containsExactly(8, 9); assertThat(map.get(0)).containsExactly(8, 9);
} }
@Test
public void bindingWithSquareBracketMap() throws Exception {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.[x [B] y]", "[ball]");
this.sources.add(source);
Map<String, String> map = this.binder.bind("foo", STRING_STRING_MAP).get();
assertThat(map).containsEntry("x [B] y", "[ball]");
}
private <K, V> Bindable<Map<K, V>> getMapBindable(Class<K> keyGeneric, ResolvableType valueType) { private <K, V> Bindable<Map<K, V>> getMapBindable(Class<K> keyGeneric, ResolvableType valueType) {
ResolvableType keyType = ResolvableType.forClass(keyGeneric); ResolvableType keyType = ResolvableType.forClass(keyGeneric);
return Bindable.of(ResolvableType.forClassWithGenerics(Map.class, keyType, valueType)); return Bindable.of(ResolvableType.forClassWithGenerics(Map.class, keyType, valueType));

@ -175,6 +175,21 @@ public class ConfigurationPropertyNameTests {
ConfigurationPropertyName.of("bar]"); ConfigurationPropertyName.of("bar]");
} }
@Test
public void ofNameWhenMultipleMismatchedBrackets() throws Exception {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("is not valid");
ConfigurationPropertyName.of("[a[[[b]ar]");
}
@Test
public void ofNameWhenNestedBrackets() throws Exception {
ConfigurationPropertyName name = ConfigurationPropertyName.of("foo[a[c][[b]ar]]");
assertThat(name.toString()).isEqualTo("foo[a[c][[b]ar]]");
assertThat(name.getElement(0, Form.ORIGINAL)).isEqualTo("foo");
assertThat(name.getElement(1, Form.ORIGINAL)).isEqualTo("a[c][[b]ar]");
}
@Test @Test
public void ofNameWithWhitespaceInName() throws Exception { public void ofNameWithWhitespaceInName() throws Exception {
this.thrown.expect(IllegalArgumentException.class); this.thrown.expect(IllegalArgumentException.class);

Loading…
Cancel
Save