Skip to content

Commit 96235ea

Browse files
committed
Fix ordering of JSON property source relative to servlet sources
Fixes gh-17652
1 parent 340a205 commit 96235ea

File tree

2 files changed

+63
-4
lines changed

2 files changed

+63
-4
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/SpringApplicationJsonEnvironmentPostProcessor.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616

1717
package org.springframework.boot.env;
1818

19+
import java.util.Arrays;
1920
import java.util.Collection;
2021
import java.util.LinkedHashMap;
22+
import java.util.LinkedHashSet;
2123
import java.util.Map;
2224
import java.util.Objects;
25+
import java.util.Set;
2326

2427
import org.springframework.boot.SpringApplication;
2528
import org.springframework.boot.json.JsonParser;
@@ -65,6 +68,11 @@ public class SpringApplicationJsonEnvironmentPostProcessor implements Environmen
6568
private static final String SERVLET_ENVIRONMENT_CLASS = "org.springframework.web."
6669
+ "context.support.StandardServletEnvironment";
6770

71+
private static final Set<String> SERVLET_ENVIRONMENT_PROPERTY_SOURCES = new LinkedHashSet<>(
72+
Arrays.asList(StandardServletEnvironment.JNDI_PROPERTY_SOURCE_NAME,
73+
StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME,
74+
StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME));
75+
6876
/**
6977
* The default order for the processor.
7078
*/
@@ -141,10 +149,13 @@ private void addJsonPropertySource(ConfigurableEnvironment environment, Property
141149
}
142150

143151
private String findPropertySource(MutablePropertySources sources) {
144-
if (ClassUtils.isPresent(SERVLET_ENVIRONMENT_CLASS, null)
145-
&& sources.contains(StandardServletEnvironment.JNDI_PROPERTY_SOURCE_NAME)) {
146-
return StandardServletEnvironment.JNDI_PROPERTY_SOURCE_NAME;
147-
152+
if (ClassUtils.isPresent(SERVLET_ENVIRONMENT_CLASS, null)) {
153+
PropertySource<?> servletPropertySource = sources.stream()
154+
.filter((source) -> SERVLET_ENVIRONMENT_PROPERTY_SOURCES.contains(source.getName())).findFirst()
155+
.orElse(null);
156+
if (servletPropertySource != null) {
157+
return servletPropertySource.getName();
158+
}
148159
}
149160
return StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME;
150161
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/env/SpringApplicationJsonEnvironmentPostProcessorTests.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@
1616

1717
package org.springframework.boot.env;
1818

19+
import java.util.Collections;
20+
1921
import org.junit.Test;
2022

2123
import org.springframework.boot.json.JsonParseException;
2224
import org.springframework.boot.origin.PropertySourceOrigin;
2325
import org.springframework.core.env.ConfigurableEnvironment;
26+
import org.springframework.core.env.MapPropertySource;
2427
import org.springframework.core.env.PropertySource;
2528
import org.springframework.core.env.StandardEnvironment;
2629
import org.springframework.test.context.support.TestPropertySourceUtils;
30+
import org.springframework.web.context.support.StandardServletEnvironment;
2731

2832
import static org.assertj.core.api.Assertions.assertThat;
2933
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -134,4 +138,48 @@ public void propertySourceShouldTrackOrigin() {
134138
assertThat(this.environment.resolvePlaceholders("${foo:}")).isEqualTo("bar");
135139
}
136140

141+
@Test
142+
public void propertySourceShouldBeOrderedBeforeJndiPropertySource() {
143+
testServletPropertySource(StandardServletEnvironment.JNDI_PROPERTY_SOURCE_NAME);
144+
}
145+
146+
@Test
147+
public void propertySourceShouldBeOrderedBeforeServletContextPropertySource() {
148+
testServletPropertySource(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME);
149+
}
150+
151+
@Test
152+
public void propertySourceShouldBeOrderedBeforeServletConfigPropertySource() {
153+
testServletPropertySource(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME);
154+
}
155+
156+
@Test
157+
public void propertySourceOrderingWhenMultipleServletSpecificPropertySources() {
158+
MapPropertySource jndi = getPropertySource(StandardServletEnvironment.JNDI_PROPERTY_SOURCE_NAME, "jndi");
159+
this.environment.getPropertySources().addFirst(jndi);
160+
MapPropertySource servlet = getPropertySource(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME,
161+
"servlet");
162+
this.environment.getPropertySources().addFirst(servlet);
163+
MapPropertySource custom = getPropertySource("custom", "custom");
164+
this.environment.getPropertySources().addFirst(custom);
165+
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
166+
"SPRING_APPLICATION_JSON={\"foo\":\"bar\"}");
167+
this.processor.postProcessEnvironment(this.environment, null);
168+
PropertySource<?> json = this.environment.getPropertySources().get("spring.application.json");
169+
assertThat(this.environment.getProperty("foo")).isEqualTo("custom");
170+
assertThat(this.environment.getPropertySources()).containsSequence(custom, json, servlet, jndi);
171+
}
172+
173+
private void testServletPropertySource(String servletContextPropertySourceName) {
174+
this.environment.getPropertySources().addFirst(getPropertySource(servletContextPropertySourceName, "servlet"));
175+
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
176+
"SPRING_APPLICATION_JSON={\"foo\":\"bar\"}");
177+
this.processor.postProcessEnvironment(this.environment, null);
178+
assertThat(this.environment.getProperty("foo")).isEqualTo("bar");
179+
}
180+
181+
private MapPropertySource getPropertySource(String name, String value) {
182+
return new MapPropertySource(name, Collections.singletonMap("foo", value));
183+
}
184+
137185
}

0 commit comments

Comments
 (0)