Skip to content

Commit c556d2b

Browse files
fahimfarookmephilwebb
authored andcommitted
Fix caching issues with map property sources
Update `SpringIterableConfigurationPropertySource` so that the cache key from a `MapPropertySource` is invalidated when the map contents changes. Prior to this commit, the actual keys of the map were used as the key. This meant that if the underlying map changed, they key wouldn't be invalidated because it ultimately pointed to the same object instance. See gh-13344
1 parent 461202b commit c556d2b

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SpringIterableConfigurationPropertySource.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,7 @@ private Cache getCache() {
142142
}
143143

144144
private Object getCacheKey() {
145-
if (getPropertySource() instanceof MapPropertySource) {
146-
return ((MapPropertySource) getPropertySource()).getSource().keySet();
147-
}
145+
// gh-13344
148146
return getPropertySource().getPropertyNames();
149147
}
150148

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerTests.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
* @author Andy Wilkinson
7777
* @author Stephane Nicoll
7878
* @author Ben Hale
79+
* @author Fahim Farook
7980
*/
8081
@RunWith(ModifiedClassPathRunner.class)
8182
@ClassPathExclusions("log4j*.jar")
@@ -500,12 +501,23 @@ public void systemPropertiesAreSetForLoggingConfiguration() {
500501
@Test
501502
public void environmentPropertiesIgnoreUnresolvablePlaceholders() {
502503
// gh-7719
504+
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
505+
"logging.pattern.console=console ${doesnotexist}");
506+
this.initializer.initialize(this.context.getEnvironment(),
507+
this.context.getClassLoader());
508+
assertThat(System.getProperty(LoggingSystemProperties.CONSOLE_LOG_PATTERN))
509+
.isEqualTo("console ${doesnotexist}");
510+
}
511+
512+
@Test
513+
public void environmentPropertiesResolvePlaceholders() {
503514
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
504515
"logging.pattern.console=console ${pid}");
505516
this.initializer.initialize(this.context.getEnvironment(),
506517
this.context.getClassLoader());
507518
assertThat(System.getProperty(LoggingSystemProperties.CONSOLE_LOG_PATTERN))
508-
.isEqualTo("console ${pid}");
519+
.isEqualTo(this.context.getEnvironment()
520+
.getProperty("logging.pattern.console"));
509521
}
510522

511523
@Test

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/SpringIterableConfigurationPropertySourceTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
*
3838
* @author Phillip Webb
3939
* @author Madhura Bhave
40+
* @author Fahim Farook
4041
*/
4142
public class SpringIterableConfigurationPropertySourceTests {
4243

@@ -157,6 +158,24 @@ public void containsDescendantOfShouldCheckSourceNames() {
157158
.isEqualTo(ConfigurationPropertyState.ABSENT);
158159
}
159160

161+
@SuppressWarnings("unchecked")
162+
@Test
163+
public void propertySourceChangeReflects() {
164+
// gh-13344
165+
final Map<String, Object> source = new LinkedHashMap<>();
166+
source.put("key1", "value1");
167+
source.put("key2", "value2");
168+
final EnumerablePropertySource<?> propertySource = new MapPropertySource("test",
169+
source);
170+
final SpringIterableConfigurationPropertySource adapter = new SpringIterableConfigurationPropertySource(
171+
propertySource, DefaultPropertyMapper.INSTANCE);
172+
assertThat(adapter.stream().count()).isEqualTo(2);
173+
174+
((Map<String, Object>) adapter.getPropertySource().getSource()).put("key3",
175+
"value3");
176+
assertThat(adapter.stream().count()).isEqualTo(3);
177+
}
178+
160179
/**
161180
* Test {@link PropertySource} that's also an {@link OriginLookup}.
162181
*/

0 commit comments

Comments
 (0)