Filter empty health contributions

Update `HealthEndpointSupport` so that aggregate elements that don't
ultimately provide a contribution are filtered out. Prior to this
commit an NPE was returned when calculating the aggregate status.

Fixes gh-18687
This commit is contained in:
Phillip Webb 2019-10-23 12:37:42 -07:00
parent 6378863771
commit 7c9ac03014
2 changed files with 13 additions and 3 deletions

View File

@ -112,10 +112,12 @@ abstract class HealthEndpointSupport<C, T> {
Map<String, T> contributions = new LinkedHashMap<>();
for (NamedContributor<C> namedContributor : namedContributors) {
String name = namedContributor.getName();
C contributor = namedContributor.getContributor();
if (group.isMember(name)) {
T contribution = getContribution(apiVersion, group, namedContributor.getContributor(), showComponents,
showDetails, null);
contributions.put(name, contribution);
T contribution = getContribution(apiVersion, group, contributor, showComponents, showDetails, null);
if (contribution != null) {
contributions.put(name, contribution);
}
}
}
if (contributions.isEmpty()) {

View File

@ -207,6 +207,14 @@ abstract class HealthEndpointSupportTests<R extends ContributorRegistry<C>, C, T
assertThat(getHealth(result)).isNotInstanceOf(SystemHealth.class);
}
@Test
void getHealthWithEmptyCompositeReturnsNullResult() { // gh-18687
this.registry.registerContributor("test", createCompositeContributor(Collections.emptyMap()));
HealthResult<T> result = create(this.registry, this.groups).getHealth(ApiVersion.V3, SecurityContext.NONE,
false);
assertThat(result).isNull();
}
protected abstract HealthEndpointSupport<C, T> create(R registry, HealthEndpointGroups groups);
protected abstract R createRegistry();