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
11649bc1
Commit
11649bc1
authored
Mar 12, 2021
by
Madhura Bhave
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '2.3.x' into 2.4.x
Closes gh-25614
parents
24d729ee
df1d1dba
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
88 additions
and
9 deletions
+88
-9
ValidationBindHandler.java
...ext/properties/bind/validation/ValidationBindHandler.java
+28
-9
ConfigurationPropertiesTests.java
...boot/context/properties/ConfigurationPropertiesTests.java
+7
-0
ValidatorPropertiesWithDefaultValues.java
...text/properties/ValidatorPropertiesWithDefaultValues.java
+52
-0
checkstyle-suppressions.xml
src/checkstyle/checkstyle-suppressions.xml
+1
-0
No files found.
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/validation/ValidationBindHandler.java
View file @
11649bc1
...
...
@@ -22,6 +22,7 @@ import java.util.Map;
import
java.util.Set
;
import
java.util.stream.Collectors
;
import
org.springframework.beans.NotReadablePropertyException
;
import
org.springframework.boot.context.properties.bind.AbstractBindHandler
;
import
org.springframework.boot.context.properties.bind.BindContext
;
import
org.springframework.boot.context.properties.bind.BindHandler
;
...
...
@@ -33,6 +34,7 @@ import org.springframework.boot.context.properties.source.ConfigurationPropertyN
import
org.springframework.core.ResolvableType
;
import
org.springframework.util.ObjectUtils
;
import
org.springframework.validation.AbstractBindingResult
;
import
org.springframework.validation.BeanPropertyBindingResult
;
import
org.springframework.validation.Validator
;
/**
...
...
@@ -143,14 +145,14 @@ public class ValidationBindHandler extends AbstractBindHandler {
/**
* {@link AbstractBindingResult} implementation backed by the bound properties.
*/
private
class
ValidationResult
extends
Abstract
BindingResult
{
private
class
ValidationResult
extends
BeanProperty
BindingResult
{
private
final
ConfigurationPropertyName
name
;
private
Object
target
;
private
final
Object
target
;
protected
ValidationResult
(
ConfigurationPropertyName
name
,
Object
target
)
{
super
(
null
);
super
(
target
,
null
);
this
.
name
=
name
;
this
.
target
=
target
;
}
...
...
@@ -160,11 +162,6 @@ public class ValidationBindHandler extends AbstractBindHandler {
return
this
.
name
.
toString
();
}
@Override
public
Object
getTarget
()
{
return
this
.
target
;
}
@Override
public
Class
<?>
getFieldType
(
String
field
)
{
ResolvableType
type
=
getBoundField
(
ValidationBindHandler
.
this
.
boundTypes
,
field
);
...
...
@@ -177,7 +174,29 @@ public class ValidationBindHandler extends AbstractBindHandler {
@Override
protected
Object
getActualFieldValue
(
String
field
)
{
return
getBoundField
(
ValidationBindHandler
.
this
.
boundResults
,
field
);
Object
boundField
=
getBoundField
(
ValidationBindHandler
.
this
.
boundResults
,
field
);
if
(
boundField
!=
null
)
{
return
boundField
;
}
try
{
return
super
.
getActualFieldValue
(
field
);
}
catch
(
Exception
ex
)
{
if
(
isPropertyNotReadable
(
ex
))
{
return
null
;
}
throw
ex
;
}
}
private
boolean
isPropertyNotReadable
(
Throwable
ex
)
{
while
(
ex
!=
null
)
{
if
(
ex
instanceof
NotReadablePropertyException
)
{
return
true
;
}
ex
=
ex
.
getCause
();
}
return
false
;
}
private
<
T
>
T
getBoundField
(
Map
<
ConfigurationPropertyName
,
T
>
boundFields
,
String
field
)
{
...
...
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java
View file @
11649bc1
...
...
@@ -696,6 +696,13 @@ class ConfigurationPropertiesTests {
});
}
@Test
void
loadWhenConfigurationPropertiesWithValidDefaultValuesShouldNotFail
()
{
AnnotationConfigApplicationContext
context
=
load
(
ValidatorPropertiesWithDefaultValues
.
class
);
ValidatorPropertiesWithDefaultValues
bean
=
context
.
getBean
(
ValidatorPropertiesWithDefaultValues
.
class
);
assertThat
(
bean
.
getBar
()).
isEqualTo
(
"a"
);
}
@Test
void
loadWhenSetterThrowsValidationExceptionShouldFail
()
{
assertThatExceptionOfType
(
BeanCreationException
.
class
)
...
...
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ValidatorPropertiesWithDefaultValues.java
0 → 100644
View file @
11649bc1
/*
* Copyright 2012-2021 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
context
.
properties
;
import
org.springframework.validation.Errors
;
import
org.springframework.validation.ValidationUtils
;
import
org.springframework.validation.Validator
;
/**
* Used for testing validation of properties that have default field values.
*
* @author Madhura Bhave
*/
@EnableConfigurationProperties
@ConfigurationProperties
class
ValidatorPropertiesWithDefaultValues
implements
Validator
{
private
String
bar
=
"a"
;
@Override
public
boolean
supports
(
Class
<?>
type
)
{
return
type
==
ValidatorPropertiesWithDefaultValues
.
class
;
}
@Override
public
void
validate
(
Object
target
,
Errors
errors
)
{
ValidationUtils
.
rejectIfEmpty
(
errors
,
"bar"
,
"foo.empty"
);
}
public
String
getBar
()
{
return
this
.
bar
;
}
public
void
setBar
(
String
bar
)
{
this
.
bar
=
bar
;
}
}
src/checkstyle/checkstyle-suppressions.xml
View file @
11649bc1
...
...
@@ -49,4 +49,5 @@
<suppress
files=
"LinuxDomainSocket"
checks=
"FinalClass"
message=
"SockaddrUn"
/>
<suppress
files=
"BsdDomainSocket"
checks=
"FinalClass"
message=
"SockaddrUn"
/>
<suppress
files=
"StringSequence"
checks=
"SpringMethodVisibility"
message=
"isEmpty"
/>
<suppress
files=
"ValidatorPropertiesWithDefaultValues\.java"
checks=
"SpringMethodVisibility"
/>
</suppressions>
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