Skip to content

Commit d532557

Browse files
committed
Add defensive check for RabbitProperties to prevent NPE
Before this commit, RabbitProperties cannot be copied when addresses is null. Signed-off-by: Yanming Zhou <[email protected]>
1 parent 6957557 commit d532557

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ public void setAddresses(List<String> addresses) {
233233
}
234234

235235
private List<Address> parseAddresses(List<String> addresses) {
236+
if (addresses == null) {
237+
return null;
238+
}
236239
List<Address> parsedAddresses = new ArrayList<>();
237240
for (String address : addresses) {
238241
parsedAddresses.add(new Address(address, Optional.ofNullable(getSsl().getEnabled()).orElse(false)));

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitPropertiesTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
2626
import org.springframework.amqp.rabbit.listener.DirectMessageListenerContainer;
2727
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
28+
import org.springframework.beans.BeanUtils;
2829
import org.springframework.boot.context.properties.source.InvalidConfigurationPropertyValueException;
2930

3031
import static org.assertj.core.api.Assertions.assertThat;
@@ -38,6 +39,7 @@
3839
* @author Stephane Nicoll
3940
* @author Rafael Carvalho
4041
* @author Scott Frederick
42+
* @author Yanming Zhou
4143
*/
4244
class RabbitPropertiesTests {
4345

@@ -381,4 +383,11 @@ void hostPropertyMustBeSingleHost() {
381383
.withMessageContaining("spring.rabbitmq.host");
382384
}
383385

386+
@Test
387+
void isSafeForCopyProperties() {
388+
this.properties.setHost("my-rmq-host.net");
389+
RabbitProperties target = new RabbitProperties();
390+
BeanUtils.copyProperties(this.properties, target);
391+
}
392+
384393
}

0 commit comments

Comments
 (0)