Skip to content

Commit 35154a9

Browse files
committed
Polish "Fix NPE in configprops endpoint"
See gh-30068
1 parent a1fe05f commit 35154a9

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/context/properties/ConfigurationPropertiesReportEndpoint.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public void keysToSanitize(String... keysToSanitize) {
121121

122122
@ReadOperation
123123
public ApplicationConfigurationProperties configurationProperties() {
124-
return extract(this.context, (bean) -> bean != null);
124+
return extract(this.context, (bean) -> true);
125125
}
126126

127127
@ReadOperation

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBean.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -143,14 +143,12 @@ public static Map<String, ConfigurationPropertiesBean> getAll(ApplicationContext
143143
return getAll((ConfigurableApplicationContext) applicationContext);
144144
}
145145
Map<String, ConfigurationPropertiesBean> propertiesBeans = new LinkedHashMap<>();
146-
applicationContext.getBeansWithAnnotation(ConfigurationProperties.class)
147-
.forEach((beanName, bean) -> {
148-
ConfigurationPropertiesBean propertiesBean = get(applicationContext, bean, beanName);
149-
if (propertiesBean == null) { //ignore for null
150-
return;
151-
}
152-
propertiesBeans.put(beanName,propertiesBean);
153-
});
146+
applicationContext.getBeansWithAnnotation(ConfigurationProperties.class).forEach((beanName, bean) -> {
147+
ConfigurationPropertiesBean propertiesBean = get(applicationContext, bean, beanName);
148+
if (propertiesBean != null) {
149+
propertiesBeans.put(beanName, propertiesBean);
150+
}
151+
});
154152
return propertiesBeans;
155153
}
156154

@@ -164,10 +162,9 @@ private static Map<String, ConfigurationPropertiesBean> getAll(ConfigurableAppli
164162
try {
165163
Object bean = beanFactory.getBean(beanName);
166164
ConfigurationPropertiesBean propertiesBean = get(applicationContext, bean, beanName);
167-
if (propertiesBean == null) { //ignore for null
168-
continue;
165+
if (propertiesBean != null) {
166+
propertiesBeans.put(beanName, propertiesBean);
169167
}
170-
propertiesBeans.put(beanName, propertiesBean);
171168
}
172169
catch (Exception ex) {
173170
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ void getAllReturnsAll() {
7878
}
7979
}
8080

81+
@Test
82+
void getAllDoesNotFindABeanDeclaredInAStaticBeanMethodOnAConfigurationAndConfigurationPropertiesAnnotatedClass() {
83+
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
84+
StaticBeanMethodConfiguration.class)) {
85+
Map<String, ConfigurationPropertiesBean> all = ConfigurationPropertiesBean.getAll(context);
86+
assertThat(all).containsOnlyKeys("configurationPropertiesBeanTests.StaticBeanMethodConfiguration");
87+
}
88+
}
89+
8190
@Test
8291
void getAllWhenHasBadBeanDoesNotFail() {
8392
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
@@ -521,4 +530,15 @@ public String[] selectImports(AnnotationMetadata importingClassMetadata) {
521530

522531
}
523532

533+
@Configuration(proxyBeanMethods = false)
534+
@ConfigurationProperties
535+
static class StaticBeanMethodConfiguration {
536+
537+
@Bean
538+
static String stringBean() {
539+
return "example";
540+
}
541+
542+
}
543+
524544
}

0 commit comments

Comments
 (0)