From 1cf0d4e7fec2465bd4127d605d219afa3b168441 Mon Sep 17 00:00:00 2001 From: chanbinme Date: Tue, 17 Jun 2025 00:09:43 +0900 Subject: [PATCH 1/2] Avoid NPE in SimpleDataSourceProperties when driver is null See gh-45976 Signed-off-by: chanbinme --- .../springframework/boot/jdbc/DataSourceBuilder.java | 6 +++++- .../boot/jdbc/DataSourceBuilderTests.java | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java index b37b8816881..002beb9843e 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java @@ -513,6 +513,9 @@ public final class DataSourceBuilder { } private String convertToString(V value) { + if (value == null) { + return null; + } if (String.class.equals(this.type)) { return (String) value; } @@ -705,7 +708,8 @@ public final class DataSourceBuilder { @SuppressWarnings("unchecked") SimpleDataSourceProperties() { add(DataSourceProperty.URL, SimpleDriverDataSource::getUrl, SimpleDriverDataSource::setUrl); - add(DataSourceProperty.DRIVER_CLASS_NAME, Class.class, (dataSource) -> dataSource.getDriver().getClass(), + add(DataSourceProperty.DRIVER_CLASS_NAME, Class.class, + (dataSource) -> dataSource.getDriver() == null ? null : dataSource.getDriver().getClass(), SimpleDriverDataSource::setDriverClass); add(DataSourceProperty.USERNAME, SimpleDriverDataSource::getUsername, SimpleDriverDataSource::setUsername); add(DataSourceProperty.PASSWORD, SimpleDriverDataSource::getPassword, SimpleDriverDataSource::setPassword); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/DataSourceBuilderTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/DataSourceBuilderTests.java index e4c39138bf3..9efb21bd3a7 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/DataSourceBuilderTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/DataSourceBuilderTests.java @@ -357,6 +357,16 @@ class DataSourceBuilderTests { .isThrownBy(() -> DataSourceBuilder.derivedFrom(dataSource).url("example.org").build()); } + @Test + void buildWhenDerivedFromSimpleDriverDataSourceWithDriverNotSetSucceeds() throws Exception { + SimpleDriverDataSource dataSource = new SimpleDriverDataSource(); + dataSource.setUsername("test"); + dataSource.setPassword("secret"); + dataSource.setUrl("jdbc:postgresql://localhost:5432/postgres"); + assertThatNoException() + .isThrownBy(() -> DataSourceBuilder.derivedFrom(dataSource).type(SimpleDriverDataSource.class).build()); + } + @Test void buildWhenDerivedFromOracleDataSourceWithPasswordSetReturnsDataSource() throws Exception { oracle.jdbc.datasource.impl.OracleDataSource dataSource = new oracle.jdbc.datasource.impl.OracleDataSource(); From 1f031f5fa6a0061f7bded91bf442208bf04528ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Nicoll?= Date: Tue, 17 Jun 2025 14:48:42 +0200 Subject: [PATCH 2/2] Polish "Avoid NPE in SimpleDataSourceProperties when driver is null" See gh-45976 --- .../java/org/springframework/boot/jdbc/DataSourceBuilder.java | 2 +- .../org/springframework/boot/jdbc/DataSourceBuilderTests.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java index 002beb9843e..b11ea161f1f 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java @@ -709,7 +709,7 @@ public final class DataSourceBuilder { SimpleDataSourceProperties() { add(DataSourceProperty.URL, SimpleDriverDataSource::getUrl, SimpleDriverDataSource::setUrl); add(DataSourceProperty.DRIVER_CLASS_NAME, Class.class, - (dataSource) -> dataSource.getDriver() == null ? null : dataSource.getDriver().getClass(), + (dataSource) -> (dataSource.getDriver() != null) ? dataSource.getDriver().getClass() : null, SimpleDriverDataSource::setDriverClass); add(DataSourceProperty.USERNAME, SimpleDriverDataSource::getUsername, SimpleDriverDataSource::setUsername); add(DataSourceProperty.PASSWORD, SimpleDriverDataSource::getPassword, SimpleDriverDataSource::setPassword); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/DataSourceBuilderTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/DataSourceBuilderTests.java index 9efb21bd3a7..6ea947ce17e 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/DataSourceBuilderTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/DataSourceBuilderTests.java @@ -358,7 +358,7 @@ class DataSourceBuilderTests { } @Test - void buildWhenDerivedFromSimpleDriverDataSourceWithDriverNotSetSucceeds() throws Exception { + void buildWhenDerivedFromSimpleDriverDataSourceAndDriverNotSetBuilds() { SimpleDriverDataSource dataSource = new SimpleDriverDataSource(); dataSource.setUsername("test"); dataSource.setPassword("secret");