This is an automated email from the ASF dual-hosted git repository.
zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 8f874b6e8ef Validate YAML dataSources before swap to avoid NPE (#37448)
8f874b6e8ef is described below
commit 8f874b6e8ef535c57b6840b2f4549a16faaa2d14
Author: Liang Zhang <[email protected]>
AuthorDate: Sat Dec 20 18:40:17 2025 +0800
Validate YAML dataSources before swap to avoid NPE (#37448)
---
.../resource/YamlDataSourceConfigurationSwapper.java | 1 +
.../resource/YamlDataSourceConfigurationSwapperTest.java | 14 ++++++++++++++
2 files changed, 15 insertions(+)
diff --git
a/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/config/swapper/resource/YamlDataSourceConfigurationSwapper.java
b/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/config/swapper/resource/YamlDataSourceConfigurationSwapper.java
index e405cc2efa4..53f2dba06d4 100644
---
a/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/config/swapper/resource/YamlDataSourceConfigurationSwapper.java
+++
b/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/config/swapper/resource/YamlDataSourceConfigurationSwapper.java
@@ -56,6 +56,7 @@ public final class YamlDataSourceConfigurationSwapper {
* @return data sources
*/
public Map<String, DataSource> swapToDataSources(final Map<String,
Map<String, Object>> yamlDataSources, final boolean cacheEnabled) {
+ Preconditions.checkArgument(null != yamlDataSources &&
!yamlDataSources.isEmpty(), "Data sources can not be empty.");
return
DataSourcePoolCreator.create(yamlDataSources.entrySet().stream().collect(Collectors.toMap(Entry::getKey,
entry -> swapToDataSourcePoolProperties(entry.getValue()))), cacheEnabled);
}
diff --git
a/infra/common/src/test/java/org/apache/shardingsphere/infra/yaml/config/swapper/resource/YamlDataSourceConfigurationSwapperTest.java
b/infra/common/src/test/java/org/apache/shardingsphere/infra/yaml/config/swapper/resource/YamlDataSourceConfigurationSwapperTest.java
index 6a7d8d03de5..d06a133dbad 100644
---
a/infra/common/src/test/java/org/apache/shardingsphere/infra/yaml/config/swapper/resource/YamlDataSourceConfigurationSwapperTest.java
+++
b/infra/common/src/test/java/org/apache/shardingsphere/infra/yaml/config/swapper/resource/YamlDataSourceConfigurationSwapperTest.java
@@ -23,6 +23,7 @@ import
org.apache.shardingsphere.test.infra.fixture.jdbc.MockedDataSource;
import org.junit.jupiter.api.Test;
import javax.sql.DataSource;
+import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
@@ -30,6 +31,7 @@ import java.util.Map;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
class YamlDataSourceConfigurationSwapperTest {
@@ -49,6 +51,18 @@ class YamlDataSourceConfigurationSwapperTest {
assertThat(actual1.getPassword(), is("root"));
}
+ @Test
+ void assertSwapToDataSourcesWithNullConfig() {
+ IllegalArgumentException actual =
assertThrows(IllegalArgumentException.class, () ->
swapper.swapToDataSources(null));
+ assertThat(actual.getMessage(), is("Data sources can not be empty."));
+ }
+
+ @Test
+ void assertSwapToDataSourcesWithEmptyConfig() {
+ IllegalArgumentException actual =
assertThrows(IllegalArgumentException.class, () ->
swapper.swapToDataSources(Collections.emptyMap()));
+ assertThat(actual.getMessage(), is("Data sources can not be empty."));
+ }
+
@Test
void assertSwapToDataSourcePoolProperties() {
Map<String, Object> yamlConfig = new HashMap<>(3, 1F);