Skip to content

Commit e52480a

Browse files
committed
Fix NPE in SimpleDataSourceProperties when driver is null
- Prevent NullPointerException in SimpleDataSourceProperties by adding null check before calling getDriver().getClass(). - Fixes potential crash when driver is not explicitly configured. - Add null checks in convertToString method to handle null Class values - Add test case to verify NPE-free operation when deriving DataSource Signed-off-by: chanbinme <gksmfcksqls@gmail.com>
1 parent 1ae0548 commit e52480a

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,9 @@ private V convertFromString(String value) {
508508
}
509509

510510
private String convertToString(V value) {
511+
if (value == null) {
512+
return null;
513+
}
511514
if (String.class.equals(this.type)) {
512515
return (String) value;
513516
}
@@ -712,7 +715,8 @@ private static class SimpleDataSourceProperties extends MappedDataSourceProperti
712715
@SuppressWarnings("unchecked")
713716
SimpleDataSourceProperties() {
714717
add(DataSourceProperty.URL, SimpleDriverDataSource::getUrl, SimpleDriverDataSource::setUrl);
715-
add(DataSourceProperty.DRIVER_CLASS_NAME, Class.class, (dataSource) -> dataSource.getDriver().getClass(),
718+
add(DataSourceProperty.DRIVER_CLASS_NAME, Class.class,
719+
(dataSource) -> dataSource.getDriver() == null ? null : dataSource.getDriver().getClass(),
716720
SimpleDriverDataSource::setDriverClass);
717721
add(DataSourceProperty.USERNAME, SimpleDriverDataSource::getUsername, SimpleDriverDataSource::setUsername);
718722
add(DataSourceProperty.PASSWORD, SimpleDriverDataSource::getPassword, SimpleDriverDataSource::setPassword);

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/DataSourceBuilderTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,11 @@
5151
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
5252

5353
import static org.assertj.core.api.Assertions.assertThat;
54+
import static org.assertj.core.api.Assertions.assertThatCode;
5455
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
5556
import static org.assertj.core.api.Assertions.assertThatNoException;
57+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
58+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
5659

5760
/**
5861
* Tests for {@link DataSourceBuilder}.
@@ -522,6 +525,16 @@ void buildWhenViburTypeSpecifiedReturnsExpectedDataSource() {
522525
assertThat(viburDataSource.getDriverClassName()).isEqualTo("com.example.Driver");
523526
}
524527

528+
@Test
529+
void buildWhenDerivedFromSimpleDriverDataSourceWithDriverNotSetSucceeds() {
530+
SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
531+
dataSource.setUsername("test");
532+
dataSource.setPassword("secret");
533+
dataSource.setUrl("jdbc:postgresql://localhost:5432/postgres");
534+
assertDoesNotThrow(
535+
() -> DataSourceBuilder.derivedFrom(dataSource).type(SimpleDriverDataSource.class).build());
536+
}
537+
525538
@Test
526539
void buildWhenJdbcUrlIsFromUnknownDriverLeavesDriverClassNameUnset() {
527540
this.dataSource = DataSourceBuilder.create()

0 commit comments

Comments
 (0)