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
13999546
Commit
13999546
authored
Jan 10, 2020
by
Madhura Bhave
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Short circuit validation in bind handler if previous exception present
Closes gh-19599
parent
7f0573df
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
11 deletions
+20
-11
ValidationBindHandler.java
...ext/properties/bind/validation/ValidationBindHandler.java
+11
-11
ValidationBindHandlerTests.java
...roperties/bind/validation/ValidationBindHandlerTests.java
+9
-0
No files found.
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/validation/ValidationBindHandler.java
View file @
13999546
...
...
@@ -16,10 +16,8 @@
package
org
.
springframework
.
boot
.
context
.
properties
.
bind
.
validation
;
import
java.util.Deque
;
import
java.util.LinkedHashMap
;
import
java.util.LinkedHashSet
;
import
java.util.LinkedList
;
import
java.util.Map
;
import
java.util.Set
;
import
java.util.stream.Collectors
;
...
...
@@ -52,7 +50,7 @@ public class ValidationBindHandler extends AbstractBindHandler {
private
final
Set
<
ConfigurationProperty
>
boundProperties
=
new
LinkedHashSet
<>();
private
final
Deque
<
BindValidationException
>
exceptions
=
new
LinkedList
<>()
;
private
BindValidationException
exception
;
public
ValidationBindHandler
(
Validator
...
validators
)
{
this
.
validators
=
validators
;
...
...
@@ -94,7 +92,7 @@ public class ValidationBindHandler extends AbstractBindHandler {
this
.
boundTypes
.
clear
();
this
.
boundResults
.
clear
();
this
.
boundProperties
.
clear
();
this
.
exception
s
.
clear
()
;
this
.
exception
=
null
;
}
@Override
...
...
@@ -105,13 +103,15 @@ public class ValidationBindHandler extends AbstractBindHandler {
}
private
void
validate
(
ConfigurationPropertyName
name
,
Bindable
<?>
target
,
BindContext
context
,
Object
result
)
{
Object
validationTarget
=
getValidationTarget
(
target
,
context
,
result
);
Class
<?>
validationType
=
target
.
getBoxedType
().
resolve
();
if
(
validationTarget
!=
null
)
{
validateAndPush
(
name
,
validationTarget
,
validationType
);
if
(
this
.
exception
==
null
)
{
Object
validationTarget
=
getValidationTarget
(
target
,
context
,
result
);
Class
<?>
validationType
=
target
.
getBoxedType
().
resolve
();
if
(
validationTarget
!=
null
)
{
validateAndPush
(
name
,
validationTarget
,
validationType
);
}
}
if
(
context
.
getDepth
()
==
0
&&
!
this
.
exceptions
.
isEmpty
()
)
{
throw
this
.
exception
s
.
pop
()
;
if
(
context
.
getDepth
()
==
0
&&
this
.
exception
!=
null
)
{
throw
this
.
exception
;
}
}
...
...
@@ -134,7 +134,7 @@ public class ValidationBindHandler extends AbstractBindHandler {
}
}
if
(
result
!=
null
&&
result
.
hasErrors
())
{
this
.
exception
s
.
push
(
new
BindValidationException
(
result
.
getValidationErrors
()
));
this
.
exception
=
new
BindValidationException
(
result
.
getValidationErrors
(
));
}
}
...
...
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/validation/ValidationBindHandlerTests.java
View file @
13999546
...
...
@@ -201,6 +201,15 @@ class ValidationBindHandlerTests {
assertThat
(
cause
.
getMessage
()).
contains
(
"rejected value [2]"
);
}
@Test
void
validationShouldBeSkippedIfPreviousValidationErrorPresent
()
{
this
.
sources
.
add
(
new
MockConfigurationPropertySource
(
"foo.inner.person-age"
,
2
));
BindValidationException
cause
=
bindAndExpectValidationError
(()
->
this
.
binder
.
bind
(
ConfigurationPropertyName
.
of
(
"foo"
),
Bindable
.
of
(
ExampleCamelCase
.
class
),
this
.
handler
));
FieldError
fieldError
=
(
FieldError
)
cause
.
getValidationErrors
().
getAllErrors
().
get
(
0
);
assertThat
(
fieldError
.
getField
()).
isEqualTo
(
"personAge"
);
}
private
BindValidationException
bindAndExpectValidationError
(
Runnable
action
)
{
try
{
action
.
run
();
...
...
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