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] 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");