Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
edea2238
Commit
edea2238
authored
Jul 03, 2019
by
Phillip Webb
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '2.1.x'
Closes gh-17422
parents
e0048f23
2f88dd73
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
54 additions
and
9 deletions
+54
-9
BindHandler.java
...ngframework/boot/context/properties/bind/BindHandler.java
+3
-1
ValidationBindHandler.java
...ext/properties/bind/validation/ValidationBindHandler.java
+9
-6
ValidationBindHandlerTests.java
...roperties/bind/validation/ValidationBindHandlerTests.java
+42
-2
No files found.
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/BindHandler.java
View file @
edea2238
...
...
@@ -92,7 +92,9 @@ public interface BindHandler {
}
/**
* Called when binding finishes, regardless of whether the property was bound or not.
* Called when binding finishes with either bound or unbound result. This method will
* not be called when binding failed, even if a handler rurns a result from
* {@link #onFailure}.
* @param name the name of the element being bound
* @param target the item being bound
* @param context the bind context
...
...
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/validation/ValidationBindHandler.java
View file @
edea2238
...
...
@@ -66,18 +66,21 @@ public class ValidationBindHandler extends AbstractBindHandler {
}
@Override
public
void
onFinish
(
ConfigurationPropertyName
name
,
Bindable
<?>
target
,
BindContext
context
,
Object
result
)
public
Object
onFailure
(
ConfigurationPropertyName
name
,
Bindable
<?>
target
,
BindContext
context
,
Exception
error
)
throws
Exception
{
Object
result
=
super
.
onFailure
(
name
,
target
,
context
,
error
);
if
(
result
!=
null
)
{
this
.
exceptions
.
clear
();
}
validate
(
name
,
target
,
context
,
result
);
super
.
onFinish
(
name
,
target
,
context
,
result
)
;
return
result
;
}
@Override
public
Object
onFailure
(
ConfigurationPropertyName
name
,
Bindable
<?>
target
,
BindContext
context
,
Exception
error
)
public
void
onFinish
(
ConfigurationPropertyName
name
,
Bindable
<?>
target
,
BindContext
context
,
Object
result
)
throws
Exception
{
Object
result
=
super
.
onFailure
(
name
,
target
,
context
,
error
);
validate
(
name
,
target
,
context
,
null
);
return
result
;
validate
(
name
,
target
,
context
,
result
);
super
.
onFinish
(
name
,
target
,
context
,
result
);
}
private
void
validate
(
ConfigurationPropertyName
name
,
Bindable
<?>
target
,
BindContext
context
,
Object
result
)
{
...
...
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/validation/ValidationBindHandlerTests.java
View file @
edea2238
...
...
@@ -167,7 +167,7 @@ class ValidationBindHandlerTests {
@Test
void
bindShouldValidateIfOtherHandlersInChainIgnoreError
()
{
TestHandler
testHandler
=
new
TestHandler
();
TestHandler
testHandler
=
new
TestHandler
(
null
);
this
.
handler
=
new
ValidationBindHandler
(
testHandler
,
this
.
validator
);
this
.
sources
.
add
(
new
MockConfigurationPropertySource
(
"foo"
,
"hello"
));
ExampleValidatedBean
bean
=
new
ExampleValidatedBean
();
...
...
@@ -177,6 +177,21 @@ class ValidationBindHandlerTests {
.
withCauseInstanceOf
(
BindValidationException
.
class
);
}
@Test
void
bindShouldValidateIfOtherHandlersInChainReplaceErrorWithResult
()
{
TestHandler
testHandler
=
new
TestHandler
(
new
ExampleValidatedBeanSubclass
());
this
.
handler
=
new
ValidationBindHandler
(
testHandler
,
this
.
validator
);
this
.
sources
.
add
(
new
MockConfigurationPropertySource
(
"foo"
,
"hello"
));
this
.
sources
.
add
(
new
MockConfigurationPropertySource
(
"foo.age"
,
"bad"
));
this
.
sources
.
add
(
new
MockConfigurationPropertySource
(
"foo.years"
,
"99"
));
ExampleValidatedBean
bean
=
new
ExampleValidatedBean
();
assertThatExceptionOfType
(
BindException
.
class
)
.
isThrownBy
(()
->
this
.
binder
.
bind
(
"foo"
,
Bindable
.
of
(
ExampleValidatedBean
.
class
).
withExistingValue
(
bean
),
this
.
handler
))
.
withCauseInstanceOf
(
BindValidationException
.
class
)
.
satisfies
((
ex
)
->
assertThat
(
ex
.
getCause
()).
hasMessageContaining
(
"years"
));
}
private
BindValidationException
bindAndExpectValidationError
(
Runnable
action
)
{
try
{
action
.
run
();
...
...
@@ -219,6 +234,25 @@ class ValidationBindHandlerTests {
}
public
static
class
ExampleValidatedBeanSubclass
extends
ExampleValidatedBean
{
@Min
(
100
)
private
int
years
;
ExampleValidatedBeanSubclass
()
{
setAge
(
20
);
}
public
int
getYears
()
{
return
this
.
years
;
}
public
void
setYears
(
int
years
)
{
this
.
years
=
years
;
}
}
@Validated
public
static
class
ExampleValidatedWithNestedBean
{
...
...
@@ -286,10 +320,16 @@ class ValidationBindHandlerTests {
static
class
TestHandler
extends
AbstractBindHandler
{
private
Object
result
;
TestHandler
(
Object
result
)
{
this
.
result
=
result
;
}
@Override
public
Object
onFailure
(
ConfigurationPropertyName
name
,
Bindable
<?>
target
,
BindContext
context
,
Exception
error
)
throws
Exception
{
return
null
;
return
this
.
result
;
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment