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
b345fc85
Commit
b345fc85
authored
Jan 10, 2019
by
Madhura Bhave
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix validation when key matching the prefix is set
Fixes gh-15597
parent
ce62a962
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
57 additions
and
3 deletions
+57
-3
ValidationBindHandler.java
...ext/properties/bind/validation/ValidationBindHandler.java
+11
-0
ValidationBindHandlerTests.java
...roperties/bind/validation/ValidationBindHandlerTests.java
+46
-3
No files found.
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/validation/ValidationBindHandler.java
View file @
b345fc85
...
...
@@ -76,6 +76,17 @@ public class ValidationBindHandler extends AbstractBindHandler {
super
.
onFinish
(
name
,
target
,
context
,
result
);
}
@Override
public
Object
onFailure
(
ConfigurationPropertyName
name
,
Bindable
<?>
target
,
BindContext
context
,
Exception
error
)
throws
Exception
{
Object
result
=
super
.
onFailure
(
name
,
target
,
context
,
error
);
validate
(
name
,
target
,
context
,
null
);
if
(!
this
.
exceptions
.
isEmpty
())
{
throw
this
.
exceptions
.
pop
();
}
return
result
;
}
private
void
validate
(
ConfigurationPropertyName
name
,
Bindable
<?>
target
,
BindContext
context
,
Object
result
)
{
Object
validationTarget
=
getValidationTarget
(
target
,
context
,
result
);
...
...
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/validation/ValidationBindHandlerTests.java
View file @
b345fc85
...
...
@@ -27,6 +27,8 @@ import javax.validation.constraints.NotNull;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.springframework.boot.context.properties.bind.AbstractBindHandler
;
import
org.springframework.boot.context.properties.bind.BindContext
;
import
org.springframework.boot.context.properties.bind.BindException
;
import
org.springframework.boot.context.properties.bind.Bindable
;
import
org.springframework.boot.context.properties.bind.Binder
;
...
...
@@ -35,6 +37,7 @@ import org.springframework.boot.context.properties.source.ConfigurationPropertyN
import
org.springframework.boot.context.properties.source.ConfigurationPropertySource
;
import
org.springframework.boot.context.properties.source.MockConfigurationPropertySource
;
import
org.springframework.boot.origin.Origin
;
import
org.springframework.core.convert.ConverterNotFoundException
;
import
org.springframework.validation.FieldError
;
import
org.springframework.validation.ObjectError
;
import
org.springframework.validation.annotation.Validated
;
...
...
@@ -57,12 +60,14 @@ public class ValidationBindHandlerTests {
private
Binder
binder
;
private
LocalValidatorFactoryBean
validator
;
@Before
public
void
setup
()
{
this
.
binder
=
new
Binder
(
this
.
sources
);
LocalValidatorFactoryBean
validator
=
new
LocalValidatorFactoryBean
();
validator
.
afterPropertiesSet
();
this
.
handler
=
new
ValidationBindHandler
(
validator
);
this
.
validator
=
new
LocalValidatorFactoryBean
();
this
.
validator
.
afterPropertiesSet
();
this
.
handler
=
new
ValidationBindHandler
(
this
.
validator
);
}
@Test
...
...
@@ -162,6 +167,34 @@ public class ValidationBindHandlerTests {
this
.
handler
);
}
@Test
public
void
bindShouldNotValidateIfOtherHandlersInChainThrowError
()
{
this
.
sources
.
add
(
new
MockConfigurationPropertySource
(
"foo"
,
"hello"
));
ExampleValidatedBean
bean
=
new
ExampleValidatedBean
();
assertThatExceptionOfType
(
BindException
.
class
)
.
isThrownBy
(
()
->
this
.
binder
.
bind
(
"foo"
,
Bindable
.
of
(
ExampleValidatedBean
.
class
)
.
withExistingValue
(
bean
),
this
.
handler
))
.
withCauseInstanceOf
(
ConverterNotFoundException
.
class
);
}
@Test
public
void
bindShouldValidateIfOtherHandlersInChainIgnoreError
()
{
TestHandler
testHandler
=
new
TestHandler
();
this
.
handler
=
new
ValidationBindHandler
(
testHandler
,
this
.
validator
);
this
.
sources
.
add
(
new
MockConfigurationPropertySource
(
"foo"
,
"hello"
));
ExampleValidatedBean
bean
=
new
ExampleValidatedBean
();
assertThatExceptionOfType
(
BindException
.
class
)
.
isThrownBy
(
()
->
this
.
binder
.
bind
(
"foo"
,
Bindable
.
of
(
ExampleValidatedBean
.
class
)
.
withExistingValue
(
bean
),
this
.
handler
))
.
withCauseInstanceOf
(
BindValidationException
.
class
);
}
private
BindValidationException
bindAndExpectValidationError
(
Runnable
action
)
{
try
{
action
.
run
();
...
...
@@ -265,4 +298,14 @@ public class ValidationBindHandlerTests {
}
static
class
TestHandler
extends
AbstractBindHandler
{
@Override
public
Object
onFailure
(
ConfigurationPropertyName
name
,
Bindable
<?>
target
,
BindContext
context
,
Exception
error
)
throws
Exception
{
return
null
;
}
}
}
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