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
84fab65b
Commit
84fab65b
authored
Jun 24, 2021
by
Phillip Webb
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '2.4.x' into 2.5.x
Closes gh-27060
parents
e9d3e0d0
ea62967e
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
64 additions
and
11 deletions
+64
-11
BindConverter.java
...framework/boot/context/properties/bind/BindConverter.java
+8
-3
BindFailureAnalyzer.java
...mework/boot/diagnostics/analyzer/BindFailureAnalyzer.java
+24
-6
BindConverterTests.java
...work/boot/context/properties/bind/BindConverterTests.java
+10
-0
BindFailureAnalyzerTests.java
...k/boot/diagnostics/analyzer/BindFailureAnalyzerTests.java
+22
-2
No files found.
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/BindConverter.java
View file @
84fab65b
...
@@ -34,7 +34,9 @@ import org.springframework.beans.propertyeditors.FileEditor;
...
@@ -34,7 +34,9 @@ import org.springframework.beans.propertyeditors.FileEditor;
import
org.springframework.boot.convert.ApplicationConversionService
;
import
org.springframework.boot.convert.ApplicationConversionService
;
import
org.springframework.core.ResolvableType
;
import
org.springframework.core.ResolvableType
;
import
org.springframework.core.convert.ConversionException
;
import
org.springframework.core.convert.ConversionException
;
import
org.springframework.core.convert.ConversionFailedException
;
import
org.springframework.core.convert.ConversionService
;
import
org.springframework.core.convert.ConversionService
;
import
org.springframework.core.convert.ConverterNotFoundException
;
import
org.springframework.core.convert.TypeDescriptor
;
import
org.springframework.core.convert.TypeDescriptor
;
import
org.springframework.core.convert.converter.ConditionalGenericConverter
;
import
org.springframework.core.convert.converter.ConditionalGenericConverter
;
import
org.springframework.core.convert.support.GenericConversionService
;
import
org.springframework.core.convert.support.GenericConversionService
;
...
@@ -98,17 +100,20 @@ final class BindConverter {
...
@@ -98,17 +100,20 @@ final class BindConverter {
}
}
private
Object
convert
(
Object
source
,
TypeDescriptor
sourceType
,
TypeDescriptor
targetType
)
{
private
Object
convert
(
Object
source
,
TypeDescriptor
sourceType
,
TypeDescriptor
targetType
)
{
for
(
int
i
=
0
;
i
<
this
.
delegates
.
size
()
-
1
;
i
++)
{
ConversionException
failure
=
null
;
for
(
ConversionService
delegate
:
this
.
delegates
)
{
try
{
try
{
ConversionService
delegate
=
this
.
delegates
.
get
(
i
);
if
(
delegate
.
canConvert
(
sourceType
,
targetType
))
{
if
(
delegate
.
canConvert
(
sourceType
,
targetType
))
{
return
delegate
.
convert
(
source
,
sourceType
,
targetType
);
return
delegate
.
convert
(
source
,
sourceType
,
targetType
);
}
}
}
}
catch
(
ConversionException
ex
)
{
catch
(
ConversionException
ex
)
{
if
(
failure
==
null
&&
ex
instanceof
ConversionFailedException
)
{
failure
=
ex
;
}
}
}
}
}
return
this
.
delegates
.
get
(
this
.
delegates
.
size
()
-
1
).
convert
(
source
,
sourceType
,
targetType
);
throw
(
failure
!=
null
)
?
failure
:
new
ConverterNotFoundException
(
sourceType
,
targetType
);
}
}
static
BindConverter
get
(
List
<
ConversionService
>
conversionServices
,
static
BindConverter
get
(
List
<
ConversionService
>
conversionServices
,
...
...
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/BindFailureAnalyzer.java
View file @
84fab65b
/*
/*
* Copyright 2012-20
19
the original author or authors.
* Copyright 2012-20
21
the original author or authors.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* you may not use this file except in compliance with the License.
...
@@ -38,6 +38,7 @@ import org.springframework.util.StringUtils;
...
@@ -38,6 +38,7 @@ import org.springframework.util.StringUtils;
*
*
* @author Andy Wilkinson
* @author Andy Wilkinson
* @author Madhura Bhave
* @author Madhura Bhave
* @author Phillip Webb
*/
*/
class
BindFailureAnalyzer
extends
AbstractFailureAnalyzer
<
BindException
>
{
class
BindFailureAnalyzer
extends
AbstractFailureAnalyzer
<
BindException
>
{
...
@@ -68,16 +69,33 @@ class BindFailureAnalyzer extends AbstractFailureAnalyzer<BindException> {
...
@@ -68,16 +69,33 @@ class BindFailureAnalyzer extends AbstractFailureAnalyzer<BindException> {
}
}
private
String
getMessage
(
BindException
cause
)
{
private
String
getMessage
(
BindException
cause
)
{
Throwable
rootCause
=
getRootCause
(
cause
.
getCause
());
ConversionFailedException
conversionFailure
=
findCause
(
cause
,
ConversionFailedException
.
class
);
ConversionFailedException
conversionFailure
=
findCause
(
cause
,
ConversionFailedException
.
class
);
if
(
conversionFailure
!=
null
)
{
if
(
conversionFailure
!=
null
)
{
return
"failed to convert "
+
conversionFailure
.
getSourceType
()
+
" to "
String
message
=
"failed to convert "
+
conversionFailure
.
getSourceType
()
+
" to "
+
conversionFailure
.
getTargetType
();
+
conversionFailure
.
getTargetType
();
if
(
rootCause
!=
null
)
{
message
+=
" (caused by "
+
getExceptionTypeAndMessage
(
rootCause
)
+
")"
;
}
return
message
;
}
if
(
rootCause
!=
null
&&
StringUtils
.
hasText
(
rootCause
.
getMessage
()))
{
return
getExceptionTypeAndMessage
(
rootCause
);
}
}
Throwable
failure
=
cause
;
return
getExceptionTypeAndMessage
(
cause
);
while
(
failure
.
getCause
()
!=
null
)
{
}
failure
=
failure
.
getCause
();
private
Throwable
getRootCause
(
Throwable
cause
)
{
Throwable
rootCause
=
cause
;
while
(
rootCause
!=
null
&&
rootCause
.
getCause
()
!=
null
)
{
rootCause
=
rootCause
.
getCause
();
}
}
return
(
StringUtils
.
hasText
(
failure
.
getMessage
())
?
failure
.
getMessage
()
:
cause
.
getMessage
());
return
rootCause
;
}
private
String
getExceptionTypeAndMessage
(
Throwable
ex
)
{
String
message
=
ex
.
getMessage
();
return
ex
.
getClass
().
getName
()
+
(
StringUtils
.
hasText
(
message
)
?
": "
+
message
:
""
);
}
}
private
FailureAnalysis
getFailureAnalysis
(
Object
description
,
BindException
cause
)
{
private
FailureAnalysis
getFailureAnalysis
(
Object
description
,
BindException
cause
)
{
...
...
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BindConverterTests.java
View file @
84fab65b
...
@@ -30,6 +30,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
...
@@ -30,6 +30,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
import
org.springframework.beans.PropertyEditorRegistry
;
import
org.springframework.beans.PropertyEditorRegistry
;
import
org.springframework.core.ResolvableType
;
import
org.springframework.core.ResolvableType
;
import
org.springframework.core.convert.ConversionFailedException
;
import
org.springframework.core.convert.ConversionService
;
import
org.springframework.core.convert.ConversionService
;
import
org.springframework.core.convert.ConverterNotFoundException
;
import
org.springframework.core.convert.ConverterNotFoundException
;
import
org.springframework.core.convert.TypeDescriptor
;
import
org.springframework.core.convert.TypeDescriptor
;
...
@@ -178,6 +179,15 @@ class BindConverterTests {
...
@@ -178,6 +179,15 @@ class BindConverterTests {
assertThat
(
result
.
getSeconds
()).
isEqualTo
(
10
);
assertThat
(
result
.
getSeconds
()).
isEqualTo
(
10
);
}
}
@Test
// gh-27028
void
convertWhenConversionFailsThrowsConversionFailedExceptionRatherThanConverterNotFoundException
()
{
BindConverter
bindConverter
=
BindConverter
.
get
(
Collections
.
singletonList
(
new
GenericConversionService
()),
null
);
assertThatExceptionOfType
(
ConversionFailedException
.
class
)
.
isThrownBy
(()
->
bindConverter
.
convert
(
"com.example.Missing"
,
ResolvableType
.
forClass
(
Class
.
class
)))
.
withRootCauseInstanceOf
(
ClassNotFoundException
.
class
);
}
private
BindConverter
getPropertyEditorOnlyBindConverter
(
private
BindConverter
getPropertyEditorOnlyBindConverter
(
Consumer
<
PropertyEditorRegistry
>
propertyEditorInitializer
)
{
Consumer
<
PropertyEditorRegistry
>
propertyEditorInitializer
)
{
return
BindConverter
.
get
(
Collections
.
singletonList
(
new
ThrowingConversionService
()),
propertyEditorInitializer
);
return
BindConverter
.
get
(
Collections
.
singletonList
(
new
ThrowingConversionService
()),
propertyEditorInitializer
);
...
...
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/BindFailureAnalyzerTests.java
View file @
84fab65b
/*
/*
* Copyright 2012-20
19
the original author or authors.
* Copyright 2012-20
21
the original author or authors.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* you may not use this file except in compliance with the License.
...
@@ -41,6 +41,7 @@ import static org.assertj.core.api.Assertions.assertThat;
...
@@ -41,6 +41,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*
*
* @author Andy Wilkinson
* @author Andy Wilkinson
* @author Madhura Bhave
* @author Madhura Bhave
* @author Phillip Webb
*/
*/
class
BindFailureAnalyzerTests
{
class
BindFailureAnalyzerTests
{
...
@@ -76,7 +77,16 @@ class BindFailureAnalyzerTests {
...
@@ -76,7 +77,16 @@ class BindFailureAnalyzerTests {
void
bindExceptionWithNestedFailureShouldDisplayNestedMessage
()
{
void
bindExceptionWithNestedFailureShouldDisplayNestedMessage
()
{
FailureAnalysis
analysis
=
performAnalysis
(
NestedFailureConfiguration
.
class
,
"test.foo.value=hello"
);
FailureAnalysis
analysis
=
performAnalysis
(
NestedFailureConfiguration
.
class
,
"test.foo.value=hello"
);
assertThat
(
analysis
.
getDescription
()).
contains
(
failure
(
"test.foo.value"
,
"hello"
,
assertThat
(
analysis
.
getDescription
()).
contains
(
failure
(
"test.foo.value"
,
"hello"
,
"\"test.foo.value\" from property source \"test\""
,
"This is a failure"
));
"\"test.foo.value\" from property source \"test\""
,
"java.lang.RuntimeException: This is a failure"
));
}
@Test
// gh-27028
void
bindExceptionDueToClassNotFoundConvertionFailure
()
{
FailureAnalysis
analysis
=
performAnalysis
(
GenericFailureConfiguration
.
class
,
"test.foo.type=com.example.Missing"
);
assertThat
(
analysis
.
getDescription
()).
contains
(
failure
(
"test.foo.type"
,
"com.example.Missing"
,
"\"test.foo.type\" from property source \"test\""
,
"failed to convert java.lang.String to java.lang.Class<?> (caused by java.lang.ClassNotFoundException: com.example.Missing"
));
}
}
private
static
String
failure
(
String
property
,
String
value
,
String
origin
,
String
reason
)
{
private
static
String
failure
(
String
property
,
String
value
,
String
origin
,
String
reason
)
{
...
@@ -178,6 +188,8 @@ class BindFailureAnalyzerTests {
...
@@ -178,6 +188,8 @@ class BindFailureAnalyzerTests {
private
int
value
;
private
int
value
;
private
Class
<?>
type
;
int
getValue
()
{
int
getValue
()
{
return
this
.
value
;
return
this
.
value
;
}
}
...
@@ -186,6 +198,14 @@ class BindFailureAnalyzerTests {
...
@@ -186,6 +198,14 @@ class BindFailureAnalyzerTests {
this
.
value
=
value
;
this
.
value
=
value
;
}
}
Class
<?>
getType
()
{
return
this
.
type
;
}
void
setType
(
Class
<?>
type
)
{
this
.
type
=
type
;
}
}
}
@ConfigurationProperties
(
"test.foo"
)
@ConfigurationProperties
(
"test.foo"
)
...
...
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