Skip to content

Commit d485708

Browse files
committed
Fix 404 when composite contributor is added to a group
Fixes gh-19974
1 parent 76c2157 commit d485708

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -81,7 +81,7 @@ private HealthResult<T> getHealth(ApiVersion apiVersion, HealthEndpointGroup gro
8181
}
8282
Object contributor = getContributor(path, pathOffset);
8383
T health = getContribution(apiVersion, group, contributor, showComponents, showDetails,
84-
isSystemHealth ? this.groups.getNames() : null);
84+
isSystemHealth ? this.groups.getNames() : null, false);
8585
return (health != null) ? new HealthResult<>(health, group) : null;
8686
}
8787

@@ -100,23 +100,24 @@ private Object getContributor(String[] path, int pathOffset) {
100100

101101
@SuppressWarnings("unchecked")
102102
private T getContribution(ApiVersion apiVersion, HealthEndpointGroup group, Object contributor,
103-
boolean showComponents, boolean showDetails, Set<String> groupNames) {
103+
boolean showComponents, boolean showDetails, Set<String> groupNames, boolean isNested) {
104104
if (contributor instanceof NamedContributors) {
105105
return getAggregateHealth(apiVersion, group, (NamedContributors<C>) contributor, showComponents,
106-
showDetails, groupNames);
106+
showDetails, groupNames, isNested);
107107
}
108108
return (contributor != null) ? getHealth((C) contributor, showDetails) : null;
109109
}
110110

111111
private T getAggregateHealth(ApiVersion apiVersion, HealthEndpointGroup group,
112-
NamedContributors<C> namedContributors, boolean showComponents, boolean showDetails,
113-
Set<String> groupNames) {
112+
NamedContributors<C> namedContributors, boolean showComponents, boolean showDetails, Set<String> groupNames,
113+
boolean isNested) {
114114
Map<String, T> contributions = new LinkedHashMap<>();
115115
for (NamedContributor<C> namedContributor : namedContributors) {
116116
String name = namedContributor.getName();
117117
C contributor = namedContributor.getContributor();
118-
if (group.isMember(name)) {
119-
T contribution = getContribution(apiVersion, group, contributor, showComponents, showDetails, null);
118+
if (group.isMember(name) || isNested) {
119+
T contribution = getContribution(apiVersion, group, contributor, showComponents, showDetails, null,
120+
true);
120121
if (contribution != null) {
121122
contributions.put(name, contribution);
122123
}

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,6 +38,7 @@
3838
* @param <C> the contributor type
3939
* @param <T> the contributed health component type
4040
* @author Phillip Webb
41+
* @author Madhura Bhave
4142
*/
4243
abstract class HealthEndpointSupportTests<R extends ContributorRegistry<C>, C, T> {
4344

@@ -215,6 +216,20 @@ void getHealthWithEmptyCompositeReturnsNullResult() { // gh-18687
215216
assertThat(result).isNull();
216217
}
217218

219+
@Test
220+
void getHealthWhenGroupContainsCompositeContributorReturnsHealth() {
221+
C contributor = createContributor(this.up);
222+
C compositeContributor = createCompositeContributor(Collections.singletonMap("spring", contributor));
223+
this.registry.registerContributor("test", compositeContributor);
224+
TestHealthEndpointGroup testGroup = new TestHealthEndpointGroup((name) -> name.startsWith("test"));
225+
HealthEndpointGroups groups = HealthEndpointGroups.of(this.primaryGroup,
226+
Collections.singletonMap("testGroup", testGroup));
227+
HealthResult<T> result = create(this.registry, groups).getHealth(ApiVersion.V3, SecurityContext.NONE, false,
228+
"testGroup");
229+
CompositeHealth health = (CompositeHealth) getHealth(result);
230+
assertThat(health.getComponents()).containsKey("test");
231+
}
232+
218233
protected abstract HealthEndpointSupport<C, T> create(R registry, HealthEndpointGroups groups);
219234

220235
protected abstract R createRegistry();

0 commit comments

Comments
 (0)