Don't attempt to set null values

Update `DataSourceBuilder` so that setters are not longer called for
`null` values. This restores Spring Boot 2.4 behavior.

Fixes gh-26633

Co-authored-by: Phillip Webb <pwebb@vmware.com>
pull/26677/head
Scott Frederick 4 years ago committed by Phillip Webb
parent a31e976ec6
commit c679b4ccd6

@ -176,8 +176,10 @@ public final class DataSourceBuilder<T extends DataSource> {
for (DataSourceProperty property : DataSourceProperty.values()) {
if (this.values.containsKey(property)) {
String value = this.values.get(property);
properties.set(dataSource, property, value);
applied.add(property);
if (value != null) {
properties.set(dataSource, property, value);
applied.add(property);
}
}
else if (deriveFromProperties != null && properties.canSet(property)) {
String value = deriveFromProperties.get(this.deriveFrom, property);

@ -73,6 +73,14 @@ class DataSourceBuilderTests {
assertThat(hikariDataSource.getJdbcUrl()).isEqualTo("jdbc:h2:test");
}
@Test // gh-26633
void buildWhenHikariDataSourceWithNullPasswordReturnsHikariDataSource() {
this.dataSource = DataSourceBuilder.create().url("jdbc:h2:test").username("test").password(null).build();
assertThat(this.dataSource).isInstanceOf(HikariDataSource.class);
HikariDataSource hikariDataSource = (HikariDataSource) this.dataSource;
assertThat(hikariDataSource.getJdbcUrl()).isEqualTo("jdbc:h2:test");
}
@Test
void buildWhenHikariNotAvailableReturnsTomcatDataSource() {
this.dataSource = DataSourceBuilder.create(new HidePackagesClassLoader("com.zaxxer.hikari")).url("jdbc:h2:test")
@ -80,6 +88,13 @@ class DataSourceBuilderTests {
assertThat(this.dataSource).isInstanceOf(org.apache.tomcat.jdbc.pool.DataSource.class);
}
@Test // gh-26633
void buildWhenTomcatDataSourceWithNullPasswordReturnsDataSource() {
this.dataSource = DataSourceBuilder.create(new HidePackagesClassLoader("com.zaxxer.hikari")).url("jdbc:h2:test")
.username("test").password(null).build();
assertThat(this.dataSource).isInstanceOf(org.apache.tomcat.jdbc.pool.DataSource.class);
}
@Test
void buildWhenHikariAndTomcatNotAvailableReturnsDbcp2DataSource() {
this.dataSource = DataSourceBuilder
@ -88,6 +103,14 @@ class DataSourceBuilderTests {
assertThat(this.dataSource).isInstanceOf(BasicDataSource.class);
}
@Test // gh-26633
void buildWhenDbcp2DataSourceWithNullPasswordReturnsDbcp2DataSource() {
this.dataSource = DataSourceBuilder
.create(new HidePackagesClassLoader("com.zaxxer.hikari", "org.apache.tomcat.jdbc.pool"))
.url("jdbc:h2:test").username("test").password(null).build();
assertThat(this.dataSource).isInstanceOf(BasicDataSource.class);
}
@Test
void buildWhenHikariAndTomcatAndDbcpNotAvailableReturnsOracleUcpDataSource() {
this.dataSource = DataSourceBuilder.create(new HidePackagesClassLoader("com.zaxxer.hikari",

Loading…
Cancel
Save