Skip to content

Commit

Permalink
Don't attempt to set null values
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
scottfrederick and philwebb committed May 26, 2021
1 parent a31e976 commit c679b4c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Expand Up @@ -176,8 +176,10 @@ public T build() {
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);
Expand Down
Expand Up @@ -73,13 +73,28 @@ void buildWhenHikariAvailableReturnsHikariDataSource() {
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")
.build();
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
Expand All @@ -88,6 +103,14 @@ void buildWhenHikariAndTomcatNotAvailableReturnsDbcp2DataSource() {
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",
Expand Down

0 comments on commit c679b4c

Please sign in to comment.