Merge branch '2.1.x'

This commit is contained in:
Madhura Bhave 2019-03-25 16:04:22 -07:00
commit 5bd2b349bc
2 changed files with 60 additions and 1 deletions

View File

@ -106,7 +106,23 @@ public class NoUnboundElementsBindHandler extends AbstractBindHandler {
private boolean isUnbound(ConfigurationPropertyName name,
ConfigurationPropertyName candidate) {
return name.isAncestorOf(candidate) && !this.boundNames.contains(candidate);
if (name.isAncestorOf(candidate)) {
if (!this.boundNames.contains(candidate)
&& !isOverriddenCollectionElement(candidate)) {
return true;
}
}
return false;
}
private boolean isOverriddenCollectionElement(ConfigurationPropertyName candidate) {
int length = candidate.getNumberOfElements();
if (candidate.isNumericIndex(length - 1)) {
ConfigurationPropertyName propertyName = candidate
.chop(candidate.getNumberOfElements() - 1);
return this.boundNames.contains(propertyName);
}
return false;
}
}

View File

@ -108,6 +108,35 @@ public class NoUnboundElementsBindHandlerTests {
assertThat(bound.getFoo()).isEqualTo("bar");
}
@Test
public void bindWhenUsingNoUnboundElementsHandlerShouldBindIfUnboundCollectionProperties() {
MockConfigurationPropertySource source1 = new MockConfigurationPropertySource();
source1.put("example.foo[0]", "bar");
MockConfigurationPropertySource source2 = new MockConfigurationPropertySource();
source2.put("example.foo[0]", "bar");
source2.put("example.foo[1]", "baz");
this.sources.add(source1);
this.sources.add(source2);
this.binder = new Binder(this.sources);
NoUnboundElementsBindHandler handler = new NoUnboundElementsBindHandler();
ExampleWithList bound = this.binder
.bind("example", Bindable.of(ExampleWithList.class), handler).get();
assertThat(bound.getFoo()).containsExactly("bar");
}
@Test
public void bindWhenUsingNoUnboundElementsHandlerAndUnboundListElementsShouldThrowException() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("example.foo[0]", "bar");
this.sources.add(source);
this.binder = new Binder(this.sources);
assertThatExceptionOfType(BindException.class)
.isThrownBy(() -> this.binder.bind("example", Bindable.of(Example.class),
new NoUnboundElementsBindHandler()))
.satisfies((ex) -> assertThat(ex.getCause().getMessage())
.contains("The elements [example.foo[0]] were left unbound"));
}
public static class Example {
private String foo;
@ -122,4 +151,18 @@ public class NoUnboundElementsBindHandlerTests {
}
public static class ExampleWithList {
private List<String> foo;
public List<String> getFoo() {
return this.foo;
}
public void setFoo(List<String> foo) {
this.foo = foo;
}
}
}