Skip to content

Commit ba30ee0

Browse files
committed
Improve health contributor null support
Update `NamedContributorsMapAdapter` to check for `null` keys or values during construction. Also update `HealthEndpointSupport` to allow null component entries. See gh-18687
1 parent 7c9ac03 commit ba30ee0

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/HealthEndpointSupport.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,19 @@ protected abstract T aggregateContributions(ApiVersion apiVersion, Map<String, T
134134

135135
protected final CompositeHealth getCompositeHealth(ApiVersion apiVersion, Map<String, HealthComponent> components,
136136
StatusAggregator statusAggregator, boolean showComponents, Set<String> groupNames) {
137-
Status status = statusAggregator.getAggregateStatus(
138-
components.values().stream().map(HealthComponent::getStatus).collect(Collectors.toSet()));
137+
Status status = statusAggregator
138+
.getAggregateStatus(components.values().stream().map(this::getStatus).collect(Collectors.toSet()));
139139
Map<String, HealthComponent> instances = showComponents ? components : null;
140140
if (groupNames != null) {
141141
return new SystemHealth(apiVersion, status, instances, groupNames);
142142
}
143143
return new CompositeHealth(apiVersion, status, instances);
144144
}
145145

146+
private Status getStatus(HealthComponent component) {
147+
return (component != null) ? component.getStatus() : Status.UNKNOWN;
148+
}
149+
146150
/**
147151
* A health result containing health and the group that created it.
148152
*

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/NamedContributorsMapAdapter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ abstract class NamedContributorsMapAdapter<V, C> implements NamedContributors<C>
4343
NamedContributorsMapAdapter(Map<String, V> map, Function<V, ? extends C> valueAdapter) {
4444
Assert.notNull(map, "Map must not be null");
4545
Assert.notNull(valueAdapter, "ValueAdapter must not be null");
46+
map.keySet().stream().forEach((key) -> Assert.notNull(key, "Map must not contain null keys"));
47+
map.values().stream().map(valueAdapter)
48+
.forEach((value) -> Assert.notNull(value, "Map must not contain null values"));
4649
this.map = Collections.unmodifiableMap(new LinkedHashMap<>(map));
4750
this.valueAdapter = valueAdapter;
4851
}

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/NamedContributorsMapAdapterTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,22 @@ void createWhenValueAdapterIsNullThrowsException() {
4848
.withMessage("ValueAdapter must not be null");
4949
}
5050

51+
@Test
52+
void createWhenMapContainsNullValueThrowsException() {
53+
assertThatIllegalArgumentException()
54+
.isThrownBy(() -> new TestNamedContributorsMapAdapter<>(Collections.singletonMap("test", null),
55+
Function.identity()))
56+
.withMessage("Map must not contain null values");
57+
}
58+
59+
@Test
60+
void createWhenMapContainsNullKeyThrowsException() {
61+
assertThatIllegalArgumentException()
62+
.isThrownBy(() -> new TestNamedContributorsMapAdapter<>(Collections.singletonMap(null, "test"),
63+
Function.identity()))
64+
.withMessage("Map must not contain null keys");
65+
}
66+
5167
@Test
5268
void iterateReturnsAdaptedEntries() {
5369
TestNamedContributorsMapAdapter<String> adapter = createAdapter();

0 commit comments

Comments
 (0)