Skip to content

Commit ebf276c

Browse files
terminuxwilkinsona
authored andcommitted
Assert that sources does not contain null elements
See gh-30878
1 parent 94c9388 commit ebf276c

File tree

2 files changed

+13
-2
lines changed
  • spring-boot-project/spring-boot/src
    • main/java/org/springframework/boot/context/properties/bind
    • test/java/org/springframework/boot/context/properties/bind

2 files changed

+13
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 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.
@@ -72,7 +72,7 @@ public class Binder {
7272
* @param sources the sources used for binding
7373
*/
7474
public Binder(ConfigurationPropertySource... sources) {
75-
this(Arrays.asList(sources), null, null, null);
75+
this((sources != null) ? Arrays.asList(sources) : null, null, null, null);
7676
}
7777

7878
/**
@@ -182,6 +182,7 @@ public Binder(Iterable<ConfigurationPropertySource> sources, PlaceholdersResolve
182182
List<ConversionService> conversionServices, Consumer<PropertyEditorRegistry> propertyEditorInitializer,
183183
BindHandler defaultBindHandler, BindConstructorProvider constructorProvider) {
184184
Assert.notNull(sources, "Sources must not be null");
185+
sources.forEach((source) -> Assert.notNull(source, "Sources cannot contain null values"));
185186
this.sources = sources;
186187
this.placeholdersResolver = (placeholdersResolver != null) ? placeholdersResolver : PlaceholdersResolver.NONE;
187188
this.bindConverter = BindConverter.get(conversionServices, propertyEditorInitializer);

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,20 @@ class BinderTests {
7171

7272
@Test
7373
void createWhenSourcesIsNullShouldThrowException() {
74+
assertThatIllegalArgumentException().isThrownBy(() -> new Binder((ConfigurationPropertySource[]) null))
75+
.withMessageContaining("Sources must not be null");
7476
assertThatIllegalArgumentException().isThrownBy(() -> new Binder((Iterable<ConfigurationPropertySource>) null))
7577
.withMessageContaining("Sources must not be null");
7678
}
7779

80+
@Test
81+
void createWhenSourcesContainNullValuesShouldThrowException() {
82+
assertThatIllegalArgumentException().isThrownBy(() -> new Binder(new ConfigurationPropertySource[] { null }))
83+
.withMessageContaining("Sources cannot contain null values");
84+
assertThatIllegalArgumentException().isThrownBy(() -> new Binder(Collections.singletonList(null)))
85+
.withMessageContaining("Sources cannot contain null values");
86+
}
87+
7888
@Test
7989
void bindWhenNameIsNullShouldThrowException() {
8090
assertThatIllegalArgumentException().isThrownBy(() -> this.binder.bind((ConfigurationPropertyName) null,

0 commit comments

Comments
 (0)