Browse Source

Protect against null names when filter is applied more than once

Update `FilteredIterableConfigurationPropertiesSource` to fix a
regression caused by commit 8f14dca1 that occurs if the `filter()`
method is called on an already filtered source.

Fixes gh-46032
pull/46457/head
Phillip Webb 6 months ago
parent
commit
7b553d9093
  1. 3
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/FilteredIterableConfigurationPropertiesSource.java
  2. 18
      spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/FilteredIterableConfigurationPropertiesSourceTests.java

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

@ -41,6 +41,9 @@ class FilteredIterableConfigurationPropertiesSource extends FilteredConfiguratio @@ -41,6 +41,9 @@ class FilteredIterableConfigurationPropertiesSource extends FilteredConfiguratio
this.filteredNames = new ConfigurationPropertyName[filterableNames.length];
this.numerOfFilteredNames = 0;
for (ConfigurationPropertyName name : filterableNames) {
if (name == null) {
break;
}
if (filter.test(name)) {
this.filteredNames[this.numerOfFilteredNames++] = name;
}

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

@ -74,6 +74,24 @@ class FilteredIterableConfigurationPropertiesSourceTests extends FilteredConfigu @@ -74,6 +74,24 @@ class FilteredIterableConfigurationPropertiesSourceTests extends FilteredConfigu
.containsExactly("a", "b", "c");
}
@Test
void iteratorWhenSpringPropertySourceAndAnotherFilterFiltersNames() {
IterableConfigurationPropertySource testSource = (IterableConfigurationPropertySource) createTestSource();
Map<String, Object> map = new LinkedHashMap<>();
for (ConfigurationPropertyName name : testSource) {
map.put(name.toString(), testSource.getConfigurationProperty(name).getValue());
}
PropertySource<?> propertySource = new OriginTrackedMapPropertySource("test", map, true);
SpringConfigurationPropertySource source = SpringConfigurationPropertySource.from(propertySource);
IterableConfigurationPropertySource filtered = (IterableConfigurationPropertySource) source
.filter(this::noBrackets);
IterableConfigurationPropertySource secondFiltered = filtered.filter((name) -> !name.toString().contains("c"));
assertThat(Extractors.byName("filteredNames").apply(filtered)).isNotNull();
assertThat(secondFiltered.iterator()).toIterable()
.extracting(ConfigurationPropertyName::toString)
.containsExactly("a", "b");
}
@Test
void containsDescendantOfWhenSpringPropertySourceUsesContents() {
Map<String, Object> map = new LinkedHashMap<>();

Loading…
Cancel
Save