From 63c8f8aa69b05ce37bb74efd3753d3d87c3a8474 Mon Sep 17 00:00:00 2001 From: croudet Date: Wed, 22 Jan 2020 16:08:33 -0500 Subject: [PATCH] Add method so that `dataSourceProperties`'s keys are not rewritten to kebab-case. (#102) --- .../jdbc/hikari/DatasourceConfiguration.java | 14 ++++++++++ .../hikari/DatasourceConfigurationSpec.groovy | 26 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/jdbc-hikari/src/main/java/io/micronaut/configuration/jdbc/hikari/DatasourceConfiguration.java b/jdbc-hikari/src/main/java/io/micronaut/configuration/jdbc/hikari/DatasourceConfiguration.java index 1b488068c..915339b63 100644 --- a/jdbc-hikari/src/main/java/io/micronaut/configuration/jdbc/hikari/DatasourceConfiguration.java +++ b/jdbc-hikari/src/main/java/io/micronaut/configuration/jdbc/hikari/DatasourceConfiguration.java @@ -19,9 +19,14 @@ import com.zaxxer.hikari.HikariConfig; import io.micronaut.context.annotation.EachProperty; import io.micronaut.context.annotation.Parameter; +import io.micronaut.core.convert.format.MapFormat; +import io.micronaut.core.naming.conventions.StringConvention; import io.micronaut.jdbc.BasicJdbcConfiguration; import io.micronaut.jdbc.CalculatedSettings; +import java.util.Map; +import java.util.Properties; + import javax.annotation.PostConstruct; /** @@ -164,4 +169,13 @@ public String getJndiName() { public void setJndiName(String jndiName) { setDataSourceJNDI(jndiName); } + + public void setDataSourceProperties(@MapFormat(transformation = MapFormat.MapTransformation.FLAT, keyFormat = StringConvention.RAW) Map dsProperties) { + super.getDataSourceProperties().putAll(dsProperties); + } + + @Override + public void setDataSourceProperties(Properties dsProperties) { + // otherwise properties will be added twice + } } diff --git a/jdbc-hikari/src/test/groovy/io/micronaut/configuration/jdbc/hikari/DatasourceConfigurationSpec.groovy b/jdbc-hikari/src/test/groovy/io/micronaut/configuration/jdbc/hikari/DatasourceConfigurationSpec.groovy index 67a9bdcea..da38077c6 100644 --- a/jdbc-hikari/src/test/groovy/io/micronaut/configuration/jdbc/hikari/DatasourceConfigurationSpec.groovy +++ b/jdbc-hikari/src/test/groovy/io/micronaut/configuration/jdbc/hikari/DatasourceConfigurationSpec.groovy @@ -251,4 +251,30 @@ class DatasourceConfigurationSpec extends Specification { applicationContext.stop() } + void "test data source properties"() { + given: + ApplicationContext applicationContext = new DefaultApplicationContext("test") + applicationContext.environment.addPropertySource(MapPropertySource.of( + 'test', + ['datasources.default.data-source-properties' : ['reWriteBatchInserts' : true, 'anotherOne' : 'value']] + )) + applicationContext.start() + + expect: + applicationContext.containsBean(DataSource) + applicationContext.containsBean(DatasourceConfiguration) + + when: + HikariDataSource dataSource = applicationContext.getBean(DataSource).targetDataSource + + then: + !dataSource.dataSourceProperties.isEmpty() + dataSource.dataSourceProperties.size() == 2 + dataSource.dataSourceProperties.get('reWriteBatchInserts') == true + dataSource.dataSourceProperties.get('anotherOne') == 'value' + + cleanup: + applicationContext.close() + } + }