From ebf276c00528b273db90719b56a655b61eaf3e3d Mon Sep 17 00:00:00 2001 From: Guirong Hu Date: Fri, 6 May 2022 12:30:02 +0800 Subject: [PATCH 1/2] Assert that sources does not contain null elements See gh-30878 --- .../boot/context/properties/bind/Binder.java | 5 +++-- .../boot/context/properties/bind/BinderTests.java | 10 ++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java index b0e67042bdf0..31983009e543 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -72,7 +72,7 @@ public class Binder { * @param sources the sources used for binding */ public Binder(ConfigurationPropertySource... sources) { - this(Arrays.asList(sources), null, null, null); + this((sources != null) ? Arrays.asList(sources) : null, null, null, null); } /** @@ -182,6 +182,7 @@ public Binder(Iterable sources, PlaceholdersResolve List conversionServices, Consumer propertyEditorInitializer, BindHandler defaultBindHandler, BindConstructorProvider constructorProvider) { Assert.notNull(sources, "Sources must not be null"); + sources.forEach((source) -> Assert.notNull(source, "Sources cannot contain null values")); this.sources = sources; this.placeholdersResolver = (placeholdersResolver != null) ? placeholdersResolver : PlaceholdersResolver.NONE; this.bindConverter = BindConverter.get(conversionServices, propertyEditorInitializer); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BinderTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BinderTests.java index a1cf88f87ac3..360e567ad68a 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BinderTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BinderTests.java @@ -71,10 +71,20 @@ class BinderTests { @Test void createWhenSourcesIsNullShouldThrowException() { + assertThatIllegalArgumentException().isThrownBy(() -> new Binder((ConfigurationPropertySource[]) null)) + .withMessageContaining("Sources must not be null"); assertThatIllegalArgumentException().isThrownBy(() -> new Binder((Iterable) null)) .withMessageContaining("Sources must not be null"); } + @Test + void createWhenSourcesContainNullValuesShouldThrowException() { + assertThatIllegalArgumentException().isThrownBy(() -> new Binder(new ConfigurationPropertySource[] { null })) + .withMessageContaining("Sources cannot contain null values"); + assertThatIllegalArgumentException().isThrownBy(() -> new Binder(Collections.singletonList(null))) + .withMessageContaining("Sources cannot contain null values"); + } + @Test void bindWhenNameIsNullShouldThrowException() { assertThatIllegalArgumentException().isThrownBy(() -> this.binder.bind((ConfigurationPropertyName) null, From 56c3a5f0abf28e5359ab722a1a5846d5aa8e4705 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 27 May 2022 10:01:06 +0100 Subject: [PATCH 2/2] Polish "Assert that sources does not contain null elements" See gh-30878 --- .../boot/context/properties/bind/Binder.java | 4 +++- .../context/properties/bind/BinderTests.java | 16 ++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java index 31983009e543..0b47cff04e5b 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java @@ -182,7 +182,9 @@ public Binder(Iterable sources, PlaceholdersResolve List conversionServices, Consumer propertyEditorInitializer, BindHandler defaultBindHandler, BindConstructorProvider constructorProvider) { Assert.notNull(sources, "Sources must not be null"); - sources.forEach((source) -> Assert.notNull(source, "Sources cannot contain null values")); + for (ConfigurationPropertySource source : sources) { + Assert.notNull(source, "Sources must not contain null elements"); + } this.sources = sources; this.placeholdersResolver = (placeholdersResolver != null) ? placeholdersResolver : PlaceholdersResolver.NONE; this.bindConverter = BindConverter.get(conversionServices, propertyEditorInitializer); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BinderTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BinderTests.java index 360e567ad68a..388348bf9a47 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BinderTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/BinderTests.java @@ -70,19 +70,27 @@ class BinderTests { private Binder binder = new Binder(this.sources); @Test - void createWhenSourcesIsNullShouldThrowException() { + void createWhenSourcesIsNullArrayShouldThrowException() { assertThatIllegalArgumentException().isThrownBy(() -> new Binder((ConfigurationPropertySource[]) null)) .withMessageContaining("Sources must not be null"); + } + + @Test + void creatWhenSourcesIsNullIterableShouldThrowException() { assertThatIllegalArgumentException().isThrownBy(() -> new Binder((Iterable) null)) .withMessageContaining("Sources must not be null"); } @Test - void createWhenSourcesContainNullValuesShouldThrowException() { + void createWhenArraySourcesContainsNullElementShouldThrowException() { assertThatIllegalArgumentException().isThrownBy(() -> new Binder(new ConfigurationPropertySource[] { null })) - .withMessageContaining("Sources cannot contain null values"); + .withMessageContaining("Sources must not contain null elements"); + } + + @Test + void createWhenIterableSourcesContainsNullElementShouldThrowException() { assertThatIllegalArgumentException().isThrownBy(() -> new Binder(Collections.singletonList(null))) - .withMessageContaining("Sources cannot contain null values"); + .withMessageContaining("Sources must not contain null elements"); } @Test