Browse Source

Avoid NPE in SimpleDataSourceProperties when driver is null

See gh-45976

Signed-off-by: chanbinme <gksmfcksqls@gmail.com>
3.3.x
chanbinme 9 months ago committed by Stéphane Nicoll
parent
commit
1cf0d4e7fe
  1. 6
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java
  2. 10
      spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/DataSourceBuilderTests.java

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

@ -513,6 +513,9 @@ public final class DataSourceBuilder<T extends DataSource> { @@ -513,6 +513,9 @@ public final class DataSourceBuilder<T extends DataSource> {
}
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<T extends DataSource> { @@ -705,7 +708,8 @@ public final class DataSourceBuilder<T extends DataSource> {
@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);

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

@ -357,6 +357,16 @@ class DataSourceBuilderTests { @@ -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();

Loading…
Cancel
Save