This is an automated email from the ASF dual-hosted git repository. jamesfredley pushed a commit to branch fix/16567-datasource-properties-yaml in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit 8247f2ca8b26d8459a3b86f3b8b36922d2a0375f Author: James Fredley <[email protected]> AuthorDate: Thu Oct 22 15:44:59 2026 -0400 Bind flattened dataSource.properties from the Spring Environment Spring's Environment does not return a nested map for dataSource.properties, so YAML pool settings such as maximumPoolSize never reached HikariCP. Rebuild that map from flattened keys when the direct lookup is empty, and leave an existing map unchanged. Fixes #16567 Assisted-by: Sisyphus:grok-4.7 --- .../DataSourceConnectionSourceFactorySpec.groovy | 292 +++++++++++++++++++++ .../mapping/config/ConfigurationBuilder.groovy | 155 ++++++++++- 2 files changed, 445 insertions(+), 2 deletions(-) diff --git a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/jdbc/connections/DataSourceConnectionSourceFactorySpec.groovy b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/jdbc/connections/DataSourceConnectionSourceFactorySpec.groovy index 0a9730c393..4b741a0cae 100644 --- a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/jdbc/connections/DataSourceConnectionSourceFactorySpec.groovy +++ b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/jdbc/connections/DataSourceConnectionSourceFactorySpec.groovy @@ -18,14 +18,305 @@ */ package org.grails.datastore.gorm.jdbc.connections +import com.zaxxer.hikari.HikariDataSource +import groovy.util.ConfigObject import org.grails.datastore.mapping.core.DatastoreUtils +import org.grails.datastore.mapping.core.exceptions.ConfigurationException import org.grails.datastore.mapping.core.connections.ConnectionSource +import org.springframework.core.convert.converter.Converter +import org.springframework.core.convert.support.DefaultConversionService +import org.springframework.core.env.MapPropertySource +import org.springframework.core.env.StandardEnvironment import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy import spock.lang.Specification class DataSourceConnectionSourceFactorySpec extends Specification { + void "create binds flattened default data source pool properties from the Spring Environment"() { + given: + def factory = new DataSourceConnectionSourceFactory() + def environment = new StandardEnvironment() + environment.propertySources.addFirst(new MapPropertySource('application.yml', [ + 'dataSource.url': 'jdbc:h2:mem:factoryEnvironmentTest', + 'dataSource.driverClassName': 'org.h2.Driver', + 'dataSource.username': 'sa', + 'dataSource.password': '', + 'dataSource.type': 'com.zaxxer.hikari.HikariDataSource', + 'dataSource.properties.maximumPoolSize': 16, + 'dataSource.properties.minimumIdle': 8, + 'dataSource.properties.connectionTimeout': 2000, + 'dataSource.properties.dataSourceProperties.connectionCollation': 'utf8mb4_unicode_ci' + ])) + + when: + def connectionSource = factory.create(ConnectionSource.DEFAULT, environment) + + then: + connectionSource.settings.url == 'jdbc:h2:mem:factoryEnvironmentTest' + connectionSource.settings.username == 'sa' + connectionSource.settings.password == '' + connectionSource.source instanceof TransactionAwareDataSourceProxy + def lazyDataSource = ((TransactionAwareDataSourceProxy) connectionSource.source).targetDataSource + lazyDataSource instanceof LazyConnectionDataSourceProxy + def dataSource = ((LazyConnectionDataSourceProxy) lazyDataSource).targetDataSource + dataSource instanceof HikariDataSource + dataSource.maximumPoolSize == 16 + dataSource.minimumIdle == 8 + dataSource.connectionTimeout == 2000 + dataSource.dataSourceProperties.getProperty('connectionCollation') == 'utf8mb4_unicode_ci' + + cleanup: + connectionSource?.close() + } + + void "create binds flattened named data source pool properties from the Spring Environment"() { + given: + def factory = new DataSourceConnectionSourceFactory() + def environment = new StandardEnvironment() + environment.propertySources.addFirst(new MapPropertySource('application.yml', [ + 'dataSources.secondary.url': 'jdbc:h2:mem:factoryNamedEnvironmentTest', + 'dataSources.secondary.type': HikariDataSource.name, + 'dataSources.secondary.properties.maximumPoolSize': 24 + ])) + + when: + def connectionSource = factory.create('secondary', environment) + + then: + connectionSource.name == 'secondary' + def dataSource = connectionSource.source.targetDataSource.targetDataSource + dataSource instanceof HikariDataSource + dataSource.maximumPoolSize == 24 + + cleanup: + connectionSource?.close() + } + + void "create uses the highest precedence Spring property source for pool properties"() { + given: + def factory = new DataSourceConnectionSourceFactory() + def environment = new StandardEnvironment() + environment.propertySources.addFirst(new MapPropertySource('application.yml', [ + 'dataSource.url': 'jdbc:h2:mem:factoryPrecedenceTest', + 'dataSource.type': HikariDataSource.name, + 'dataSource.properties.maximumPoolSize': 16, + 'dataSource.properties.minimumIdle': 8 + ])) + environment.propertySources.addFirst(new MapPropertySource('override', [ + 'dataSource.properties.maximumPoolSize': 32 + ])) + + when: + def connectionSource = factory.create(ConnectionSource.DEFAULT, environment) + + then: + def dataSource = connectionSource.source.targetDataSource.targetDataSource + dataSource instanceof HikariDataSource + dataSource.maximumPoolSize == 32 + dataSource.minimumIdle == 8 + + cleanup: + connectionSource?.close() + } + + void "create still binds an existing nested properties map from DatastoreUtils"() { + given: + def factory = new DataSourceConnectionSourceFactory() + def dataSourceConfig = new ConfigObject() + dataSourceConfig.putAll([ + url: 'jdbc:h2:mem:factoryNestedMapTest', + type: HikariDataSource, + properties: [maximumPoolSize: 20] + ]) + def applicationConfig = new ConfigObject() + applicationConfig.put('dataSource', dataSourceConfig) + def config = DatastoreUtils.createPropertyResolver(applicationConfig) + + when: + def connectionSource = factory.create(ConnectionSource.DEFAULT, config) + + then: + config.getProperty('dataSource.properties', Map) == [maximumPoolSize: 20] + def dataSource = connectionSource.source.targetDataSource.targetDataSource + dataSource instanceof HikariDataSource + dataSource.maximumPoolSize == 20 + + cleanup: + connectionSource?.close() + } + + void "create preserves a nonempty direct properties map without merging flattened keys"() { + given: + def factory = new DataSourceConnectionSourceFactory() + def properties = [maximumPoolSize: 20] + def environment = new StandardEnvironment() + environment.propertySources.addFirst(new MapPropertySource('application', [ + 'dataSource.url': 'jdbc:h2:mem:factoryDirectMapTest', + 'dataSource.type': HikariDataSource.name, + 'dataSource.properties': properties, + 'dataSource.properties.maximumPoolSize': 32, + 'dataSource.properties.minimumIdle': 8 + ])) + + when: + def connectionSource = factory.create(ConnectionSource.DEFAULT, environment) + + then: + connectionSource.settings.properties.is(properties) + connectionSource.settings.properties == [maximumPoolSize: 20] + connectionSource.source.targetDataSource.targetDataSource.maximumPoolSize == 20 + + cleanup: + connectionSource?.close() + } + + void "create rebuilds flattened properties when the direct map is #directValue and conversion fails is #conversionFails"() { + given: + def factory = new DataSourceConnectionSourceFactory() + def environment = new StandardEnvironment() + if (conversionFails) { + environment.conversionService = new DefaultConversionService() + environment.conversionService.addConverter(String, Map, { String value -> + throw new IllegalArgumentException('Cannot convert value to a map') + } as Converter<String, Map>) + } + environment.propertySources.addFirst(new MapPropertySource('application', [ + 'dataSource.url': 'jdbc:h2:mem:factoryMapFallbackTest', + 'dataSource.type': HikariDataSource.name, + 'dataSource.properties': directValue, + 'dataSource.properties.maximumPoolSize': '${poolSize}', + 'poolSize': 16, + 'dataSource.propertiesFoo.maximumPoolSize': 99, + 'dataSource.properties.dataSourceProperties': 'replaced by nested map', + 'dataSource.properties.dataSourceProperties.connectionCollation': 'utf8mb4_unicode_ci', + 'dataSource.properties.dataSourceProperties.empty': '', + 'dataSource.properties.dataSourceProperties.absent': null + ])) + + when: + def connectionSource = factory.create(ConnectionSource.DEFAULT, environment) + + then: + connectionSource.settings.properties == [ + maximumPoolSize: '16', + dataSourceProperties: [connectionCollation: 'utf8mb4_unicode_ci', empty: ''] + ] + def dataSource = connectionSource.source.targetDataSource.targetDataSource + dataSource instanceof HikariDataSource + dataSource.maximumPoolSize == 16 + dataSource.dataSourceProperties.getProperty('connectionCollation') == 'utf8mb4_unicode_ci' + dataSource.dataSourceProperties.getProperty('empty') == '' + !dataSource.dataSourceProperties.containsKey('absent') + + cleanup: + connectionSource?.close() + + where: + directValue | conversionFails + null | false + [:] | false + 'not a map' | false + 'not a map' | true + } + + void "create binds a map-valued descendant without forcing it to a string"() { + given: + def factory = new DataSourceConnectionSourceFactory() + def environment = new StandardEnvironment() + environment.propertySources.addFirst(new MapPropertySource('application', [ + 'dataSource.url': 'jdbc:h2:mem:factoryMapDescendantTest', + 'dataSource.type': HikariDataSource.name, + 'dataSource.properties.maximumPoolSize': 16, + 'dataSource.properties.dataSourceProperties': [connectionCollation: 'utf8mb4_unicode_ci', empty: ''] + ])) + + when: + def connectionSource = factory.create(ConnectionSource.DEFAULT, environment) + + then: + connectionSource.settings.properties.maximumPoolSize == 16 + connectionSource.settings.properties.dataSourceProperties == [connectionCollation: 'utf8mb4_unicode_ci', empty: ''] + def dataSource = connectionSource.source.targetDataSource.targetDataSource + dataSource instanceof HikariDataSource + dataSource.maximumPoolSize == 16 + dataSource.dataSourceProperties.getProperty('connectionCollation') == 'utf8mb4_unicode_ci' + dataSource.dataSourceProperties.getProperty('empty') == '' + + cleanup: + connectionSource?.close() + } + + void "create keeps a higher precedence map entry over a lower flattened descendant"() { + given: + def factory = new DataSourceConnectionSourceFactory() + def environment = new StandardEnvironment() + environment.propertySources.addLast(new MapPropertySource('low', [ + 'dataSource.url': 'jdbc:h2:mem:factoryMapPrecedenceTest', + 'dataSource.type': HikariDataSource.name, + 'dataSource.properties.maximumPoolSize': 16, + 'dataSource.properties.dataSourceProperties.connectionCollation': 'low' + ])) + environment.propertySources.addFirst(new MapPropertySource('high', [ + 'dataSource.properties.dataSourceProperties': [connectionCollation: 'high'] + ])) + + when: + def connectionSource = factory.create(ConnectionSource.DEFAULT, environment) + + then: + connectionSource.settings.properties.dataSourceProperties.connectionCollation == 'high' + def dataSource = connectionSource.source.targetDataSource.targetDataSource + dataSource instanceof HikariDataSource + dataSource.maximumPoolSize == 16 + dataSource.dataSourceProperties.getProperty('connectionCollation') == 'high' + + cleanup: + connectionSource?.close() + } + + void "create keeps a higher precedence nested JDBC property over a lower dotted map key"() { + given: + def factory = new DataSourceConnectionSourceFactory() + def environment = new StandardEnvironment() + environment.propertySources.addLast(new MapPropertySource('low', [ + 'dataSource.url': 'jdbc:h2:mem:factoryDottedKeyPrecedenceTest', + 'dataSource.type': HikariDataSource.name, + 'dataSource.properties.dataSourceProperties': ['oracle.jdbc.sendBooleanAsNativeBoolean': 'false'] + ])) + environment.propertySources.addFirst(new MapPropertySource('high', [ + 'dataSource.properties.dataSourceProperties.oracle.jdbc.sendBooleanAsNativeBoolean': 'true' + ])) + + when: + def connectionSource = factory.create(ConnectionSource.DEFAULT, environment) + + then: + def dataSource = connectionSource.source.targetDataSource.targetDataSource + dataSource instanceof HikariDataSource + dataSource.dataSourceProperties.getProperty('oracle.jdbc.sendBooleanAsNativeBoolean') == 'true' + !dataSource.dataSourceProperties.containsKey('oracle') + + cleanup: + connectionSource?.close() + } + + void "create rejects a non-map properties value when no flattened properties exist"() { + given: + def factory = new DataSourceConnectionSourceFactory() + def environment = new StandardEnvironment() + environment.propertySources.addFirst(new MapPropertySource('application', [ + 'dataSource.url': 'jdbc:h2:mem:factoryInvalidPropertiesTest', + 'dataSource.properties': 'not a map' + ])) + + when: + factory.create(ConnectionSource.DEFAULT, environment) + + then: + thrown(ConfigurationException) + } + void "create resolves settings for the default data source from the 'dataSource' prefix"() { given: def factory = new DataSourceConnectionSourceFactory() @@ -41,6 +332,7 @@ class DataSourceConnectionSourceFactorySpec extends Specification { then: connectionSource.name == ConnectionSource.DEFAULT connectionSource.settings.url == 'jdbc:h2:mem:factoryDefaultTest;DB_CLOSE_DELAY=-1' + connectionSource.settings.properties.isEmpty() connectionSource.source != null cleanup: diff --git a/grails-datastore-core/src/main/groovy/org/grails/datastore/mapping/config/ConfigurationBuilder.groovy b/grails-datastore-core/src/main/groovy/org/grails/datastore/mapping/config/ConfigurationBuilder.groovy index 02118c4ea8..c098534fcf 100644 --- a/grails-datastore-core/src/main/groovy/org/grails/datastore/mapping/config/ConfigurationBuilder.groovy +++ b/grails-datastore-core/src/main/groovy/org/grails/datastore/mapping/config/ConfigurationBuilder.groovy @@ -32,7 +32,10 @@ import groovy.util.logging.Slf4j import org.springframework.core.convert.ConversionFailedException import org.springframework.core.convert.ConverterNotFoundException +import org.springframework.core.env.ConfigurableEnvironment +import org.springframework.core.env.EnumerablePropertySource import org.springframework.core.env.PropertyResolver +import org.springframework.core.env.PropertySource import org.springframework.util.ClassUtils import org.springframework.util.ReflectionUtils @@ -258,7 +261,7 @@ abstract class ConfigurationBuilder<B, C> { } if (newBuilder instanceof Map) { - Map subMap = propertyResolver.getProperty(propertyPath, Map, Collections.emptyMap()) + Map subMap = resolveMapProperty(propertyPath, Collections.emptyMap()) if (!subMap.isEmpty()) { ((Map) newBuilder).putAll(subMap) } @@ -395,7 +398,9 @@ abstract class ConfigurationBuilder<B, C> { def value try { - value = propertyResolver.getProperty(propertyPathForArg, argType, fallBackValue) + value = argType == Map + ? resolveMapProperty(propertyPathForArg, (Map) fallBackValue) + : propertyResolver.getProperty(propertyPathForArg, argType, fallBackValue) } catch (ConversionFailedException e) { value = handleConversionException(e, argType, propertyPathForArg, fallBackValue) } catch (ConverterNotFoundException e) { @@ -425,6 +430,152 @@ abstract class ConfigurationBuilder<B, C> { } + private Map resolveMapProperty(String propertyPath, Map fallBackValue) { + Map directValue + RuntimeException conversionFailure = null + try { + directValue = propertyResolver.getProperty(propertyPath, Map) + } catch (ConversionFailedException | ConverterNotFoundException e) { + conversionFailure = e + directValue = null + } + if (directValue != null && !directValue.isEmpty()) { + return directValue + } + if (propertyResolver instanceof ConfigurableEnvironment) { + ConfigurableEnvironment environment = (ConfigurableEnvironment) propertyResolver + String prefix = propertyPath + '.' + Map<String, Object> result = new LinkedHashMap<>() + Set<String> resolvedNames = new HashSet<>() + for (PropertySource<?> propertySource : environment.propertySources) { + if (propertySource instanceof EnumerablePropertySource) { + for (String name : ((EnumerablePropertySource<?>) propertySource).propertyNames) { + if (!name.startsWith(prefix) || !resolvedNames.add(name)) { + continue + } + Object value = readEnvironmentValue(environment, propertySource, name) + if (value == null) { + continue + } + String[] segments = name.substring(prefix.length()).split('\\.') + Map<String, Object> current = result + for (int segmentIndex = 0; segmentIndex < segments.length - 1; segmentIndex++) { + String segment = segments[segmentIndex] + Object child = current.get(segment) + if (!(child instanceof Map)) { + child = new LinkedHashMap<String, Object>() + current.put(segment, child) + } + current = (Map<String, Object>) child + } + String leaf = segments[segments.length - 1] + if (value instanceof Map) { + Map<String, Object> copied = copyConfigMap((Map<?, ?>) value) + Object existing = current.get(leaf) + if (existing instanceof Map) { + mergeMissing((Map<String, Object>) existing, copied) + } + else { + current.put(leaf, copied) + } + claimMapPaths(resolvedNames, name, copied) + } + else if (!(current.get(leaf) instanceof Map)) { + current.put(leaf, value) + } + } + } + } + if (!result.isEmpty()) { + return result + } + } + if (conversionFailure != null) { + throw conversionFailure + } + return directValue != null ? directValue : fallBackValue + } + + /** + * Read one flattened entry without forcing it to String. A descendant may already be a + * Map, as Hibernate listener settings are, and String conversion throws before that map + * can be bound. + */ + private static Object readEnvironmentValue(ConfigurableEnvironment environment, PropertySource<?> propertySource, String name) { + try { + return environment.getProperty(name, Object) + } catch (ConversionFailedException | ConverterNotFoundException ignored) { + return propertySource.getProperty(name) + } + } + + private static Map<String, Object> copyConfigMap(Map<?, ?> source) { + Map<String, Object> copy = new LinkedHashMap<>() + for (Map.Entry<?, ?> entry : source.entrySet()) { + if (entry.key == null || entry.value == null) { + continue + } + Object value = entry.value instanceof Map ? copyConfigMap((Map<?, ?>) entry.value) : entry.value + putNormalized(copy, entry.key.toString(), value) + } + return copy + } + + /** + * A ConfigSlurper map may store {@code oracle.jdbc.flag} as one literal key, while YAML + * stores the same setting as nested keys. Expand the literal form so precedence compares + * one structure. {@code DataSourceBuilder} flattens it back to the JDBC property name. + */ + private static void putNormalized(Map<String, Object> target, String key, Object value) { + int dot = key.indexOf('.') + if (dot < 0) { + Object existing = target.get(key) + if (existing instanceof Map && value instanceof Map) { + mergeMissing((Map<String, Object>) existing, (Map<String, Object>) value) + } + else if (!target.containsKey(key)) { + target.put(key, value) + } + return + } + String head = key.substring(0, dot) + String tail = key.substring(dot + 1) + Object child = target.get(head) + if (!(child instanceof Map)) { + if (target.containsKey(head)) { + return + } + child = new LinkedHashMap<String, Object>() + target.put(head, child) + } + putNormalized((Map<String, Object>) child, tail, value) + } + + private static void claimMapPaths(Set<String> resolvedNames, String name, Map<?, ?> value) { + for (Map.Entry<?, ?> entry : value.entrySet()) { + if (entry.key == null) { + continue + } + String childName = name + '.' + entry.key.toString() + resolvedNames.add(childName) + if (entry.value instanceof Map) { + claimMapPaths(resolvedNames, childName, (Map<?, ?>) entry.value) + } + } + } + + private static void mergeMissing(Map<String, Object> target, Map<String, Object> extra) { + for (Map.Entry<String, Object> entry : extra.entrySet()) { + Object existing = target.get(entry.key) + if (existing instanceof Map && entry.value instanceof Map) { + mergeMissing((Map<String, Object>) existing, (Map<String, Object>) entry.value) + } + else if (!target.containsKey(entry.key)) { + target.put(entry.key, entry.value) + } + } + } + protected Object newChildBuilderForFallback(Object childBuilder, Object fallbackConfig) { return childBuilder }
