Fix NPE in configprops endpoint

This works around spring-projects/spring-framework#28298. The bug
means that when a @Configuration class is annotated with
@ConfigurationProperties any bean defined by a static @Bean method
is considered to be annotated with @ConfigurationProperties.

See gh-30068
This commit is contained in:
qxo 2022-03-04 22:55:31 +08:00 committed by Andy Wilkinson
parent e3859fadd6
commit a1fe05f40b
2 changed files with 11 additions and 2 deletions

View File

@ -121,7 +121,7 @@ public class ConfigurationPropertiesReportEndpoint implements ApplicationContext
@ReadOperation
public ApplicationConfigurationProperties configurationProperties() {
return extract(this.context, (bean) -> true);
return extract(this.context, (bean) -> bean != null);
}
@ReadOperation

View File

@ -144,7 +144,13 @@ public final class ConfigurationPropertiesBean {
}
Map<String, ConfigurationPropertiesBean> propertiesBeans = new LinkedHashMap<>();
applicationContext.getBeansWithAnnotation(ConfigurationProperties.class)
.forEach((beanName, bean) -> propertiesBeans.put(beanName, get(applicationContext, bean, beanName)));
.forEach((beanName, bean) -> {
ConfigurationPropertiesBean propertiesBean = get(applicationContext, bean, beanName);
if (propertiesBean == null) { //ignore for null
return;
}
propertiesBeans.put(beanName,propertiesBean);
});
return propertiesBeans;
}
@ -158,6 +164,9 @@ public final class ConfigurationPropertiesBean {
try {
Object bean = beanFactory.getBean(beanName);
ConfigurationPropertiesBean propertiesBean = get(applicationContext, bean, beanName);
if (propertiesBean == null) { //ignore for null
continue;
}
propertiesBeans.put(beanName, propertiesBean);
}
catch (Exception ex) {