Browse Source

Defer decision on BindingResult type until access

Closes gh-24347
pull/25098/head
Rossen Stoyanchev 6 years ago
parent
commit
6403a9f10e
  1. 11
      spring-context/src/main/java/org/springframework/validation/DataBinder.java
  2. 9
      spring-context/src/test/java/org/springframework/validation/DataBinderTests.java

11
spring-context/src/main/java/org/springframework/validation/DataBinder.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -129,6 +129,8 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter { @@ -129,6 +129,8 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter {
@Nullable
private AbstractPropertyBindingResult bindingResult;
private boolean directFieldAccess = false;
@Nullable
private SimpleTypeConverter typeConverter;
@ -249,7 +251,7 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter { @@ -249,7 +251,7 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter {
public void initBeanPropertyAccess() {
Assert.state(this.bindingResult == null,
"DataBinder is already initialized - call initBeanPropertyAccess before other configuration methods");
this.bindingResult = createBeanPropertyBindingResult();
this.directFieldAccess = false;
}
/**
@ -280,7 +282,7 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter { @@ -280,7 +282,7 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter {
public void initDirectFieldAccess() {
Assert.state(this.bindingResult == null,
"DataBinder is already initialized - call initDirectFieldAccess before other configuration methods");
this.bindingResult = createDirectFieldBindingResult();
this.directFieldAccess = true;
}
/**
@ -308,7 +310,8 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter { @@ -308,7 +310,8 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter {
*/
protected AbstractPropertyBindingResult getInternalBindingResult() {
if (this.bindingResult == null) {
initBeanPropertyAccess();
this.bindingResult = (this.directFieldAccess ?
createDirectFieldBindingResult(): createBeanPropertyBindingResult());
}
return this.bindingResult;
}

9
spring-context/src/test/java/org/springframework/validation/DataBinderTests.java

@ -2050,9 +2050,18 @@ public class DataBinderTests { @@ -2050,9 +2050,18 @@ public class DataBinderTests {
assertThatIllegalStateException().isThrownBy(() ->
binder.setMessageCodesResolver(new DefaultMessageCodesResolver()))
.withMessageContaining("DataBinder is already initialized with MessageCodesResolver");
}
@Test // gh-24347
public void overrideBindingResultType() {
TestBean testBean = new TestBean();
DataBinder binder = new DataBinder(testBean, "testBean");
binder.initDirectFieldAccess();
binder.initBeanPropertyAccess();
assertThat(binder.getBindingResult()).isInstanceOf(BeanPropertyBindingResult.class);
}
@SuppressWarnings("unused")
private static class BeanWithIntegerList {

Loading…
Cancel
Save