Skip to content

Commit d0e2823

Browse files
saraswathy-krishwilkinsona
authored andcommitted
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
1 parent 44a9531 commit d0e2823

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

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

+10-5
Original file line numberDiff line numberDiff line change
@@ -281,17 +281,22 @@ public String toString() {
281281
}
282282

283283
Method findSetter(Class<?> type) {
284-
return extracted("set", type);
284+
return extracted("set", type, true);
285285
}
286286

287287
Method findGetter(Class<?> type) {
288-
return extracted("get", type);
288+
return extracted("get", type, false);
289289
}
290290

291-
private Method extracted(String prefix, Class<?> type) {
291+
private Method extracted(String prefix, Class<?> type, boolean hasParameter) {
292292
for (String candidate : this.names) {
293-
Method method = ReflectionUtils.findMethod(type, prefix + StringUtils.capitalize(candidate),
294-
String.class);
293+
Method method;
294+
if (hasParameter) {
295+
method = ReflectionUtils.findMethod(type, prefix + StringUtils.capitalize(candidate), String.class);
296+
}
297+
else {
298+
method = ReflectionUtils.findMethod(type, prefix + StringUtils.capitalize(candidate));
299+
}
295300
if (method != null) {
296301
return method;
297302
}

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

+13
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,19 @@ void buildWhenDerivedFromExistingDatabaseWithTypeChange() {
331331
assertThat(built.getUrl()).isEqualTo("jdbc:postgresql://localhost:5432/postgres");
332332
}
333333

334+
@Test // gh -27295
335+
void buildWhenDerivedFromCustomTypeSpecifiedReturnsDataSource() {
336+
CustomDataSource dataSource = new CustomDataSource();
337+
dataSource.setUsername("test");
338+
dataSource.setPassword("secret");
339+
dataSource.setUrl("jdbc:postgresql://localhost:5432/postgres");
340+
DataSourceBuilder<?> builder = DataSourceBuilder.derivedFrom(dataSource).type(SimpleDriverDataSource.class);
341+
SimpleDriverDataSource testSource = (SimpleDriverDataSource) builder.build();
342+
assertThat(testSource.getUsername()).isEqualTo("test");
343+
assertThat(testSource.getUrl()).isEqualTo("jdbc:postgresql://localhost:5432/postgres");
344+
assertThat(testSource.getPassword()).isEqualTo("secret");
345+
}
346+
334347
final class HidePackagesClassLoader extends URLClassLoader {
335348

336349
private final String[] hiddenPackages;

0 commit comments

Comments
 (0)