Skip to content

Commit d9b0efb

Browse files
committed
Merge pull request #30068 from qxo
* gh-30068: Polish "Fix NPE in configprops endpoint" Fix NPE in configprops endpoint Closes gh-30068
2 parents e3859fa + 35154a9 commit d9b0efb

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

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

Lines changed: 10 additions & 4 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,8 +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) -> propertiesBeans.put(beanName, get(applicationContext, bean, beanName)));
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+
});
148152
return propertiesBeans;
149153
}
150154

@@ -158,7 +162,9 @@ private static Map<String, ConfigurationPropertiesBean> getAll(ConfigurableAppli
158162
try {
159163
Object bean = beanFactory.getBean(beanName);
160164
ConfigurationPropertiesBean propertiesBean = get(applicationContext, bean, beanName);
161-
propertiesBeans.put(beanName, propertiesBean);
165+
if (propertiesBean != null) {
166+
propertiesBeans.put(beanName, propertiesBean);
167+
}
162168
}
163169
catch (Exception ex) {
164170
}

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)