From d0e2823c49db54c60deda2776c5daa0e489f2629 Mon Sep 17 00:00:00 2001 From: saraswathy-krish Date: Thu, 22 Jul 2021 02:40:46 -0700 Subject: [PATCH] Fix deriving DataSources from custom type Eliminate the unsupported datasource property exception thrown when trying to derive a datasource from an unknown datasource type. See gh-27453 --- .../boot/jdbc/DataSourceBuilder.java | 15 ++++++++++----- .../boot/jdbc/DataSourceBuilderTests.java | 13 +++++++++++++ 2 files changed, 23 insertions(+), 5 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 5b7aaa7a915b..c0cc317f77e5 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 @@ -281,17 +281,22 @@ public String toString() { } Method findSetter(Class type) { - return extracted("set", type); + return extracted("set", type, true); } Method findGetter(Class type) { - return extracted("get", type); + return extracted("get", type, false); } - private Method extracted(String prefix, Class type) { + private Method extracted(String prefix, Class type, boolean hasParameter) { for (String candidate : this.names) { - Method method = ReflectionUtils.findMethod(type, prefix + StringUtils.capitalize(candidate), - String.class); + Method method; + if (hasParameter) { + method = ReflectionUtils.findMethod(type, prefix + StringUtils.capitalize(candidate), String.class); + } + else { + method = ReflectionUtils.findMethod(type, prefix + StringUtils.capitalize(candidate)); + } if (method != null) { return method; } 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 c0cbbe687d8f..d807b93b50e9 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 @@ -331,6 +331,19 @@ void buildWhenDerivedFromExistingDatabaseWithTypeChange() { assertThat(built.getUrl()).isEqualTo("jdbc:postgresql://localhost:5432/postgres"); } + @Test // gh -27295 + void buildWhenDerivedFromCustomTypeSpecifiedReturnsDataSource() { + CustomDataSource dataSource = new CustomDataSource(); + dataSource.setUsername("test"); + dataSource.setPassword("secret"); + dataSource.setUrl("jdbc:postgresql://localhost:5432/postgres"); + DataSourceBuilder builder = DataSourceBuilder.derivedFrom(dataSource).type(SimpleDriverDataSource.class); + SimpleDriverDataSource testSource = (SimpleDriverDataSource) builder.build(); + assertThat(testSource.getUsername()).isEqualTo("test"); + assertThat(testSource.getUrl()).isEqualTo("jdbc:postgresql://localhost:5432/postgres"); + assertThat(testSource.getPassword()).isEqualTo("secret"); + } + final class HidePackagesClassLoader extends URLClassLoader { private final String[] hiddenPackages;