Skip to content

Commit 7b553d9

Browse files
committed
Protect against null names when filter is applied more than once
Update `FilteredIterableConfigurationPropertiesSource` to fix a regression caused by commit 8f14dca that occurs if the `filter()` method is called on an already filtered source. Fixes gh-46032
1 parent 440ea79 commit 7b553d9

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ class FilteredIterableConfigurationPropertiesSource extends FilteredConfiguratio
4141
this.filteredNames = new ConfigurationPropertyName[filterableNames.length];
4242
this.numerOfFilteredNames = 0;
4343
for (ConfigurationPropertyName name : filterableNames) {
44+
if (name == null) {
45+
break;
46+
}
4447
if (filter.test(name)) {
4548
this.filteredNames[this.numerOfFilteredNames++] = name;
4649
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,24 @@ void iteratorWhenSpringPropertySourceFiltersNames() {
7474
.containsExactly("a", "b", "c");
7575
}
7676

77+
@Test
78+
void iteratorWhenSpringPropertySourceAndAnotherFilterFiltersNames() {
79+
IterableConfigurationPropertySource testSource = (IterableConfigurationPropertySource) createTestSource();
80+
Map<String, Object> map = new LinkedHashMap<>();
81+
for (ConfigurationPropertyName name : testSource) {
82+
map.put(name.toString(), testSource.getConfigurationProperty(name).getValue());
83+
}
84+
PropertySource<?> propertySource = new OriginTrackedMapPropertySource("test", map, true);
85+
SpringConfigurationPropertySource source = SpringConfigurationPropertySource.from(propertySource);
86+
IterableConfigurationPropertySource filtered = (IterableConfigurationPropertySource) source
87+
.filter(this::noBrackets);
88+
IterableConfigurationPropertySource secondFiltered = filtered.filter((name) -> !name.toString().contains("c"));
89+
assertThat(Extractors.byName("filteredNames").apply(filtered)).isNotNull();
90+
assertThat(secondFiltered.iterator()).toIterable()
91+
.extracting(ConfigurationPropertyName::toString)
92+
.containsExactly("a", "b");
93+
}
94+
7795
@Test
7896
void containsDescendantOfWhenSpringPropertySourceUsesContents() {
7997
Map<String, Object> map = new LinkedHashMap<>();

0 commit comments

Comments
 (0)