Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5695192

Browse filesBrowse files
committed
Ensure descendants are always recalculated on cache refresh
Fix `SpringIterableConfigurationPropertySource` to ensure that cached descendants are always updated on a refresh. The cache implementation assumes that it is safe to reuse previous `mappings` and `reverseMappings` data since it doesn't matter if superfluous values are included. For `descendants` however, we always want to recalculate values so that we don't get false positives. Unfortunately, prior to this commit, we only updated the descendants if a reverseMapping was added. The meant that on a cache refresh, existing descendants were removed. Fixes gh-45639
1 parent 31f549e commit 5695192
Copy full SHA for 5695192

File tree

Expand file treeCollapse file tree

2 files changed

+19
-2
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+19
-2
lines changed

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

Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SpringIterableConfigurationPropertySource.java
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,11 +274,13 @@ private void tryUpdate(EnumerablePropertySource<?> propertySource) {
274274
if (configurationPropertyName != null && !configurationPropertyName.isEmpty()) {
275275
add(mappings, configurationPropertyName, propertyName);
276276
reverseMappings.put(propertyName, configurationPropertyName);
277-
addParents(descendants, configurationPropertyName);
278277
}
279278
}
280279
}
281280
}
281+
for (String propertyName : propertyNames) {
282+
addParents(descendants, reverseMappings.get(propertyName));
283+
}
282284
ConfigurationPropertyName[] configurationPropertyNames = this.immutable
283285
? reverseMappings.values().toArray(new ConfigurationPropertyName[0]) : null;
284286
lastUpdated = this.immutable ? null : propertyNames;
@@ -296,7 +298,7 @@ private <K, V> Map<K, V> cloneOrCreate(Map<K, V> source, int size) {
296298
}
297299

298300
private void addParents(Set<ConfigurationPropertyName> descendants, ConfigurationPropertyName name) {
299-
if (descendants == null || name.isEmpty()) {
301+
if (descendants == null || name == null || name.isEmpty()) {
300302
return;
301303
}
302304
ConfigurationPropertyName parent = name.getParent();

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

Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/SpringIterableConfigurationPropertySourceTests.java
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,21 @@ void orderOfUnderlyingSourceIsPreserved() {
239239
"test.map.bravo", "test.map.charlie", "test.map.delta");
240240
}
241241

242+
@Test
243+
void cacheRefreshRecalculatesDescendants() {
244+
// gh-45639
245+
Map<String, Object> map = new LinkedHashMap<>();
246+
map.put("one.two.three", "test");
247+
EnumerablePropertySource<?> source = new OriginTrackedMapPropertySource("test", map, false);
248+
SpringIterableConfigurationPropertySource propertySource = new SpringIterableConfigurationPropertySource(source,
249+
false, DefaultPropertyMapper.INSTANCE);
250+
assertThat(propertySource.containsDescendantOf(ConfigurationPropertyName.of("one.two")))
251+
.isEqualTo(ConfigurationPropertyState.PRESENT);
252+
map.put("new", "value");
253+
assertThat(propertySource.containsDescendantOf(ConfigurationPropertyName.of("one.two")))
254+
.isEqualTo(ConfigurationPropertyState.PRESENT);
255+
}
256+
242257
/**
243258
* Test {@link PropertySource} that's also an {@link OriginLookup}.
244259
*

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.