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
0484e5cb
Commit
0484e5cb
authored
Feb 25, 2015
by
Phillip Webb
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2479 from linead/master
* pull2479: Supported relaxed binding on inner classes
parents
a1cbd93d
f0ed6193
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
62 additions
and
4 deletions
+62
-4
RelaxedDataBinder.java
...java/org/springframework/boot/bind/RelaxedDataBinder.java
+33
-3
RelaxedDataBinderTests.java
...org/springframework/boot/bind/RelaxedDataBinderTests.java
+29
-1
No files found.
spring-boot/src/main/java/org/springframework/boot/bind/RelaxedDataBinder.java
View file @
0484e5cb
...
@@ -286,12 +286,38 @@ public class RelaxedDataBinder extends DataBinder {
...
@@ -286,12 +286,38 @@ public class RelaxedDataBinder extends DataBinder {
}
}
private
String
getActualPropertyName
(
BeanWrapper
target
,
String
prefix
,
String
name
)
{
private
String
getActualPropertyName
(
BeanWrapper
target
,
String
prefix
,
String
name
)
{
prefix
=
StringUtils
.
hasText
(
prefix
)
?
prefix
+
"."
:
""
;
String
propertyName
=
resolvePropertyName
(
target
,
prefix
,
name
);
if
(
propertyName
==
null
)
{
propertyName
=
resolveNestedPropertyName
(
target
,
prefix
,
name
);
}
return
(
propertyName
==
null
?
name
:
propertyName
);
}
private
String
resolveNestedPropertyName
(
BeanWrapper
target
,
String
prefix
,
String
name
)
{
StringBuilder
candidate
=
new
StringBuilder
();
for
(
String
field
:
name
.
split
(
"[_\\-\\.]"
))
{
candidate
.
append
(
candidate
.
length
()
>
0
?
"."
:
""
);
candidate
.
append
(
field
);
String
nested
=
resolvePropertyName
(
target
,
prefix
,
candidate
.
toString
());
if
(
nested
!=
null
)
{
String
propertyName
=
resolvePropertyName
(
target
,
joinString
(
prefix
,
nested
),
name
.
substring
(
candidate
.
length
()
+
1
));
if
(
propertyName
!=
null
)
{
return
joinString
(
nested
,
propertyName
);
}
}
}
return
null
;
}
private
String
resolvePropertyName
(
BeanWrapper
target
,
String
prefix
,
String
name
)
{
Iterable
<
String
>
names
=
getNameAndAliases
(
name
);
Iterable
<
String
>
names
=
getNameAndAliases
(
name
);
for
(
String
nameOrAlias
:
names
)
{
for
(
String
nameOrAlias
:
names
)
{
for
(
String
candidate
:
new
RelaxedNames
(
nameOrAlias
))
{
for
(
String
candidate
:
new
RelaxedNames
(
nameOrAlias
))
{
try
{
try
{
if
(
target
.
getPropertyType
(
prefix
+
candidate
)
!=
null
)
{
if
(
target
.
getPropertyType
(
joinString
(
prefix
,
candidate
)
)
!=
null
)
{
return
candidate
;
return
candidate
;
}
}
}
}
...
@@ -300,7 +326,11 @@ public class RelaxedDataBinder extends DataBinder {
...
@@ -300,7 +326,11 @@ public class RelaxedDataBinder extends DataBinder {
}
}
}
}
}
}
return
name
;
return
null
;
}
private
String
joinString
(
String
prefix
,
String
name
)
{
return
(
StringUtils
.
hasLength
(
prefix
)
?
prefix
+
"."
+
name
:
name
);
}
}
private
Iterable
<
String
>
getNameAndAliases
(
String
name
)
{
private
Iterable
<
String
>
getNameAndAliases
(
String
name
)
{
...
...
spring-boot/src/test/java/org/springframework/boot/bind/RelaxedDataBinderTests.java
View file @
0484e5cb
/*
/*
* Copyright 2012-201
4
the original author or authors.
* Copyright 2012-201
5
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.
...
@@ -201,6 +201,22 @@ public class RelaxedDataBinderTests {
...
@@ -201,6 +201,22 @@ public class RelaxedDataBinderTests {
assertEquals
(
123
,
target
.
getNested
().
getValue
());
assertEquals
(
123
,
target
.
getNested
().
getValue
());
}
}
@Test
public
void
testBindRelaxedNestedValue
()
throws
Exception
{
TargetWithNestedObject
target
=
new
TargetWithNestedObject
();
bind
(
target
,
"nested_foo_Baz: bar\n"
+
"nested_value: 123"
);
assertEquals
(
"bar"
,
target
.
getNested
().
getFooBaz
());
assertEquals
(
123
,
target
.
getNested
().
getValue
());
}
@Test
public
void
testBindRelaxedNestedCamelValue
()
throws
Exception
{
TargetWithNestedObject
target
=
new
TargetWithNestedObject
();
bind
(
target
,
"another_nested_foo_Baz: bar\n"
+
"another-nested_value: 123"
);
assertEquals
(
"bar"
,
target
.
getAnotherNested
().
getFooBaz
());
assertEquals
(
123
,
target
.
getAnotherNested
().
getValue
());
}
@Test
@Test
public
void
testBindNestedWithEnviromentStyle
()
throws
Exception
{
public
void
testBindNestedWithEnviromentStyle
()
throws
Exception
{
TargetWithNestedObject
target
=
new
TargetWithNestedObject
();
TargetWithNestedObject
target
=
new
TargetWithNestedObject
();
...
@@ -736,8 +752,11 @@ public class RelaxedDataBinderTests {
...
@@ -736,8 +752,11 @@ public class RelaxedDataBinderTests {
}
}
public
static
class
TargetWithNestedObject
{
public
static
class
TargetWithNestedObject
{
private
VanillaTarget
nested
;
private
VanillaTarget
nested
;
private
VanillaTarget
anotherNested
;
public
VanillaTarget
getNested
()
{
public
VanillaTarget
getNested
()
{
return
this
.
nested
;
return
this
.
nested
;
}
}
...
@@ -745,6 +764,15 @@ public class RelaxedDataBinderTests {
...
@@ -745,6 +764,15 @@ public class RelaxedDataBinderTests {
public
void
setNested
(
VanillaTarget
nested
)
{
public
void
setNested
(
VanillaTarget
nested
)
{
this
.
nested
=
nested
;
this
.
nested
=
nested
;
}
}
public
VanillaTarget
getAnotherNested
()
{
return
this
.
anotherNested
;
}
public
void
setAnotherNested
(
VanillaTarget
anotherNested
)
{
this
.
anotherNested
=
anotherNested
;
}
}
}
public
static
class
VanillaTarget
{
public
static
class
VanillaTarget
{
...
...
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