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
96235ea6
Commit
96235ea6
authored
Jul 31, 2019
by
Madhura Bhave
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix ordering of JSON property source relative to servlet sources
Fixes gh-17652
parent
340a2059
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
63 additions
and
4 deletions
+63
-4
SpringApplicationJsonEnvironmentPostProcessor.java
...ot/env/SpringApplicationJsonEnvironmentPostProcessor.java
+15
-4
SpringApplicationJsonEnvironmentPostProcessorTests.java
...v/SpringApplicationJsonEnvironmentPostProcessorTests.java
+48
-0
No files found.
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/SpringApplicationJsonEnvironmentPostProcessor.java
View file @
96235ea6
...
...
@@ -16,10 +16,13 @@
package
org
.
springframework
.
boot
.
env
;
import
java.util.Arrays
;
import
java.util.Collection
;
import
java.util.LinkedHashMap
;
import
java.util.LinkedHashSet
;
import
java.util.Map
;
import
java.util.Objects
;
import
java.util.Set
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.json.JsonParser
;
...
...
@@ -65,6 +68,11 @@ public class SpringApplicationJsonEnvironmentPostProcessor implements Environmen
private
static
final
String
SERVLET_ENVIRONMENT_CLASS
=
"org.springframework.web."
+
"context.support.StandardServletEnvironment"
;
private
static
final
Set
<
String
>
SERVLET_ENVIRONMENT_PROPERTY_SOURCES
=
new
LinkedHashSet
<>(
Arrays
.
asList
(
StandardServletEnvironment
.
JNDI_PROPERTY_SOURCE_NAME
,
StandardServletEnvironment
.
SERVLET_CONTEXT_PROPERTY_SOURCE_NAME
,
StandardServletEnvironment
.
SERVLET_CONFIG_PROPERTY_SOURCE_NAME
));
/**
* The default order for the processor.
*/
...
...
@@ -141,10 +149,13 @@ public class SpringApplicationJsonEnvironmentPostProcessor implements Environmen
}
private
String
findPropertySource
(
MutablePropertySources
sources
)
{
if
(
ClassUtils
.
isPresent
(
SERVLET_ENVIRONMENT_CLASS
,
null
)
&&
sources
.
contains
(
StandardServletEnvironment
.
JNDI_PROPERTY_SOURCE_NAME
))
{
return
StandardServletEnvironment
.
JNDI_PROPERTY_SOURCE_NAME
;
if
(
ClassUtils
.
isPresent
(
SERVLET_ENVIRONMENT_CLASS
,
null
))
{
PropertySource
<?>
servletPropertySource
=
sources
.
stream
()
.
filter
((
source
)
->
SERVLET_ENVIRONMENT_PROPERTY_SOURCES
.
contains
(
source
.
getName
())).
findFirst
()
.
orElse
(
null
);
if
(
servletPropertySource
!=
null
)
{
return
servletPropertySource
.
getName
();
}
}
return
StandardEnvironment
.
SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME
;
}
...
...
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/env/SpringApplicationJsonEnvironmentPostProcessorTests.java
View file @
96235ea6
...
...
@@ -16,14 +16,18 @@
package
org
.
springframework
.
boot
.
env
;
import
java.util.Collections
;
import
org.junit.Test
;
import
org.springframework.boot.json.JsonParseException
;
import
org.springframework.boot.origin.PropertySourceOrigin
;
import
org.springframework.core.env.ConfigurableEnvironment
;
import
org.springframework.core.env.MapPropertySource
;
import
org.springframework.core.env.PropertySource
;
import
org.springframework.core.env.StandardEnvironment
;
import
org.springframework.test.context.support.TestPropertySourceUtils
;
import
org.springframework.web.context.support.StandardServletEnvironment
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThatExceptionOfType
;
...
...
@@ -134,4 +138,48 @@ public class SpringApplicationJsonEnvironmentPostProcessorTests {
assertThat
(
this
.
environment
.
resolvePlaceholders
(
"${foo:}"
)).
isEqualTo
(
"bar"
);
}
@Test
public
void
propertySourceShouldBeOrderedBeforeJndiPropertySource
()
{
testServletPropertySource
(
StandardServletEnvironment
.
JNDI_PROPERTY_SOURCE_NAME
);
}
@Test
public
void
propertySourceShouldBeOrderedBeforeServletContextPropertySource
()
{
testServletPropertySource
(
StandardServletEnvironment
.
SERVLET_CONTEXT_PROPERTY_SOURCE_NAME
);
}
@Test
public
void
propertySourceShouldBeOrderedBeforeServletConfigPropertySource
()
{
testServletPropertySource
(
StandardServletEnvironment
.
SERVLET_CONFIG_PROPERTY_SOURCE_NAME
);
}
@Test
public
void
propertySourceOrderingWhenMultipleServletSpecificPropertySources
()
{
MapPropertySource
jndi
=
getPropertySource
(
StandardServletEnvironment
.
JNDI_PROPERTY_SOURCE_NAME
,
"jndi"
);
this
.
environment
.
getPropertySources
().
addFirst
(
jndi
);
MapPropertySource
servlet
=
getPropertySource
(
StandardServletEnvironment
.
SERVLET_CONTEXT_PROPERTY_SOURCE_NAME
,
"servlet"
);
this
.
environment
.
getPropertySources
().
addFirst
(
servlet
);
MapPropertySource
custom
=
getPropertySource
(
"custom"
,
"custom"
);
this
.
environment
.
getPropertySources
().
addFirst
(
custom
);
TestPropertySourceUtils
.
addInlinedPropertiesToEnvironment
(
this
.
environment
,
"SPRING_APPLICATION_JSON={\"foo\":\"bar\"}"
);
this
.
processor
.
postProcessEnvironment
(
this
.
environment
,
null
);
PropertySource
<?>
json
=
this
.
environment
.
getPropertySources
().
get
(
"spring.application.json"
);
assertThat
(
this
.
environment
.
getProperty
(
"foo"
)).
isEqualTo
(
"custom"
);
assertThat
(
this
.
environment
.
getPropertySources
()).
containsSequence
(
custom
,
json
,
servlet
,
jndi
);
}
private
void
testServletPropertySource
(
String
servletContextPropertySourceName
)
{
this
.
environment
.
getPropertySources
().
addFirst
(
getPropertySource
(
servletContextPropertySourceName
,
"servlet"
));
TestPropertySourceUtils
.
addInlinedPropertiesToEnvironment
(
this
.
environment
,
"SPRING_APPLICATION_JSON={\"foo\":\"bar\"}"
);
this
.
processor
.
postProcessEnvironment
(
this
.
environment
,
null
);
assertThat
(
this
.
environment
.
getProperty
(
"foo"
)).
isEqualTo
(
"bar"
);
}
private
MapPropertySource
getPropertySource
(
String
name
,
String
value
)
{
return
new
MapPropertySource
(
name
,
Collections
.
singletonMap
(
"foo"
,
value
));
}
}
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