From edfa9f56d0a1ee45deb143dba3690bfa98770908 Mon Sep 17 00:00:00 2001 From: James Fredley Date: Thu, 22 Oct 2026 15:44:59 -0400 Subject: [PATCH 001/115] 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 --- ...taSourceConnectionSourceFactorySpec.groovy | 292 ++++++++++++++++++ .../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 0a9730c393e..4b741a0cae4 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) + } + 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 02118c4ea89..c098534fcf1 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 { } 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 { 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 { } + 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 result = new LinkedHashMap<>() + Set 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 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() + current.put(segment, child) + } + current = (Map) child + } + String leaf = segments[segments.length - 1] + if (value instanceof Map) { + Map copied = copyConfigMap((Map) value) + Object existing = current.get(leaf) + if (existing instanceof Map) { + mergeMissing((Map) 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 copyConfigMap(Map source) { + Map 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 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) existing, (Map) 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() + target.put(head, child) + } + putNormalized((Map) child, tail, value) + } + + private static void claimMapPaths(Set 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 target, Map extra) { + for (Map.Entry entry : extra.entrySet()) { + Object existing = target.get(entry.key) + if (existing instanceof Map && entry.value instanceof Map) { + mergeMissing((Map) existing, (Map) entry.value) + } + else if (!target.containsKey(entry.key)) { + target.put(entry.key, entry.value) + } + } + } + protected Object newChildBuilderForFallback(Object childBuilder, Object fallbackConfig) { return childBuilder } From 6daea2655e7b9168e07a05bc2ff32e272ee2d1dd Mon Sep 17 00:00:00 2001 From: James Fredley Date: Thu, 22 Oct 2026 16:19:07 -0400 Subject: [PATCH 002/115] Unwrap datasource proxies in pool property tests The Hikari assertions no longer assume a fixed two-proxy chain. Assisted-by: Sisyphus:grok-4.7 --- ...taSourceConnectionSourceFactorySpec.groovy | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 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 4b741a0cae4..150f179db15 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 @@ -31,6 +31,9 @@ import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy import spock.lang.Specification +import javax.sql.DataSource + + class DataSourceConnectionSourceFactorySpec extends Specification { void "create binds flattened default data source pool properties from the Spring Environment"() { @@ -85,7 +88,7 @@ class DataSourceConnectionSourceFactorySpec extends Specification { then: connectionSource.name == 'secondary' - def dataSource = connectionSource.source.targetDataSource.targetDataSource + def dataSource = unwrap(connectionSource.source) dataSource instanceof HikariDataSource dataSource.maximumPoolSize == 24 @@ -111,7 +114,7 @@ class DataSourceConnectionSourceFactorySpec extends Specification { def connectionSource = factory.create(ConnectionSource.DEFAULT, environment) then: - def dataSource = connectionSource.source.targetDataSource.targetDataSource + def dataSource = unwrap(connectionSource.source) dataSource instanceof HikariDataSource dataSource.maximumPoolSize == 32 dataSource.minimumIdle == 8 @@ -138,7 +141,7 @@ class DataSourceConnectionSourceFactorySpec extends Specification { then: config.getProperty('dataSource.properties', Map) == [maximumPoolSize: 20] - def dataSource = connectionSource.source.targetDataSource.targetDataSource + def dataSource = unwrap(connectionSource.source) dataSource instanceof HikariDataSource dataSource.maximumPoolSize == 20 @@ -165,7 +168,7 @@ class DataSourceConnectionSourceFactorySpec extends Specification { then: connectionSource.settings.properties.is(properties) connectionSource.settings.properties == [maximumPoolSize: 20] - connectionSource.source.targetDataSource.targetDataSource.maximumPoolSize == 20 + unwrap(connectionSource.source).maximumPoolSize == 20 cleanup: connectionSource?.close() @@ -202,7 +205,7 @@ class DataSourceConnectionSourceFactorySpec extends Specification { maximumPoolSize: '16', dataSourceProperties: [connectionCollation: 'utf8mb4_unicode_ci', empty: ''] ] - def dataSource = connectionSource.source.targetDataSource.targetDataSource + def dataSource = unwrap(connectionSource.source) dataSource instanceof HikariDataSource dataSource.maximumPoolSize == 16 dataSource.dataSourceProperties.getProperty('connectionCollation') == 'utf8mb4_unicode_ci' @@ -237,7 +240,7 @@ class DataSourceConnectionSourceFactorySpec extends Specification { then: connectionSource.settings.properties.maximumPoolSize == 16 connectionSource.settings.properties.dataSourceProperties == [connectionCollation: 'utf8mb4_unicode_ci', empty: ''] - def dataSource = connectionSource.source.targetDataSource.targetDataSource + def dataSource = unwrap(connectionSource.source) dataSource instanceof HikariDataSource dataSource.maximumPoolSize == 16 dataSource.dataSourceProperties.getProperty('connectionCollation') == 'utf8mb4_unicode_ci' @@ -266,7 +269,7 @@ class DataSourceConnectionSourceFactorySpec extends Specification { then: connectionSource.settings.properties.dataSourceProperties.connectionCollation == 'high' - def dataSource = connectionSource.source.targetDataSource.targetDataSource + def dataSource = unwrap(connectionSource.source) dataSource instanceof HikariDataSource dataSource.maximumPoolSize == 16 dataSource.dataSourceProperties.getProperty('connectionCollation') == 'high' @@ -292,7 +295,7 @@ class DataSourceConnectionSourceFactorySpec extends Specification { def connectionSource = factory.create(ConnectionSource.DEFAULT, environment) then: - def dataSource = connectionSource.source.targetDataSource.targetDataSource + def dataSource = unwrap(connectionSource.source) dataSource instanceof HikariDataSource dataSource.dataSourceProperties.getProperty('oracle.jdbc.sendBooleanAsNativeBoolean') == 'true' !dataSource.dataSourceProperties.containsKey('oracle') @@ -404,4 +407,14 @@ class DataSourceConnectionSourceFactorySpec extends Specification { expect: factory.connectionSourcesConfigurationKey == 'dataSources' } + + private static DataSource unwrap(DataSource dataSource) { + DataSource current = dataSource + while (current instanceof TransactionAwareDataSourceProxy || current instanceof LazyConnectionDataSourceProxy) { + current = current instanceof TransactionAwareDataSourceProxy ? + ((TransactionAwareDataSourceProxy) current).targetDataSource : + ((LazyConnectionDataSourceProxy) current).targetDataSource + } + return current + } } From bcfa9c7beb21b81a58ece8722cc46da84fbf8055 Mon Sep 17 00:00:00 2001 From: James Fredley Date: Thu, 22 Oct 2026 17:14:27 -0400 Subject: [PATCH 003/115] Cover Grails environment blocks in datasource YAML development, test, and production each apply their own pool settings. An unset grails.env uses development. A key the active block does not set stays at the root value. Assisted-by: Sisyphus:grok-4.7 --- grails-datasource/build.gradle | 1 + .../DataSourceEnvironmentYamlSpec.groovy | 152 ++++++++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 grails-datasource/src/test/groovy/org/grails/plugins/datasource/DataSourceEnvironmentYamlSpec.groovy diff --git a/grails-datasource/build.gradle b/grails-datasource/build.gradle index 26e5072f136..fd65f5a955a 100644 --- a/grails-datasource/build.gradle +++ b/grails-datasource/build.gradle @@ -58,6 +58,7 @@ dependencies { compileOnly 'jakarta.servlet:jakarta.servlet-api' testImplementation 'com.h2database:h2' + testImplementation 'com.zaxxer:HikariCP' testImplementation 'jakarta.servlet:jakarta.servlet-api' testImplementation 'org.springframework:spring-test' testImplementation 'org.apache.groovy:groovy-json', { diff --git a/grails-datasource/src/test/groovy/org/grails/plugins/datasource/DataSourceEnvironmentYamlSpec.groovy b/grails-datasource/src/test/groovy/org/grails/plugins/datasource/DataSourceEnvironmentYamlSpec.groovy new file mode 100644 index 00000000000..6613148bfe4 --- /dev/null +++ b/grails-datasource/src/test/groovy/org/grails/plugins/datasource/DataSourceEnvironmentYamlSpec.groovy @@ -0,0 +1,152 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.plugins.datasource + +import javax.sql.DataSource + +import com.zaxxer.hikari.HikariDataSource + +import org.springframework.core.env.StandardEnvironment +import org.springframework.core.io.ByteArrayResource +import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy +import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy + +import grails.util.Environment +import org.grails.config.yaml.YamlPropertySourceLoader +import org.grails.datastore.gorm.jdbc.connections.DataSourceConnectionSourceFactory +import org.grails.datastore.mapping.core.connections.ConnectionSource + +import spock.lang.Specification + +class DataSourceEnvironmentYamlSpec extends Specification { + + private static final String APPLICATION_YML = '''\ +dataSource: + pooled: true + driverClassName: org.h2.Driver + username: sa + password: '' + type: com.zaxxer.hikari.HikariDataSource + properties: + maximumPoolSize: 4 + minimumIdle: 1 + connectionTimeout: 1000 +environments: + development: + dataSource: + dbCreate: create-drop + url: jdbc:h2:mem:devDb;DB_CLOSE_DELAY=-1 + properties: + maximumPoolSize: 16 + minimumIdle: 8 + connectionTimeout: 2000 + test: + dataSource: + dbCreate: update + url: jdbc:h2:mem:testDb;DB_CLOSE_DELAY=-1 + properties: + maximumPoolSize: 2 + minimumIdle: 1 + production: + dataSource: + dbCreate: none + url: jdbc:h2:mem:prodDb;DB_CLOSE_DELAY=-1 + properties: + maximumPoolSize: 32 + minimumIdle: 4 + connectionTimeout: 5000 +''' + + private String previousEnvironment + + def setup() { + previousEnvironment = System.getProperty(Environment.KEY) + } + + def cleanup() { + if (previousEnvironment == null) { + System.clearProperty(Environment.KEY) + } + else { + System.setProperty(Environment.KEY, previousEnvironment) + } + Environment.reset() + } + + void "application.yml #environment block supplies the dataSource properties"() { + given: + System.setProperty(Environment.KEY, environment) + Environment.reset() + def connectionSource = createFromApplicationYaml() + def dataSource = unwrap(connectionSource.source) + + expect: + connectionSource.settings.url == url + dataSource instanceof HikariDataSource + dataSource.maximumPoolSize == maximumPoolSize + dataSource.minimumIdle == minimumIdle + dataSource.connectionTimeout == connectionTimeout + + cleanup: + connectionSource?.close() + + where: + environment | url | maximumPoolSize | minimumIdle | connectionTimeout + 'development' | 'jdbc:h2:mem:devDb;DB_CLOSE_DELAY=-1' | 16 | 8 | 2000 + 'test' | 'jdbc:h2:mem:testDb;DB_CLOSE_DELAY=-1' | 2 | 1 | 1000 + 'production' | 'jdbc:h2:mem:prodDb;DB_CLOSE_DELAY=-1' | 32 | 4 | 5000 + } + + void "unset grails.env uses the development dataSource properties"() { + given: + System.clearProperty(Environment.KEY) + Environment.reset() + def connectionSource = createFromApplicationYaml() + def dataSource = unwrap(connectionSource.source) + + expect: + connectionSource.settings.url == 'jdbc:h2:mem:devDb;DB_CLOSE_DELAY=-1' + dataSource instanceof HikariDataSource + dataSource.maximumPoolSize == 16 + dataSource.minimumIdle == 8 + dataSource.connectionTimeout == 2000 + + cleanup: + connectionSource?.close() + } + + private static createFromApplicationYaml() { + def loaded = new YamlPropertySourceLoader().load( + 'application.yml', + new ByteArrayResource(APPLICATION_YML.getBytes('UTF-8'))) + def environment = new StandardEnvironment() + environment.propertySources.addFirst(loaded.first()) + new DataSourceConnectionSourceFactory().create(ConnectionSource.DEFAULT, environment) + } + + private static DataSource unwrap(DataSource dataSource) { + DataSource current = dataSource + while (current instanceof TransactionAwareDataSourceProxy || current instanceof LazyConnectionDataSourceProxy) { + current = current instanceof TransactionAwareDataSourceProxy ? + ((TransactionAwareDataSourceProxy) current).targetDataSource : + ((LazyConnectionDataSourceProxy) current).targetDataSource + } + return current + } +} From 5ac0a4a68310d91b650ea642a78e86840433ef89 Mon Sep 17 00:00:00 2001 From: James Fredley Date: Thu, 29 Oct 2026 03:28:13 -0400 Subject: [PATCH 004/115] Keep source precedence when rebuilding dataSource.properties The map is now rebuilt from each property source in precedence order, so the highest source wins for every path whatever its shape: - A scalar or map from a higher source is not replaced by a lower nested key, map, or unresolved placeholder. - A map stored at the exact path is read per source instead of being returned early, so higher flattened keys still win over it. - Indexed keys such as connectionInitSqls[0] build a List, copying an existing list instead of mutating it. - Literal dotted keys such as oracle.jdbc.flag are kept as written and block the same nested path from lower sources and from later entries of the same source. - A string the conversion service turns into a map is used at its own precedence, after placeholder resolution. String values still go through Environment.getProperty, so the unresolvable-placeholder setting is kept. ConfigurationBuilderSpec now covers the Environment path, including a map-backed @Builder(SimpleStrategy) type and an unchanged DatastoreUtils resolver. The datasource specs cover a YAML list and the dotted application.groovy form. Assisted-by: Sisyphus:grok-4.7 Assisted-by: Sisyphus:claude-opus-5-5 --- ...taSourceConnectionSourceFactorySpec.groovy | 143 +++++ .../DataSourceEnvironmentYamlSpec.groovy | 63 +++ .../config/ConfigurationBuilder.groovy | 347 ++++++++++-- .../config/ConfigurationBuilderSpec.groovy | 511 ++++++++++++++++++ 4 files changed, 1009 insertions(+), 55 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 150f179db15..22f184b8c6a 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 @@ -123,6 +123,87 @@ class DataSourceConnectionSourceFactorySpec extends Specification { connectionSource?.close() } + void "create keeps a higher precedence scalar over a lower flattened descendant"() { + given: + def factory = new DataSourceConnectionSourceFactory() + def environment = new StandardEnvironment() + environment.propertySources.addFirst(new MapPropertySource('low', [ + 'dataSource.url': 'jdbc:h2:mem:factoryScalarPrecedenceTest', + 'dataSource.type': HikariDataSource.name, + 'dataSource.properties.dataSourceProperties.foo.x': 'low', + 'dataSource.properties.foo.x': 'low', + 'dataSource.properties.maximumPoolSize.nested': '${unresolvedLowerPoolSize}' + ])) + environment.propertySources.addFirst(new MapPropertySource('high', [ + 'dataSource.properties.dataSourceProperties.foo': 'bar', + 'dataSource.properties.foo': 'bar', + 'dataSource.properties.maximumPoolSize': 50 + ])) + + when: + def connectionSource = factory.create(ConnectionSource.DEFAULT, environment) + + then: + connectionSource.settings.properties == [maximumPoolSize: 50, dataSourceProperties: [foo: 'bar'], foo: 'bar'] + def dataSource = unwrap(connectionSource.source) + dataSource.maximumPoolSize == 50 + dataSource.dataSourceProperties.getProperty('foo') == 'bar' + !dataSource.dataSourceProperties.containsKey('foo.x') + + cleanup: + connectionSource?.close() + } + + void "create merges a lower direct map behind higher flattened pool properties"() { + given: + def factory = new DataSourceConnectionSourceFactory() + def environment = new StandardEnvironment() + def lowerProperties = [maximumPoolSize: 20, minimumIdle: 2] + environment.propertySources.addFirst(new MapPropertySource('low', [ + 'dataSource.url': 'jdbc:h2:mem:factoryDirectMapPrecedenceTest', + 'dataSource.type': HikariDataSource.name, + 'dataSource.properties': lowerProperties + ])) + environment.propertySources.addFirst(new MapPropertySource('high', [ + 'dataSource.properties.maximumPoolSize': 50 + ])) + + when: + def connectionSource = factory.create(ConnectionSource.DEFAULT, environment) + + then: + connectionSource.settings.properties == [maximumPoolSize: 50, minimumIdle: 2] + def dataSource = unwrap(connectionSource.source) + dataSource.maximumPoolSize == 50 + dataSource.minimumIdle == 2 + lowerProperties == [maximumPoolSize: 20, minimumIdle: 2] + + cleanup: + connectionSource?.close() + } + + void "create collects indexed pool properties into a list in numeric order"() { + given: + def factory = new DataSourceConnectionSourceFactory() + def environment = new StandardEnvironment() + environment.propertySources.addFirst(new MapPropertySource('application', [ + 'dataSource.url': 'jdbc:h2:mem:factoryIndexedPropertiesTest', + 'dataSource.type': ListDataSource.name, + 'dataSource.properties.connectionInitSqls[1]': 'SET B=2', + 'dataSource.properties.connectionInitSqls[0]': 'SET A=1' + ])) + + when: + def connectionSource = factory.create(ConnectionSource.DEFAULT, environment) + + then: + connectionSource.settings.properties == [connectionInitSqls: ['SET A=1', 'SET B=2']] + unwrap(connectionSource.source).connectionInitSqls == ['SET A=1', 'SET B=2'] + + cleanup: + connectionSource?.close() + } + void "create still binds an existing nested properties map from DatastoreUtils"() { given: def factory = new DataSourceConnectionSourceFactory() @@ -304,6 +385,62 @@ class DataSourceConnectionSourceFactorySpec extends Specification { connectionSource?.close() } + void "create keeps a higher precedence dotted map key over a lower flattened JDBC property"() { + given: + def factory = new DataSourceConnectionSourceFactory() + def environment = new StandardEnvironment() + environment.propertySources.addLast(new MapPropertySource('low', [ + 'dataSource.url': 'jdbc:h2:mem:factoryLiteralKeyPrecedenceTest', + '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: + connectionSource.settings.properties.dataSourceProperties == ['oracle.jdbc.sendBooleanAsNativeBoolean': 'true'] + def dataSource = unwrap(connectionSource.source) + dataSource instanceof HikariDataSource + dataSource.dataSourceProperties.getProperty('oracle.jdbc.sendBooleanAsNativeBoolean') == 'true' + !dataSource.dataSourceProperties.containsKey('oracle') + + cleanup: + connectionSource?.close() + } + + void "create keeps a higher literal JDBC key over a lower nested map"() { + given: + def factory = new DataSourceConnectionSourceFactory() + def environment = new StandardEnvironment() + environment.propertySources.addLast(new MapPropertySource('low', [ + 'dataSource.url': 'jdbc:h2:mem:factoryNestedMapPrecedenceTest', + 'dataSource.type': HikariDataSource.name, + 'dataSource.properties.dataSourceProperties.oracle': [jdbc: [sendBooleanAsNativeBoolean: false], pool: 'kept'] + ])) + environment.propertySources.addFirst(new MapPropertySource('high', [ + 'dataSource.properties.dataSourceProperties': ['oracle.jdbc.sendBooleanAsNativeBoolean': 'true'] + ])) + + when: + def connectionSource = factory.create(ConnectionSource.DEFAULT, environment) + + then: + connectionSource.settings.properties.dataSourceProperties['oracle.jdbc.sendBooleanAsNativeBoolean'] == 'true' + connectionSource.settings.properties.dataSourceProperties.oracle.pool == 'kept' + connectionSource.settings.properties.dataSourceProperties.oracle.jdbc == null + def dataSource = unwrap(connectionSource.source) + dataSource instanceof HikariDataSource + dataSource.dataSourceProperties.getProperty('oracle.jdbc.sendBooleanAsNativeBoolean') == 'true' + dataSource.dataSourceProperties.getProperty('oracle.pool') == 'kept' + + cleanup: + connectionSource?.close() + } + void "create rejects a non-map properties value when no flattened properties exist"() { given: def factory = new DataSourceConnectionSourceFactory() @@ -408,6 +545,11 @@ class DataSourceConnectionSourceFactorySpec extends Specification { factory.connectionSourcesConfigurationKey == 'dataSources' } + static class ListDataSource extends HikariDataSource { + + List connectionInitSqls + } + private static DataSource unwrap(DataSource dataSource) { DataSource current = dataSource while (current instanceof TransactionAwareDataSourceProxy || current instanceof LazyConnectionDataSourceProxy) { @@ -418,3 +560,4 @@ class DataSourceConnectionSourceFactorySpec extends Specification { return current } } + diff --git a/grails-datasource/src/test/groovy/org/grails/plugins/datasource/DataSourceEnvironmentYamlSpec.groovy b/grails-datasource/src/test/groovy/org/grails/plugins/datasource/DataSourceEnvironmentYamlSpec.groovy index 6613148bfe4..948542ae57c 100644 --- a/grails-datasource/src/test/groovy/org/grails/plugins/datasource/DataSourceEnvironmentYamlSpec.groovy +++ b/grails-datasource/src/test/groovy/org/grails/plugins/datasource/DataSourceEnvironmentYamlSpec.groovy @@ -18,17 +18,22 @@ */ package org.grails.plugins.datasource +import java.nio.file.Files +import java.nio.file.Path + import javax.sql.DataSource import com.zaxxer.hikari.HikariDataSource import org.springframework.core.env.StandardEnvironment import org.springframework.core.io.ByteArrayResource +import org.springframework.core.io.FileSystemResource import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy import grails.util.Environment import org.grails.config.yaml.YamlPropertySourceLoader +import org.grails.core.cfg.GroovyConfigPropertySourceLoader import org.grails.datastore.gorm.jdbc.connections.DataSourceConnectionSourceFactory import org.grails.datastore.mapping.core.connections.ConnectionSource @@ -131,6 +136,64 @@ environments: connectionSource?.close() } + void "application.yml binds a list under dataSource properties"() { + given: + def yaml = """\ +dataSource: + url: jdbc:h2:mem:yamlListPropertiesTest + type: ${ListDataSource.name} + properties: + connectionInitSqls: + - SET A=1 + - SET B=2 +""" + def loaded = new YamlPropertySourceLoader().load( + 'application.yml', new ByteArrayResource(yaml.getBytes('UTF-8'))) + def environment = new StandardEnvironment() + environment.propertySources.addFirst(loaded.first()) + + when: + def connectionSource = new DataSourceConnectionSourceFactory().create(ConnectionSource.DEFAULT, environment) + + then: + connectionSource.settings.properties.connectionInitSqls == ['SET A=1', 'SET B=2'] + unwrap(connectionSource.source).connectionInitSqls == ['SET A=1', 'SET B=2'] + + cleanup: + connectionSource?.close() + } + + void "application.groovy dotted properties configure the connection pool"() { + given: + def directory = Files.createTempDirectory(Path.of('build'), 'datasource-groovy-').toFile() + def application = new File(directory, 'application.groovy') + application.setText('''\ +dataSource.url = 'jdbc:h2:mem:groovyDottedPropertiesTest' +dataSource.type = 'com.zaxxer.hikari.HikariDataSource' +dataSource.properties.maximumPoolSize = 100 +''', 'UTF-8') + def loaded = new GroovyConfigPropertySourceLoader().load( + 'application.groovy', new FileSystemResource(application)) + def environment = new StandardEnvironment() + environment.propertySources.addFirst(loaded.first()) + + when: + def connectionSource = new DataSourceConnectionSourceFactory().create(ConnectionSource.DEFAULT, environment) + + then: + connectionSource.settings.properties.maximumPoolSize == 100 + unwrap(connectionSource.source).maximumPoolSize == 100 + + cleanup: + connectionSource?.close() + directory?.deleteDir() + } + + static class ListDataSource extends HikariDataSource { + + List connectionInitSqls + } + private static createFromApplicationYaml() { def loaded = new YamlPropertySourceLoader().load( 'application.yml', 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 c098534fcf1..51c914350b5 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 @@ -33,6 +33,7 @@ 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.util.PlaceholderResolutionException import org.springframework.core.env.EnumerablePropertySource import org.springframework.core.env.PropertyResolver import org.springframework.core.env.PropertySource @@ -439,26 +440,45 @@ abstract class ConfigurationBuilder { conversionFailure = e directValue = null } - if (directValue != null && !directValue.isEmpty()) { + if (!(propertyResolver instanceof ConfigurableEnvironment) && directValue != null && !directValue.isEmpty()) { return directValue } if (propertyResolver instanceof ConfigurableEnvironment) { ConfigurableEnvironment environment = (ConfigurableEnvironment) propertyResolver String prefix = propertyPath + '.' Map result = new LinkedHashMap<>() - Set resolvedNames = new HashSet<>() for (PropertySource propertySource : environment.propertySources) { - if (propertySource instanceof EnumerablePropertySource) { + Object sourceValue = propertySource.getProperty(propertyPath) + Map sourceMap = new LinkedHashMap<>() + if (sourceValue instanceof Map && !((Map) sourceValue).isEmpty()) { + sourceMap = copyConfigMap((Map) sourceValue) + } + else if (propertySource instanceof EnumerablePropertySource) { for (String name : ((EnumerablePropertySource) propertySource).propertyNames) { - if (!name.startsWith(prefix) || !resolvedNames.add(name)) { + String relativeName = name.startsWith(prefix) ? name.substring(prefix.length()) : null + if (relativeName == null || hasResolvedValue(result, relativeName) || leafAlreadySet(sourceMap, relativeName)) { + continue + } + // An exact path already stored, including an empty map, wins over a lower + // scalar. Skip that scalar before placeholder resolution. A lower map still + // merges, so its missing children are kept. + if ((exactPathPresent(result, relativeName) || indexedLiteralClaimed(result, relativeName)) && !firstRawValueIsMap(environment, name)) { continue } - Object value = readEnvironmentValue(environment, propertySource, name) + Object value = readEnvironmentValue(environment, name) if (value == null) { continue } + if (value instanceof Map) { + Map copied = copyConfigMap((Map) value) + Map filtered = withoutSourceAliases(sourceMap, relativeName, copied) + if (filtered.isEmpty() && !copied.isEmpty()) { + continue + } + value = filtered + } String[] segments = name.substring(prefix.length()).split('\\.') - Map current = result + Map current = sourceMap for (int segmentIndex = 0; segmentIndex < segments.length - 1; segmentIndex++) { String segment = segments[segmentIndex] Object child = current.get(segment) @@ -469,7 +489,19 @@ abstract class ConfigurationBuilder { current = (Map) child } String leaf = segments[segments.length - 1] - if (value instanceof Map) { + int bracket = leaf.lastIndexOf('[') + if (bracket > 0 && leaf.endsWith(']') && leaf.substring(bracket + 1, leaf.length() - 1).matches('\\d+')) { + int index = Integer.parseInt(leaf.substring(bracket + 1, leaf.length() - 1)) + String listName = leaf.substring(0, bracket) + Object existing = current.get(listName) + List values = existing instanceof List ? new ArrayList((List) existing) : new ArrayList() + while (values.size() <= index) { + values.add(null) + } + values.set(index, value) + current.put(listName, values) + } + else if (value instanceof Map) { Map copied = copyConfigMap((Map) value) Object existing = current.get(leaf) if (existing instanceof Map) { @@ -478,16 +510,22 @@ abstract class ConfigurationBuilder { else { current.put(leaf, copied) } - claimMapPaths(resolvedNames, name, copied) } - else if (!(current.get(leaf) instanceof Map)) { + else if (current.get(leaf) == null) { current.put(leaf, value) } } } + if (sourceMap.isEmpty() && sourceValue != null && !(sourceValue instanceof Map)) { + Map converted = convertSourceMap(environment, sourceValue) + if (converted != null) { + sourceMap = converted + } + } + mergeMissing(result, sourceMap) } if (!result.isEmpty()) { - return result + return result == directValue ? directValue : result } } if (conversionFailure != null) { @@ -497,85 +535,283 @@ abstract class ConfigurationBuilder { } /** - * 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. + * Read the highest precedence entry, preserving maps and lists. String values are resolved + * through the environment so its unresolvable-placeholder setting is kept. */ - 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 Object readEnvironmentValue(ConfigurableEnvironment environment, String name) { + for (PropertySource propertySource : environment.propertySources) { + Object value = propertySource.getProperty(name) + if (value == null) { + continue + } + if (!(value instanceof String)) { + return value + } + String resolved = environment.getProperty(name) + return resolved != null ? resolved : value } + return null } - private static Map copyConfigMap(Map source) { - Map copy = new LinkedHashMap<>() - for (Map.Entry entry : source.entrySet()) { - if (entry.key == null || entry.value == null) { - continue + private static boolean hasResolvedValue(Map result, String name) { + for (Map.Entry entry : result.entrySet()) { + if (name == entry.key && !(entry.value instanceof Map)) { + return true + } + if (name.startsWith(entry.key + '.')) { + if (!(entry.value instanceof Map) || hasResolvedValue((Map) entry.value, name.substring(entry.key.length() + 1))) { + return true + } } - Object value = entry.value instanceof Map ? copyConfigMap((Map) entry.value) : entry.value - putNormalized(copy, entry.key.toString(), value) } - return copy + int dot = name.indexOf('.') + String head = dot < 0 ? name : name.substring(0, dot) + int bracket = head.indexOf('[') + if (bracket > 0) { + head = head.substring(0, bracket) + } + if (!result.containsKey(head)) { + return false + } + Object value = result.get(head) + if (!(value instanceof Map)) { + return true + } + return dot >= 0 && hasResolvedValue((Map) value, name.substring(dot + 1)) } /** - * 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. + * True when this enumeration already stored the named leaf. + * A map container is not a claim, so a later parent map can still add missing children. + * A scalar or list parent is not a claim either, so a later same-source nested key can replace it. */ - private static void putNormalized(Map 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) existing, (Map) value) + private static boolean leafAlreadySet(Map source, String name) { + for (Map.Entry entry : source.entrySet()) { + if (name == entry.key) { + return !(entry.value instanceof Map) } - else if (!target.containsKey(key)) { - target.put(key, value) + if (entry.value instanceof Map && name.startsWith(entry.key + '.')) { + if (leafAlreadySet((Map) entry.value, name.substring(entry.key.length() + 1))) { + return true + } } - return } - String head = key.substring(0, dot) - String tail = key.substring(dot + 1) - Object child = target.get(head) + int dot = name.indexOf('.') + if (dot < 0 || name.substring(0, dot).indexOf('[') > 0) { + return false + } + Object child = source.get(name.substring(0, dot)) if (!(child instanceof Map)) { - if (target.containsKey(head)) { - return + return false + } + return leafAlreadySet((Map) child, name.substring(dot + 1)) + } + + /** + * True when this exact path is already stored. An empty map counts. + * A literal dotted key such as {@code a.b} is a real prefix, not only the + * first segment. An index segment is not a claim, so a later entry can still + * update a list. + */ + private static boolean exactPathPresent(Map source, String name) { + if (source.get(name) != null) { + return true + } + if (name.indexOf('[') < 0) { + for (Map.Entry entry : source.entrySet()) { + if (entry.value instanceof Map && name.startsWith(entry.key + '.')) { + if (exactPathPresent((Map) entry.value, name.substring(entry.key.length() + 1))) { + return true + } + } } - child = new LinkedHashMap() - target.put(head, child) } - putNormalized((Map) child, tail, value) + int dot = name.indexOf('.') + if (dot < 0 || name.substring(0, dot).indexOf('[') > 0) { + return false + } + Object child = source.get(name.substring(0, dot)) + if (!(child instanceof Map)) { + return false + } + return exactPathPresent((Map) child, name.substring(dot + 1)) } - private static void claimMapPaths(Set resolvedNames, String name, Map value) { - for (Map.Entry entry : value.entrySet()) { - if (entry.key == null) { + /** + * True when {@code name} is an index under a stored literal key whose value is not a map, + * such as {@code a.b[0]} when {@code a.b} already holds a list. A map value is not a claim. + */ + private static boolean indexedLiteralClaimed(Map source, String name) { + int bracket = name.indexOf('[') + if (bracket <= 0) { + return false + } + return nonMapLiteralPresent(source, name.substring(0, bracket)) + } + + /** + * Copy a map from the same source, omitting leaves already stored under a literal key. + * Missing siblings are kept. A scalar ancestor is not a claim. + */ + private static Map withoutSourceAliases(Map root, String prefix, Map extra) { + Map copy = new LinkedHashMap<>() + for (Map.Entry entry : extra.entrySet()) { + String path = prefix + '.' + entry.key + if (!(entry.value instanceof Map) && exactPathPresent(root, path)) { continue } - String childName = name + '.' + entry.key.toString() - resolvedNames.add(childName) if (entry.value instanceof Map) { - claimMapPaths(resolvedNames, childName, (Map) entry.value) + if (((Map) entry.value).isEmpty()) { + copy.put(entry.key, new LinkedHashMap()) + } + else { + Map nested = withoutSourceAliases(root, path, (Map) entry.value) + if (!nested.isEmpty()) { + copy.put(entry.key, nested) + } + } + } + else { + copy.put(entry.key, entry.value) + } + } + return copy + } + + private static boolean nonMapLiteralPresent(Map source, String name) { + Object direct = source.get(name) + if (direct != null && !(direct instanceof Map)) { + return true + } + for (Map.Entry entry : source.entrySet()) { + if (entry.value instanceof Map && name.startsWith(entry.key + '.')) { + if (nonMapLiteralPresent((Map) entry.value, name.substring(entry.key.length() + 1))) { + return true + } + } + } + int dot = name.indexOf('.') + if (dot < 0) { + return false + } + Object child = source.get(name.substring(0, dot)) + if (!(child instanceof Map)) { + return false + } + return nonMapLiteralPresent((Map) child, name.substring(dot + 1)) + } + + /** + * A raw value that is not itself a map can still convert to one, for example a + * higher-precedence string the environment's conversion service understands. + * A string is resolved with the environment's placeholder settings first, so a + * value such as {@code pool} referenced by a placeholder converts as that text. + * Unresolvable placeholder text is left unchanged and fails conversion instead of + * failing the build: a lower source must not throw, and the winning value was + * already resolved by {@code getProperty}. Dotted keys on that same source are + * used instead when enumeration already found some. + */ + private static Map convertSourceMap(ConfigurableEnvironment environment, Object sourceValue) { + Object convertible = sourceValue + if (convertible instanceof String) { + try { + convertible = environment.resolvePlaceholders((String) convertible) + } catch (PlaceholderResolutionException ignored) { + return null + } + } + try { + Map converted = environment.getConversionService().convert(convertible, Map) + if (converted == null || converted.isEmpty()) { + return null + } + return copyConfigMap(converted) + } catch (ConversionFailedException | ConverterNotFoundException ignored) { + return null + } + } + + /** + * Look at the first raw value only. Placeholder resolution is intentionally not done here. + */ + private static boolean firstRawValueIsMap(ConfigurableEnvironment environment, String name) { + for (PropertySource propertySource : environment.propertySources) { + Object value = propertySource.getProperty(name) + if (value != null) { + return value instanceof Map } } + return false + } + + private static Map copyConfigMap(Map source) { + Map 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 + copy.put(entry.key.toString(), value) + } + return copy } private static void mergeMissing(Map target, Map extra) { + mergeMissing(target, extra, target, '') + } + + private static void mergeMissing(Map root, Map extra, Map target, String prefix) { for (Map.Entry entry : extra.entrySet()) { + String path = prefix ? prefix + '.' + entry.key : entry.key Object existing = target.get(entry.key) if (existing instanceof Map && entry.value instanceof Map) { - mergeMissing((Map) existing, (Map) entry.value) + mergeMissing(root, (Map) entry.value, (Map) existing, path) } - else if (!target.containsKey(entry.key)) { - target.put(entry.key, entry.value) + else if (!target.containsKey(entry.key) && !hasResolvedValue(root, path)) { + Object value = entry.value + if (!(value instanceof Map) && exactPathPresent(root, path)) { + continue + } + if (value instanceof Map) { + value = withoutResolvedDescendants(root, path, (Map) value) + if (((Map) value).isEmpty() && !((Map) entry.value).isEmpty()) { + continue + } + } + target.put(entry.key, value) } } } + /** + * Copy a lower-precedence map, omitting leaves already supplied by a higher literal key. + * Stored keys are not split or rewritten. + */ + private static Map withoutResolvedDescendants(Map root, String prefix, Map extra) { + Map copy = new LinkedHashMap<>() + for (Map.Entry entry : extra.entrySet()) { + String path = prefix + '.' + entry.key + if (hasResolvedValue(root, path) || (!(entry.value instanceof Map) && exactPathPresent(root, path))) { + continue + } + if (entry.value instanceof Map) { + if (((Map) entry.value).isEmpty()) { + copy.put(entry.key, new LinkedHashMap()) + } + else { + Map nested = withoutResolvedDescendants(root, path, (Map) entry.value) + if (!nested.isEmpty()) { + copy.put(entry.key, nested) + } + } + } + else { + copy.put(entry.key, entry.value) + } + } + return copy + } + protected Object newChildBuilderForFallback(Object childBuilder, Object fallbackConfig) { return childBuilder } @@ -831,3 +1067,4 @@ abstract class ConfigurationBuilder { return ClassUtils.isAssignableValue(propertyType, nestedValue) ? nestedValue : null } } + diff --git a/grails-datastore-core/src/test/groovy/org/grails/datastore/mapping/config/ConfigurationBuilderSpec.groovy b/grails-datastore-core/src/test/groovy/org/grails/datastore/mapping/config/ConfigurationBuilderSpec.groovy index af787366710..d7af8e85c57 100644 --- a/grails-datastore-core/src/test/groovy/org/grails/datastore/mapping/config/ConfigurationBuilderSpec.groovy +++ b/grails-datastore-core/src/test/groovy/org/grails/datastore/mapping/config/ConfigurationBuilderSpec.groovy @@ -19,6 +19,9 @@ package org.grails.datastore.mapping.config +import groovy.transform.builder.Builder +import groovy.transform.builder.SimpleStrategy + import org.grails.datastore.mapping.core.DatastoreUtils import org.grails.datastore.mapping.core.exceptions.ConfigurationException import org.grails.datastore.mapping.core.connections.ConnectionSourceSettings @@ -29,6 +32,10 @@ import org.springframework.core.convert.TypeDescriptor import org.grails.datastore.mapping.multitenancy.TenantResolver import org.grails.datastore.mapping.multitenancy.resolvers.FixedTenantResolver import org.springframework.core.env.PropertyResolver +import org.springframework.core.env.PropertySource +import org.springframework.core.env.MapPropertySource +import org.springframework.core.env.StandardEnvironment +import org.springframework.core.env.SystemEnvironmentPropertySource import org.springframework.util.ReflectionUtils import spock.lang.Specification @@ -45,6 +52,457 @@ import java.util.concurrent.TimeUnit */ class ConfigurationBuilderSpec extends Specification { + void "Test discovered dotted map properties use higher relaxed environment values"() { + given: + def environment = new StandardEnvironment() + environment.propertySources.addFirst(new MapPropertySource('low', [ + 'dataSource.properties.maximumPoolSize': 10 + ])) + environment.propertySources.addFirst(new SystemEnvironmentPropertySource('high', [ + DATASOURCE_PROPERTIES_MAXIMUMPOOLSIZE: '50' + ])) + + expect: + new MapConfigurationBuilder(environment).build().properties == [maximumPoolSize: '50'] + } + + void "Test discovered dotted map properties use higher non-enumerable values without conversion"() { + given: + def environment = new StandardEnvironment() + def values = [maximumPoolSize: '${pool.size}', options: [enabled: true], connectionInitSqls: ['SET A=1', 'SET B=2']] + environment.propertySources.addFirst(new MapPropertySource('low', [ + 'dataSource.properties.maximumPoolSize': 10, + 'dataSource.properties.options': [enabled: false], + 'dataSource.properties.connectionInitSqls': ['LOW'], + 'pool.size': '50' + ])) + environment.propertySources.addFirst(new PropertySource('high', values) { + @Override + Object getProperty(String name) { + return name.startsWith('dataSource.properties.') ? getSource().get(name.substring('dataSource.properties.'.length())) : null + } + }) + + when: + def configuration = new MapConfigurationBuilder(environment).build() + + then: + configuration.properties == [maximumPoolSize: '50', options: [enabled: true], connectionInitSqls: ['SET A=1', 'SET B=2']] + configuration.properties.connectionInitSqls.is(values.connectionInitSqls) + !configuration.properties.options.is(values.options) + values.maximumPoolSize == '${pool.size}' + } + + void "Test existing maps preserve literal dotted keys at #propertyPath"() { + given: + def options = [ + 'dbms.memory.pagecache.size': '512M', + nested: ['dbms.memory.heap.max_size': '1G'] + ] + def environment = new StandardEnvironment() + environment.propertySources.addFirst(new MapPropertySource('configuration', [(propertyPath): options])) + + when: + def properties = new MapConfigurationBuilder(environment).build().properties + def copied = nested ? properties.options : properties + + then: + copied == options + copied['dbms.memory.pagecache.size'] == '512M' + copied.nested == ['dbms.memory.heap.max_size': '1G'] + options == ['dbms.memory.pagecache.size': '512M', nested: ['dbms.memory.heap.max_size': '1G']] + !nested || (!copied.is(options) && !copied.nested.is(options.nested)) + + where: + propertyPath | nested + 'dataSource.properties' | false + 'dataSource.properties.options' | true + } + + void "Test lower literal map keys fill only paths missing from higher flattened properties"() { + given: + def environment = new StandardEnvironment() + def options = ['dbms.memory.pagecache.size': '512M', 'dbms.memory.heap.max_size': '2G'] + environment.propertySources.addFirst(new MapPropertySource('low', [ + 'dataSource.properties.options': options + ])) + environment.propertySources.addFirst(new MapPropertySource('high', [ + 'dataSource.properties.options.dbms.memory.pagecache.size': '1G' + ])) + + when: + def properties = new MapConfigurationBuilder(environment).build().properties + + then: + properties.options == [dbms: [memory: [pagecache: [size: '1G']]], 'dbms.memory.heap.max_size': '2G'] + options == ['dbms.memory.pagecache.size': '512M', 'dbms.memory.heap.max_size': '2G'] + } + + void "Test higher literal map keys resolve flattened descendants for #options"() { + given: + def environment = new StandardEnvironment() + environment.propertySources.addFirst(new MapPropertySource('low', [ + 'dataSource.properties.options.dbms.memory.pagecache.size': '512M' + ])) + environment.propertySources.addFirst(new MapPropertySource('high', [ + 'dataSource.properties.options': options + ])) + + expect: + new MapConfigurationBuilder(environment).build().properties.options == expected + + where: + options | expected + ['dbms.memory.pagecache.size': '1G'] | ['dbms.memory.pagecache.size': '1G'] + ['dbms.memory': '1G'] | ['dbms.memory': '1G'] + ['dbms.memory': [pagecache: [size: '1G']]] | ['dbms.memory': [pagecache: [size: '1G']]] + ['dbms.memory': [heap: [max_size: '2G']]] | ['dbms.memory': [heap: [max_size: '2G']], dbms: [memory: [pagecache: [size: '512M']]]] + ['dbms.mem': '1G'] | ['dbms.mem': '1G', dbms: [memory: [pagecache: [size: '512M']]]] + } + + void "Test a higher literal key blocks only the matching leaf of a lower map"() { + given: + def environment = new StandardEnvironment() + environment.propertySources.addFirst(new MapPropertySource('low', [ + 'dataSource.properties.options.oracle': [jdbc: [sendBooleanAsNativeBoolean: false], pool: 'kept'] + ])) + environment.propertySources.addFirst(new MapPropertySource('mid', [ + 'dataSource.properties.options.oracle.other': 'kept' + ])) + environment.propertySources.addFirst(new MapPropertySource('high', [ + 'dataSource.properties.options': ['oracle.jdbc.sendBooleanAsNativeBoolean': 'true'] + ])) + + when: + def options = new MapConfigurationBuilder(environment).build().properties.options + + then: + options['oracle.jdbc.sendBooleanAsNativeBoolean'] == 'true' + options.oracle.pool == 'kept' + options.oracle.other == 'kept' + options.oracle.jdbc == null + } + + void "Test string placeholders follow the environment policy"() { + given: + def resolving = new StandardEnvironment() + resolving.propertySources.addFirst(new MapPropertySource('defined', [ + literal: 'ok', + 'dataSource.properties.connectionInitSql': 'SELECT ${literal}' + ])) + def lenient = new StandardEnvironment() + lenient.setIgnoreUnresolvableNestedPlaceholders(true) + lenient.propertySources.addFirst(new MapPropertySource('open', [ + 'dataSource.properties.connectionInitSql': 'SELECT ${literal}' + ])) + + expect: + new MapConfigurationBuilder(resolving).build().properties.connectionInitSql == 'SELECT ok' + new MapConfigurationBuilder(lenient).build().properties.connectionInitSql == 'SELECT ${literal}' + } + + void "Test indexed entries copy an existing list instead of mutating it"() { + given: + List original = List.of('SELECT 1') + def environment = new StandardEnvironment() + environment.propertySources.addFirst(new MapPropertySource('config', [ + 'dataSource.properties.connectionInitSqls': original, + 'dataSource.properties.connectionInitSqls[0]': 'SELECT 2' + ])) + + when: + def sqls = new MapConfigurationBuilder(environment).build().properties.connectionInitSqls + + then: + sqls == ['SELECT 2'] + original == ['SELECT 1'] + } + + void "Test a higher map keeps its leaf when a lower source repeats that descendant"() { + given: + def environment = new StandardEnvironment() + environment.propertySources.addLast(new MapPropertySource('low', [ + 'dataSource.properties.options': [enabled: false], + 'dataSource.properties.options.enabled': false + ])) + environment.propertySources.addFirst(new PropertySource('high') { + @Override + Object getProperty(String name) { + return name == 'dataSource.properties.options' ? [enabled: true] : null + } + }) + + expect: + new MapConfigurationBuilder(environment).build().properties.options == [enabled: true] + } + + void "Test a higher map leaf is not replaced by a lower map or unresolved placeholder"() { + given: + def environment = new StandardEnvironment() + environment.propertySources.addLast(new MapPropertySource('low', [ + 'dataSource.properties.options': [enabled: false], + 'dataSource.properties.options.enabled': '${missing}', + 'dataSource.properties.pool': [size: 1], + 'dataSource.properties.pool.size': [nested: 'LOW'] + ])) + environment.propertySources.addFirst(new PropertySource('high') { + @Override + Object getProperty(String name) { + if (name == 'dataSource.properties.options') { + return [enabled: true] + } + if (name == 'dataSource.properties.pool') { + return [size: 4] + } + return null + } + }) + + when: + def properties = new MapConfigurationBuilder(environment).build().properties + + then: + properties.options == [enabled: true] + properties.pool == [size: 4] + } + + void "Test a later parent map still contributes keys that a flattened child did not set"() { + given: + def environment = new StandardEnvironment() + environment.propertySources.addFirst(new MapPropertySource('config', [ + 'dataSource.properties.dataSourceProperties.reWriteBatchedInserts': true, + 'dataSource.properties.dataSourceProperties': [prepareThreshold: 0] + ])) + + expect: + new MapConfigurationBuilder(environment).build().properties.dataSourceProperties == [ + reWriteBatchedInserts: true, + prepareThreshold: 0 + ] + } + + void "Test a higher literal dotted key blocks a lower flattened alias"() { + given: + def environment = new StandardEnvironment() + environment.propertySources.addLast(new MapPropertySource('low', [ + 'dataSource.properties.options': ['oracle.jdbc.flag': 'LOW'], + 'dataSource.properties.options.oracle.jdbc.flag': '${missing}' + ])) + environment.propertySources.addFirst(new PropertySource('high') { + @Override + Object getProperty(String name) { + return name == 'dataSource.properties.options' ? ['oracle.jdbc.flag': 'HIGH'] : null + } + }) + + expect: + new MapConfigurationBuilder(environment).build().properties.options == ['oracle.jdbc.flag': 'HIGH'] + } + + void "Test an explicit empty nested map blocks a lower value"() { + given: + def environment = new StandardEnvironment() + environment.propertySources.addLast(new MapPropertySource('low', [ + 'dataSource.properties.options.nested': 'LOW' + ])) + environment.propertySources.addFirst(new MapPropertySource('high', [ + 'dataSource.properties.options': [nested: [:]] + ])) + + expect: + new MapConfigurationBuilder(environment).build().properties.options == [nested: [:]] + } + + void "Test an explicit empty nested map blocks a lower unresolved placeholder"() { + given: + def environment = new StandardEnvironment() + environment.propertySources.addLast(new MapPropertySource('low', [ + 'dataSource.properties.options.nested': '${missing}', + 'dataSource.properties.options.nested.child': 'kept', + 'dataSource.properties.options.other': 'kept', + 'dataSource.properties.options.added': [child: 'kept'] + ])) + environment.propertySources.addFirst(new MapPropertySource('high', [ + 'dataSource.properties.options': [nested: [:]] + ])) + + expect: + new MapConfigurationBuilder(environment).build().properties.options == [ + nested: [child: 'kept'], + other: 'kept', + added: [child: 'kept'] + ] + } + + void "Test an empty map under a literal dotted key blocks a lower unresolved placeholder"() { + given: + def environment = new StandardEnvironment() + environment.propertySources.addLast(new MapPropertySource('low', [ + 'dataSource.properties.options.dbms.memory.nested': '${missing}' + ])) + environment.propertySources.addFirst(new MapPropertySource('high', [ + 'dataSource.properties.options': ['dbms.memory': [nested: [:]]] + ])) + + expect: + new MapConfigurationBuilder(environment).build().properties.options == ['dbms.memory': [nested: [:]]] + } + + void "Test a lower map keeps a missing sibling and drops a scalar shadowed by a literal dotted key"() { + given: + def environment = new StandardEnvironment() + environment.propertySources.addLast(new MapPropertySource('low', [ + 'dataSource.properties.options.dbms.memory': [nested: 'LOW', kept: 'kept'] + ])) + environment.propertySources.addFirst(new MapPropertySource('high', [ + 'dataSource.properties.options': ['dbms.memory': [nested: [:]]] + ])) + + expect: + new MapConfigurationBuilder(environment).build().properties.options == [ + 'dbms.memory': [nested: [:]], + dbms: [memory: [kept: 'kept']] + ] + } + + void "Test a converted string map keeps its values over a lower flattened property"() { + given: + def environment = new StandardEnvironment() + environment.conversionService.addConverter(String, Map) { String source -> + if (source == 'pool:50') { + return [maximumPoolSize: 50] + } + throw new ConverterNotFoundException(TypeDescriptor.valueOf(String), TypeDescriptor.valueOf(Map)) + } + environment.propertySources.addLast(new MapPropertySource('low', [ + 'dataSource.properties.maximumPoolSize': 10, + 'dataSource.properties.minimumIdle': 2 + ])) + environment.propertySources.addFirst(new MapPropertySource('high', [ + 'dataSource.properties': 'pool:50' + ])) + + expect: + new MapConfigurationBuilder(environment).build().properties == [maximumPoolSize: 50, minimumIdle: 2] + } + + void "Test a placeholder string map keeps its resolved values over a lower flattened property"() { + given: + def environment = new StandardEnvironment() + environment.conversionService.addConverter(String, Map) { String source -> + if (source == 'pool:50') { + return [maximumPoolSize: 50] + } + throw new ConverterNotFoundException(TypeDescriptor.valueOf(String), TypeDescriptor.valueOf(Map)) + } + environment.propertySources.addLast(new MapPropertySource('low', [ + 'dataSource.properties.maximumPoolSize': 10, + 'dataSource.properties.minimumIdle': 2 + ])) + environment.propertySources.addFirst(new MapPropertySource('high', [ + 'dataSource.properties': '${pool}', + pool: 'pool:50' + ])) + + expect: + new MapConfigurationBuilder(environment).build().properties == [maximumPoolSize: 50, minimumIdle: 2] + } + + void "Test a literal dotted list shadows a lower indexed placeholder"() { + given: + def environment = new StandardEnvironment() + environment.propertySources.addLast(new MapPropertySource('low', [ + 'dataSource.properties.a.b[0]': '${missing}' + ])) + environment.propertySources.addFirst(new MapPropertySource('high', [ + 'dataSource.properties': ['a.b': ['HIGH']] + ])) + + expect: + new MapConfigurationBuilder(environment).build().properties == ['a.b': ['HIGH']] + } + + void "Test a later nested map keeps a same-source literal key and its missing siblings"() { + given: + def environment = new StandardEnvironment() + environment.propertySources.addFirst(new MapPropertySource('config', [ + 'dataSource.properties.dataSourceProperties': ['oracle.jdbc.flag': 'true'], + 'dataSource.properties.dataSourceProperties.oracle.jdbc': [flag: 'false', other: 'kept'] + ])) + + when: + def properties = new MapConfigurationBuilder(environment).build().properties.dataSourceProperties + + then: + properties['oracle.jdbc.flag'] == 'true' + properties.oracle.jdbc.other == 'kept' + properties.oracle.jdbc.flag == null + } + + void "Test a lower circular placeholder string does not replace a higher map"() { + given: + def environment = new StandardEnvironment() + environment.propertySources.addLast(new MapPropertySource('low', [ + 'dataSource.properties': '${loop}', + loop: '${loop}' + ])) + environment.propertySources.addFirst(new MapPropertySource('high', [ + 'dataSource.properties': [x: 50] + ])) + + expect: + new MapConfigurationBuilder(environment).build().properties == [x: 50] + } + + void "Test Spring Environment rebuilds map-backed builder settings with source precedence"() { + given: + def environment = new StandardEnvironment() + environment.propertySources.addFirst(new MapPropertySource('low', [ + 'grails.gorm.hibernate.flush.mode.nested': 'low', + 'grails.gorm.hibernate.options': [size: 20, inherited: true], + 'grails.gorm.hibernate.local': 'replaced by nested map', + 'grails.gorm.hibernate.local.enabled': true + ])) + environment.propertySources.addFirst(new MapPropertySource('high', [ + 'grails.gorm.hibernate.configClass': 'configured', + 'grails.gorm.hibernate.flush.mode': 'HIGH', + 'grails.gorm.hibernate.options.size': 50, + 'grails.gorm.hibernate.connectionInitSqls[1]': 'SET B=2', + 'grails.gorm.hibernate.connectionInitSqls[0]': 'SET A=1' + ])) + + when: + def configuration = new SpringMapConfigurationBuilder(environment).build() + + then: + configuration.hibernate.getConfigClass() == 'configured' + configuration.hibernate['flush'] == [mode: 'HIGH'] + configuration.hibernate['options'] == [size: 50, inherited: true] + configuration.hibernate['local'] == [enabled: true] + configuration.hibernate['connectionInitSqls'] == ['SET A=1', 'SET B=2'] + } + + void "Test DatastoreUtils resolver retains existing map-backed builder binding"() { + given: + def source = [ + 'grails.gorm.hibernate.configClass': 'configured', + 'grails.gorm.hibernate.flush.mode': 'AUTO', + 'grails.gorm.hibernate.connectionInitSqls': ['SET A=1', 'SET B=2'] + ] + def resolver = DatastoreUtils.createPropertyResolver(source) + def original = new LinkedHashMap(resolver.getProperty('grails.gorm.hibernate', Map)) + + when: + def configuration = new SpringMapConfigurationBuilder(resolver).build() + + then: + configuration.hibernate.getConfigClass() == 'configured' + configuration.hibernate['flush.mode'] == 'AUTO' + configuration.hibernate['connectionInitSqls'] == ['SET A=1', 'SET B=2'] + resolver.getProperty('grails.gorm.hibernate', Map) == original + source.size() == 3 + } + void "Test configuration builder with getter and setter"() { given:"A configuration" def map = [ @@ -560,6 +1018,58 @@ class ConfigurationBuilderSpec extends Specification { configuration.mapBacked.keySet().every { it instanceof String } } + static class MapConfigurationBuilder extends ConfigurationBuilder { + + MapConfigurationBuilder(PropertyResolver propertyResolver) { + super(propertyResolver, 'dataSource') + } + + @Override + protected MapConfiguration createBuilder() { + return new MapConfiguration() + } + + @Override + protected MapConfiguration toConfiguration(MapConfiguration builder) { + return builder + } + } + + @Builder(builderStrategy = SimpleStrategy, prefix = '') + static class MapConfiguration { + + Map properties + } + + static class SpringMapConfigurationBuilder extends ConfigurationBuilder { + + SpringMapConfigurationBuilder(PropertyResolver propertyResolver) { + super(propertyResolver, Settings.PREFIX) + } + + @Override + protected SpringMapConfig createBuilder() { + return new SpringMapConfig() + } + + @Override + protected SpringMapConfig toConfiguration(SpringMapConfig builder) { + return builder + } + } + + @Builder(builderStrategy = SimpleStrategy, prefix = '') + static class SpringMapConfig { + + SpringMapSettings hibernate + } + + @Builder(builderStrategy = SimpleStrategy, prefix = '') + static class SpringMapSettings extends LinkedHashMap { + + String configClass + } + static class TestConfigurationBuilder extends ConfigurationBuilder { TestConfigurationBuilder(PropertyResolver propertyResolver) { @@ -771,3 +1281,4 @@ class ConfigurationBuilderSpec extends Specification { } } } + From e204f2ec2d083284895afc8adc41a457c4352608 Mon Sep 17 00:00:00 2001 From: James Fredley Date: Thu, 29 Oct 2026 16:59:18 -0400 Subject: [PATCH 005/115] Keep lower configuration maps when ignoring JVM property sources A DatastoreEnvironment still merges prepared sources, so a key that exists only on a lower map is kept. The walk skips the systemProperties and systemEnvironment sources StandardEnvironment adds. Assisted-by: sisyphus-cursor-grok:grok-4.7 --- .../config/ConfigurationBuilder.groovy | 10 +++++ .../config/ConfigurationBuilderSpec.groovy | 43 +++++++++++++++++++ 2 files changed, 53 insertions(+) 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 51c914350b5..b67f9143b35 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 @@ -37,6 +37,7 @@ import org.springframework.util.PlaceholderResolutionException import org.springframework.core.env.EnumerablePropertySource import org.springframework.core.env.PropertyResolver import org.springframework.core.env.PropertySource +import org.springframework.core.env.StandardEnvironment import org.springframework.util.ClassUtils import org.springframework.util.ReflectionUtils @@ -448,6 +449,15 @@ abstract class ConfigurationBuilder { String prefix = propertyPath + '.' Map result = new LinkedHashMap<>() for (PropertySource propertySource : environment.propertySources) { + // StandardEnvironment also exposes JVM system properties. They are not + // prepared configuration. A lower configuration map still has to merge. + if (environment instanceof DatastoreEnvironment) { + String sourceName = propertySource.getName() + if (sourceName == StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME || + sourceName == StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME) { + continue + } + } Object sourceValue = propertySource.getProperty(propertyPath) Map sourceMap = new LinkedHashMap<>() if (sourceValue instanceof Map && !((Map) sourceValue).isEmpty()) { diff --git a/grails-datastore-core/src/test/groovy/org/grails/datastore/mapping/config/ConfigurationBuilderSpec.groovy b/grails-datastore-core/src/test/groovy/org/grails/datastore/mapping/config/ConfigurationBuilderSpec.groovy index d7af8e85c57..7cb891a75a7 100644 --- a/grails-datastore-core/src/test/groovy/org/grails/datastore/mapping/config/ConfigurationBuilderSpec.groovy +++ b/grails-datastore-core/src/test/groovy/org/grails/datastore/mapping/config/ConfigurationBuilderSpec.groovy @@ -484,6 +484,8 @@ class ConfigurationBuilderSpec extends Specification { void "Test DatastoreUtils resolver retains existing map-backed builder binding"() { given: + String systemKey = 'grails.gorm.hibernate.minimumIdle' + String previousIdle = System.setProperty(systemKey, '3') def source = [ 'grails.gorm.hibernate.configClass': 'configured', 'grails.gorm.hibernate.flush.mode': 'AUTO', @@ -499,8 +501,49 @@ class ConfigurationBuilderSpec extends Specification { configuration.hibernate.getConfigClass() == 'configured' configuration.hibernate['flush.mode'] == 'AUTO' configuration.hibernate['connectionInitSqls'] == ['SET A=1', 'SET B=2'] + configuration.hibernate['minimumIdle'] == null resolver.getProperty('grails.gorm.hibernate', Map) == original source.size() == 3 + + cleanup: + if (previousIdle == null) { + System.clearProperty(systemKey) + } + else { + System.setProperty(systemKey, previousIdle) + } + } + + void "Test DatastoreUtils resolver keeps keys from a lower configuration map"() { + given: + String systemKey = 'grails.gorm.hibernate.minimumIdle' + String previousIdle = System.setProperty(systemKey, '3') + def low = [ + 'grails.gorm.hibernate.shared': 'low', + 'grails.gorm.hibernate.base': 'retained' + ] + def high = [ + 'grails.gorm.hibernate.shared': 'high', + 'grails.gorm.hibernate.override': 'new' + ] + def resolver = DatastoreUtils.createPropertyResolvers(low, high) + + when: + def configuration = new SpringMapConfigurationBuilder(resolver).build() + + then: + configuration.hibernate['shared'] == 'high' + configuration.hibernate['override'] == 'new' + configuration.hibernate['base'] == 'retained' + configuration.hibernate['minimumIdle'] == null + + cleanup: + if (previousIdle == null) { + System.clearProperty(systemKey) + } + else { + System.setProperty(systemKey, previousIdle) + } } void "Test configuration builder with getter and setter"() { From 0c43b8a15cff1994efb042ed9a5b1f76da69ec16 Mon Sep 17 00:00:00 2001 From: James Fredley Date: Fri, 30 Oct 2026 15:14:28 -0400 Subject: [PATCH 006/115] Limit Environment map binding to flattened names A raw Spring Environment binds a configuration map from flattened names only. A map stored at the exact path is used only when no flattened name exists. DatastoreEnvironment still returns its prepared map and does not walk JVM or lower configuration sources. Assisted-by: Sisyphus:grok-4.7 --- ...taSourceConnectionSourceFactorySpec.groovy | 159 ++----- .../config/ConfigurationBuilder.groovy | 445 +++++------------- .../config/ConfigurationBuilderSpec.groovy | 397 +++------------- 3 files changed, 209 insertions(+), 792 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 22f184b8c6a..843750b726f 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 @@ -154,15 +154,14 @@ class DataSourceConnectionSourceFactorySpec extends Specification { connectionSource?.close() } - void "create merges a lower direct map behind higher flattened pool properties"() { + void "create does not merge a direct properties map when a flattened descendant exists"() { given: def factory = new DataSourceConnectionSourceFactory() def environment = new StandardEnvironment() - def lowerProperties = [maximumPoolSize: 20, minimumIdle: 2] environment.propertySources.addFirst(new MapPropertySource('low', [ - 'dataSource.url': 'jdbc:h2:mem:factoryDirectMapPrecedenceTest', + 'dataSource.url': 'jdbc:h2:mem:factoryUnmergedMapTest', 'dataSource.type': HikariDataSource.name, - 'dataSource.properties': lowerProperties + 'dataSource.properties': [maximumPoolSize: 20, minimumIdle: 2] ])) environment.propertySources.addFirst(new MapPropertySource('high', [ 'dataSource.properties.maximumPoolSize': 50 @@ -172,11 +171,8 @@ class DataSourceConnectionSourceFactorySpec extends Specification { def connectionSource = factory.create(ConnectionSource.DEFAULT, environment) then: - connectionSource.settings.properties == [maximumPoolSize: 50, minimumIdle: 2] - def dataSource = unwrap(connectionSource.source) - dataSource.maximumPoolSize == 50 - dataSource.minimumIdle == 2 - lowerProperties == [maximumPoolSize: 20, minimumIdle: 2] + connectionSource.settings.properties == [maximumPoolSize: 50] + unwrap(connectionSource.source).maximumPoolSize == 50 cleanup: connectionSource?.close() @@ -230,31 +226,6 @@ class DataSourceConnectionSourceFactorySpec extends Specification { 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] - unwrap(connectionSource.source).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() @@ -272,7 +243,6 @@ class DataSourceConnectionSourceFactorySpec extends Specification { '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 @@ -304,138 +274,79 @@ class DataSourceConnectionSourceFactorySpec extends Specification { 'not a map' | true } - void "create binds a map-valued descendant without forcing it to a string"() { + void "create binds flattened pool size when the exact properties path is an unresolved placeholder"() { given: def factory = new DataSourceConnectionSourceFactory() def environment = new StandardEnvironment() environment.propertySources.addFirst(new MapPropertySource('application', [ - 'dataSource.url': 'jdbc:h2:mem:factoryMapDescendantTest', + 'dataSource.url': 'jdbc:h2:mem:factoryMissingExactPath', 'dataSource.type': HikariDataSource.name, - 'dataSource.properties.maximumPoolSize': 16, - 'dataSource.properties.dataSourceProperties': [connectionCollation: 'utf8mb4_unicode_ci', empty: ''] + 'dataSource.properties': '${missing}', + 'dataSource.properties.maximumPoolSize': 16 ])) 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 = unwrap(connectionSource.source) - dataSource instanceof HikariDataSource - dataSource.maximumPoolSize == 16 - dataSource.dataSourceProperties.getProperty('connectionCollation') == 'utf8mb4_unicode_ci' - dataSource.dataSourceProperties.getProperty('empty') == '' + connectionSource.settings.properties == [maximumPoolSize: 16] + unwrap(connectionSource.source).maximumPoolSize == 16 cleanup: connectionSource?.close() } - void "create keeps a higher precedence map entry over a lower flattened descendant"() { + void "create does not invoke a string to map converter when a flattened descendant exists"() { 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 = unwrap(connectionSource.source) - 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 = unwrap(connectionSource.source) - dataSource instanceof HikariDataSource - dataSource.dataSourceProperties.getProperty('oracle.jdbc.sendBooleanAsNativeBoolean') == 'true' - !dataSource.dataSourceProperties.containsKey('oracle') - - cleanup: - connectionSource?.close() - } - - void "create keeps a higher precedence dotted map key over a lower flattened JDBC property"() { - given: - def factory = new DataSourceConnectionSourceFactory() - def environment = new StandardEnvironment() - environment.propertySources.addLast(new MapPropertySource('low', [ - 'dataSource.url': 'jdbc:h2:mem:factoryLiteralKeyPrecedenceTest', + def invoked = [] + environment.conversionService = new DefaultConversionService() + environment.conversionService.addConverter(String, Map, { String value -> + invoked.add(value) + throw new IllegalArgumentException('Cannot convert value to a map') + } as Converter) + environment.propertySources.addFirst(new MapPropertySource('application', [ + 'dataSource.url': 'jdbc:h2:mem:factoryConverterSkipped', 'dataSource.type': HikariDataSource.name, - 'dataSource.properties.dataSourceProperties.oracle.jdbc.sendBooleanAsNativeBoolean': false - ])) - environment.propertySources.addFirst(new MapPropertySource('high', [ - 'dataSource.properties.dataSourceProperties': ['oracle.jdbc.sendBooleanAsNativeBoolean': 'true'] + 'dataSource.properties': 'not a map', + 'dataSource.properties.maximumPoolSize': 16 ])) when: def connectionSource = factory.create(ConnectionSource.DEFAULT, environment) then: - connectionSource.settings.properties.dataSourceProperties == ['oracle.jdbc.sendBooleanAsNativeBoolean': 'true'] - def dataSource = unwrap(connectionSource.source) - dataSource instanceof HikariDataSource - dataSource.dataSourceProperties.getProperty('oracle.jdbc.sendBooleanAsNativeBoolean') == 'true' - !dataSource.dataSourceProperties.containsKey('oracle') + invoked.empty + connectionSource.settings.properties == [maximumPoolSize: 16] + unwrap(connectionSource.source).maximumPoolSize == 16 cleanup: connectionSource?.close() } - void "create keeps a higher literal JDBC key over a lower nested map"() { + void "create binds a map-valued descendant without forcing it to a string"() { given: def factory = new DataSourceConnectionSourceFactory() def environment = new StandardEnvironment() - environment.propertySources.addLast(new MapPropertySource('low', [ - 'dataSource.url': 'jdbc:h2:mem:factoryNestedMapPrecedenceTest', + environment.propertySources.addFirst(new MapPropertySource('application', [ + 'dataSource.url': 'jdbc:h2:mem:factoryMapDescendantTest', 'dataSource.type': HikariDataSource.name, - 'dataSource.properties.dataSourceProperties.oracle': [jdbc: [sendBooleanAsNativeBoolean: false], pool: 'kept'] - ])) - environment.propertySources.addFirst(new MapPropertySource('high', [ - 'dataSource.properties.dataSourceProperties': ['oracle.jdbc.sendBooleanAsNativeBoolean': 'true'] + 'dataSource.properties.maximumPoolSize': 16, + 'dataSource.properties.dataSourceProperties': [connectionCollation: 'utf8mb4_unicode_ci', empty: ''] ])) when: def connectionSource = factory.create(ConnectionSource.DEFAULT, environment) then: - connectionSource.settings.properties.dataSourceProperties['oracle.jdbc.sendBooleanAsNativeBoolean'] == 'true' - connectionSource.settings.properties.dataSourceProperties.oracle.pool == 'kept' - connectionSource.settings.properties.dataSourceProperties.oracle.jdbc == null + connectionSource.settings.properties.maximumPoolSize == 16 + connectionSource.settings.properties.dataSourceProperties == [connectionCollation: 'utf8mb4_unicode_ci', empty: ''] def dataSource = unwrap(connectionSource.source) dataSource instanceof HikariDataSource - dataSource.dataSourceProperties.getProperty('oracle.jdbc.sendBooleanAsNativeBoolean') == 'true' - dataSource.dataSourceProperties.getProperty('oracle.pool') == 'kept' + dataSource.maximumPoolSize == 16 + dataSource.dataSourceProperties.getProperty('connectionCollation') == 'utf8mb4_unicode_ci' + dataSource.dataSourceProperties.getProperty('empty') == '' cleanup: connectionSource?.close() 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 b67f9143b35..5150acd9870 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 @@ -33,11 +33,9 @@ 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.util.PlaceholderResolutionException import org.springframework.core.env.EnumerablePropertySource import org.springframework.core.env.PropertyResolver import org.springframework.core.env.PropertySource -import org.springframework.core.env.StandardEnvironment import org.springframework.util.ClassUtils import org.springframework.util.ReflectionUtils @@ -433,113 +431,51 @@ abstract class ConfigurationBuilder { } 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 (!(propertyResolver instanceof ConfigurableEnvironment) && directValue != null && !directValue.isEmpty()) { - return directValue - } - if (propertyResolver instanceof ConfigurableEnvironment) { - ConfigurableEnvironment environment = (ConfigurableEnvironment) propertyResolver - String prefix = propertyPath + '.' - Map result = new LinkedHashMap<>() - for (PropertySource propertySource : environment.propertySources) { - // StandardEnvironment also exposes JVM system properties. They are not - // prepared configuration. A lower configuration map still has to merge. - if (environment instanceof DatastoreEnvironment) { - String sourceName = propertySource.getName() - if (sourceName == StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME || - sourceName == StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME) { - continue - } - } - Object sourceValue = propertySource.getProperty(propertyPath) - Map sourceMap = new LinkedHashMap<>() - if (sourceValue instanceof Map && !((Map) sourceValue).isEmpty()) { - sourceMap = copyConfigMap((Map) sourceValue) + if (propertyResolver instanceof DatastoreEnvironment || !(propertyResolver instanceof ConfigurableEnvironment)) { + return readDirectMap(propertyPath, fallBackValue) + } + ConfigurableEnvironment environment = (ConfigurableEnvironment) propertyResolver + String prefix = propertyPath + '.' + Map result = new LinkedHashMap<>() + Set> containers = Collections.newSetFromMap(new IdentityHashMap, Boolean>()) + Set> builtLists = Collections.newSetFromMap(new IdentityHashMap, Boolean>()) + containers.add(result) + boolean stored = false + for (PropertySource propertySource : environment.propertySources) { + if (!(propertySource instanceof EnumerablePropertySource)) { + continue + } + for (String name : ((EnumerablePropertySource) propertySource).propertyNames) { + if (name == null || !name.startsWith(prefix)) { + continue } - else if (propertySource instanceof EnumerablePropertySource) { - for (String name : ((EnumerablePropertySource) propertySource).propertyNames) { - String relativeName = name.startsWith(prefix) ? name.substring(prefix.length()) : null - if (relativeName == null || hasResolvedValue(result, relativeName) || leafAlreadySet(sourceMap, relativeName)) { - continue - } - // An exact path already stored, including an empty map, wins over a lower - // scalar. Skip that scalar before placeholder resolution. A lower map still - // merges, so its missing children are kept. - if ((exactPathPresent(result, relativeName) || indexedLiteralClaimed(result, relativeName)) && !firstRawValueIsMap(environment, name)) { - continue - } - Object value = readEnvironmentValue(environment, name) - if (value == null) { - continue - } - if (value instanceof Map) { - Map copied = copyConfigMap((Map) value) - Map filtered = withoutSourceAliases(sourceMap, relativeName, copied) - if (filtered.isEmpty() && !copied.isEmpty()) { - continue - } - value = filtered - } - String[] segments = name.substring(prefix.length()).split('\\.') - Map current = sourceMap - 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() - current.put(segment, child) - } - current = (Map) child - } - String leaf = segments[segments.length - 1] - int bracket = leaf.lastIndexOf('[') - if (bracket > 0 && leaf.endsWith(']') && leaf.substring(bracket + 1, leaf.length() - 1).matches('\\d+')) { - int index = Integer.parseInt(leaf.substring(bracket + 1, leaf.length() - 1)) - String listName = leaf.substring(0, bracket) - Object existing = current.get(listName) - List values = existing instanceof List ? new ArrayList((List) existing) : new ArrayList() - while (values.size() <= index) { - values.add(null) - } - values.set(index, value) - current.put(listName, values) - } - else if (value instanceof Map) { - Map copied = copyConfigMap((Map) value) - Object existing = current.get(leaf) - if (existing instanceof Map) { - mergeMissing((Map) existing, copied) - } - else { - current.put(leaf, copied) - } - } - else if (current.get(leaf) == null) { - current.put(leaf, value) - } - } + String relativeName = name.substring(prefix.length()) + if (relativeName.isEmpty() || pathBlocked(result, builtLists, relativeName)) { + continue } - if (sourceMap.isEmpty() && sourceValue != null && !(sourceValue instanceof Map)) { - Map converted = convertSourceMap(environment, sourceValue) - if (converted != null) { - sourceMap = converted - } + Object value = readEnvironmentValue(environment, name) + if (value == null) { + continue } - mergeMissing(result, sourceMap) - } - if (!result.isEmpty()) { - return result == directValue ? directValue : result + putFlattenedName(result, containers, builtLists, relativeName, value) + stored = true } } - if (conversionFailure != null) { - throw conversionFailure + if (!stored) { + // A map stored at the exact path is used only when no flattened name exists. + return readDirectMap(propertyPath, fallBackValue) + } + return result + } + + /** + * Read the exact path. ConversionFailedException and ConverterNotFoundException propagate + * from this call, which a raw environment reaches only after the walk stored nothing. + */ + private Map readDirectMap(String propertyPath, Map fallBackValue) { + Map directValue = propertyResolver.getProperty(propertyPath, Map) + if (directValue != null && !directValue.isEmpty()) { + return directValue } return directValue != null ? directValue : fallBackValue } @@ -563,263 +499,96 @@ abstract class ConfigurationBuilder { return null } - private static boolean hasResolvedValue(Map result, String name) { - for (Map.Entry entry : result.entrySet()) { - if (name == entry.key && !(entry.value instanceof Map)) { - return true - } - if (name.startsWith(entry.key + '.')) { - if (!(entry.value instanceof Map) || hasResolvedValue((Map) entry.value, name.substring(entry.key.length() + 1))) { - return true - } - } - } - int dot = name.indexOf('.') - String head = dot < 0 ? name : name.substring(0, dot) - int bracket = head.indexOf('[') - if (bracket > 0) { - head = head.substring(0, bracket) - } - if (!result.containsKey(head)) { - return false - } - Object value = result.get(head) - if (!(value instanceof Map)) { - return true - } - return dot >= 0 && hasResolvedValue((Map) value, name.substring(dot + 1)) - } - /** - * True when this enumeration already stored the named leaf. - * A map container is not a claim, so a later parent map can still add missing children. - * A scalar or list parent is not a claim either, so a later same-source nested key can replace it. + * True when this path is already stored, or an ancestor holds a non-map. + * A stored map is not a blocking ancestor of a missing leaf. + * Only a list this walk built from a numeric index accepts a later index. */ - private static boolean leafAlreadySet(Map source, String name) { - for (Map.Entry entry : source.entrySet()) { - if (name == entry.key) { - return !(entry.value instanceof Map) + private static boolean pathBlocked(Map result, Set> builtLists, String name) { + int bracket = indexedBracket(name) + String listPath = bracket > 0 ? name.substring(0, bracket) : name + String[] segments = listPath.split('\\.') + Map current = result + for (int i = 0; i < segments.length - 1; i++) { + if (!current.containsKey(segments[i])) { + return false } - if (entry.value instanceof Map && name.startsWith(entry.key + '.')) { - if (leafAlreadySet((Map) entry.value, name.substring(entry.key.length() + 1))) { - return true - } + Object child = current.get(segments[i]) + if (!(child instanceof Map)) { + return true } + current = (Map) child } - int dot = name.indexOf('.') - if (dot < 0 || name.substring(0, dot).indexOf('[') > 0) { + String leaf = segments[segments.length - 1] + if (!current.containsKey(leaf)) { return false } - Object child = source.get(name.substring(0, dot)) - if (!(child instanceof Map)) { - return false - } - return leafAlreadySet((Map) child, name.substring(dot + 1)) - } - - /** - * True when this exact path is already stored. An empty map counts. - * A literal dotted key such as {@code a.b} is a real prefix, not only the - * first segment. An index segment is not a claim, so a later entry can still - * update a list. - */ - private static boolean exactPathPresent(Map source, String name) { - if (source.get(name) != null) { + if (bracket < 0) { return true } - if (name.indexOf('[') < 0) { - for (Map.Entry entry : source.entrySet()) { - if (entry.value instanceof Map && name.startsWith(entry.key + '.')) { - if (exactPathPresent((Map) entry.value, name.substring(entry.key.length() + 1))) { - return true - } - } - } - } - int dot = name.indexOf('.') - if (dot < 0 || name.substring(0, dot).indexOf('[') > 0) { - return false - } - Object child = source.get(name.substring(0, dot)) - if (!(child instanceof Map)) { - return false - } - return exactPathPresent((Map) child, name.substring(dot + 1)) - } - - /** - * True when {@code name} is an index under a stored literal key whose value is not a map, - * such as {@code a.b[0]} when {@code a.b} already holds a list. A map value is not a claim. - */ - private static boolean indexedLiteralClaimed(Map source, String name) { - int bracket = name.indexOf('[') - if (bracket <= 0) { - return false + Object existing = current.get(leaf) + if (!(existing instanceof List) || !builtLists.contains((List) existing)) { + return true } - return nonMapLiteralPresent(source, name.substring(0, bracket)) + int index = Integer.parseInt(name.substring(bracket + 1, name.length() - 1)) + List values = (List) existing + return index < values.size() && values.get(index) != null } - /** - * Copy a map from the same source, omitting leaves already stored under a literal key. - * Missing siblings are kept. A scalar ancestor is not a claim. - */ - private static Map withoutSourceAliases(Map root, String prefix, Map extra) { - Map copy = new LinkedHashMap<>() - for (Map.Entry entry : extra.entrySet()) { - String path = prefix + '.' + entry.key - if (!(entry.value instanceof Map) && exactPathPresent(root, path)) { - continue + private static void putFlattenedName(Map result, Set> containers, + Set> builtLists, String name, Object value) { + int bracket = indexedBracket(name) + String listPath = bracket > 0 ? name.substring(0, bracket) : name + String[] segments = listPath.split('\\.') + Map current = result + for (int i = 0; i < segments.length - 1; i++) { + Object child = current.get(segments[i]) + if (child instanceof Map && !containers.contains((Map) child)) { + Map copied = new LinkedHashMap<>((Map) child) + containers.add(copied) + current.put(segments[i], copied) + child = copied } - if (entry.value instanceof Map) { - if (((Map) entry.value).isEmpty()) { - copy.put(entry.key, new LinkedHashMap()) - } - else { - Map nested = withoutSourceAliases(root, path, (Map) entry.value) - if (!nested.isEmpty()) { - copy.put(entry.key, nested) - } - } - } - else { - copy.put(entry.key, entry.value) - } - } - return copy - } - - private static boolean nonMapLiteralPresent(Map source, String name) { - Object direct = source.get(name) - if (direct != null && !(direct instanceof Map)) { - return true - } - for (Map.Entry entry : source.entrySet()) { - if (entry.value instanceof Map && name.startsWith(entry.key + '.')) { - if (nonMapLiteralPresent((Map) entry.value, name.substring(entry.key.length() + 1))) { - return true - } + if (!(child instanceof Map) || !containers.contains((Map) child)) { + Map created = new LinkedHashMap<>() + containers.add(created) + current.put(segments[i], created) + child = created } + current = (Map) child } - int dot = name.indexOf('.') - if (dot < 0) { - return false + String leaf = segments[segments.length - 1] + if (bracket < 0) { + current.put(leaf, value) + return } - Object child = source.get(name.substring(0, dot)) - if (!(child instanceof Map)) { - return false + int index = Integer.parseInt(name.substring(bracket + 1, name.length() - 1)) + Object existing = current.get(leaf) + List values + if (existing instanceof List && builtLists.contains((List) existing)) { + values = (List) existing } - return nonMapLiteralPresent((Map) child, name.substring(dot + 1)) - } - - /** - * A raw value that is not itself a map can still convert to one, for example a - * higher-precedence string the environment's conversion service understands. - * A string is resolved with the environment's placeholder settings first, so a - * value such as {@code pool} referenced by a placeholder converts as that text. - * Unresolvable placeholder text is left unchanged and fails conversion instead of - * failing the build: a lower source must not throw, and the winning value was - * already resolved by {@code getProperty}. Dotted keys on that same source are - * used instead when enumeration already found some. - */ - private static Map convertSourceMap(ConfigurableEnvironment environment, Object sourceValue) { - Object convertible = sourceValue - if (convertible instanceof String) { - try { - convertible = environment.resolvePlaceholders((String) convertible) - } catch (PlaceholderResolutionException ignored) { - return null - } - } - try { - Map converted = environment.getConversionService().convert(convertible, Map) - if (converted == null || converted.isEmpty()) { - return null - } - return copyConfigMap(converted) - } catch (ConversionFailedException | ConverterNotFoundException ignored) { - return null - } - } - - /** - * Look at the first raw value only. Placeholder resolution is intentionally not done here. - */ - private static boolean firstRawValueIsMap(ConfigurableEnvironment environment, String name) { - for (PropertySource propertySource : environment.propertySources) { - Object value = propertySource.getProperty(name) - if (value != null) { - return value instanceof Map - } + else { + values = new ArrayList<>() + builtLists.add(values) } - return false - } - - private static Map copyConfigMap(Map source) { - Map 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 - copy.put(entry.key.toString(), value) + while (values.size() <= index) { + values.add(null) } - return copy - } - - private static void mergeMissing(Map target, Map extra) { - mergeMissing(target, extra, target, '') + values.set(index, value) + current.put(leaf, values) } - private static void mergeMissing(Map root, Map extra, Map target, String prefix) { - for (Map.Entry entry : extra.entrySet()) { - String path = prefix ? prefix + '.' + entry.key : entry.key - Object existing = target.get(entry.key) - if (existing instanceof Map && entry.value instanceof Map) { - mergeMissing(root, (Map) entry.value, (Map) existing, path) - } - else if (!target.containsKey(entry.key) && !hasResolvedValue(root, path)) { - Object value = entry.value - if (!(value instanceof Map) && exactPathPresent(root, path)) { - continue - } - if (value instanceof Map) { - value = withoutResolvedDescendants(root, path, (Map) value) - if (((Map) value).isEmpty() && !((Map) entry.value).isEmpty()) { - continue - } - } - target.put(entry.key, value) - } + private static int indexedBracket(String name) { + int bracket = name.lastIndexOf('[') + if (bracket <= 0 || !name.endsWith(']')) { + return -1 } - } - - /** - * Copy a lower-precedence map, omitting leaves already supplied by a higher literal key. - * Stored keys are not split or rewritten. - */ - private static Map withoutResolvedDescendants(Map root, String prefix, Map extra) { - Map copy = new LinkedHashMap<>() - for (Map.Entry entry : extra.entrySet()) { - String path = prefix + '.' + entry.key - if (hasResolvedValue(root, path) || (!(entry.value instanceof Map) && exactPathPresent(root, path))) { - continue - } - if (entry.value instanceof Map) { - if (((Map) entry.value).isEmpty()) { - copy.put(entry.key, new LinkedHashMap()) - } - else { - Map nested = withoutResolvedDescendants(root, path, (Map) entry.value) - if (!nested.isEmpty()) { - copy.put(entry.key, nested) - } - } - } - else { - copy.put(entry.key, entry.value) - } + if (name.lastIndexOf('.') > bracket) { + return -1 } - return copy + String indexText = name.substring(bracket + 1, name.length() - 1) + return indexText.matches('\\d+') ? bracket : -1 } protected Object newChildBuilderForFallback(Object childBuilder, Object fallbackConfig) { diff --git a/grails-datastore-core/src/test/groovy/org/grails/datastore/mapping/config/ConfigurationBuilderSpec.groovy b/grails-datastore-core/src/test/groovy/org/grails/datastore/mapping/config/ConfigurationBuilderSpec.groovy index 7cb891a75a7..71e3cf5d0f9 100644 --- a/grails-datastore-core/src/test/groovy/org/grails/datastore/mapping/config/ConfigurationBuilderSpec.groovy +++ b/grails-datastore-core/src/test/groovy/org/grails/datastore/mapping/config/ConfigurationBuilderSpec.groovy @@ -89,7 +89,7 @@ class ConfigurationBuilderSpec extends Specification { then: configuration.properties == [maximumPoolSize: '50', options: [enabled: true], connectionInitSqls: ['SET A=1', 'SET B=2']] configuration.properties.connectionInitSqls.is(values.connectionInitSqls) - !configuration.properties.options.is(values.options) + configuration.properties.options.is(values.options) values.maximumPoolSize == '${pool.size}' } @@ -104,14 +104,13 @@ class ConfigurationBuilderSpec extends Specification { when: def properties = new MapConfigurationBuilder(environment).build().properties - def copied = nested ? properties.options : properties + def stored = nested ? properties.options : properties then: - copied == options - copied['dbms.memory.pagecache.size'] == '512M' - copied.nested == ['dbms.memory.heap.max_size': '1G'] + stored == options + stored['dbms.memory.pagecache.size'] == '512M' + stored.nested == ['dbms.memory.heap.max_size': '1G'] options == ['dbms.memory.pagecache.size': '512M', nested: ['dbms.memory.heap.max_size': '1G']] - !nested || (!copied.is(options) && !copied.nested.is(options.nested)) where: propertyPath | nested @@ -119,70 +118,6 @@ class ConfigurationBuilderSpec extends Specification { 'dataSource.properties.options' | true } - void "Test lower literal map keys fill only paths missing from higher flattened properties"() { - given: - def environment = new StandardEnvironment() - def options = ['dbms.memory.pagecache.size': '512M', 'dbms.memory.heap.max_size': '2G'] - environment.propertySources.addFirst(new MapPropertySource('low', [ - 'dataSource.properties.options': options - ])) - environment.propertySources.addFirst(new MapPropertySource('high', [ - 'dataSource.properties.options.dbms.memory.pagecache.size': '1G' - ])) - - when: - def properties = new MapConfigurationBuilder(environment).build().properties - - then: - properties.options == [dbms: [memory: [pagecache: [size: '1G']]], 'dbms.memory.heap.max_size': '2G'] - options == ['dbms.memory.pagecache.size': '512M', 'dbms.memory.heap.max_size': '2G'] - } - - void "Test higher literal map keys resolve flattened descendants for #options"() { - given: - def environment = new StandardEnvironment() - environment.propertySources.addFirst(new MapPropertySource('low', [ - 'dataSource.properties.options.dbms.memory.pagecache.size': '512M' - ])) - environment.propertySources.addFirst(new MapPropertySource('high', [ - 'dataSource.properties.options': options - ])) - - expect: - new MapConfigurationBuilder(environment).build().properties.options == expected - - where: - options | expected - ['dbms.memory.pagecache.size': '1G'] | ['dbms.memory.pagecache.size': '1G'] - ['dbms.memory': '1G'] | ['dbms.memory': '1G'] - ['dbms.memory': [pagecache: [size: '1G']]] | ['dbms.memory': [pagecache: [size: '1G']]] - ['dbms.memory': [heap: [max_size: '2G']]] | ['dbms.memory': [heap: [max_size: '2G']], dbms: [memory: [pagecache: [size: '512M']]]] - ['dbms.mem': '1G'] | ['dbms.mem': '1G', dbms: [memory: [pagecache: [size: '512M']]]] - } - - void "Test a higher literal key blocks only the matching leaf of a lower map"() { - given: - def environment = new StandardEnvironment() - environment.propertySources.addFirst(new MapPropertySource('low', [ - 'dataSource.properties.options.oracle': [jdbc: [sendBooleanAsNativeBoolean: false], pool: 'kept'] - ])) - environment.propertySources.addFirst(new MapPropertySource('mid', [ - 'dataSource.properties.options.oracle.other': 'kept' - ])) - environment.propertySources.addFirst(new MapPropertySource('high', [ - 'dataSource.properties.options': ['oracle.jdbc.sendBooleanAsNativeBoolean': 'true'] - ])) - - when: - def options = new MapConfigurationBuilder(environment).build().properties.options - - then: - options['oracle.jdbc.sendBooleanAsNativeBoolean'] == 'true' - options.oracle.pool == 'kept' - options.oracle.other == 'kept' - options.oracle.jdbc == null - } - void "Test string placeholders follow the environment policy"() { given: def resolving = new StandardEnvironment() @@ -201,248 +136,110 @@ class ConfigurationBuilderSpec extends Specification { new MapConfigurationBuilder(lenient).build().properties.connectionInitSql == 'SELECT ${literal}' } - void "Test indexed entries copy an existing list instead of mutating it"() { + void "Test Spring Environment rebuilds map-backed builder settings with source precedence"() { given: - List original = List.of('SELECT 1') def environment = new StandardEnvironment() - environment.propertySources.addFirst(new MapPropertySource('config', [ - 'dataSource.properties.connectionInitSqls': original, - 'dataSource.properties.connectionInitSqls[0]': 'SELECT 2' + environment.propertySources.addFirst(new MapPropertySource('low', [ + 'grails.gorm.hibernate.flush.mode.nested': 'low', + 'grails.gorm.hibernate.options': [size: 20, inherited: true], + 'grails.gorm.hibernate.local': 'replaced by nested map', + 'grails.gorm.hibernate.local.enabled': true + ])) + environment.propertySources.addFirst(new MapPropertySource('high', [ + 'grails.gorm.hibernate.configClass': 'configured', + 'grails.gorm.hibernate.flush.mode': 'HIGH', + 'grails.gorm.hibernate.options.size': 50, + 'grails.gorm.hibernate.connectionInitSqls[1]': 'SET B=2', + 'grails.gorm.hibernate.connectionInitSqls[0]': 'SET A=1' ])) when: - def sqls = new MapConfigurationBuilder(environment).build().properties.connectionInitSqls + def configuration = new SpringMapConfigurationBuilder(environment).build() then: - sqls == ['SELECT 2'] - original == ['SELECT 1'] + configuration.hibernate.getConfigClass() == 'configured' + configuration.hibernate['flush'] == [mode: 'HIGH'] + configuration.hibernate['options'] == [size: 50] + configuration.hibernate['local'] == 'replaced by nested map' + configuration.hibernate['connectionInitSqls'] == ['SET A=1', 'SET B=2'] } - void "Test a higher map keeps its leaf when a lower source repeats that descendant"() { + void "Test a higher map value keeps a missing lower flattened leaf without mutating the source"() { given: + def options = [enabled: true] def environment = new StandardEnvironment() - environment.propertySources.addLast(new MapPropertySource('low', [ - 'dataSource.properties.options': [enabled: false], - 'dataSource.properties.options.enabled': false + environment.propertySources.addFirst(new MapPropertySource('low', [ + 'dataSource.properties.options.timeout': 1000 ])) - environment.propertySources.addFirst(new PropertySource('high') { - @Override - Object getProperty(String name) { - return name == 'dataSource.properties.options' ? [enabled: true] : null - } - }) - - expect: - new MapConfigurationBuilder(environment).build().properties.options == [enabled: true] - } - - void "Test a higher map leaf is not replaced by a lower map or unresolved placeholder"() { - given: - def environment = new StandardEnvironment() - environment.propertySources.addLast(new MapPropertySource('low', [ - 'dataSource.properties.options': [enabled: false], - 'dataSource.properties.options.enabled': '${missing}', - 'dataSource.properties.pool': [size: 1], - 'dataSource.properties.pool.size': [nested: 'LOW'] + environment.propertySources.addFirst(new MapPropertySource('high', [ + 'dataSource.properties.options': options ])) - environment.propertySources.addFirst(new PropertySource('high') { - @Override - Object getProperty(String name) { - if (name == 'dataSource.properties.options') { - return [enabled: true] - } - if (name == 'dataSource.properties.pool') { - return [size: 4] - } - return null - } - }) when: def properties = new MapConfigurationBuilder(environment).build().properties then: - properties.options == [enabled: true] - properties.pool == [size: 4] - } - - void "Test a later parent map still contributes keys that a flattened child did not set"() { - given: - def environment = new StandardEnvironment() - environment.propertySources.addFirst(new MapPropertySource('config', [ - 'dataSource.properties.dataSourceProperties.reWriteBatchedInserts': true, - 'dataSource.properties.dataSourceProperties': [prepareThreshold: 0] - ])) - - expect: - new MapConfigurationBuilder(environment).build().properties.dataSourceProperties == [ - reWriteBatchedInserts: true, - prepareThreshold: 0 - ] - } - - void "Test a higher literal dotted key blocks a lower flattened alias"() { - given: - def environment = new StandardEnvironment() - environment.propertySources.addLast(new MapPropertySource('low', [ - 'dataSource.properties.options': ['oracle.jdbc.flag': 'LOW'], - 'dataSource.properties.options.oracle.jdbc.flag': '${missing}' - ])) - environment.propertySources.addFirst(new PropertySource('high') { - @Override - Object getProperty(String name) { - return name == 'dataSource.properties.options' ? ['oracle.jdbc.flag': 'HIGH'] : null - } - }) - - expect: - new MapConfigurationBuilder(environment).build().properties.options == ['oracle.jdbc.flag': 'HIGH'] - } - - void "Test an explicit empty nested map blocks a lower value"() { - given: - def environment = new StandardEnvironment() - environment.propertySources.addLast(new MapPropertySource('low', [ - 'dataSource.properties.options.nested': 'LOW' - ])) - environment.propertySources.addFirst(new MapPropertySource('high', [ - 'dataSource.properties.options': [nested: [:]] - ])) - - expect: - new MapConfigurationBuilder(environment).build().properties.options == [nested: [:]] + properties.options == [enabled: true, timeout: 1000] + !properties.options.is(options) + options == [enabled: true] } - void "Test an explicit empty nested map blocks a lower unresolved placeholder"() { + void "Test a higher source list blocks a lower index and an unresolved placeholder"() { given: + def sqls = ['HIGH'] + def emptySqls = [] def environment = new StandardEnvironment() - environment.propertySources.addLast(new MapPropertySource('low', [ - 'dataSource.properties.options.nested': '${missing}', - 'dataSource.properties.options.nested.child': 'kept', - 'dataSource.properties.options.other': 'kept', - 'dataSource.properties.options.added': [child: 'kept'] - ])) - environment.propertySources.addFirst(new MapPropertySource('high', [ - 'dataSource.properties.options': [nested: [:]] - ])) - - expect: - new MapConfigurationBuilder(environment).build().properties.options == [ - nested: [child: 'kept'], - other: 'kept', - added: [child: 'kept'] - ] - } - - void "Test an empty map under a literal dotted key blocks a lower unresolved placeholder"() { - given: - def environment = new StandardEnvironment() - environment.propertySources.addLast(new MapPropertySource('low', [ - 'dataSource.properties.options.dbms.memory.nested': '${missing}' - ])) - environment.propertySources.addFirst(new MapPropertySource('high', [ - 'dataSource.properties.options': ['dbms.memory': [nested: [:]]] - ])) - - expect: - new MapConfigurationBuilder(environment).build().properties.options == ['dbms.memory': [nested: [:]]] - } - - void "Test a lower map keeps a missing sibling and drops a scalar shadowed by a literal dotted key"() { - given: - def environment = new StandardEnvironment() - environment.propertySources.addLast(new MapPropertySource('low', [ - 'dataSource.properties.options.dbms.memory': [nested: 'LOW', kept: 'kept'] - ])) - environment.propertySources.addFirst(new MapPropertySource('high', [ - 'dataSource.properties.options': ['dbms.memory': [nested: [:]]] - ])) - - expect: - new MapConfigurationBuilder(environment).build().properties.options == [ - 'dbms.memory': [nested: [:]], - dbms: [memory: [kept: 'kept']] - ] - } - - void "Test a converted string map keeps its values over a lower flattened property"() { - given: - def environment = new StandardEnvironment() - environment.conversionService.addConverter(String, Map) { String source -> - if (source == 'pool:50') { - return [maximumPoolSize: 50] - } - throw new ConverterNotFoundException(TypeDescriptor.valueOf(String), TypeDescriptor.valueOf(Map)) - } - environment.propertySources.addLast(new MapPropertySource('low', [ - 'dataSource.properties.maximumPoolSize': 10, - 'dataSource.properties.minimumIdle': 2 + environment.propertySources.addFirst(new MapPropertySource('low', [ + 'dataSource.properties.sqls[1]': 'LOW', + 'dataSource.properties.emptySqls[1]': '${missing}' ])) environment.propertySources.addFirst(new MapPropertySource('high', [ - 'dataSource.properties': 'pool:50' + 'dataSource.properties.sqls': sqls, + 'dataSource.properties.emptySqls': emptySqls, + 'dataSource.properties.built[1]': 'SET B=2', + 'dataSource.properties.built[0]': 'SET A=1' ])) - expect: - new MapConfigurationBuilder(environment).build().properties == [maximumPoolSize: 50, minimumIdle: 2] - } - - void "Test a placeholder string map keeps its resolved values over a lower flattened property"() { - given: - def environment = new StandardEnvironment() - environment.conversionService.addConverter(String, Map) { String source -> - if (source == 'pool:50') { - return [maximumPoolSize: 50] - } - throw new ConverterNotFoundException(TypeDescriptor.valueOf(String), TypeDescriptor.valueOf(Map)) - } - environment.propertySources.addLast(new MapPropertySource('low', [ - 'dataSource.properties.maximumPoolSize': 10, - 'dataSource.properties.minimumIdle': 2 - ])) - environment.propertySources.addFirst(new MapPropertySource('high', [ - 'dataSource.properties': '${pool}', - pool: 'pool:50' - ])) + when: + def properties = new MapConfigurationBuilder(environment).build().properties - expect: - new MapConfigurationBuilder(environment).build().properties == [maximumPoolSize: 50, minimumIdle: 2] + then: + properties.sqls.is(sqls) + properties.emptySqls.is(emptySqls) + properties.built == ['SET A=1', 'SET B=2'] + sqls == ['HIGH'] + emptySqls.isEmpty() } - void "Test a literal dotted list shadows a lower indexed placeholder"() { + void "Test a higher map leaf is not replaced by a lower map or unresolved placeholder"() { given: + def options = [enabled: true] + def pool = [size: 4] def environment = new StandardEnvironment() - environment.propertySources.addLast(new MapPropertySource('low', [ - 'dataSource.properties.a.b[0]': '${missing}' + environment.propertySources.addFirst(new MapPropertySource('low', [ + 'dataSource.properties.options': [enabled: false], + 'dataSource.properties.options.enabled': '${missing}', + 'dataSource.properties.pool': '${missing}' ])) environment.propertySources.addFirst(new MapPropertySource('high', [ - 'dataSource.properties': ['a.b': ['HIGH']] - ])) - - expect: - new MapConfigurationBuilder(environment).build().properties == ['a.b': ['HIGH']] - } - - void "Test a later nested map keeps a same-source literal key and its missing siblings"() { - given: - def environment = new StandardEnvironment() - environment.propertySources.addFirst(new MapPropertySource('config', [ - 'dataSource.properties.dataSourceProperties': ['oracle.jdbc.flag': 'true'], - 'dataSource.properties.dataSourceProperties.oracle.jdbc': [flag: 'false', other: 'kept'] + 'dataSource.properties.options': options, + 'dataSource.properties.pool': pool ])) when: - def properties = new MapConfigurationBuilder(environment).build().properties.dataSourceProperties + def properties = new MapConfigurationBuilder(environment).build().properties then: - properties['oracle.jdbc.flag'] == 'true' - properties.oracle.jdbc.other == 'kept' - properties.oracle.jdbc.flag == null + properties.options.is(options) + properties.pool.is(pool) + options == [enabled: true] + pool == [size: 4] } - void "Test a lower circular placeholder string does not replace a higher map"() { + void "Test a lower circular placeholder does not replace a higher map"() { given: def environment = new StandardEnvironment() - environment.propertySources.addLast(new MapPropertySource('low', [ + environment.propertySources.addFirst(new MapPropertySource('low', [ 'dataSource.properties': '${loop}', loop: '${loop}' ])) @@ -454,34 +251,6 @@ class ConfigurationBuilderSpec extends Specification { new MapConfigurationBuilder(environment).build().properties == [x: 50] } - void "Test Spring Environment rebuilds map-backed builder settings with source precedence"() { - given: - def environment = new StandardEnvironment() - environment.propertySources.addFirst(new MapPropertySource('low', [ - 'grails.gorm.hibernate.flush.mode.nested': 'low', - 'grails.gorm.hibernate.options': [size: 20, inherited: true], - 'grails.gorm.hibernate.local': 'replaced by nested map', - 'grails.gorm.hibernate.local.enabled': true - ])) - environment.propertySources.addFirst(new MapPropertySource('high', [ - 'grails.gorm.hibernate.configClass': 'configured', - 'grails.gorm.hibernate.flush.mode': 'HIGH', - 'grails.gorm.hibernate.options.size': 50, - 'grails.gorm.hibernate.connectionInitSqls[1]': 'SET B=2', - 'grails.gorm.hibernate.connectionInitSqls[0]': 'SET A=1' - ])) - - when: - def configuration = new SpringMapConfigurationBuilder(environment).build() - - then: - configuration.hibernate.getConfigClass() == 'configured' - configuration.hibernate['flush'] == [mode: 'HIGH'] - configuration.hibernate['options'] == [size: 50, inherited: true] - configuration.hibernate['local'] == [enabled: true] - configuration.hibernate['connectionInitSqls'] == ['SET A=1', 'SET B=2'] - } - void "Test DatastoreUtils resolver retains existing map-backed builder binding"() { given: String systemKey = 'grails.gorm.hibernate.minimumIdle' @@ -514,38 +283,6 @@ class ConfigurationBuilderSpec extends Specification { } } - void "Test DatastoreUtils resolver keeps keys from a lower configuration map"() { - given: - String systemKey = 'grails.gorm.hibernate.minimumIdle' - String previousIdle = System.setProperty(systemKey, '3') - def low = [ - 'grails.gorm.hibernate.shared': 'low', - 'grails.gorm.hibernate.base': 'retained' - ] - def high = [ - 'grails.gorm.hibernate.shared': 'high', - 'grails.gorm.hibernate.override': 'new' - ] - def resolver = DatastoreUtils.createPropertyResolvers(low, high) - - when: - def configuration = new SpringMapConfigurationBuilder(resolver).build() - - then: - configuration.hibernate['shared'] == 'high' - configuration.hibernate['override'] == 'new' - configuration.hibernate['base'] == 'retained' - configuration.hibernate['minimumIdle'] == null - - cleanup: - if (previousIdle == null) { - System.clearProperty(systemKey) - } - else { - System.setProperty(systemKey, previousIdle) - } - } - void "Test configuration builder with getter and setter"() { given:"A configuration" def map = [ From 49fda504e8a13fe79001526fe2a5cfae3a3c97fe Mon Sep 17 00:00:00 2001 From: Mattias Reichel Date: Fri, 6 Nov 2026 11:03:53 +0100 Subject: [PATCH 007/115] Let a higher source's indexed list replace a lower one A list built from name[n] kept accepting indexes from lower property sources, so -DdataSource.properties.connectionInitSqls[0]='SET X=9' over a three-item list in application.yml gave [SET X=9, SET B=2, SET C=3]. A list stored as a value already replaced a lower list whole, as Spring Boot does. A list now takes indexes only from the source that built it, so both forms give [SET X=9]. --- .../config/ConfigurationBuilder.groovy | 6 +++--- .../config/ConfigurationBuilderSpec.groovy | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) 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 5150acd9870..f7d51167c39 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 @@ -438,13 +438,13 @@ abstract class ConfigurationBuilder { String prefix = propertyPath + '.' Map result = new LinkedHashMap<>() Set> containers = Collections.newSetFromMap(new IdentityHashMap, Boolean>()) - Set> builtLists = Collections.newSetFromMap(new IdentityHashMap, Boolean>()) containers.add(result) boolean stored = false for (PropertySource propertySource : environment.propertySources) { if (!(propertySource instanceof EnumerablePropertySource)) { continue } + Set> builtLists = Collections.newSetFromMap(new IdentityHashMap, Boolean>()) for (String name : ((EnumerablePropertySource) propertySource).propertyNames) { if (name == null || !name.startsWith(prefix)) { continue @@ -502,7 +502,8 @@ abstract class ConfigurationBuilder { /** * True when this path is already stored, or an ancestor holds a non-map. * A stored map is not a blocking ancestor of a missing leaf. - * Only a list this walk built from a numeric index accepts a later index. + * Only a list built from the numeric indexes of the current source accepts a later index, + * so a higher source's list replaces a lower one whole. */ private static boolean pathBlocked(Map result, Set> builtLists, String name) { int bracket = indexedBracket(name) @@ -846,4 +847,3 @@ abstract class ConfigurationBuilder { return ClassUtils.isAssignableValue(propertyType, nestedValue) ? nestedValue : null } } - diff --git a/grails-datastore-core/src/test/groovy/org/grails/datastore/mapping/config/ConfigurationBuilderSpec.groovy b/grails-datastore-core/src/test/groovy/org/grails/datastore/mapping/config/ConfigurationBuilderSpec.groovy index 71e3cf5d0f9..41aaac0c2b1 100644 --- a/grails-datastore-core/src/test/groovy/org/grails/datastore/mapping/config/ConfigurationBuilderSpec.groovy +++ b/grails-datastore-core/src/test/groovy/org/grails/datastore/mapping/config/ConfigurationBuilderSpec.groovy @@ -211,6 +211,25 @@ class ConfigurationBuilderSpec extends Specification { emptySqls.isEmpty() } + void "Test a higher source indexed list replaces a longer lower list"() { + given: + def environment = new StandardEnvironment() + environment.propertySources.addFirst(new MapPropertySource('low', [ + 'dataSource.properties.connectionInitSqls[0]': 'SET A=1', + 'dataSource.properties.connectionInitSqls[1]': 'SET B=2', + 'dataSource.properties.connectionInitSqls[2]': 'SET C=3' + ])) + environment.propertySources.addFirst(new MapPropertySource('high', [ + 'dataSource.properties.connectionInitSqls[0]': 'SET X=9' + ])) + + when: + def properties = new MapConfigurationBuilder(environment).build().properties + + then: + properties.connectionInitSqls == ['SET X=9'] + } + void "Test a higher map leaf is not replaced by a lower map or unresolved placeholder"() { given: def options = [enabled: true] From eae18f18cb3796f82443fdc500ab191ebec55cea Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sat, 14 Nov 2026 10:41:25 -0800 Subject: [PATCH 008/115] Expose the URL mapping that handles a request as its best matching handler UrlMappingsHandlerMapping now sets HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE to the UrlMappingInfo it returns, as Spring MVC's own handler mappings set it for the handler they find, and takes it off the request once the dispatch completes, with an interceptor first in the chain so that it completes last, whichever interceptor stops the request. A dispatch another handler mapping handles in the meantime, such as a forward to a Spring MVC endpoint, keeps the attribute it set. Content negotiation can then tell a request a URL mapping handles from one a Spring MVC endpoint handles, and from what Spring Security negotiates after the dispatch. The version lookup no longer fails for a request whose response type resolves to none. --- .../mvc/UrlMappingsHandlerMapping.groovy | 36 ++++- .../mvc/UrlMappingsHandlerMappingSpec.groovy | 135 +++++++++++++++++- 2 files changed, 164 insertions(+), 7 deletions(-) diff --git a/grails-web-url-mappings/src/main/groovy/org/grails/web/mapping/mvc/UrlMappingsHandlerMapping.groovy b/grails-web-url-mappings/src/main/groovy/org/grails/web/mapping/mvc/UrlMappingsHandlerMapping.groovy index 0bcd0d52f9c..a4a673b371a 100644 --- a/grails-web-url-mappings/src/main/groovy/org/grails/web/mapping/mvc/UrlMappingsHandlerMapping.groovy +++ b/grails-web-url-mappings/src/main/groovy/org/grails/web/mapping/mvc/UrlMappingsHandlerMapping.groovy @@ -74,6 +74,7 @@ class UrlMappingsHandlerMapping extends AbstractHandlerMapping { // Both are stateless, so one shared instance each rather than two allocations per request. private static final HandlerInterceptor OBSERVATION_ROUTE_HANDLER = new ObservationRouteHandler() private static final HandlerInterceptor ERROR_HANDLING_HANDLER = new ErrorHandlingHandler() + private static final HandlerInterceptor HANDLED_REQUEST_HANDLER = new HandledRequestHandler() /** * Whether to resolve a "_method" parameter on a POST into the overridden request method while matching @@ -164,6 +165,8 @@ class UrlMappingsHandlerMapping extends AbstractHandlerMapping { // before(), so that a failure in one is reported under the route that failed chain.addInterceptor(webRequestInterceptorCount, OBSERVATION_ROUTE_HANDLER) chain.addInterceptor(ERROR_HANDLING_HANDLER) + // First, so that it completes last, whichever interceptor stops the request + chain.addInterceptor(0, HANDLED_REQUEST_HANDLER) return chain } @@ -178,8 +181,22 @@ class UrlMappingsHandlerMapping extends AbstractHandlerMapping { return match == null && !rootCause.is(thrown) ? urlMappingsHolder.matchStatusCode(status, rootCause) : match } + /** + * The mapping that handles the request, which is also the request's + * {@link org.springframework.web.servlet.HandlerMapping#BEST_MATCHING_HANDLER_ATTRIBUTE}, as each of Spring MVC's + * handler mappings sets it for the handler it finds, until the dispatch completes, so that what Spring MVC + * negotiates for the request while a URL mapping handles it can tell that one does. + */ @Override protected Object getHandlerInternal(HttpServletRequest request) throws Exception { + Object handler = matchHandler(request) + if (handler != null) { + request.setAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE, handler) + } + return handler + } + + private Object matchHandler(HttpServletRequest request) { def includedInfo = WebUtils.isInclude(request) ? request.getAttribute(INCLUDED_MAPPING_INFO) : null if (includedInfo != null) { @@ -273,8 +290,9 @@ class UrlMappingsHandlerMapping extends AbstractHandlerMapping { protected String findRequestedVersion(GrailsWebRequest currentRequest) { String version = currentRequest.getHeader(HttpHeaders.ACCEPT_VERSION) if (!version && mimeTypeResolver) { + // none where the request accepts no configured type, as with grails.mime.negotiation.spring MimeType mimeType = mimeTypeResolver.resolveResponseMimeType(currentRequest) - version = mimeType.version + version = mimeType?.version } return version } @@ -331,6 +349,22 @@ class UrlMappingsHandlerMapping extends AbstractHandlerMapping { } } + /** + * Takes the mapping off the request as its best matching handler once the dispatch it handles completes, so that + * what negotiates for the request afterwards, such as Spring Security's filters for an exception the dispatch left + * unresolved, or the handler mapping of an error dispatch, does not take it for a request a URL mapping handles. + */ + @CompileStatic + static class HandledRequestHandler implements HandlerInterceptor { + + @Override + void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { + if (request.getAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE).is(handler)) { + request.removeAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE) + } + } + } + static class ErrorHandlingHandler implements HandlerInterceptor { @Override diff --git a/grails-web-url-mappings/src/test/groovy/org/grails/web/mapping/mvc/UrlMappingsHandlerMappingSpec.groovy b/grails-web-url-mappings/src/test/groovy/org/grails/web/mapping/mvc/UrlMappingsHandlerMappingSpec.groovy index a8c84a72805..72233cada8b 100644 --- a/grails-web-url-mappings/src/test/groovy/org/grails/web/mapping/mvc/UrlMappingsHandlerMappingSpec.groovy +++ b/grails-web-url-mappings/src/test/groovy/org/grails/web/mapping/mvc/UrlMappingsHandlerMappingSpec.groovy @@ -21,6 +21,8 @@ package org.grails.web.mapping.mvc import java.sql.SQLException import jakarta.servlet.DispatcherType +import jakarta.servlet.http.HttpServletRequest +import jakarta.servlet.http.HttpServletResponse import grails.artefact.Artefact import grails.core.DefaultGrailsApplication @@ -29,6 +31,7 @@ import grails.util.GrailsWebMockUtil import grails.web.Action import grails.web.HyphenatedUrlConverter import grails.web.mapping.AbstractUrlMappingsSpec +import grails.web.mime.MimeTypeResolver import org.grails.web.errors.GrailsWrappedRuntimeException import org.grails.web.mapping.DefaultUrlMappingData import org.grails.web.mapping.DefaultUrlMappingInfo @@ -44,6 +47,7 @@ import org.springframework.web.context.request.WebRequestInterceptor import org.springframework.web.context.support.StaticWebApplicationContext import org.springframework.web.filter.ServerHttpObservationFilter import org.springframework.web.servlet.HandlerInterceptor +import org.springframework.web.servlet.HandlerMapping import org.springframework.web.servlet.handler.WebRequestHandlerInterceptorAdapter import org.springframework.web.servlet.view.InternalResourceView import spock.lang.Issue @@ -352,7 +356,7 @@ class UrlMappingsHandlerMappingSpec extends AbstractUrlMappingsSpec { result.view.getUrl() == "/index.html" } - void "the handler chain keeps WebRequestInterceptors first, then the route, and appends the error handling last"() { + void "the handler chain completes the handled request last, keeps WebRequestInterceptors first, then the route, and appends the error handling last"() { given: "a handler mapping with both a WebRequestInterceptor and an ordinary HandlerInterceptor" def handlerMapping = handlerMapping() def plainInterceptor = new HandlerInterceptor() {} @@ -370,13 +374,16 @@ class UrlMappingsHandlerMappingSpec extends AbstractUrlMappingsSpec { webRequest.request.setRequestURI("/foo/bar") def interceptors = handlerMapping.getHandler(webRequest.request).interceptorList - then: "the WebRequestInterceptor is adapted and ordered first, ahead of ordinary interceptors" - interceptors[0] instanceof WebRequestHandlerInterceptorAdapter + then: "the handled request is completed by the first interceptor, which completes last whichever stops the request" + interceptors[0].class.simpleName == 'HandledRequestHandler' + + and: "the WebRequestInterceptor is adapted and ordered next, ahead of ordinary interceptors" + interceptors[1] instanceof WebRequestHandlerInterceptorAdapter interceptors.contains(plainInterceptor) and: "the route is set ahead of the ordinary interceptors, which can fail, and the error handling is last" - interceptors[1].class.simpleName == 'ObservationRouteHandler' - interceptors.indexOf(plainInterceptor) > 1 + interceptors[2].class.simpleName == 'ObservationRouteHandler' + interceptors.indexOf(plainInterceptor) > 2 interceptors[-1].class.simpleName == 'ErrorHandlingHandler' } @@ -394,7 +401,8 @@ class UrlMappingsHandlerMappingSpec extends AbstractUrlMappingsSpec { second.request.setRequestURI("/foo/bar") def secondChain = handlerMapping.getHandler(second.request).interceptorList - then: "both chains end with the very same interceptor instances" + then: "both chains start and end with the very same interceptor instances" + firstChain[0].is(secondChain[0]) firstChain[-1].is(secondChain[-1]) firstChain[-2].is(secondChain[-2]) } @@ -647,6 +655,121 @@ class UrlMappingsHandlerMappingSpec extends AbstractUrlMappingsSpec { context.pathPattern == '/foo/bar' } + void 'the mapping that handles a request is its best matching handler, as a Spring MVC handler mapping sets it'() { + given: + def grailsApplication = new DefaultGrailsApplication(FooController) + grailsApplication.initialise() + def handler = new UrlMappingsHandlerMapping(new GrailsControllerUrlMappings(grailsApplication, getUrlMappingsHolder { + '/foo/bar'(controller: 'foo', action: 'bar') + '/'(view: '/index') + })) + def request = GrailsWebMockUtil.bindMockWebRequest().request + + when: 'a request a controller mapping matches' + request.requestURI = '/foo/bar' + def chain = handler.getHandler(request) + + then: + request.getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE).is(chain.handler) + chain.handler instanceof GrailsControllerUrlMappingInfo + + when: 'a request a view mapping matches' + request.removeAttribute(UrlMappingsHandlerMapping.MATCHED_REQUEST) + request.requestURI = '/' + chain = handler.getHandler(request) + + then: + request.getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE).is(chain.handler) + chain.handler.viewName == '/index' + } + + void 'the mapping is no longer the best matching handler once its dispatch completes, #how'() { + given: + def grailsApplication = new DefaultGrailsApplication(FooController) + grailsApplication.initialise() + def handler = new UrlMappingsHandlerMapping(new GrailsControllerUrlMappings(grailsApplication, getUrlMappingsHolder { + '/foo/bar'(controller: 'foo', action: 'bar') + })) + boolean proceeds = proceed + handler.setHandlerInterceptors([new HandlerInterceptor() { + @Override + boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object mapped) { + proceeds + } + }] as HandlerInterceptor[]) + def webRequest = GrailsWebMockUtil.bindMockWebRequest() + def request = webRequest.request + request.requestURI = '/foo/bar' + def chain = handler.getHandler(request) + + when: 'the dispatcher completes, in reverse, the interceptors that proceeded, and not one that stopped the request' + def completed = chain.interceptorList.takeWhile { interceptor -> + interceptor.preHandle(request, webRequest.response, chain.handler) + } + completed.reverseEach { interceptor -> interceptor.afterCompletion(request, webRequest.response, chain.handler, null) } + + then: + request.getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE) == null + + where: + how | proceed + 'after the action' | true + 'also where an interceptor stops the request' | false + } + + void 'a dispatch another handler mapping handles meanwhile keeps the best matching handler it set'() { + given: + def grailsApplication = new DefaultGrailsApplication(FooController) + grailsApplication.initialise() + def handler = new UrlMappingsHandlerMapping(new GrailsControllerUrlMappings(grailsApplication, getUrlMappingsHolder { + '/foo/bar'(controller: 'foo', action: 'bar') + })) + def webRequest = GrailsWebMockUtil.bindMockWebRequest() + def request = webRequest.request + request.requestURI = '/foo/bar' + def chain = handler.getHandler(request) + + when: 'the action forwards to a handler of Spring MVC, which takes the attribute' + def springHandler = new Object() + request.setAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE, springHandler) + chain.interceptorList.reverseEach { interceptor -> interceptor.afterCompletion(request, webRequest.response, chain.handler, null) } + + then: + request.getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE).is(springHandler) + } + + void 'a request no mapping matches keeps the best matching handler another handler mapping set'() { + given: + def handler = new UrlMappingsHandlerMapping(getUrlMappingsHolder { + '/foo/bar'(controller: 'foo', action: 'bar') + }) + def request = GrailsWebMockUtil.bindMockWebRequest().request + def springHandler = new Object() + request.setAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE, springHandler) + request.requestURI = '/spring/endpoint' + + expect: + handler.getHandler(request) == null + request.getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE).is(springHandler) + } + + void 'a request that accepts no configured type is matched on the version of its Accept-Version header alone'() { + given: 'a resolver that resolves no type for the response, as Spring negotiation does for such a request' + def grailsApplication = new DefaultGrailsApplication(FooController) + grailsApplication.initialise() + def handler = new UrlMappingsHandlerMapping(new GrailsControllerUrlMappings(grailsApplication, getUrlMappingsHolder { + '/foo/bar'(controller: 'foo', action: 'bar') + })) + handler.mimeTypeResolver = Stub(MimeTypeResolver) { + resolveResponseMimeType(_) >> null + } + def request = GrailsWebMockUtil.bindMockWebRequest().request + request.requestURI = '/foo/bar' + + expect: + handler.getHandler(request).handler.actionName == 'bar' + } + void cleanup() { RequestContextHolder.resetRequestAttributes() } From 88b004d0ed2f827732f6fcda5877ef520491a908 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sat, 14 Nov 2026 10:41:45 -0800 Subject: [PATCH 009/115] Resolve Grails formats through Spring MVC's content negotiation (#16698) The opt-in grails.mime.negotiation.spring (default false in Grails 10) resolves request.format, response.format, withFormat and the format respond writes in with Spring MVC's ContentNegotiationManager, as the formats grails.mime.types gives the media types it resolves: - The Accept header is read by Spring MVC's manager, with its strategies and spring.mvc.contentnegotiation.*, in its order: quality, then specificity, then the order of the header. The text/xml merge and the +xml preference are gone. - A type with the +json or +xml suffix that no configured type names is the json or xml format, as Spring MVC's converters write such types; this extends the rule Grails 9 had for the problem types of RFC 9457. - */* is the all format, also where it is not configured. Another wildcard, such as text/*, is a type of the all format that withFormat answers with its first block, and respond with the first of the action's formats, whose format has a type compatible with it. - A request that accepts no configured type, or whose Accept header Spring cannot read, has no format: response.format is null, respond answers 406 and withFormat runs its * block or none. - The format parameter and URL extensions are kept, ahead of Spring MVC's strategies; an unknown format is not acceptable and all leaves the format to the Accept header. request.format reads Content-Type with Spring's parser. The format parameter, URL extensions and the grails.mime Accept header settings are installed into Spring MVC's manager, by a post-processor that replaces the mvcContentNegotiationManager bean with the setting only, as strategies that apply while a URL mapping handles the request, so that what Spring MVC writes for an action, such as a ResponseEntity or a ProblemDetail, is negotiated as Grails resolves its format. Spring MVC endpoints and Spring Security, before a mapping has matched the request, negotiate as before, and GrailsContentNegotiationStrategy is still no Spring ContentNegotiationStrategy bean. A web unit test marks the request of the action it calls as handled by a URL mapping, as in an application. Without the setting, every answer is that of Grails 9. --- .../main/groovy/grails/config/Settings.groovy | 18 + .../web/api/MimeTypesApiSupport.groovy | 94 +++- .../web/mime/MimeTypesConfiguration.groovy | 31 +- ...eptHeaderContentNegotiationStrategy.groovy | 73 +++ .../GrailsContentNegotiationManager.groovy | 90 ++++ ...tentNegotiationManagerPostProcessor.groovy | 69 +++ .../GrailsContentNegotiationStrategy.groovy | 377 +++++++++++++- ...ilsFormatContentNegotiationStrategy.groovy | 74 +++ .../mime/HttpServletRequestExtension.groovy | 10 +- .../mime/HttpServletResponseExtension.groovy | 54 +-- .../mime/MimeTypesConfigurationSpec.groovy | 2 +- .../mime/SpringFormatNegotiationSpec.groovy | 459 ++++++++++++++++++ .../RequestAndResponseMimeTypesApiSpec.groovy | 23 +- .../artefact/controller/RestResponder.groovy | 31 +- .../MigrationSettingsMetadataSpec.groovy | 1 + .../SpringFormatNegotiationRespondSpec.groovy | 234 +++++++++ .../testing/web/GrailsWebUnitTest.groovy | 6 + ...itional-spring-configuration-metadata.json | 6 + 18 files changed, 1584 insertions(+), 68 deletions(-) create mode 100644 grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsAcceptHeaderContentNegotiationStrategy.groovy create mode 100644 grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationManager.groovy create mode 100644 grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationManagerPostProcessor.groovy create mode 100644 grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsFormatContentNegotiationStrategy.groovy create mode 100644 grails-mimetypes/src/test/groovy/org/grails/web/mime/SpringFormatNegotiationSpec.groovy create mode 100644 grails-test-suite-web/src/test/groovy/grails/rest/web/SpringFormatNegotiationRespondSpec.groovy diff --git a/grails-core/src/main/groovy/grails/config/Settings.groovy b/grails-core/src/main/groovy/grails/config/Settings.groovy index c5f7bf615f9..339a6c75bac 100644 --- a/grails-core/src/main/groovy/grails/config/Settings.groovy +++ b/grails-core/src/main/groovy/grails/config/Settings.groovy @@ -212,6 +212,24 @@ interface Settings { */ String MIME_DISABLE_ACCEPT_HEADER_FOR_USER_AGENTS_XHR = 'grails.mime.disable.accept.header.userAgentsXhr' + /** + * Whether {@code request.format}, {@code response.format}, {@code withFormat} and the format {@code respond} writes + * in are resolved with Spring MVC's content negotiation manager, which also applies the {@code grails.mime} rules + * Grails keeps to what Spring MVC writes for the requests a URL mapping handles, rather than by Grails' own rules. + * Defaults to {@link #MIME_NEGOTIATION_SPRING_DEFAULT}. + * + * @since 10.0 + */ + String MIME_NEGOTIATION_SPRING = 'grails.mime.negotiation.spring' + + /** + * The default of {@link #MIME_NEGOTIATION_SPRING}, as a property placeholder reads it: {@code false} in Grails 10, + * which keeps the negotiation of Grails 9. + * + * @since 10.0 + */ + String MIME_NEGOTIATION_SPRING_DEFAULT = 'false' + /** * The default scope for controllers */ diff --git a/grails-mimetypes/src/main/groovy/org/grails/plugins/web/api/MimeTypesApiSupport.groovy b/grails-mimetypes/src/main/groovy/org/grails/plugins/web/api/MimeTypesApiSupport.groovy index 86622bfcb9e..378032de564 100644 --- a/grails-mimetypes/src/main/groovy/org/grails/plugins/web/api/MimeTypesApiSupport.groovy +++ b/grails-mimetypes/src/main/groovy/org/grails/plugins/web/api/MimeTypesApiSupport.groovy @@ -27,6 +27,7 @@ import jakarta.servlet.http.HttpServletResponse import grails.web.mime.MimeType import org.grails.plugins.web.mime.FormatInterceptor +import org.grails.web.mime.GrailsContentNegotiationStrategy import org.grails.web.servlet.mvc.GrailsWebRequest import org.grails.web.util.GrailsApplicationAttributes @@ -39,6 +40,9 @@ import org.grails.web.util.GrailsApplicationAttributes @CompileStatic class MimeTypesApiSupport { + /** What {@link #resolveAllFormat(Object, LinkedHashMap, MimeType)} answers where no block is of a compatible format. */ + protected static final Object NO_FORMAT = new Object() + def T withFormat(HttpServletRequest request, Closure callable) { return (T) withFormatInternal(request, getDefinedFormats(callable)) } @@ -52,33 +56,38 @@ class MimeTypesApiSupport { String format = lookupFormat(formatProvider) if (formats) { if (format == 'all') { - result = resolveAllFormat(formatProvider, formats) + result = resolveAllFormat(formatProvider, formats, acceptedWildcard(formatProvider)) + if (!result.is(NO_FORMAT)) { + return result + } + result = null + } + // if the format has been specified then use that + if (formats.containsKey(format)) { + result = getResponseForFormat(formats[format], format, formatProvider) } + // otherwise look for the best match else { - // if the format has been specified then use that - if (formats.containsKey(format)) { - result = getResponseForFormat(formats[format], format, formatProvider) - } - // otherwise look for the best match - else { - boolean matchFound = false - for (MimeType mime in lookupMimeTypes(formatProvider)) { - if (formats.containsKey(mime.extension)) { - matchFound = true - result = getResponseForFormat(formats[mime.extension], mime.extension, formatProvider) - break - } - else { - if (mime.extension == 'all') { + boolean matchFound = false + for (MimeType mime in lookupMimeTypes(formatProvider)) { + if (formats.containsKey(mime.extension)) { + matchFound = true + result = getResponseForFormat(formats[mime.extension], mime.extension, formatProvider) + break + } + else { + if (mime.extension == 'all') { + result = resolveAllFormat(formatProvider, formats, mime) + if (!result.is(NO_FORMAT)) { matchFound = true - result = resolveAllFormat(formatProvider, formats) break } + result = null } } - if (!matchFound && formats.containsKey('*')) { - result = getResponseForFormat(formats['*'], format, formatProvider) - } + } + if (!matchFound && formats.containsKey('*')) { + result = getResponseForFormat(formats['*'], format, formatProvider) } } } @@ -106,6 +115,51 @@ class MimeTypesApiSupport { getResponseForFormat(formats[formatKey], format, formatProvider) } + /** + * Resolves a type of the {@code all} format the request accepts: {@code */*} as any format, and, with + * {@code grails.mime.negotiation.spring}, a wildcard such as {@code text/*}, which it resolves an {@code Accept} header + * to, as the {@code *} block or else the first block of a format with a type compatible with it, as Spring MVC answers + * a wildcard with the first of its candidates that is compatible with it. + * + * @param formatProvider the request or the response + * @param formats the blocks, by format + * @param accepted the type of the {@code all} format + * @return the result of the block, or {@link #NO_FORMAT} where none is of a format compatible with the wildcard + * @since 10.0 + */ + protected Object resolveAllFormat(formatProvider, LinkedHashMap formats, MimeType accepted) { + if (accepted == null || accepted.extension != 'all' || accepted.name == MimeType.ALL.name || formats.containsKey('*')) { + return resolveAllFormat(formatProvider, formats) + } + GrailsContentNegotiationStrategy strategy = GrailsContentNegotiationStrategy.forRequest(request(formatProvider)) + if (!strategy.springNegotiation) { + return resolveAllFormat(formatProvider, formats) + } + String compatible = formats.keySet().find { String format -> strategy.acceptsFormat(accepted, format) } + return compatible != null ? getResponseForFormat(formats[compatible], compatible, formatProvider) : NO_FORMAT + } + + /** + * The wildcard the {@code all} format of the response stands for, where it is the first type the request accepts + * rather than a format the request names or {@code withFormat} selected, which is saved as the response format: only + * {@code grails.mime.negotiation.spring} resolves a wildcard such as {@code text/*} to it, and does not save it. It is + * read from the types the request accepts, which saves no type for the response. + */ + private MimeType acceptedWildcard(formatProvider) { + if (!(formatProvider instanceof HttpServletResponse)) { + return null + } + HttpServletRequest request = GrailsWebRequest.lookup().currentRequest + if (request.getAttribute(GrailsApplicationAttributes.RESPONSE_FORMAT) != null) { + return null + } + return lookupMimeTypes(formatProvider)?.find { it } + } + + private static HttpServletRequest request(formatProvider) { + formatProvider instanceof HttpServletRequest ? (HttpServletRequest) formatProvider : GrailsWebRequest.lookup().currentRequest + } + @CompileStatic(TypeCheckingMode.SKIP) protected MimeType[] lookupMimeTypes(formatProvider) { formatProvider.mimeTypes diff --git a/grails-mimetypes/src/main/groovy/org/grails/plugins/web/mime/MimeTypesConfiguration.groovy b/grails-mimetypes/src/main/groovy/org/grails/plugins/web/mime/MimeTypesConfiguration.groovy index 64952768c2a..c336b6f884a 100644 --- a/grails-mimetypes/src/main/groovy/org/grails/plugins/web/mime/MimeTypesConfiguration.groovy +++ b/grails-mimetypes/src/main/groovy/org/grails/plugins/web/mime/MimeTypesConfiguration.groovy @@ -24,9 +24,12 @@ import groovy.transform.CompileStatic import groovy.transform.TypeCheckingMode import org.springframework.beans.factory.ObjectProvider +import org.springframework.beans.factory.annotation.Qualifier import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration import org.springframework.context.annotation.Primary +import org.springframework.core.env.Environment +import org.springframework.web.accept.ContentNegotiationManager import grails.config.Config import grails.config.Settings @@ -37,6 +40,7 @@ import grails.web.mime.MimeTypeResolver import grails.web.mime.MimeUtility import org.grails.web.mime.DefaultMimeTypeResolver import org.grails.web.mime.DefaultMimeUtility +import org.grails.web.mime.GrailsContentNegotiationManagerPostProcessor import org.grails.web.mime.GrailsContentNegotiationStrategy import org.grails.web.mime.GrailsMimeTypesWebMvcConfigurer @@ -142,22 +146,43 @@ class MimeTypesConfiguration { * formats it registers with Spring MVC are those of the configuration: those * {@code grails.mime.types} declares, or the defaults where it declares none or * {@code grails.mime.mergeDefaults} adds them, which Spring MVC's content negotiation manager - * needs as it is created, and not those a {@code MimeTypeProvider} adds. + * needs as it is created, and not those a {@code MimeTypeProvider} adds. The strategy it builds resolves formats, + * with {@code grails.mime.negotiation.spring}, with Spring MVC's content negotiation manager, which is asked for + * once a request is negotiated, since it is created after the configurer. */ @Bean static GrailsMimeTypesWebMvcConfigurer grailsMimeTypesWebMvcConfigurer( - ObjectProvider mimeUtility, GrailsApplication grailsApplication) { + ObjectProvider mimeUtility, GrailsApplication grailsApplication, + @Qualifier(GrailsContentNegotiationStrategy.MVC_CONTENT_NEGOTIATION_MANAGER) + ObjectProvider contentNegotiationManager) { // A user agent that is not a regular expression is reported as the application starts, rather than by every // request, which builds the strategy once it is first asked for GrailsContentNegotiationStrategy.disabledUserAgentPatterns(grailsApplication.config) // The MIME types request.format and response.format resolve against as well + Supplier manager = () -> contentNegotiationManager.getIfAvailable() Supplier strategy = () -> - new GrailsContentNegotiationStrategy(mimeUtility.getObject().knownMimeTypes as MimeType[], grailsApplication.config) + new GrailsContentNegotiationStrategy(mimeUtility.getObject().knownMimeTypes as MimeType[], + grailsApplication.config, manager) Supplier formats = () -> configuredMimeTypes(grailsApplication.config, declaredMimeTypes(grailsApplication.config)) return new GrailsMimeTypesWebMvcConfigurer(strategy, formats) } + /** + * With {@code grails.mime.negotiation.spring}, has Spring MVC's content negotiation manager apply the + * {@code grails.mime} rules Grails keeps to the requests a URL mapping handles. Static, as a post-processor is + * created before the other beans, and the strategy it applies them with is that of the configurer, once a request + * is negotiated. + * + * @since 10.0 + */ + @Bean + static GrailsContentNegotiationManagerPostProcessor grailsContentNegotiationManagerPostProcessor( + Environment environment, ObjectProvider grailsMimeTypesWebMvcConfigurer) { + return new GrailsContentNegotiationManagerPostProcessor(environment, () -> + grailsMimeTypesWebMvcConfigurer.getObject().contentNegotiationStrategy) + } + /** * The MIME types the {@code mimeTypes} bean, and so the application's {@code MimeUtility}, starts from, before those * of the {@code MimeTypeProvider} beans. The formats registered with Spring MVC are those {@code grails.mime.types} diff --git a/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsAcceptHeaderContentNegotiationStrategy.groovy b/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsAcceptHeaderContentNegotiationStrategy.groovy new file mode 100644 index 00000000000..6edfed24860 --- /dev/null +++ b/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsAcceptHeaderContentNegotiationStrategy.groovy @@ -0,0 +1,73 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.web.mime + +import java.util.function.Supplier + +import groovy.transform.CompileStatic + +import jakarta.servlet.http.HttpServletRequest + +import org.springframework.http.MediaType +import org.springframework.web.HttpMediaTypeNotAcceptableException +import org.springframework.web.accept.HeaderContentNegotiationStrategy +import org.springframework.web.context.request.NativeWebRequest + +/** + * The strategy of Spring MVC's content negotiation manager that reads the {@code Accept} header in place of Spring MVC's + * own, and ignores it for a request a URL mapping handles where {@code grails.mime.use.accept.header} or + * {@code grails.mime.disable.accept.header.*} say so, so that what Spring MVC writes for it is in the format Grails + * resolves for it. Any other request, such as one a Spring MVC endpoint handles, is negotiated by the strategy it + * replaces. It is a {@link HeaderContentNegotiationStrategy}, so that it is found as Spring MVC's is. + * + * @since 10.0 + */ +@CompileStatic +class GrailsAcceptHeaderContentNegotiationStrategy extends HeaderContentNegotiationStrategy { + + private final HeaderContentNegotiationStrategy springStrategy + + private final Supplier grailsStrategy + + /** + * @param springStrategy the strategy of Spring MVC's manager that it replaces + * @param grailsStrategy supplies the strategy that resolves Grails formats, which Spring MVC's manager is created + * before + */ + GrailsAcceptHeaderContentNegotiationStrategy(HeaderContentNegotiationStrategy springStrategy, + Supplier grailsStrategy) { + this.springStrategy = springStrategy + this.grailsStrategy = grailsStrategy + } + + /** @return the strategy of Spring MVC's manager it replaces */ + HeaderContentNegotiationStrategy getSpringStrategy() { + return springStrategy + } + + @Override + List resolveMediaTypes(NativeWebRequest webRequest) throws HttpMediaTypeNotAcceptableException { + HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest) + if (request != null && GrailsContentNegotiationStrategy.handlesRequest(request) && + grailsStrategy.get().ignoresAcceptHeader(request)) { + return MEDIA_TYPE_ALL_LIST + } + return springStrategy.resolveMediaTypes(webRequest) + } +} diff --git a/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationManager.groovy b/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationManager.groovy new file mode 100644 index 00000000000..6c8aa49dba6 --- /dev/null +++ b/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationManager.groovy @@ -0,0 +1,90 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.web.mime + +import java.util.function.Supplier + +import groovy.transform.CompileStatic + +import org.springframework.http.MediaType +import org.springframework.web.accept.ContentNegotiationManager +import org.springframework.web.accept.ContentNegotiationStrategy +import org.springframework.web.accept.HeaderContentNegotiationStrategy +import org.springframework.web.accept.MediaTypeFileExtensionResolver + +/** + * Spring MVC's content negotiation manager with {@code grails.mime.negotiation.spring}: its strategies, after the one + * that resolves the format a request a URL mapping handles names, and with the {@code Accept} header read by one that + * ignores it for such a request where the {@code grails.mime} settings say so. Any other request is negotiated as by + * Spring MVC's manager, whose formats and file extensions it answers with. + * + * @since 10.0 + */ +@CompileStatic +class GrailsContentNegotiationManager extends ContentNegotiationManager { + + private final ContentNegotiationManager springManager + + /** + * @param springManager Spring MVC's manager + * @param grailsStrategy supplies the strategy that resolves Grails formats, which Spring MVC's manager is created + * before + */ + GrailsContentNegotiationManager(ContentNegotiationManager springManager, + Supplier grailsStrategy) { + super(strategies(springManager, grailsStrategy)) + this.springManager = springManager + } + + /** @return Spring MVC's manager, whose strategies this one applies to the requests no URL mapping handles */ + ContentNegotiationManager getSpringManager() { + return springManager + } + + @Override + void addFileExtensionResolvers(MediaTypeFileExtensionResolver... resolvers) { + springManager.addFileExtensionResolvers(resolvers) + } + + @Override + List resolveFileExtensions(MediaType mediaType) { + return springManager.resolveFileExtensions(mediaType) + } + + @Override + List getAllFileExtensions() { + return springManager.allFileExtensions + } + + @Override + Map getMediaTypeMappings() { + return springManager.mediaTypeMappings + } + + private static List strategies(ContentNegotiationManager springManager, + Supplier grailsStrategy) { + List strategies = [new GrailsFormatContentNegotiationStrategy(grailsStrategy)] as List + for (ContentNegotiationStrategy strategy in springManager.strategies) { + strategies << (strategy instanceof HeaderContentNegotiationStrategy ? + new GrailsAcceptHeaderContentNegotiationStrategy((HeaderContentNegotiationStrategy) strategy, grailsStrategy) : + strategy) + } + return strategies + } +} diff --git a/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationManagerPostProcessor.groovy b/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationManagerPostProcessor.groovy new file mode 100644 index 00000000000..2299b6d562d --- /dev/null +++ b/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationManagerPostProcessor.groovy @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.web.mime + +import java.util.function.Supplier + +import groovy.transform.CompileStatic + +import org.springframework.beans.BeansException +import org.springframework.beans.factory.config.BeanPostProcessor +import org.springframework.core.env.Environment +import org.springframework.web.accept.ContentNegotiationManager + +import grails.config.Settings + +/** + * With {@code grails.mime.negotiation.spring}, replaces Spring MVC's content negotiation manager with one that applies + * the {@code grails.mime} rules Grails keeps to the requests a URL mapping handles (see + * {@link GrailsContentNegotiationManager}), so that what Spring MVC writes for them, such as a {@code ResponseEntity} or + * a {@code ProblemDetail} an action returns or {@code respond} writes, is negotiated as Grails resolves their formats. + * Without it, Spring MVC's manager is left as it is. + * + * @since 10.0 + */ +@CompileStatic +class GrailsContentNegotiationManagerPostProcessor implements BeanPostProcessor { + + private final Environment environment + + private final Supplier grailsStrategy + + /** + * @param environment the environment the setting is read from + * @param grailsStrategy supplies the strategy that resolves Grails formats, which Spring MVC's manager is created + * before + */ + GrailsContentNegotiationManagerPostProcessor(Environment environment, + Supplier grailsStrategy) { + this.environment = environment + this.grailsStrategy = grailsStrategy + } + + @Override + Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { + if (beanName == GrailsContentNegotiationStrategy.MVC_CONTENT_NEGOTIATION_MANAGER && + bean instanceof ContentNegotiationManager && !(bean instanceof GrailsContentNegotiationManager) && + environment.getProperty(Settings.MIME_NEGOTIATION_SPRING, Boolean, + Boolean.valueOf(Settings.MIME_NEGOTIATION_SPRING_DEFAULT))) { + return new GrailsContentNegotiationManager((ContentNegotiationManager) bean, grailsStrategy) + } + return bean + } +} diff --git a/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationStrategy.groovy b/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationStrategy.groovy index 05dd5dabec8..68a18d7a4ec 100644 --- a/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationStrategy.groovy +++ b/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationStrategy.groovy @@ -18,6 +18,7 @@ */ package org.grails.web.mime +import java.util.function.Supplier import java.util.regex.Pattern import java.util.regex.PatternSyntaxException @@ -25,13 +26,33 @@ import groovy.transform.CompileStatic import jakarta.servlet.http.HttpServletRequest +import org.slf4j.Logger +import org.slf4j.LoggerFactory +import org.springframework.context.ApplicationContext +import org.springframework.http.InvalidMediaTypeException +import org.springframework.http.MediaType +import org.springframework.web.HttpMediaTypeNotAcceptableException +import org.springframework.web.accept.ContentNegotiationManager +import org.springframework.web.accept.ContentNegotiationStrategy +import org.springframework.web.accept.HeaderContentNegotiationStrategy +import org.springframework.web.context.request.ServletWebRequest +import org.springframework.web.context.support.WebApplicationContextUtils +import org.springframework.web.servlet.HandlerMapping + import grails.config.Config import grails.config.Settings +import grails.core.GrailsApplication import grails.web.http.HttpHeaders +import grails.web.mapping.UrlMappingInfo import grails.web.mime.MimeType +import grails.web.mime.MimeUtility +import org.grails.config.PropertySourcesConfig +import org.grails.web.servlet.mvc.GrailsWebRequest /** - * Resolves the MIME types of a request's {@code Accept} header under the {@code grails.mime} rules. + * Resolves the formats of a request: the MIME types of its {@code Accept} header under the {@code grails.mime} rules, + * or, with {@code grails.mime.negotiation.spring}, the media types Spring MVC's content negotiation manager resolves for + * it, as the formats {@code grails.mime.types} gives them. * It is deliberately not a Spring {@code ContentNegotiationStrategy}: Spring MVC keeps its own * negotiation, and Spring Security adopts any such bean it finds. * @@ -40,12 +61,43 @@ import grails.web.mime.MimeType @CompileStatic class GrailsContentNegotiationStrategy { + private static final Logger LOG = LoggerFactory.getLogger(GrailsContentNegotiationStrategy) + + /** The name of the bean Spring MVC's content negotiation manager is, which its handler adapter is given by name. */ + static final String MVC_CONTENT_NEGOTIATION_MANAGER = 'mvcContentNegotiationManager' + + private static final String FORMAT_PARAMETER = 'format' + + private static final String ALL_FORMAT = 'all' + + /** The formats a type with the structured syntax suffix of RFC 6839 of their name is a document of. */ + private static final Set SUFFIX_FORMATS = Set.of('json', 'xml') + private final MimeType[] mimeTypes private final boolean useAcceptHeader private final boolean useAcceptHeaderXhr private final List disableForUserAgents + private final boolean springNegotiation + private final Supplier contentNegotiationManager + + /** The media type of each configured MIME type, in its place, or {@code null} for one Spring cannot read. */ + private final MediaType[] mediaTypes GrailsContentNegotiationStrategy(MimeType[] mimeTypes, Config config) { + this(mimeTypes, config, null) + } + + /** + * @param mimeTypes the MIME types the formats are resolved against + * @param config the application's configuration + * @param contentNegotiationManager supplies Spring MVC's content negotiation manager, which, with + * {@code grails.mime.negotiation.spring}, the formats are resolved with; asked for each request, since the + * strategy is created before it; or {@code null}, or one that supplies none, for one that reads the + * {@code Accept} header alone, as Spring MVC's default does + * @since 10.0 + */ + GrailsContentNegotiationStrategy(MimeType[] mimeTypes, Config config, + Supplier contentNegotiationManager) { this.mimeTypes = mimeTypes this.useAcceptHeader = config.getProperty(Settings.MIME_USE_ACCEPT_HEADER, Boolean, true) this.useAcceptHeaderXhr = !config.getProperty( @@ -54,6 +106,10 @@ class GrailsContentNegotiationStrategy { false ) this.disableForUserAgents = disabledUserAgentPatterns(config) + this.springNegotiation = config.getProperty(Settings.MIME_NEGOTIATION_SPRING, Boolean, + Boolean.valueOf(Settings.MIME_NEGOTIATION_SPRING_DEFAULT)) + this.contentNegotiationManager = contentNegotiationManager + this.mediaTypes = mimeTypes.collect { MimeType mimeType -> mediaTypeOf(mimeType) } as MediaType[] } /** @@ -64,12 +120,27 @@ class GrailsContentNegotiationStrategy { return mimeTypes } + /** + * @return whether the formats are resolved with Spring MVC's content negotiation manager, as + * {@code grails.mime.negotiation.spring} says + * @since 10.0 + */ + boolean isSpringNegotiation() { + return springNegotiation + } + /** * @return the MIME types of the {@code Accept} header under the {@code grails.mime} rules, without * any {@code format} applied. This is what {@code response.mimeTypes}, and so {@code withFormat} - * when the format has no block of its own, resolves. + * when the format has no block of its own, resolves. With {@code grails.mime.negotiation.spring}, they are the + * configured types the media types Spring MVC's content negotiation manager resolves for the request stand for, in + * its order, without the format the request names, and none where the request accepts no configured type. */ MimeType[] resolveAcceptedMimeTypes(HttpServletRequest request) { + if (springNegotiation) { + List requested = requestedMediaTypes(request) + return requested == null ? new MimeType[0] : acceptedMimeTypes(requested) + } String acceptHeader = resolveAcceptHeader(request) MimeType[] resolved = new DefaultAcceptHeaderParser(mimeTypes).parse(acceptHeader) if (!acceptHeader) { @@ -81,17 +152,311 @@ class GrailsContentNegotiationStrategy { return resolved } - private String resolveAcceptHeader(HttpServletRequest request) { - if (!useAcceptHeader) { + /** + * The MIME type a format a request names, with the {@code format} parameter or a URL extension, or that + * {@code withFormat} selected, stands for. + * + * @param format the format + * @return its first configured type; for a format that is not configured, the first configured type, as Grails 9 + * answers, or, with {@code grails.mime.negotiation.spring}, {@code null}, since the request accepts no + * configured type + * @since 10.0 + */ + MimeType resolveFormat(Object format) { + MimeType mimeType = mimeTypes.find { MimeType configured -> configured.extension == format } + if (mimeType != null || springNegotiation) { + return mimeType + } + return mimeTypes.find { it } + } + + /** + * The format the request names with its {@code format} parameter or a URL extension, which overrides its + * {@code Accept} header: with {@code grails.mime.negotiation.spring}, the first value of the parameter, as Spring MVC + * reads a parameter it negotiates with, unless it names no type, as the {@code all} format does, which leaves the + * negotiation to the {@code Accept} header, as Spring MVC's content negotiation manager does. + * + * @param webRequest the request + * @return the format, or {@code null} where the request names none + * @since 10.0 + */ + Object requestedFormat(GrailsWebRequest webRequest) { + Object format = webRequest?.params?.get(FORMAT_PARAMETER) + if (!springNegotiation || !format) { + return format + } + String named = format instanceof String[] ? (((String[]) format).length ? ((String[]) format)[0] : null) : + format.toString() + if (!named) { return null } + List types = mediaTypesOf(named) + return types != null && types.every { MediaType type -> type.isWildcardType() } ? null : named + } + + /** + * The MIME types of the {@code Content-Type} of a request, with {@code grails.mime.negotiation.spring}: the type + * Spring reads it as, as the format {@code grails.mime.types} gives it, a type with the {@code +json} or {@code +xml} + * suffix it does not configure as the {@code json} or {@code xml} format, and another type as it is, with no format. + * A request without one is read, as Grails 9 reads it, as of every configured type, so that its format is the first. + * + * @param request the request + * @return the MIME types of its content + * @since 10.0 + */ + MimeType[] resolveContentMimeTypes(HttpServletRequest request) { + String header = request.contentType ?: request.getHeader(HttpHeaders.CONTENT_TYPE) + if (!header) { + return mimeTypes + } + MediaType contentType + try { + contentType = MediaType.parseMediaType(header) + } + catch (InvalidMediaTypeException e) { + LOG.debug('The Content-Type [{}] is not a media type Spring reads: {}', header, e.message) + MimeType unread = new MimeType(header) + unread.name = unread.name?.toLowerCase(Locale.ROOT) + return [unread] as MimeType[] + } + MimeType configured = configuredFor(contentType) + return [configured != null ? withParameters(configured, contentType) : SpringMediaTypeAdapter.toMimeType(contentType)] as MimeType[] + } + + /** + * The media types a Grails format stands for, which Spring MVC's content negotiation manager resolves a request that + * names the format to: its configured types, and for the {@code json} and {@code xml} formats any type with their + * suffix, as Spring MVC's JSON and XML converters write them. + * + * @param format the format + * @return the media types, or {@code null} where no type of the format is configured + * @since 10.0 + */ + List mediaTypesOf(String format) { + List types = [] + for (int i = 0; i < mimeTypes.length; i++) { + if (mimeTypes[i].extension == format && mediaTypes[i] != null && !types.contains(mediaTypes[i])) { + types << mediaTypes[i] + } + } + if (types.isEmpty()) { + return null + } + if (format != null && SUFFIX_FORMATS.contains(format)) { + MediaType suffixed = new MediaType('application', "*+${format}".toString()) + if (!types.contains(suffixed)) { + types << suffixed + } + } + return types + } + /** + * @return the media types of every configured MIME type Spring can read, which a request that names a format no type + * is configured for is not acceptable in + * @since 10.0 + */ + List getConfiguredMediaTypes() { + return mediaTypes.findAll { it }.unique() as List + } + + /** + * @return whether the {@code Accept} header of a request is ignored, as {@code grails.mime.use.accept.header} and + * {@code grails.mime.disable.accept.header.*} say + * @since 10.0 + */ + boolean ignoresAcceptHeader(HttpServletRequest request) { + if (!useAcceptHeader) { + return true + } String userAgent = request.getHeader(HttpHeaders.USER_AGENT) boolean xhr = request.getHeader('X-Requested-With') == 'XMLHttpRequest' - boolean disabledForUserAgent = !(useAcceptHeaderXhr && xhr) && + return !(useAcceptHeaderXhr && xhr) && userAgent != null && disableForUserAgents.any { Pattern pattern -> pattern.matcher(userAgent).find() } - return disabledForUserAgent ? null : request.getHeader(HttpHeaders.ACCEPT) + } + + /** + * Whether a URL mapping handles a request, for which the {@code grails.mime} rules Grails keeps apply to what Spring + * MVC negotiates: the mapping is the request's best matching handler, which each of Spring MVC's handler mappings sets + * for the dispatch it handles, and which a URL mapping takes off the request once its dispatch completes. A web unit + * test sets it for the action it calls. + * + * @param request the request + * @return whether a URL mapping handles it + * @since 10.0 + */ + static boolean handlesRequest(HttpServletRequest request) { + return request.getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE) instanceof UrlMappingInfo + } + + /** + * The strategy that resolves the formats of a request: that of the application's {@code MimeTypesConfiguration}, or, + * in a context without it, such as one a test builds by hand, one built for the request from the application's + * settings. + * + * @param request the request + * @return the strategy + * @since 10.0 + */ + static GrailsContentNegotiationStrategy forRequest(HttpServletRequest request) { + ApplicationContext applicationContext = GrailsWebRequest.lookup()?.applicationContext + if (applicationContext == null && request.servletContext != null) { + applicationContext = WebApplicationContextUtils.getWebApplicationContext(request.servletContext) + } + GrailsContentNegotiationStrategy strategy = applicationContext?.getBeanProvider( + GrailsMimeTypesWebMvcConfigurer + )?.getIfAvailable()?.contentNegotiationStrategy + if (strategy != null) { + return strategy + } + Config config = applicationContext?.getBeanProvider(GrailsApplication)?.getIfAvailable()?.config + MimeUtility mimeUtility = applicationContext?.getBeanProvider(MimeUtility)?.getIfAvailable() + MimeType[] mimeTypes = mimeUtility?.knownMimeTypes as MimeType[] ?: MimeType.createDefaults() + ApplicationContext context = applicationContext + Supplier manager = () -> context?.containsBean(MVC_CONTENT_NEGOTIATION_MANAGER) ? + context.getBean(MVC_CONTENT_NEGOTIATION_MANAGER, ContentNegotiationManager) : null + return new GrailsContentNegotiationStrategy(mimeTypes, config ?: new PropertySourcesConfig(), manager) + } + + /** + * The media types Spring MVC's content negotiation manager resolves for a request, in its order, without the format + * the request names, which Grails resolves on its own, and with its {@code Accept} header ignored where the + * {@code grails.mime} settings ignore it, whether or not a URL mapping handles the request. + * + * @return the media types, or {@code null} where the manager finds the request not acceptable, as for an + * {@code Accept} header Spring cannot read + */ + private List requestedMediaTypes(HttpServletRequest request) { + ContentNegotiationManager manager = contentNegotiationManager?.get() + List strategies = manager != null ? manager.strategies : + [new HeaderContentNegotiationStrategy()] as List + boolean acceptHeaderIgnored = ignoresAcceptHeader(request) + ServletWebRequest webRequest = new ServletWebRequest(request) + try { + for (ContentNegotiationStrategy strategy in strategies) { + if (strategy instanceof GrailsFormatContentNegotiationStrategy || + (acceptHeaderIgnored && strategy instanceof HeaderContentNegotiationStrategy)) { + continue + } + List resolved = strategy.resolveMediaTypes(webRequest) + if (resolved != ContentNegotiationStrategy.MEDIA_TYPE_ALL_LIST) { + return resolved + } + } + } + catch (HttpMediaTypeNotAcceptableException e) { + LOG.debug('The request accepts no format: {}', e.message) + return null + } + return ContentNegotiationStrategy.MEDIA_TYPE_ALL_LIST + } + + /** + * The configured MIME types media types stand for, in the order of the media types, each once, with the parameters, + * such as the quality, of the first that stands for it; and a wildcard no type is configured with the name of, such as + * {@code text/*}, where a configured type is compatible with it, as itself, of the {@code all} format, which + * {@code withFormat} and {@code respond} answer, as Spring MVC answers a wildcard, in the first of their formats that + * has a type compatible with it. + */ + private MimeType[] acceptedMimeTypes(List requested) { + Set listed = Collections.newSetFromMap(new IdentityHashMap()) + Set wildcards = [] + List resolved = [] + for (MediaType mediaType in requested) { + MimeType configured = configuredFor(mediaType) + if (configured != null) { + if (listed.add(configured)) { + resolved << withParameters(configured, mediaType) + } + continue + } + String name = "${mediaType.type}/${mediaType.subtype}" + if (!mediaType.isConcrete() && compatibleWithConfigured(mediaType) && wildcards.add(name)) { + resolved << withParameters(new MimeType(name, ALL_FORMAT), mediaType) + } + } + return resolved as MimeType[] + } + + /** + * Whether a type a request accepts accepts a format: a type of the format; or, of the {@code all} format, the type + * {@code */*}, which accepts any, or a wildcard, such as {@code text/*}, which accepts a format of which a + * configured type is compatible with it. + * + * @param accepted a type {@code response.mimeTypes} resolves + * @param format a format + * @return whether it accepts the format + * @since 10.0 + */ + boolean acceptsFormat(MimeType accepted, String format) { + if (accepted == null) { + return false + } + if (accepted.extension != ALL_FORMAT) { + return accepted.extension == format + } + MediaType wildcard = mediaTypeOf(new MimeType(accepted.name)) + if (wildcard == null || wildcard.isWildcardType()) { + return wildcard != null + } + for (int i = 0; i < mimeTypes.length; i++) { + if (mimeTypes[i].extension == format && mediaTypes[i] != null && wildcard.isCompatibleWith(mediaTypes[i])) { + return true + } + } + return false + } + + /** Whether a configured type that is not itself a wildcard is compatible with a wildcard. */ + private boolean compatibleWithConfigured(MediaType wildcard) { + return mediaTypes.any { MediaType configured -> configured != null && configured.isConcrete() && wildcard.isCompatibleWith(configured) } + } + + /** + * The configured MIME type a media type stands for: the one of its name, of its version where several are; and, for + * a type that is not a wildcard with the {@code +json} or {@code +xml} suffix no type is configured with the name of, + * the own type of the format of that suffix, {@code application/json} or {@code application/xml} where that is + * configured, and otherwise the first type of the format. + * + * @return the type, or {@code null} where no configured type stands for it + */ + private MimeType configuredFor(MediaType mediaType) { + String name = "${mediaType.type}/${mediaType.subtype}" + List named = mimeTypes.findAll { MimeType configured -> configured.name?.equalsIgnoreCase(name) } as List + if (named) { + String version = mediaType.getParameter('v') + return (version ? named.find { MimeType configured -> configured.version == version } : null) ?: named.first() + } + String format = mediaType.isConcrete() ? mediaType.subtypeSuffix?.toLowerCase(Locale.ROOT) : null + if (format != null && SUFFIX_FORMATS.contains(format)) { + String own = "application/${format}" + return mimeTypes.find { MimeType configured -> + configured.extension == format && configured.name?.equalsIgnoreCase(own) + } ?: mimeTypes.find { MimeType configured -> configured.extension == format } + } + return null + } + + /** A configured MIME type with the parameters of the media type it is resolved for, as Grails reads them. */ + private static MimeType withParameters(MimeType configured, MediaType mediaType) { + Map parameters = [:] + mediaType.parameters.each { String name, String value -> parameters.put(name.toLowerCase(Locale.ROOT), value) } + return new MimeType(configured.name, configured.extension, parameters) + } + + /** The media type of a configured MIME type, without the quality Grails gives every MIME type. */ + private static MediaType mediaTypeOf(MimeType mimeType) { + try { + return SpringMediaTypeAdapter.toMediaType(mimeType)?.removeQualityValue() + } + catch (IllegalArgumentException ignored) { + return null + } + } + + private String resolveAcceptHeader(HttpServletRequest request) { + return ignoresAcceptHeader(request) ? null : request.getHeader(HttpHeaders.ACCEPT) } /** diff --git a/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsFormatContentNegotiationStrategy.groovy b/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsFormatContentNegotiationStrategy.groovy new file mode 100644 index 00000000000..b5e15c878ed --- /dev/null +++ b/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsFormatContentNegotiationStrategy.groovy @@ -0,0 +1,74 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.web.mime + +import java.util.function.Supplier + +import groovy.transform.CompileStatic + +import jakarta.servlet.http.HttpServletRequest + +import org.springframework.http.MediaType +import org.springframework.web.HttpMediaTypeNotAcceptableException +import org.springframework.web.accept.ContentNegotiationStrategy +import org.springframework.web.context.request.NativeWebRequest + +import org.grails.web.servlet.mvc.GrailsWebRequest + +/** + * The strategy of Spring MVC's content negotiation manager that resolves the format a request a URL mapping handles + * names, with its {@code format} parameter or a URL extension, to the media types {@code grails.mime.types} gives it, + * ahead of Spring MVC's own strategies, so that what Spring MVC writes for it is in the format Grails resolves for it. It + * leaves any other request, such as one a Spring MVC endpoint handles, to them, and so a request whose format names no + * type, as the {@code all} format does. A request that names a format no type is configured for is not acceptable. + * + * @since 10.0 + */ +@CompileStatic +class GrailsFormatContentNegotiationStrategy implements ContentNegotiationStrategy { + + private final Supplier grailsStrategy + + /** + * @param grailsStrategy supplies the strategy that resolves Grails formats, which Spring MVC's manager is created + * before + */ + GrailsFormatContentNegotiationStrategy(Supplier grailsStrategy) { + this.grailsStrategy = grailsStrategy + } + + @Override + List resolveMediaTypes(NativeWebRequest webRequest) throws HttpMediaTypeNotAcceptableException { + HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest) + if (request == null || !GrailsContentNegotiationStrategy.handlesRequest(request)) { + return MEDIA_TYPE_ALL_LIST + } + GrailsWebRequest grailsRequest = GrailsWebRequest.lookup(request) + GrailsContentNegotiationStrategy strategy = grailsStrategy.get() + Object format = grailsRequest != null ? strategy.requestedFormat(grailsRequest) : null + if (format == null) { + return MEDIA_TYPE_ALL_LIST + } + List mediaTypes = strategy.mediaTypesOf(format.toString()) + if (mediaTypes == null) { + throw new HttpMediaTypeNotAcceptableException(strategy.configuredMediaTypes) + } + return mediaTypes + } +} diff --git a/grails-mimetypes/src/main/groovy/org/grails/web/mime/HttpServletRequestExtension.groovy b/grails-mimetypes/src/main/groovy/org/grails/web/mime/HttpServletRequestExtension.groovy index 8ab811ab03d..3ebde5ca051 100644 --- a/grails-mimetypes/src/main/groovy/org/grails/web/mime/HttpServletRequestExtension.groovy +++ b/grails-mimetypes/src/main/groovy/org/grails/web/mime/HttpServletRequestExtension.groovy @@ -79,7 +79,9 @@ class HttpServletRequestExtension { } /** - * Obtains a list of configured {@link grails.web.mime.MimeType} instances for the request + * Obtains a list of configured {@link grails.web.mime.MimeType} instances for the request: with + * {@code grails.mime.negotiation.spring}, the type of its {@code Content-Type} as Spring reads it, as the format + * {@code grails.mime.types} gives it * * @param request The request * @return A list of configured mime types @@ -87,6 +89,12 @@ class HttpServletRequestExtension { static MimeType[] getMimeTypes(HttpServletRequest request) { MimeType[] result = (MimeType[]) request.getAttribute(GrailsApplicationAttributes.REQUEST_FORMATS) if (!result) { + GrailsContentNegotiationStrategy strategy = GrailsContentNegotiationStrategy.forRequest(request) + if (strategy.springNegotiation) { + result = strategy.resolveContentMimeTypes(request) + request.setAttribute(GrailsApplicationAttributes.REQUEST_FORMATS, result) + return result + } // First try to get context from GrailsWebRequest ApplicationContext context = null GrailsWebRequest webRequest = GrailsWebRequest.lookup() diff --git a/grails-mimetypes/src/main/groovy/org/grails/web/mime/HttpServletResponseExtension.groovy b/grails-mimetypes/src/main/groovy/org/grails/web/mime/HttpServletResponseExtension.groovy index f416c9cabcd..391c6c2aeb3 100755 --- a/grails-mimetypes/src/main/groovy/org/grails/web/mime/HttpServletResponseExtension.groovy +++ b/grails-mimetypes/src/main/groovy/org/grails/web/mime/HttpServletResponseExtension.groovy @@ -24,13 +24,9 @@ import jakarta.servlet.http.HttpServletRequest import jakarta.servlet.http.HttpServletResponse import org.springframework.context.ApplicationContext -import org.springframework.web.context.support.WebApplicationContextUtils -import grails.config.Config -import grails.core.GrailsApplication import grails.web.mime.MimeType import grails.web.mime.MimeUtility -import org.grails.config.PropertySourcesConfig import org.grails.plugins.web.api.MimeTypesApiSupport import org.grails.web.servlet.mvc.GrailsWebRequest import org.grails.web.util.GrailsApplicationAttributes @@ -71,7 +67,13 @@ class HttpServletResponseExtension { final mimeType = getMimeType(response) if (mimeType) { result = mimeType.extension - request.setAttribute(GrailsApplicationAttributes.RESPONSE_FORMAT, result) + // A wildcard such as text/*, which grails.mime.negotiation.spring resolves to a type of the all format, + // is not saved as the format: withFormat and respond answer it with the formats compatible with it, and + // the all format the saved format stands for would answer with any + if (!(mimeType.extension == 'all' && mimeType.name != MimeType.ALL.name && + GrailsContentNegotiationStrategy.forRequest(request).springNegotiation)) { + request.setAttribute(GrailsApplicationAttributes.RESPONSE_FORMAT, result) + } } } return result @@ -93,14 +95,15 @@ class HttpServletResponseExtension { HttpServletRequest request = webRequest.getRequest() MimeType result = (MimeType) request.getAttribute(GrailsApplicationAttributes.RESPONSE_MIME_TYPE) if (!result) { - def formatOverride = webRequest?.params?.format + GrailsContentNegotiationStrategy strategy = GrailsContentNegotiationStrategy.forRequest(request) + def formatOverride = strategy.requestedFormat(webRequest) if (!formatOverride) { formatOverride = request.getAttribute(GrailsApplicationAttributes.RESPONSE_FORMAT) } if (formatOverride) { - def allMimes = getMimeTypes() - MimeType mime = allMimes?.find { MimeType it -> it.extension == formatOverride } - result = mime ? mime : allMimes?.find { it } + // the configured type of the format, or, for a format that is not configured, the first configured + // type, or, with grails.mime.negotiation.spring, none + result = strategy.resolveFormat(formatOverride) // Save the evaluated format as a request attribute. // This is a blatant hack because we should to this @@ -115,7 +118,9 @@ class HttpServletResponseExtension { // - so the saved format is wrong request.setAttribute(GrailsApplicationAttributes.RESPONSE_MIME_TYPE, result) } else { - result = getMimeTypesInternal(request)[0] + // none where the request accepts no configured type, as with grails.mime.negotiation.spring + MimeType[] accepted = getMimeTypesInternal(request) + result = accepted ? accepted[0] : null } } @@ -142,15 +147,17 @@ class HttpServletResponseExtension { GrailsWebRequest webRequest = GrailsWebRequest.lookup() HttpServletRequest request = webRequest.getRequest() MimeType[] result = (MimeType[]) request.getAttribute(GrailsApplicationAttributes.RESPONSE_MIME_TYPES) - if (!result) { - def formatOverride = webRequest?.params?.format + if (result == null) { + GrailsContentNegotiationStrategy strategy = GrailsContentNegotiationStrategy.forRequest(request) + def formatOverride = strategy.requestedFormat(webRequest) if (!formatOverride) { formatOverride = request.getAttribute(GrailsApplicationAttributes.RESPONSE_FORMAT) } if (formatOverride) { - def allMimes = getMimeTypes() - MimeType mime = allMimes.find { MimeType it -> it.extension == formatOverride } - result = [ mime ? mime : getMimeTypes()[0] ] as MimeType[] + // with grails.mime.negotiation.spring, none for a format that is not configured, which the request + // accepts no configured type in + MimeType mime = strategy.resolveFormat(formatOverride) + result = mime != null ? [mime] as MimeType[] : new MimeType[0] // Save the evaluated format as a request attribute. // This is a blatant hack because we should to this @@ -185,21 +192,8 @@ class HttpServletResponseExtension { private static MimeType[] getMimeTypesInternal(HttpServletRequest request) { MimeType[] result = (MimeType[]) request.getAttribute(GrailsApplicationAttributes.RESPONSE_FORMATS) - if (!result) { - ApplicationContext applicationContext = GrailsWebRequest.lookup()?.applicationContext - if (applicationContext == null && request.servletContext != null) { - applicationContext = WebApplicationContextUtils.getWebApplicationContext(request.servletContext) - } - GrailsContentNegotiationStrategy strategy = applicationContext?.getBeanProvider( - GrailsMimeTypesWebMvcConfigurer - )?.getIfAvailable()?.contentNegotiationStrategy - if (strategy == null) { - // A context without MimeTypesConfiguration, such as one a test builds by hand, has no - // configurer: the strategy is built for the request from the application's settings - Config config = applicationContext?.getBeanProvider(GrailsApplication)?.getIfAvailable()?.config - strategy = new GrailsContentNegotiationStrategy(getMimeTypes(), config ?: new PropertySourcesConfig()) - } - result = strategy.resolveAcceptedMimeTypes(request) + if (result == null) { + result = GrailsContentNegotiationStrategy.forRequest(request).resolveAcceptedMimeTypes(request) request.setAttribute(GrailsApplicationAttributes.RESPONSE_FORMATS, result) } return result diff --git a/grails-mimetypes/src/test/groovy/org/grails/plugins/web/mime/MimeTypesConfigurationSpec.groovy b/grails-mimetypes/src/test/groovy/org/grails/plugins/web/mime/MimeTypesConfigurationSpec.groovy index 0f4850d6782..3f757c8b055 100644 --- a/grails-mimetypes/src/test/groovy/org/grails/plugins/web/mime/MimeTypesConfigurationSpec.groovy +++ b/grails-mimetypes/src/test/groovy/org/grails/plugins/web/mime/MimeTypesConfigurationSpec.groovy @@ -125,7 +125,7 @@ class MimeTypesConfigurationSpec extends Specification { def mimeUtility = Stub(ObjectProvider) { getObject() >> new DefaultMimeUtility(custom) } expect: - MimeTypesConfiguration.grailsMimeTypesWebMvcConfigurer(mimeUtility, new DefaultGrailsApplication()) + MimeTypesConfiguration.grailsMimeTypesWebMvcConfigurer(mimeUtility, new DefaultGrailsApplication(), Stub(ObjectProvider)) .contentNegotiationStrategy.configuredMimeTypes as List == custom as List } diff --git a/grails-mimetypes/src/test/groovy/org/grails/web/mime/SpringFormatNegotiationSpec.groovy b/grails-mimetypes/src/test/groovy/org/grails/web/mime/SpringFormatNegotiationSpec.groovy new file mode 100644 index 00000000000..11915007f54 --- /dev/null +++ b/grails-mimetypes/src/test/groovy/org/grails/web/mime/SpringFormatNegotiationSpec.groovy @@ -0,0 +1,459 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.web.mime + +import org.springframework.context.ApplicationContext +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.context.annotation.Import +import org.springframework.core.env.ConfigurableEnvironment +import org.springframework.core.env.MapPropertySource +import org.springframework.http.MediaType +import org.springframework.mock.web.MockHttpServletRequest +import org.springframework.mock.web.MockHttpServletResponse +import org.springframework.mock.web.MockServletContext +import org.springframework.web.HttpMediaTypeNotAcceptableException +import org.springframework.web.accept.ContentNegotiationManager +import org.springframework.web.accept.ContentNegotiationStrategy +import org.springframework.web.accept.HeaderContentNegotiationStrategy +import org.springframework.web.accept.ParameterContentNegotiationStrategy +import org.springframework.web.context.request.RequestContextHolder +import org.springframework.web.context.request.ServletWebRequest +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext +import org.springframework.web.method.HandlerMethod +import org.springframework.web.servlet.HandlerMapping +import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer +import org.springframework.web.servlet.config.annotation.EnableWebMvc +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer + +import grails.config.Settings +import grails.core.DefaultGrailsApplication +import grails.core.GrailsApplication +import grails.util.GrailsWebMockUtil +import grails.web.mapping.UrlMappingInfo +import org.grails.config.PropertySourcesConfig +import org.grails.plugins.web.mime.MimeTypesConfiguration +import org.grails.web.servlet.mvc.GrailsWebRequest +import org.grails.web.util.GrailsApplicationAttributes +import spock.lang.AutoCleanup +import spock.lang.Specification + +/** + * With {@code grails.mime.negotiation.spring}, Grails formats are resolved with Spring MVC's content negotiation manager, + * as the formats {@code grails.mime.types} gives the media types it resolves, and the manager applies the + * {@code grails.mime} rules Grails keeps to the requests a URL mapping handles. + */ +class SpringFormatNegotiationSpec extends Specification { + + @AutoCleanup + AnnotationConfigWebApplicationContext context + + void cleanup() { + RequestContextHolder.resetRequestAttributes() + } + + void "without the setting, Spring MVC's manager is left as it is"() { + when: + start([:]) + + then: + manager().getClass() == ContentNegotiationManager + } + + void "with the setting, Spring MVC's manager resolves a Grails format ahead of its own strategies, and keeps its formats"() { + when: + start((Settings.MIME_NEGOTIATION_SPRING): true) + def manager = manager() + + then: 'the format strategy goes first, and the Accept header is still read by a header strategy' + manager instanceof GrailsContentNegotiationManager + manager.strategies.first() instanceof GrailsFormatContentNegotiationStrategy + manager.getStrategy(HeaderContentNegotiationStrategy) instanceof GrailsAcceptHeaderContentNegotiationStrategy + + and: 'the formats Grails registers with Spring MVC are those of its manager' + manager.mediaTypeMappings['hal'] == MediaType.parseMediaType('application/hal+json') + manager.resolveFileExtensions(MediaType.APPLICATION_JSON) == ['json'] + manager.mediaTypeMappings == ((GrailsContentNegotiationManager) manager).springManager.mediaTypeMappings + + and: 'it is the one content negotiation strategy bean, which Spring Security adopts' + context.getBeansOfType(ContentNegotiationStrategy).keySet() == [GrailsContentNegotiationStrategy.MVC_CONTENT_NEGOTIATION_MANAGER] as Set + } + + void 'the format a request names decides what Spring MVC writes for it only while a URL mapping handles it: #handled'() { + given: + start((Settings.MIME_NEGOTIATION_SPRING): true) + def request = request('Accept': 'application/json') + handledBy(request, handler) + GrailsWebRequest.lookup().params.format = 'xml' + + expect: + manager().resolveMediaTypes(new ServletWebRequest(request)) == resolved + + where: + handled | handler || resolved + 'a URL mapping' | urlMapping() || ['text/xml', 'application/xml', 'application/*+xml'].collect { MediaType.parseMediaType(it) } + 'a Spring MVC endpoint' | endpoint() || [MediaType.APPLICATION_JSON] + 'a filter, before any handler' | null || [MediaType.APPLICATION_JSON] + 'a filter, after a controller dispatch ended' | 'controller' || [MediaType.APPLICATION_JSON] + } + + void 'a request a URL mapping handles that names a format no type is configured for is not acceptable to Spring MVC'() { + given: + start((Settings.MIME_NEGOTIATION_SPRING): true) + def request = request('Accept': 'application/json') + handledBy(request, urlMapping()) + GrailsWebRequest.lookup().params.format = 'unknown' + + when: + manager().resolveMediaTypes(new ServletWebRequest(request)) + + then: + HttpMediaTypeNotAcceptableException e = thrown() + e.supportedMediaTypes.contains(MediaType.APPLICATION_JSON) + + and: 'Grails resolves no format for it' + GrailsWebRequest.lookup().currentResponse.format == null + GrailsWebRequest.lookup().currentResponse.mimeType == null + } + + void 'the all format names no type, so the Accept header decides, for Spring MVC and for Grails'() { + given: + start((Settings.MIME_NEGOTIATION_SPRING): true) + def request = request('Accept': 'application/json') + handledBy(request, urlMapping()) + GrailsWebRequest.lookup().params.format = 'all' + + expect: + manager().resolveMediaTypes(new ServletWebRequest(request)) == [MediaType.APPLICATION_JSON] + GrailsWebRequest.lookup().currentResponse.format == 'json' + } + + void 'the first value of the format parameter is the format, as Spring MVC reads its parameter'() { + given: + start((Settings.MIME_NEGOTIATION_SPRING): true) + def request = request([:]) + request.addParameter('format', 'xml', 'json') + handledBy(request, urlMapping()) + + expect: + GrailsWebRequest.lookup().currentResponse.format == 'xml' + manager().resolveMediaTypes(new ServletWebRequest(request)).first() == MediaType.TEXT_XML + } + + void 'the Accept header of a user agent the settings ignore it for is ignored by Spring MVC for a #handled request only'() { + given: + start((Settings.MIME_NEGOTIATION_SPRING): true, (Settings.MIME_DISABLE_ACCEPT_HEADER_FOR_USER_AGENTS): ['Gecko']) + def request = request('Accept': 'application/json', 'User-Agent': 'Mozilla/5.0 Gecko/20100101') + handledBy(request, handler) + + expect: + manager().resolveMediaTypes(new ServletWebRequest(request)) == resolved + GrailsWebRequest.lookup().currentResponse.format == 'all' + + where: + handled | handler || resolved + 'URL mapping' | urlMapping() || ContentNegotiationStrategy.MEDIA_TYPE_ALL_LIST + 'Spring MVC endpoint' | endpoint() || [MediaType.APPLICATION_JSON] + } + + void 'an XHR request keeps its Accept header unless the settings ignore it for XHR requests too'() { + given: + start((Settings.MIME_NEGOTIATION_SPRING): true, (Settings.MIME_DISABLE_ACCEPT_HEADER_FOR_USER_AGENTS): ['Gecko'], + (Settings.MIME_DISABLE_ACCEPT_HEADER_FOR_USER_AGENTS_XHR): xhrToo) + def request = request('Accept': 'application/json', 'User-Agent': 'Gecko', 'X-Requested-With': 'XMLHttpRequest') + handledBy(request, urlMapping()) + + expect: + manager().resolveMediaTypes(new ServletWebRequest(request)) == resolved + GrailsWebRequest.lookup().currentResponse.format == format + + where: + xhrToo || resolved | format + false || [MediaType.APPLICATION_JSON] | 'json' + true || ContentNegotiationStrategy.MEDIA_TYPE_ALL_LIST | 'all' + } + + void 'grails.mime.use.accept.header: false ignores the Accept header of a request a URL mapping handles'() { + given: + start((Settings.MIME_NEGOTIATION_SPRING): true, (Settings.MIME_USE_ACCEPT_HEADER): false) + def request = request('Accept': 'application/json') + handledBy(request, urlMapping()) + + expect: + manager().resolveMediaTypes(new ServletWebRequest(request)) == ContentNegotiationStrategy.MEDIA_TYPE_ALL_LIST + GrailsWebRequest.lookup().currentResponse.format == 'all' + } + + void 'the Accept header #accept resolves to the formats #formats, in the order Spring MVC resolves its types in'() { + given: + start((Settings.MIME_NEGOTIATION_SPRING): true) + request('Accept': accept) + + expect: + GrailsWebRequest.lookup().currentResponse.mimeTypes*.extension == formats + GrailsWebRequest.lookup().currentResponse.format == formats[0] + + where: + accept || formats + '*/*' || ['all'] + '*/*, application/json' || ['json', 'all'] + 'application/json;q=0.5, application/xml' || ['xml', 'json'] + 'text/html;q=0.9, application/json;q=0.9' || ['html', 'json'] + 'text/*' || ['all'] + 'application/*+json' || ['all'] + 'image/*' || [] + 'application/problem+json' || ['json'] + 'application/vnd.api+json, application/xml' || ['json', 'xml'] + 'application/soap+xml' || ['xml'] + 'application/xml, application/atom+xml' || ['xml', 'atom'] + 'text/xml;q=0.5, application/xml;q=0.9, application/json' || ['json', 'xml', 'xml'] + 'image/png' || [] + 'application/json;q=abc' || [] + } + + void 'a configured type of the Accept header keeps its parameters, the quality among them'() { + given: + start((Settings.MIME_NEGOTIATION_SPRING): true) + request('Accept': 'application/json;q=0.8;profile=compact') + + expect: + with(GrailsWebRequest.lookup().currentResponse.mimeTypes.first()) { + name == 'application/json' + extension == 'json' + parameters == [q: '0.8', profile: 'compact'] + } + } + + void 'a client that accepts any type is of the all format, also where it is not configured'() { + given: + start((Settings.MIME_NEGOTIATION_SPRING): true, 'grails.mime.types': [json: 'application/json', html: 'text/html']) + request('Accept': '*/*') + + expect: + with(GrailsWebRequest.lookup().currentResponse.mimeTypes.toList()) { + it*.name == ['*/*'] + it*.extension == ['all'] + } + GrailsWebRequest.lookup().currentResponse.withFormat { + html { 'html' } + json { 'json' } + } == 'html' + } + + void 'a wildcard is of the all format, which withFormat answers with the first block compatible with it: #result for #accept'() { + given: + start((Settings.MIME_NEGOTIATION_SPRING): true) + request('Accept': accept) + + expect: + GrailsWebRequest.lookup().currentResponse.mimeTypes*.name == [accept] + GrailsWebRequest.lookup().currentResponse.format == 'all' + GrailsWebRequest.lookup().currentResponse.withFormat { + pdf { 'pdf' } + json { 'json' } + html { 'html' } + } == result + + where: + accept || result + 'text/*' || 'json' + 'application/*+xml' || 'html' + 'application/*' || 'pdf' + } + + void 'a wildcard no block is compatible with leaves withFormat to the next type the request accepts'() { + given: + start((Settings.MIME_NEGOTIATION_SPRING): true) + request('Accept': 'text/*, application/pdf;q=0.5') + + expect: + GrailsWebRequest.lookup().currentResponse.format == 'all' + GrailsWebRequest.lookup().currentResponse.withFormat { + pdf { 'pdf' } + } == 'pdf' + } + + void 'withFormat runs the block of the first format the request accepts that has one: #result for #accept'() { + given: + start((Settings.MIME_NEGOTIATION_SPRING): true) + request('Accept': accept) + + expect: + GrailsWebRequest.lookup().currentResponse.withFormat { + json { 'json' } + text { 'text' } + html { 'html' } + } == result + + where: + accept || result + 'application/xml, */*, text/html' || 'html' + 'application/xml, text/csv, */*' || 'json' + 'application/xml, text/csv' || null + 'image/png' || null + } + + void "withFormat runs its * block for a request that accepts no configured type"() { + given: + start((Settings.MIME_NEGOTIATION_SPRING): true) + request('Accept': 'image/png') + + expect: + GrailsWebRequest.lookup().currentResponse.withFormat { + json { 'json' } + '*' { 'any' } + } == 'any' + } + + void "Spring MVC's parameter negotiation, which the application enables, resolves Grails formats too"() { + given: 'parameter negotiation with a parameter of its own name, as spring.mvc.contentnegotiation.* enables it' + start((Settings.MIME_NEGOTIATION_SPRING): true, (ParameterConfiguration.ENABLED): true) + def request = request('Accept': 'application/json') + request.addParameter('mediaType', 'xml') + + expect: + manager().getStrategy(ParameterContentNegotiationStrategy) != null + GrailsWebRequest.lookup().currentResponse.format == 'xml' + } + + void 'the Content-Type #contentType is the request format #format'() { + given: + start((Settings.MIME_NEGOTIATION_SPRING): true) + def request = request([:]) + if (contentType) { + request.contentType = contentType + } + + expect: + request.format == format + + where: + contentType || format + null || 'all' + 'application/json;charset=UTF-8' || 'json' + 'APPLICATION/JSON' || 'json' + 'text/xml' || 'xml' + 'application/hal+json' || 'hal' + 'application/vnd.acme+json' || 'json' + 'application/problem+xml' || 'xml' + 'application/x-www-form-urlencoded' || 'form' + 'image/png' || null + 'application/json;charset=' || null + } + + void 'a Content-Type keeps its parameters, and one no type is configured for is the type itself, in lower case'() { + given: + start((Settings.MIME_NEGOTIATION_SPRING): true) + def request = request([:]) + + when: + request.contentType = 'application/json;charset=UTF-8' + + then: + request.mimeTypes*.name == ['application/json'] + request.mimeTypes[0].parameters.charset == 'UTF-8' + + when: + request.removeAttribute(GrailsApplicationAttributes.REQUEST_FORMATS) + request.contentType = 'Image/PNG' + + then: + request.mimeTypes*.name == ['image/png'] + } + + void 'without the setting, the Grails 9 rules resolve the formats'() { + given: + start([:]) + request('Accept': '*/*, application/json') + + expect: + GrailsWebRequest.lookup().currentResponse.format == 'all' + } + + private void start(Map properties) { + context = new AnnotationConfigWebApplicationContext() + context.servletContext = new MockServletContext() + context.environment.propertySources.addFirst(new MapPropertySource('test', properties)) + context.register(NegotiationConfiguration) + context.refresh() + } + + private ContentNegotiationManager manager() { + context.getBean(GrailsContentNegotiationStrategy.MVC_CONTENT_NEGOTIATION_MANAGER, ContentNegotiationManager) + } + + private MockHttpServletRequest request(Map headers) { + def request = new MockHttpServletRequest(context.servletContext) + headers.each { String name, String value -> request.addHeader(name, value) } + GrailsWebMockUtil.bindMockWebRequest(context, request, new MockHttpServletResponse()) + request + } + + private static void handledBy(MockHttpServletRequest request, Object handler) { + if (handler == 'controller') { + request.setAttribute(GrailsApplicationAttributes.CONTROLLER, new Object()) + } + else if (handler != null) { + request.setAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE, handler) + } + } + + private UrlMappingInfo urlMapping() { + Stub(UrlMappingInfo) + } + + private static HandlerMethod endpoint() { + new HandlerMethod(new Object(), Object.getMethod('toString')) + } + + @Configuration(proxyBeanMethods = false) + @EnableWebMvc + @Import([MimeTypesConfiguration, ParameterConfiguration]) + static class NegotiationConfiguration { + + @Bean + GrailsApplication grailsApplication(ApplicationContext applicationContext, ConfigurableEnvironment environment) { + def application = new DefaultGrailsApplication() + application.config = new PropertySourcesConfig(environment.propertySources) + application.applicationContext = applicationContext + application.mainContext = applicationContext + application + } + } + + /** Enables Spring MVC's parameter negotiation, as {@code spring.mvc.contentnegotiation.*} does, where the test says. */ + @Configuration(proxyBeanMethods = false) + static class ParameterConfiguration implements WebMvcConfigurer { + + static final String ENABLED = 'test.parameter.negotiation' + + private final boolean enabled + + ParameterConfiguration(ConfigurableEnvironment environment) { + enabled = environment.getProperty(ENABLED, Boolean, false) + } + + @Override + void configureContentNegotiation(ContentNegotiationConfigurer configurer) { + if (enabled) { + configurer.favorParameter(true).parameterName('mediaType') + } + } + } +} diff --git a/grails-mimetypes/src/test/groovy/org/grails/web/servlet/mvc/RequestAndResponseMimeTypesApiSpec.groovy b/grails-mimetypes/src/test/groovy/org/grails/web/servlet/mvc/RequestAndResponseMimeTypesApiSpec.groovy index 83e21580f58..9534626371a 100644 --- a/grails-mimetypes/src/test/groovy/org/grails/web/servlet/mvc/RequestAndResponseMimeTypesApiSpec.groovy +++ b/grails-mimetypes/src/test/groovy/org/grails/web/servlet/mvc/RequestAndResponseMimeTypesApiSpec.groovy @@ -226,7 +226,7 @@ class RequestAndResponseMimeTypesApiSpec extends Specification{ def context = (GenericWebApplicationContext) webRequest.applicationContext def own = new DefaultMimeUtility((buildMimeTypes() + new MimeType('application/vnd.own+json', 'own')) as MimeType[]) def configurer = Spy(MimeTypesConfiguration.grailsMimeTypesWebMvcConfigurer( - Stub(ObjectProvider) { getObject() >> own }, (GrailsApplication) application)) + Stub(ObjectProvider) { getObject() >> own }, (GrailsApplication) application, Stub(ObjectProvider))) context.beanFactory.registerSingleton('grailsMimeTypesWebMvcConfigurer', configurer) webRequest.currentRequest.addHeader('Accept', accept) @@ -337,6 +337,23 @@ class RequestAndResponseMimeTypesApiSpec extends Specification{ } + void "the response type is that of the block withFormat runs for a client that accepts any type"() { + given: + final webRequest = boundMimeTypeRequest() + webRequest.currentRequest.addHeader('Accept', '*/*') + + when: + def result = webRequest.currentResponse.withFormat { + xml { 'got xml' } + html { 'got html' } + } + + then: 'as a view is looked up for it afterwards' + result == 'got xml' + webRequest.currentResponse.format == 'xml' + webRequest.currentResponse.mimeType.extension == 'xml' + } + @Issue("GRAILS-10973") void "request.withFormat should choose wildcard choice when format == all"() { when: @@ -464,7 +481,7 @@ class RequestAndResponseMimeTypesApiSpec extends Specification{ webRequest.currentRequest.addHeader('User-Agent', 'Trident/7.0') expect: 'the configurer is created, and the other entry is found' - MimeTypesConfiguration.grailsMimeTypesWebMvcConfigurer(Stub(ObjectProvider), (GrailsApplication) application) + MimeTypesConfiguration.grailsMimeTypesWebMvcConfigurer(Stub(ObjectProvider), (GrailsApplication) application, Stub(ObjectProvider)) webRequest.currentResponse.withFormat { json { 'got json' } html { 'got html' } @@ -478,7 +495,7 @@ class RequestAndResponseMimeTypesApiSpec extends Specification{ application.setConfig(config) when: 'the configurer an application has is created' - MimeTypesConfiguration.grailsMimeTypesWebMvcConfigurer(Stub(ObjectProvider), (GrailsApplication) application) + MimeTypesConfiguration.grailsMimeTypesWebMvcConfigurer(Stub(ObjectProvider), (GrailsApplication) application, Stub(ObjectProvider)) then: IllegalArgumentException e = thrown() diff --git a/grails-rest-transforms/src/main/groovy/grails/artefact/controller/RestResponder.groovy b/grails-rest-transforms/src/main/groovy/grails/artefact/controller/RestResponder.groovy index 8a0a983716b..0cd6cfb0bd7 100644 --- a/grails-rest-transforms/src/main/groovy/grails/artefact/controller/RestResponder.groovy +++ b/grails-rest-transforms/src/main/groovy/grails/artefact/controller/RestResponder.groovy @@ -30,6 +30,7 @@ import org.springframework.beans.factory.annotation.Autowired import org.springframework.context.ApplicationContext import org.springframework.context.i18n.LocaleContextHolder import org.springframework.http.HttpStatus +import org.springframework.http.InvalidMediaTypeException import org.springframework.http.MediaType import org.springframework.http.ProblemDetail import org.springframework.validation.BeanPropertyBindingResult @@ -50,6 +51,7 @@ import org.grails.plugins.web.rest.render.SelectiveRenderer import org.grails.plugins.web.rest.render.ServletRenderContext import org.grails.plugins.web.rest.render.html.DefaultHtmlRenderer import org.grails.web.gsp.io.GrailsConventionGroovyPageLocator +import org.grails.web.mime.GrailsContentNegotiationStrategy import org.grails.web.servlet.mvc.GrailsWebRequest import org.grails.web.util.GrailsApplicationAttributes @@ -169,6 +171,8 @@ trait RestResponder { List formats = calculateFormats(webRequest.actionName, value, args) final response = webRequest.getCurrentResponse() MimeType[] mimeTypes = getResponseFormat(response) + // with grails.mime.negotiation.spring, a wildcard such as text/* is a type of the all format too + boolean springNegotiation = GrailsContentNegotiationStrategy.forRequest(webRequest.currentRequest).springNegotiation def registry = rendererRegistry if (registry == null) { // a controller Spring did not create, such as one a test creates with new, responds with the registry an @@ -209,7 +213,9 @@ trait RestResponder { } // The response is in the first type the client accepts, in its order, that the action responds in and a renderer - // renders the value in; a client that accepts any type is answered in the first of the action's formats that is + // renders the value in; a client that accepts any type is answered in the first of the action's formats that is, + // and one that accepts a wildcard such as text/*, which grails.mime.negotiation.spring resolves to a type of the + // all format, in the first of them with a type compatible with it, as Spring MVC answers it MimeType matched = null boolean negotiating = true while (negotiating) { @@ -217,10 +223,11 @@ trait RestResponder { negotiation: for (MimeType accepted in mimeTypes) { List candidates = [accepted] - if (accepted == MimeType.ALL && formats) { + if ((accepted == MimeType.ALL || (springNegotiation && accepted.extension == 'all')) && formats) { final allMimeTypes = MimeType.getConfiguredMimeTypes() - candidates = formats.collect { String format -> allMimeTypes.find { MimeType mt -> mt.extension == format } } - .findAll { MimeType mt -> mt != null } + candidates = formats.collect { String format -> + allMimeTypes.find { MimeType mt -> mt.extension == format && compatible(accepted, mt) } + }.findAll { MimeType mt -> mt != null } } for (MimeType mimeType in candidates) { if (!formats.contains(mimeType.extension)) { @@ -332,6 +339,22 @@ trait RestResponder { callRender([status: statusCode ?: HttpStatus.NOT_ACCEPTABLE.value() ]) } + /** + * Whether a configured type is compatible with a type of the {@code all} format a client accepts: any is with + * {@code */*}, and a wildcard such as {@code text/*} is with the types Spring MVC finds compatible with it. + */ + private static boolean compatible(MimeType accepted, MimeType configured) { + if (accepted.name == MimeType.ALL.name) { + return true + } + try { + return MediaType.parseMediaType(accepted.name).isCompatibleWith(MediaType.parseMediaType(configured.name)) + } + catch (InvalidMediaTypeException ignored) { + return false + } + } + /** * Whether a renderer renders a value: one that renders some values only, such as that of markup views without a * renderer of XML, says so, as a Spring message converter says whether it writes a value. diff --git a/grails-test-suite-web/src/test/groovy/grails/config/MigrationSettingsMetadataSpec.groovy b/grails-test-suite-web/src/test/groovy/grails/config/MigrationSettingsMetadataSpec.groovy index ea39cabb6ef..3aca9f92f83 100644 --- a/grails-test-suite-web/src/test/groovy/grails/config/MigrationSettingsMetadataSpec.groovy +++ b/grails-test-suite-web/src/test/groovy/grails/config/MigrationSettingsMetadataSpec.groovy @@ -39,6 +39,7 @@ class MigrationSettingsMetadataSpec extends Specification { Settings.DATABINDING_JSON_JACKSON | Settings.DATABINDING_JSON_JACKSON_DEFAULT Settings.SETTING_EXCEPTION_RESOLVER_SPRING_HANDLERS | Settings.SETTING_EXCEPTION_RESOLVER_SPRING_HANDLERS_DEFAULT Settings.CONTROLLERS_RESPONSE_BODY | Settings.CONTROLLERS_RESPONSE_BODY_DEFAULT + Settings.MIME_NEGOTIATION_SPRING | Settings.MIME_NEGOTIATION_SPRING_DEFAULT } private static Map metadataDefaults() { diff --git a/grails-test-suite-web/src/test/groovy/grails/rest/web/SpringFormatNegotiationRespondSpec.groovy b/grails-test-suite-web/src/test/groovy/grails/rest/web/SpringFormatNegotiationRespondSpec.groovy new file mode 100644 index 00000000000..5a4195e05af --- /dev/null +++ b/grails-test-suite-web/src/test/groovy/grails/rest/web/SpringFormatNegotiationRespondSpec.groovy @@ -0,0 +1,234 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package grails.rest.web + +import org.springframework.http.HttpStatus +import org.springframework.http.MediaType +import org.springframework.http.ProblemDetail +import org.springframework.web.accept.ContentNegotiationManager +import org.springframework.web.context.request.ServletWebRequest + +import grails.artefact.Artefact +import grails.config.Settings +import grails.testing.web.controllers.ControllerUnitTest +import grails.validation.Validateable +import org.grails.web.mime.GrailsContentNegotiationManager +import spock.lang.Specification + +/** + * With {@code grails.mime.negotiation.spring}, {@code respond} answers in the format Spring MVC's content negotiation + * resolves for the request, and a request that accepts none of the action's formats is not acceptable. + */ +class SpringFormatNegotiationRespondSpec extends Specification implements ControllerUnitTest { + + Closure doWithConfig() { + { config -> + config[Settings.WEB_RENDERING_JSON_SPRING] = true + config[Settings.MIME_NEGOTIATION_SPRING] = true + } + } + + void "a web unit test negotiates with the manager Spring MVC negotiates with in an application"() { + expect: + applicationContext.getBean('mvcContentNegotiationManager', ContentNegotiationManager) instanceof GrailsContentNegotiationManager + } + + void 'what Spring MVC writes for an action of a web unit test is negotiated with the format the request names, as in an application'() { + given: + request.addHeader('Accept', 'application/json') + params.format = 'xml' + + when: 'the test calls the action of the controller' + controller + + then: + applicationContext.getBean('mvcContentNegotiationManager', ContentNegotiationManager) + .resolveMediaTypes(new ServletWebRequest(request)).first() == MediaType.TEXT_XML + } + + void 'a body of a type with the +json suffix is bound as JSON'() { + given: + request.method = 'POST' + request.contentType = 'application/merge-patch+json' + request.content = '{"title":"Grails"}'.bytes + + when: + def model = controller.save() + + then: + request.format == 'json' + model.title == 'Grails' + } + + void 'a client that accepts #accept is answered in #format by an action that responds in XML and JSON'() { + given: + request.addHeader('Accept', accept) + + when: + controller.respond([title: 'Grails'], formats: ['xml', 'json']) + + then: + response.status == 200 + response.contentType.contains(format) + + where: + accept || format + '*/*, application/json' || 'json' + 'application/*+json' || 'json' + 'text/*' || 'xml' + 'application/merge-patch+json' || 'json' + 'application/soap+xml' || 'xml' + } + + void 'a client that accepts no format of the action is not acceptable, as to Spring MVC'() { + given: + request.addHeader('Accept', accept) + + when: + controller.respond([title: 'Grails'], formats: ['json']) + + then: + response.status == 406 + + where: + accept << ['image/png', 'image/*', 'application/json;q=2'] + } + + void 'a request that names a format no type is configured for is not acceptable'() { + given: + request.addHeader('Accept', 'application/json') + params.format = 'unknown' + + when: + controller.respond([title: 'Grails'], formats: ['json']) + + then: + response.status == 406 + } + + void 'the all format names no type, so the Accept header decides'() { + given: + request.addHeader('Accept', 'application/json') + params.format = 'all' + + when: + controller.respond([title: 'Grails'], formats: ['xml', 'json']) + + then: + response.contentType.startsWith('application/json') + response.json == [title: 'Grails'] + } + + void 'a problem for a client whose Accept header Spring MVC cannot read is answered with its status alone, as Spring MVC answers it'() { + given: + request.addHeader('Accept', 'application/json;q=2') + + when: + controller.respond(ProblemDetail.forStatus(HttpStatus.CONFLICT), formats: ['json']) + + then: + response.status == 409 + response.contentAsString == '' + } +} + +/** + * Without {@code grails.mime.negotiation.spring}, the Grails 9 rules resolve the format {@code respond} answers in. + */ +class GrailsFormatNegotiationRespondSpec extends Specification implements ControllerUnitTest { + + Closure doWithConfig() { + { config -> config[Settings.WEB_RENDERING_JSON_SPRING] = true } + } + + void "Spring MVC's manager is left as it is"() { + expect: + applicationContext.getBean('mvcContentNegotiationManager', ContentNegotiationManager).getClass() == ContentNegotiationManager + } + + void 'a client that accepts #accept is answered in #format, as in Grails 9'() { + given: + request.addHeader('Accept', accept) + + when: + controller.respond([title: 'Grails'], formats: ['xml', 'json']) + + then: + response.status == 200 + response.contentType.contains(format) + + where: + accept || format + '*/*, application/json' || 'xml' + 'application/*+json' || 'xml' + 'application/merge-patch+json' || 'xml' + 'image/png' || 'xml' + } + + void 'a body of a type with the +json suffix is of no format, and not bound'() { + given: + request.method = 'POST' + request.contentType = 'application/merge-patch+json' + request.content = '{"title":"Grails"}'.bytes + + when: + def model = controller.save() + + then: + request.format == null + model.title == null + } + + void 'a client that accepts any type of a version is answered 406, as in Grails 9'() { + given: + request.addHeader('Accept', '*/*;v=2') + + when: + controller.respond([title: 'Grails'], formats: ['json']) + + then: + response.status == 406 + } + + void 'a request that names a format no type is configured for is answered in the first configured type'() { + given: + request.addHeader('Accept', 'application/json') + params.format = 'unknown' + + when: + controller.respond([title: 'Grails'], formats: ['xml', 'json']) + + then: 'the first configured type is the all format, which the first format of the action answers' + response.status == 200 + response.contentType.contains('xml') + } +} + +@Artefact('Controller') +class NegotiatedResponseController { + + def save(NegotiatedCommand command) { + [title: command.title] + } +} + +class NegotiatedCommand implements Validateable { + + String title +} diff --git a/grails-testing-support-web/src/main/groovy/grails/testing/web/GrailsWebUnitTest.groovy b/grails-testing-support-web/src/main/groovy/grails/testing/web/GrailsWebUnitTest.groovy index 678d3b85f26..a66b100fd89 100644 --- a/grails-testing-support-web/src/main/groovy/grails/testing/web/GrailsWebUnitTest.groovy +++ b/grails-testing-support-web/src/main/groovy/grails/testing/web/GrailsWebUnitTest.groovy @@ -24,6 +24,7 @@ import groovy.transform.CompileStatic import org.springframework.mock.web.MockHttpSession import org.springframework.mock.web.MockServletContext +import org.springframework.web.servlet.HandlerMapping import grails.artefact.Controller import grails.artefact.TagLibrary @@ -44,6 +45,7 @@ import org.grails.plugins.testing.GrailsMockHttpServletRequest import org.grails.plugins.testing.GrailsMockHttpServletResponse import org.grails.taglib.TagLibraryLookup import org.grails.taglib.TagLibraryMetaUtils +import org.grails.web.mapping.ForwardUrlMappingInfo import org.grails.testing.GrailsUnitTest import org.grails.web.servlet.mvc.GrailsWebRequest import org.grails.web.util.GrailsApplicationAttributes @@ -148,6 +150,10 @@ trait GrailsWebUnitTest implements GrailsUnitTest { webRequest.request.setAttribute(GrailsApplicationAttributes.CONTROLLER, controller) webRequest.controllerName = GrailsNameUtils.getLogicalPropertyName(controller.class.name, ControllerArtefactHandler.TYPE) + // A URL mapping handles the request, as in an application, so that what Spring MVC writes for the action is + // negotiated as there + webRequest.request.setAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE, + new ForwardUrlMappingInfo(controllerName: webRequest.controllerName)) controller as T } diff --git a/grails-web-core/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/grails-web-core/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 0f764ab2fe8..7a236c80de6 100644 --- a/grails-web-core/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/grails-web-core/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -132,6 +132,12 @@ "description": "When true, XHR requests also respect the grails.mime.disable.accept.header.userAgents setting, while by default XHR requests ignore user agent filtering.", "defaultValue": false }, + { + "name": "grails.mime.negotiation.spring", + "type": "java.lang.Boolean", + "description": "Whether request.format, response.format, withFormat and the format respond writes in are resolved with Spring MVC's content negotiation manager, mapping the media types it resolves to the formats grails.mime.types gives them, rather than by the Grails rules. The format parameter, URL extensions and the grails.mime Accept header settings are kept, as strategies of Spring MVC's manager that also apply to what Spring MVC writes for the requests a URL mapping handles. Grails 9 negotiation stays the default in Grails 10.", + "defaultValue": false + }, { "name": "grails.converters.encoding", "type": "java.lang.String", From 56fa0da656200462e87e99f4cacc21087e71bb1f Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sat, 14 Nov 2026 10:41:59 -0800 Subject: [PATCH 010/115] Match URL mappings for security when a request accepts no configured type ReflectionUtils.matchAllUrlMappings read the version of the response type, which grails.mime.negotiation.spring resolves to none for a request that accepts no configured type, such as one that sends only Accept: image/png, so every such request failed in the security filter with a NullPointerException. It now matches any version, as for a type without one. --- .../springsecurity/ReflectionUtils.groovy | 3 ++- .../springsecurity/ReflectionUtilsSpec.groovy | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/grails-spring-security/plugin/src/main/groovy/grails/plugin/springsecurity/ReflectionUtils.groovy b/grails-spring-security/plugin/src/main/groovy/grails/plugin/springsecurity/ReflectionUtils.groovy index 5248fac5f11..1b1831212c5 100644 --- a/grails-spring-security/plugin/src/main/groovy/grails/plugin/springsecurity/ReflectionUtils.groovy +++ b/grails-spring-security/plugin/src/main/groovy/grails/plugin/springsecurity/ReflectionUtils.groovy @@ -212,7 +212,8 @@ class ReflectionUtils { // the mapping for a different action than the one about to execute - and authorize that one instead. // Under the servlet filter the request already reports the overridden method and this resolves to it. String method = HiddenHttpMethod.resolveOverride(grailsRequest.request) ?: grailsRequest.request.method - String version = grailsRequest.getHeader(ACCEPT_VERSION) ?: extension.getMimeTypeForRequest(grailsRequest).version + // none where the request accepts no configured type, as with grails.mime.negotiation.spring + String version = grailsRequest.getHeader(ACCEPT_VERSION) ?: extension.getMimeTypeForRequest(grailsRequest)?.version urlMappingsHolder.matchAll requestUrl, method, version == null ? UrlMapping.ANY_VERSION : version } diff --git a/grails-spring-security/plugin/src/test/groovy/grails/plugin/springsecurity/ReflectionUtilsSpec.groovy b/grails-spring-security/plugin/src/test/groovy/grails/plugin/springsecurity/ReflectionUtilsSpec.groovy index c4b380bae5a..78c5b1d1317 100644 --- a/grails-spring-security/plugin/src/test/groovy/grails/plugin/springsecurity/ReflectionUtilsSpec.groovy +++ b/grails-spring-security/plugin/src/test/groovy/grails/plugin/springsecurity/ReflectionUtilsSpec.groovy @@ -22,6 +22,7 @@ import org.springframework.http.HttpMethod import org.springframework.mock.web.MockHttpServletRequest import org.springframework.mock.web.MockHttpServletResponse +import grails.web.mapping.UrlMapping import grails.web.mapping.UrlMappingInfo import grails.web.mapping.UrlMappingsHolder import grails.web.mime.MimeType @@ -261,4 +262,24 @@ class ReflectionUtilsSpec extends AbstractUnitSpec { then: matchedWith == 'POST' } + + void 'url mappings are matched on any version for a request that accepts no configured type'() { + given: 'a request for which no type is resolved, as grails.mime.negotiation.spring resolves none for it' + def request = new MockHttpServletRequest(method: 'GET', requestURI: '/book') + request.addHeader('Accept', 'image/png') + def grailsRequest = new GrailsWebRequest(request, new MockHttpServletResponse(), servletContext) + + String matchedVersion = null + def holder = [matchAll: { String uri, String httpMethod, String version -> + matchedVersion = version + new UrlMappingInfo[0] + }] as UrlMappingsHolder + + when: + ReflectionUtils.matchAllUrlMappings(holder, '/book', grailsRequest, + [getMimeTypeForRequest: { Object... a -> null }] as HttpServletResponseExtension) + + then: + matchedVersion == UrlMapping.ANY_VERSION + } } From 11cb4b92d7c9d89690ffa8d310f6382a1ae98821 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sat, 14 Nov 2026 10:41:59 -0800 Subject: [PATCH 011/115] Record every answer Grails and Spring MVC negotiation give differently FormatNegotiationComparisonSpec runs a matrix of requests a URL mapping handles through both policies, with and without grails.mime.negotiation.spring, under several grails.mime.types and Accept header configurations: Accept headers with qualities, wildcards and +json/+xml types, browser and XHR user agents, the format parameter, and Content-Type. It records each answer that differs, from response.format, withFormat, request.format and the media types Spring MVC's manager resolves for what it writes, so that a change to either policy that changes an answer fails it, and checks that with the setting the first type the manager resolves is of the format Grails resolves. --- .../FormatNegotiationComparisonSpec.groovy | 476 ++++++++++++++++++ 1 file changed, 476 insertions(+) create mode 100644 grails-mimetypes/src/test/groovy/org/grails/web/mime/FormatNegotiationComparisonSpec.groovy diff --git a/grails-mimetypes/src/test/groovy/org/grails/web/mime/FormatNegotiationComparisonSpec.groovy b/grails-mimetypes/src/test/groovy/org/grails/web/mime/FormatNegotiationComparisonSpec.groovy new file mode 100644 index 00000000000..8ec724d1d45 --- /dev/null +++ b/grails-mimetypes/src/test/groovy/org/grails/web/mime/FormatNegotiationComparisonSpec.groovy @@ -0,0 +1,476 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.web.mime + +import org.springframework.context.ApplicationContext +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.context.annotation.Import +import org.springframework.core.env.ConfigurableEnvironment +import org.springframework.core.env.MapPropertySource +import org.springframework.mock.web.MockHttpServletRequest +import org.springframework.mock.web.MockHttpServletResponse +import org.springframework.mock.web.MockServletContext +import org.springframework.web.HttpMediaTypeNotAcceptableException +import org.springframework.web.accept.ContentNegotiationManager +import org.springframework.web.context.request.RequestContextHolder +import org.springframework.web.context.request.ServletWebRequest +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext +import org.springframework.web.servlet.HandlerMapping +import org.springframework.web.servlet.config.annotation.EnableWebMvc + +import grails.config.Settings +import grails.core.DefaultGrailsApplication +import grails.core.GrailsApplication +import grails.util.GrailsWebMockUtil +import grails.web.mapping.UrlMappingInfo +import org.grails.config.PropertySourcesConfig +import org.grails.plugins.web.mime.MimeTypesConfiguration +import org.grails.web.servlet.mvc.GrailsWebRequest +import spock.lang.Shared +import spock.lang.Specification + +/** + * Runs a matrix of requests a URL mapping handles through Grails' negotiation and through Spring MVC's, which + * {@code grails.mime.negotiation.spring} selects, under several configurations, and records every answer that differs: + * the format {@code response.format} resolves, the block {@code withFormat} runs, the format {@code request.format} + * resolves and the media types Spring MVC's content negotiation manager resolves for what it writes for the request. + * The record is the evidence the upgrade guide summarizes, and a change to either policy that changes an answer fails it. + */ +class FormatNegotiationComparisonSpec extends Specification { + + private static final String CHROME = 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8' + + private static final String BROWSER = 'Mozilla/5.0 (X11; Linux x86_64; rv:140.0) Gecko/20100101 Firefox/140.0' + + /** The configurations the requests are negotiated under. */ + @Shared + Map> configurations = [ + 'defaults' : [:], + 'merged vendor type' : [(Settings.MIME_TYPES_MERGE_DEFAULTS): true, (Settings.MIME_TYPES): [ + all: '*/*', + vnd: 'application/vnd.app1.v1+json' + ]], + 'without all' : [(Settings.MIME_TYPES): [ + html: ['text/html', 'application/xhtml+xml'], + json: 'application/json', + xml : ['text/xml', 'application/xml'] + ]], + 'browser user agents' : [(Settings.MIME_DISABLE_ACCEPT_HEADER_FOR_USER_AGENTS): ['Gecko', 'WebKit', 'Presto', 'Trident']], + 'use.accept.header: false': [(Settings.MIME_USE_ACCEPT_HEADER): false] + ] + + /** + * The requests the configurations that change how the {@code Accept} header is read are negotiated with, which + * negotiate any other request as the defaults do. + */ + @Shared + Map>> acceptHeaderRequests = [ + 'browser user agents' : [ + [accept: 'application/json'], + [accept: 'application/json', userAgent: BROWSER], + [accept: 'application/json', userAgent: BROWSER, xhr: true], + [accept: CHROME, userAgent: BROWSER], + [accept: 'application/json', userAgent: BROWSER, format: 'xml'] + ], + 'use.accept.header: false': [ + [accept: 'application/json'], + [accept: CHROME], + [accept: 'application/json;q=abc'], + [accept: 'application/json', format: 'xml'] + ] + ] + + /** The requests, each with the headers and the format it is sent with. */ + @Shared + List> requests = [ + [:], + [accept: '*/*'], + [accept: 'text/html'], + [accept: CHROME], + [accept: 'application/json'], + [accept: 'Application/JSON'], + [accept: 'application/json;q=0.5, application/xml'], + [accept: '*/*, application/json'], + [accept: 'application/json, */*'], + [accept: 'application/xml, */*, text/html'], + [accept: 'text/*'], + [accept: 'application/*'], + [accept: 'application/problem+json'], + [accept: 'application/problem+xml'], + [accept: 'application/hal+json'], + [accept: 'application/vnd.api+json'], + [accept: 'application/soap+xml'], + [accept: 'application/xml, application/atom+xml'], + [accept: 'text/xml;q=0.5, application/xml;q=0.9, application/json;q=0.7'], + [accept: 'application/json;q=0, */*;q=0.1'], + [accept: 'image/png'], + [accept: 'application/json;q=abc'], + [accept: '*'], + [accept: 'application/json', userAgent: BROWSER], + [accept: 'application/json', userAgent: BROWSER, xhr: true], + [accept: CHROME, userAgent: BROWSER], + [accept: 'application/json', format: 'xml'], + [accept: 'application/json', format: 'html'], + [accept: 'application/json', format: 'all'], + [accept: 'application/json', format: 'unknown'], + [accept: 'text/html', format: 'json'], + [contentType: 'application/json;charset=UTF-8'], + [contentType: 'text/xml'], + [contentType: 'application/vnd.api+json'], + [contentType: 'application/problem+json'], + [contentType: 'application/json, application/xml'], + [contentType: 'application/json;charset='], + [contentType: 'image/png'] + ] + + /** Every answer that differs, as {@code configuration | request | answer: Grails 9 -> Spring}. */ + private static final List RECORDED = [ + 'defaults | Accept: */*, application/json | response.format: all -> json', + 'defaults | Accept: */*, application/json | withFormat(html, json, xml): html -> json', + 'defaults | Accept: */*, application/json | withFormat(json, xml, *): * -> json', + 'defaults | Accept: application/xml, */*, text/html | withFormat(pdf, html): pdf -> html', + 'defaults | Accept: text/* | withFormat(pdf, html): pdf -> html', + 'defaults | Accept: application/vnd.api+json | response.format: all -> json', + 'defaults | Accept: application/vnd.api+json | withFormat(html, json, xml): html -> json', + 'defaults | Accept: application/vnd.api+json | withFormat(json, xml, *): * -> json', + 'defaults | Accept: application/vnd.api+json | withFormat(pdf, html): pdf -> null', + 'defaults | Accept: application/soap+xml | response.format: all -> xml', + 'defaults | Accept: application/soap+xml | withFormat(html, json, xml): html -> xml', + 'defaults | Accept: application/soap+xml | withFormat(json, xml, *): * -> xml', + 'defaults | Accept: application/soap+xml | withFormat(pdf, html): pdf -> null', + 'defaults | Accept: application/xml, application/atom+xml | response.format: atom -> xml', + 'defaults | Accept: image/png | response.format: all -> null', + 'defaults | Accept: image/png | withFormat(html, json, xml): html -> null', + 'defaults | Accept: image/png | withFormat(pdf, html): pdf -> null', + 'defaults | Accept: application/json;q=abc | response.format: json -> null', + 'defaults | Accept: application/json;q=abc | withFormat(html, json, xml): json -> null', + 'defaults | Accept: application/json;q=abc | withFormat(json, xml, *): json -> *', + 'defaults | Accept: application/json, format=xml | Spring MVC: [application/json] -> [text/xml, application/xml, application/*+xml]', + 'defaults | Accept: application/json, format=html | Spring MVC: [application/json] -> [text/html, application/xhtml+xml]', + 'defaults | Accept: application/json, format=all | response.format: all -> json', + 'defaults | Accept: application/json, format=all | withFormat(html, json, xml): html -> json', + 'defaults | Accept: application/json, format=all | withFormat(json, xml, *): * -> json', + 'defaults | Accept: application/json, format=all | withFormat(pdf, html): pdf -> null', + 'defaults | Accept: application/json, format=unknown | response.format: all -> null', + 'defaults | Accept: application/json, format=unknown | withFormat(html, json, xml): html -> json', + 'defaults | Accept: application/json, format=unknown | withFormat(json, xml, *): * -> json', + 'defaults | Accept: application/json, format=unknown | withFormat(pdf, html): pdf -> null', + 'defaults | Accept: application/json, format=unknown | Spring MVC: [application/json] -> not acceptable', + 'defaults | Accept: text/html, format=json | Spring MVC: [text/html] -> [application/json, text/json, application/*+json]', + 'defaults | Content-Type: application/vnd.api+json | request.format: null -> json', + 'defaults | Content-Type: application/json, application/xml | request.format: json -> null', + 'defaults | Content-Type: application/json;charset= | request.format: json -> null', + 'merged vendor type | Accept: */*, application/json | response.format: all -> json', + 'merged vendor type | Accept: */*, application/json | withFormat(html, json, xml): html -> json', + 'merged vendor type | Accept: */*, application/json | withFormat(json, xml, *): * -> json', + 'merged vendor type | Accept: application/xml, */*, text/html | withFormat(pdf, html): pdf -> html', + 'merged vendor type | Accept: text/* | withFormat(pdf, html): pdf -> html', + 'merged vendor type | Accept: application/vnd.api+json | response.format: all -> json', + 'merged vendor type | Accept: application/vnd.api+json | withFormat(html, json, xml): html -> json', + 'merged vendor type | Accept: application/vnd.api+json | withFormat(json, xml, *): * -> json', + 'merged vendor type | Accept: application/vnd.api+json | withFormat(pdf, html): pdf -> null', + 'merged vendor type | Accept: application/soap+xml | response.format: all -> xml', + 'merged vendor type | Accept: application/soap+xml | withFormat(html, json, xml): html -> xml', + 'merged vendor type | Accept: application/soap+xml | withFormat(json, xml, *): * -> xml', + 'merged vendor type | Accept: application/soap+xml | withFormat(pdf, html): pdf -> null', + 'merged vendor type | Accept: application/xml, application/atom+xml | response.format: atom -> xml', + 'merged vendor type | Accept: image/png | response.format: all -> null', + 'merged vendor type | Accept: image/png | withFormat(html, json, xml): html -> null', + 'merged vendor type | Accept: image/png | withFormat(pdf, html): pdf -> null', + 'merged vendor type | Accept: application/json;q=abc | response.format: json -> null', + 'merged vendor type | Accept: application/json;q=abc | withFormat(html, json, xml): json -> null', + 'merged vendor type | Accept: application/json;q=abc | withFormat(json, xml, *): json -> *', + 'merged vendor type | Accept: application/json, format=xml | Spring MVC: [application/json] -> [text/xml, application/xml, application/*+xml]', + 'merged vendor type | Accept: application/json, format=html | Spring MVC: [application/json] -> [text/html, application/xhtml+xml]', + 'merged vendor type | Accept: application/json, format=all | response.format: all -> json', + 'merged vendor type | Accept: application/json, format=all | withFormat(html, json, xml): html -> json', + 'merged vendor type | Accept: application/json, format=all | withFormat(json, xml, *): * -> json', + 'merged vendor type | Accept: application/json, format=all | withFormat(pdf, html): pdf -> null', + 'merged vendor type | Accept: application/json, format=unknown | response.format: all -> null', + 'merged vendor type | Accept: application/json, format=unknown | withFormat(html, json, xml): html -> json', + 'merged vendor type | Accept: application/json, format=unknown | withFormat(json, xml, *): * -> json', + 'merged vendor type | Accept: application/json, format=unknown | withFormat(pdf, html): pdf -> null', + 'merged vendor type | Accept: application/json, format=unknown | Spring MVC: [application/json] -> not acceptable', + 'merged vendor type | Accept: text/html, format=json | Spring MVC: [text/html] -> [application/json, text/json, application/*+json]', + 'merged vendor type | Content-Type: application/vnd.api+json | request.format: null -> json', + 'merged vendor type | Content-Type: application/json, application/xml | request.format: json -> null', + 'merged vendor type | Content-Type: application/json;charset= | request.format: json -> null', + 'without all | no headers | response.format: html -> all', + 'without all | no headers | withFormat(json, xml, *): json -> *', + 'without all | no headers | withFormat(pdf, html): html -> pdf', + 'without all | Accept: */*, application/json | withFormat(pdf, html): null -> pdf', + 'without all | Accept: application/json, */* | withFormat(pdf, html): null -> pdf', + 'without all | Accept: text/* | withFormat(pdf, html): pdf -> html', + 'without all | Accept: application/* | withFormat(pdf, html): pdf -> html', + 'without all | Accept: application/hal+json | response.format: all -> json', + 'without all | Accept: application/hal+json | withFormat(html, json, xml): html -> json', + 'without all | Accept: application/hal+json | withFormat(json, xml, *): * -> json', + 'without all | Accept: application/hal+json | withFormat(pdf, html): pdf -> null', + 'without all | Accept: application/vnd.api+json | response.format: all -> json', + 'without all | Accept: application/vnd.api+json | withFormat(html, json, xml): html -> json', + 'without all | Accept: application/vnd.api+json | withFormat(json, xml, *): * -> json', + 'without all | Accept: application/vnd.api+json | withFormat(pdf, html): pdf -> null', + 'without all | Accept: application/soap+xml | response.format: all -> xml', + 'without all | Accept: application/soap+xml | withFormat(html, json, xml): html -> xml', + 'without all | Accept: application/soap+xml | withFormat(json, xml, *): * -> xml', + 'without all | Accept: application/soap+xml | withFormat(pdf, html): pdf -> null', + 'without all | Accept: application/json;q=0, */*;q=0.1 | response.format: json -> all', + 'without all | Accept: application/json;q=0, */*;q=0.1 | withFormat(html, json, xml): json -> html', + 'without all | Accept: application/json;q=0, */*;q=0.1 | withFormat(json, xml, *): json -> *', + 'without all | Accept: application/json;q=0, */*;q=0.1 | withFormat(pdf, html): null -> pdf', + 'without all | Accept: image/png | response.format: all -> null', + 'without all | Accept: image/png | withFormat(html, json, xml): html -> null', + 'without all | Accept: image/png | withFormat(pdf, html): pdf -> null', + 'without all | Accept: application/json;q=abc | response.format: json -> null', + 'without all | Accept: application/json;q=abc | withFormat(html, json, xml): json -> null', + 'without all | Accept: application/json;q=abc | withFormat(json, xml, *): json -> *', + 'without all | Accept: application/json, format=xml | Spring MVC: [application/json] -> [text/xml, application/xml, application/*+xml]', + 'without all | Accept: application/json, format=html | Spring MVC: [application/json] -> [text/html, application/xhtml+xml]', + 'without all | Accept: application/json, format=all | response.format: html -> null', + 'without all | Accept: application/json, format=all | withFormat(html, json, xml): html -> json', + 'without all | Accept: application/json, format=all | withFormat(pdf, html): html -> null', + 'without all | Accept: application/json, format=all | Spring MVC: [application/json] -> not acceptable', + 'without all | Accept: application/json, format=unknown | response.format: html -> null', + 'without all | Accept: application/json, format=unknown | withFormat(html, json, xml): html -> json', + 'without all | Accept: application/json, format=unknown | withFormat(pdf, html): html -> null', + 'without all | Accept: application/json, format=unknown | Spring MVC: [application/json] -> not acceptable', + 'without all | Accept: text/html, format=json | Spring MVC: [text/html] -> [application/json, application/*+json]', + 'without all | Content-Type: application/json;charset=UTF-8 | response.format: html -> all', + 'without all | Content-Type: application/json;charset=UTF-8 | withFormat(json, xml, *): json -> *', + 'without all | Content-Type: application/json;charset=UTF-8 | withFormat(pdf, html): html -> pdf', + 'without all | Content-Type: text/xml | response.format: html -> all', + 'without all | Content-Type: text/xml | withFormat(json, xml, *): json -> *', + 'without all | Content-Type: text/xml | withFormat(pdf, html): html -> pdf', + 'without all | Content-Type: application/vnd.api+json | response.format: html -> all', + 'without all | Content-Type: application/vnd.api+json | withFormat(json, xml, *): json -> *', + 'without all | Content-Type: application/vnd.api+json | withFormat(pdf, html): html -> pdf', + 'without all | Content-Type: application/vnd.api+json | request.format: null -> json', + 'without all | Content-Type: application/problem+json | response.format: html -> all', + 'without all | Content-Type: application/problem+json | withFormat(json, xml, *): json -> *', + 'without all | Content-Type: application/problem+json | withFormat(pdf, html): html -> pdf', + 'without all | Content-Type: application/json, application/xml | response.format: html -> all', + 'without all | Content-Type: application/json, application/xml | withFormat(json, xml, *): json -> *', + 'without all | Content-Type: application/json, application/xml | withFormat(pdf, html): html -> pdf', + 'without all | Content-Type: application/json, application/xml | request.format: json -> null', + 'without all | Content-Type: application/json;charset= | response.format: html -> all', + 'without all | Content-Type: application/json;charset= | withFormat(json, xml, *): json -> *', + 'without all | Content-Type: application/json;charset= | withFormat(pdf, html): html -> pdf', + 'without all | Content-Type: application/json;charset= | request.format: json -> null', + 'without all | Content-Type: image/png | response.format: html -> all', + 'without all | Content-Type: image/png | withFormat(json, xml, *): json -> *', + 'without all | Content-Type: image/png | withFormat(pdf, html): html -> pdf', + 'browser user agents | Accept: application/json, browser User-Agent | Spring MVC: [application/json] -> [*/*]', + 'browser user agents | Accept: , browser User-Agent | Spring MVC: [text/html, application/xhtml+xml, image/avif, image/webp, application/xml;q=0.9, */*;q=0.8] -> [*/*]', + 'browser user agents | Accept: application/json, browser User-Agent, format=xml | Spring MVC: [application/json] -> [text/xml, application/xml, application/*+xml]', + 'use.accept.header: false | Accept: application/json | Spring MVC: [application/json] -> [*/*]', + 'use.accept.header: false | Accept: | Spring MVC: [text/html, application/xhtml+xml, image/avif, image/webp, application/xml;q=0.9, */*;q=0.8] -> [*/*]', + 'use.accept.header: false | Accept: application/json;q=abc | Spring MVC: not acceptable -> [*/*]', + 'use.accept.header: false | Accept: application/json, format=xml | Spring MVC: [application/json] -> [text/xml, application/xml, application/*+xml]' + ] + + @Shared + Map contexts = [:] + + void cleanupSpec() { + contexts.values()*.close() + } + + void cleanup() { + RequestContextHolder.resetRequestAttributes() + } + + void 'every answer that differs between the two policies is recorded'() { + when: + List differences = [] + configurations.each { String configuration, Map properties -> + for (Map sent in requestsFor(configuration)) { + Map grails = answers(configuration, properties, sent, false) + Map spring = answers(configuration, properties, sent, true) + grails.each { String answer, Object value -> + if (value != spring[answer]) { + differences << "${configuration} | ${describe(sent)} | ${answer}: ${value} -> ${spring[answer]}".toString() + } + } + } + } + + then: + differences == RECORDED + } + + void "with the setting, the first type Spring MVC's manager resolves for a request a URL mapping handles is of the format Grails resolves"() { + when: 'the format of the first media type the manager resolves for each request of the matrix is not the one response.format resolves' + List diverging = [] + configurations.each { String configuration, Map properties -> + requestsFor(configuration).findAll { !it.contentType }.each { Map sent -> + Map spring = answers(configuration, properties, sent, true) + Object format = spring['Spring MVC'] == 'not acceptable' ? null : formatOf(configuration, properties, sent) + if (spring['response.format'] != format) { + diverging << "${configuration} | ${describe(sent)} | ${spring['response.format']} -> ${format}".toString() + } + } + } + + then: + diverging.isEmpty() + } + + private Map answers(String configuration, Map properties, Map sent, + boolean springNegotiation) { + Map answers = [:] + answers['response.format'] = respond(configuration, properties, sent, springNegotiation) { GrailsWebRequest webRequest -> + webRequest.currentResponse.format + } + answers['withFormat(html, json, xml)'] = respond(configuration, properties, sent, springNegotiation) { GrailsWebRequest webRequest -> + webRequest.currentResponse.withFormat { + html { 'html' } + json { 'json' } + xml { 'xml' } + } + } + answers['withFormat(json, xml, *)'] = respond(configuration, properties, sent, springNegotiation) { GrailsWebRequest webRequest -> + webRequest.currentResponse.withFormat { + json { 'json' } + xml { 'xml' } + '*' { '*' } + } + } + answers['withFormat(pdf, html)'] = respond(configuration, properties, sent, springNegotiation) { GrailsWebRequest webRequest -> + webRequest.currentResponse.withFormat { + pdf { 'pdf' } + html { 'html' } + } + } + if (sent.contentType) { + answers['request.format'] = respond(configuration, properties, sent, springNegotiation) { GrailsWebRequest webRequest -> + webRequest.currentRequest.format + } + } + answers['Spring MVC'] = respond(configuration, properties, sent, springNegotiation) { GrailsWebRequest webRequest -> + try { + manager(configuration, properties, springNegotiation).resolveMediaTypes(new ServletWebRequest(webRequest.currentRequest)).toString() + } + catch (HttpMediaTypeNotAcceptableException ignored) { + 'not acceptable' + } + } + answers + } + + /** + * The format of the first media type Spring MVC's manager resolves for a request, as Grails resolves an + * {@code Accept} header of that type alone under the MIME types of the configuration. + */ + private Object formatOf(String configuration, Map properties, Map sent) { + Object first = respond(configuration, properties, sent, true) { GrailsWebRequest webRequest -> + manager(configuration, properties, true).resolveMediaTypes(new ServletWebRequest(webRequest.currentRequest)).first() + } + Map types = properties.findAll { String key, Object value -> key.startsWith('grails.mime.types') || key == Settings.MIME_TYPES_MERGE_DEFAULTS } + respond("${configuration} types".toString(), types, [accept: first.toString()], true) { GrailsWebRequest webRequest -> + webRequest.currentResponse.format + } + } + + private List> requestsFor(String configuration) { + acceptHeaderRequests[configuration] ?: requests + } + + private Object respond(String configuration, Map properties, Map sent, + boolean springNegotiation, Closure answer) { + AnnotationConfigWebApplicationContext context = context(configuration, properties, springNegotiation) + def request = new MockHttpServletRequest(context.servletContext) + if (sent.accept) { + request.addHeader('Accept', sent.accept) + } + if (sent.userAgent) { + request.addHeader('User-Agent', sent.userAgent) + } + if (sent.xhr) { + request.addHeader('X-Requested-With', 'XMLHttpRequest') + } + if (sent.contentType) { + request.contentType = sent.contentType + } + request.setAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE, Stub(UrlMappingInfo)) + GrailsWebRequest webRequest = GrailsWebMockUtil.bindMockWebRequest(context, request, new MockHttpServletResponse()) + if (sent.format) { + webRequest.params.format = sent.format + } + try { + return answer.call(webRequest) + } + finally { + RequestContextHolder.resetRequestAttributes() + } + } + + private ContentNegotiationManager manager(String configuration, Map properties, boolean springNegotiation) { + context(configuration, properties, springNegotiation) + .getBean(GrailsContentNegotiationStrategy.MVC_CONTENT_NEGOTIATION_MANAGER, ContentNegotiationManager) + } + + private AnnotationConfigWebApplicationContext context(String configuration, Map properties, + boolean springNegotiation) { + contexts.computeIfAbsent("${configuration}:${springNegotiation}".toString()) { + def context = new AnnotationConfigWebApplicationContext() + context.servletContext = new MockServletContext() + context.environment.propertySources.addFirst(new MapPropertySource('test', + properties + [(Settings.MIME_NEGOTIATION_SPRING): springNegotiation])) + context.register(ComparisonConfiguration) + context.refresh() + context + } + } + + private static String describe(Map sent) { + List parts = [] + if (sent.accept) { + parts << "Accept: ${sent.accept == CHROME ? '' : sent.accept}".toString() + } + if (sent.userAgent) { + parts << 'browser User-Agent' + } + if (sent.xhr) { + parts << 'XHR' + } + if (sent.format) { + parts << "format=${sent.format}".toString() + } + if (sent.contentType) { + parts << "Content-Type: ${sent.contentType}".toString() + } + parts ? parts.join(', ') : 'no headers' + } + + @Configuration(proxyBeanMethods = false) + @EnableWebMvc + @Import(MimeTypesConfiguration) + static class ComparisonConfiguration { + + @Bean + GrailsApplication grailsApplication(ApplicationContext applicationContext, ConfigurableEnvironment environment) { + def application = new DefaultGrailsApplication() + application.config = new PropertySourcesConfig(environment.propertySources) + application.applicationContext = applicationContext + application.mainContext = applicationContext + application + } + } +} From 1982f075152024fdb81c064c02754691ae43335a Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sat, 14 Nov 2026 10:41:59 -0800 Subject: [PATCH 012/115] Test Spring MVC format negotiation end to end app1 answers the same requests with and without grails.mime.negotiation.spring: a ResponseEntity an action returns is written in the format of its URL extension, or not acceptable where no converter writes it so, an unknown format is not acceptable, the formats Grails resolves follow Spring MVC's order, a +json body is of the json format, and a Spring MVC endpoint keeps its negotiation. --- .../contentneg/SpringWrittenController.groovy | 40 +++++++ .../init/functionaltests/Application.groovy | 11 ++ .../SpringFormatNegotiationSpec.groovy | 105 ++++++++++++++++++ .../contentneg/SpringNegotiationApi.groovy | 35 ++++++ 4 files changed, 191 insertions(+) create mode 100644 grails-test-examples/app1/grails-app/controllers/functionaltests/contentneg/SpringWrittenController.groovy create mode 100644 grails-test-examples/app1/src/integration-test/groovy/functionaltests/contentneg/SpringFormatNegotiationSpec.groovy create mode 100644 grails-test-examples/app1/src/main/groovy/functionaltests/contentneg/SpringNegotiationApi.groovy diff --git a/grails-test-examples/app1/grails-app/controllers/functionaltests/contentneg/SpringWrittenController.groovy b/grails-test-examples/app1/grails-app/controllers/functionaltests/contentneg/SpringWrittenController.groovy new file mode 100644 index 00000000000..a07bf04f2b2 --- /dev/null +++ b/grails-test-examples/app1/grails-app/controllers/functionaltests/contentneg/SpringWrittenController.groovy @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package functionaltests.contentneg + +import org.springframework.http.ResponseEntity + +/** + * Actions whose values Spring MVC writes, and one that answers with the formats Grails resolves for the request, which + * {@code grails.mime.negotiation.spring} negotiates alike. + */ +class SpringWrittenController { + + def entity() { + ResponseEntity.ok([title: 'Grails']) + } + + def text() { + ResponseEntity.ok('Grails') + } + + def formats() { + render(contentType: 'text/plain', text: "${response.format}|${request.format}") + } +} diff --git a/grails-test-examples/app1/grails-app/init/functionaltests/Application.groovy b/grails-test-examples/app1/grails-app/init/functionaltests/Application.groovy index dcc99a5d58b..4019ce93a86 100644 --- a/grails-test-examples/app1/grails-app/init/functionaltests/Application.groovy +++ b/grails-test-examples/app1/grails-app/init/functionaltests/Application.groovy @@ -19,8 +19,11 @@ package functionaltests +import org.springframework.context.annotation.Bean + import grails.boot.GrailsApp import grails.boot.config.GrailsAutoConfiguration +import functionaltests.contentneg.SpringNegotiationApi class Application extends GrailsAutoConfiguration { static { @@ -30,4 +33,12 @@ class Application extends GrailsAutoConfiguration { static void main(String[] args) { GrailsApp.run(Application) } + + /** + * A Spring MVC endpoint beside the Grails controllers, which content negotiation leaves to Spring MVC. + */ + @Bean + SpringNegotiationApi springNegotiationApi() { + new SpringNegotiationApi() + } } diff --git a/grails-test-examples/app1/src/integration-test/groovy/functionaltests/contentneg/SpringFormatNegotiationSpec.groovy b/grails-test-examples/app1/src/integration-test/groovy/functionaltests/contentneg/SpringFormatNegotiationSpec.groovy new file mode 100644 index 00000000000..e7f691c56fd --- /dev/null +++ b/grails-test-examples/app1/src/integration-test/groovy/functionaltests/contentneg/SpringFormatNegotiationSpec.groovy @@ -0,0 +1,105 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package functionaltests.contentneg + +import org.springframework.test.context.TestPropertySource +import spock.lang.Specification +import spock.lang.Tag + +import grails.testing.mixin.integration.Integration +import org.apache.grails.testing.http.client.HttpClientSupport + +/** + * With {@code grails.mime.negotiation.spring}, Grails formats are resolved with Spring MVC's content negotiation, and + * what Spring MVC writes for an action is negotiated with the format the request names, while a Spring MVC endpoint is + * negotiated as before. + */ +@Integration +@Tag('http-client') +@TestPropertySource(properties = 'grails.mime.negotiation.spring=true') +class SpringFormatNegotiationSpec extends Specification implements HttpClientSupport { + + void 'a value Spring MVC writes for an action is in the format of its URL extension, which a map cannot be written in as HTML'() { + expect: + http('/springWritten/entity.html', Accept: '*/*').assertStatus(406) + http('/springWritten/entity.json', Accept: 'text/html').assertStatus(200).contentType.startsWith('application/json') + } + + void 'a text Spring MVC writes for an action is labelled with the format of its URL extension'() { + expect: + http('/springWritten/text.json', Accept: 'text/plain').assertStatus(200).contentType.startsWith('application/json') + } + + void 'a request that names a format no type is configured for is not acceptable'() { + expect: + http('/springWritten/entity?format=unknown', Accept: 'application/json').assertStatus(406) + } + + void 'the formats Grails resolves follow the order Spring MVC resolves the Accept header in: #accept'() { + expect: + http('/springWritten/formats', Accept: accept).assertStatus(200).assertEquals(formats) + + where: + accept || formats + '*/*, application/json' || 'json|all' + 'image/png' || 'null|all' + 'application/soap+xml' || 'xml|all' + } + + void 'a body of a type with the +json suffix is of the json format'() { + expect: + http('/springWritten/formats', Accept: 'application/json', 'Content-Type': 'application/merge-patch+json') + .assertStatus(200).assertEquals('json|json') + } + + void 'a Spring MVC endpoint keeps its negotiation: #description'() { + expect: + http("/springNegotiation${query}", Accept: accept).assertStatus(status) + + where: + description | query | accept || status + 'the format parameter is ignored' | '?format=xml' | 'application/json' || 200 + 'an unknown type is not acceptable' | '' | 'image/png' || 406 + } +} + +/** + * Without {@code grails.mime.negotiation.spring}, Grails resolves formats with its own rules, and Spring MVC writes the + * values of actions as the {@code Accept} header asks. + */ +@Integration +@Tag('http-client') +class GrailsFormatNegotiationSpec extends Specification implements HttpClientSupport { + + void 'a value Spring MVC writes for an action is in the type the Accept header asks for, whatever its URL extension'() { + expect: + http('/springWritten/entity.html', Accept: '*/*').assertStatus(200).contentType.startsWith('application/json') + http('/springWritten/text.json', Accept: 'text/plain').assertStatus(200).contentType.startsWith('text/plain') + } + + void 'the formats Grails resolves follow the Grails rules: #accept'() { + expect: + http('/springWritten/formats', Accept: accept).assertStatus(200).assertEquals(formats) + + where: + accept || formats + '*/*, application/json' || 'all|all' + 'image/png' || 'all|all' + } +} diff --git a/grails-test-examples/app1/src/main/groovy/functionaltests/contentneg/SpringNegotiationApi.groovy b/grails-test-examples/app1/src/main/groovy/functionaltests/contentneg/SpringNegotiationApi.groovy new file mode 100644 index 00000000000..e50b73ae05c --- /dev/null +++ b/grails-test-examples/app1/src/main/groovy/functionaltests/contentneg/SpringNegotiationApi.groovy @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package functionaltests.contentneg + +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.RestController + +/** + * A Spring MVC endpoint beside the Grails controllers, whose content negotiation {@code grails.mime.negotiation.spring} + * leaves as Spring MVC configures it. + */ +@RestController +class SpringNegotiationApi { + + @GetMapping('/springNegotiation') + Map negotiation() { + [title: 'Grails'] + } +} From c901261665fe77f44da31609a46d418f22d2d347 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sat, 14 Nov 2026 10:41:59 -0800 Subject: [PATCH 013/115] Document grails.mime.negotiation.spring The content negotiation guide describes how the setting resolves formats with Spring MVC's manager and what becomes of each Grails rule: kept, extended or dropped. The new Upgrading from Grails 9 to Grails 10 section lists the answers that change, which FormatNegotiationComparisonSpec records, and the built-in properties list the setting. --- .../en/guide/conf/config/builtInOptions.adoc | 1 + .../guide/theWebLayer/contentNegotiation.adoc | 81 +++++++++++++++++++ grails-doc/src/en/guide/toc.yml | 1 + .../src/en/guide/upgrading/upgrading100x.adoc | 80 ++++++++++++++++++ 4 files changed, 163 insertions(+) create mode 100644 grails-doc/src/en/guide/upgrading/upgrading100x.adoc diff --git a/grails-doc/src/en/guide/conf/config/builtInOptions.adoc b/grails-doc/src/en/guide/conf/config/builtInOptions.adoc index 64ffb941a45..7828aba7588 100644 --- a/grails-doc/src/en/guide/conf/config/builtInOptions.adoc +++ b/grails-doc/src/en/guide/conf/config/builtInOptions.adoc @@ -34,6 +34,7 @@ On the runtime front, i.e. `grails-app/conf/application.yml`, there are quite a * `grails.views.gsp.encoding` - The file encoding used for GSP source files (default: 'utf-8'). * `grails.mime.file.extensions` - Whether to use the file extension to dictate the mime type in link:theWebLayer.html#contentNegotiation[Content Negotiation] (default: true). * `grails.mime.types` - A map of supported mime types used for link:theWebLayer.html#contentNegotiation[Content Negotiation]. +* `grails.mime.negotiation.spring` - Whether `request.format`, `response.format`, `withFormat` and the format `respond` writes in are resolved with Spring MVC's content negotiation manager, as the formats `grails.mime.types` gives the media types it resolves, rather than by the Grails rules; the `format` parameter, URL extensions and the `grails.mime` settings of the `Accept` header then also decide what Spring MVC writes for an action (default: false). See <>. * `grails.serverURL` - A string specifying the server URL portion of absolute links, including server name e.g. grails.serverURL="https://my.yourportal.com". See link:{gspTagsRef}createLink.html[createLink]. Also used by redirects. * `grails.views.gsp.layout.preprocess` - Determines whether layout preprocessing happens. Disabling this slows down page rendering, but if you need Grails' layout implementation to parse the generated HTML from a GSP view, then disabling it is the right option. Don't worry if you don't understand this advanced property: leave it set to true. * `grails.reload.excludes` and `grails.reload.includes` - Configuring these directives determines the reload behavior for project specific source files. Each directive takes a list of strings that are the class names for project source files that should be excluded from reloading behavior or included accordingly when running the application in development with the `bootRun` task. If the `grails.reload.includes` directive is configured, then only the classes in that list will be reloaded. diff --git a/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc b/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc index e878767b4e7..797b16a7ba5 100644 --- a/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc +++ b/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc @@ -263,6 +263,87 @@ Note the inclusion of the `format` variable in the path. If you do not wish to u +[[springFormatNegotiation]] +==== Resolving Formats with Spring MVC Content Negotiation + +Grails resolves `request.format`, `response.format`, `withFormat` and the format `respond` writes in by the rules this +section describes, while Spring MVC's content negotiation manager decides the type of what Spring MVC writes for an +action: a `ResponseEntity`, a `ProblemDetail` or another value an action returns (see +<>), a problem `respond` writes, and the JSON `respond` writes with +Spring MVC. The two can answer one request differently. Set `grails.mime.negotiation.spring` to `true` to resolve the +Grails formats with Spring MVC's manager too: + +[source,yaml] +.grails-app/conf/application.yml +---- +grails: + mime: + negotiation: + spring: true +---- + +Grails {grailsMajorVersion} keeps the Grails rules by default. With the setting: + +* The `Accept` header is read by Spring MVC's manager, with its strategies, so the `spring.mvc.contentnegotiation.*` +properties apply to the Grails formats as they do to Spring MVC, and its media types are taken in the order Spring MVC +takes them: by quality, then a more specific type ahead of a less specific one, then in the order the header lists them. +`Accept: */*, application/json` is the `json` format, where the Grails rules give `all`, and +`Accept: application/xml, application/atom+xml` is `xml`, where they give `atom`. +* Each media type is the format `grails.mime.types`, or a `MimeTypeProvider` bean such as a renderer, gives it. A type +with the `+json` or `+xml` suffix that no configured type names, such as `application/merge-patch+json` or +`application/problem+xml`, is the `json` or the `xml` format, as Spring MVC's JSON and XML converters write such types. +* `*/*` is the `all` format, also where `grails.mime.types` does not list it. So is another wildcard, such as `text/*`, +for which `withFormat` runs its `*` block or else the first block of a format with a type compatible with the wildcard, +and `respond` answers in the first of the action's formats with such a type, as Spring MVC answers a wildcard with the +first of its candidates that is compatible with it. +* A request that accepts no configured type, such as one that sends only `Accept: image/png`, or whose `Accept` header +Spring cannot read, has no format: `response.format` and `response.mimeType` are `null`, `respond` answers +`406 Not Acceptable`, and `withFormat` runs its `*` block, or no block. The Grails rules answer such a request as one +that accepts any type, with the `all` format. +* The `format` parameter and a URL extension name the format, ahead of the `Accept` header, as before. The first value +of the parameter counts, as for Spring MVC; a format no type is configured for is not acceptable, where the Grails rules +answer in the first configured format; and the `all` format leaves the format to the `Accept` header. +* `grails.mime.use.accept.header` and `grails.mime.disable.accept.header.*` ignore the `Accept` header as before. +* The `format` parameter, the URL extension and the `Accept` header settings also decide what Spring MVC writes for a +request a URL mapping handles, so that it is in the format Grails resolves for the request: an action that returns a +`ResponseEntity` of a map answers `/book/show/1.json` in JSON whatever the `Accept` header asks for, and +`/book/show/1.html` with `406 Not Acceptable`, since no message converter writes a map as HTML. +* `request.format` reads the `Content-Type` header as Spring reads it, as one media type, which is the format the +`Accept` header would make it: a body sent as `application/merge-patch+json` is of the `json` format, and data binding +reads it as JSON. A type no configured type stands for, or that Spring cannot read, is of no format, and a request +without a `Content-Type` is of the first configured format, as before. + +A Spring MVC endpoint, such as an `@RestController` or an actuator endpoint, and Spring Security, whose filters negotiate +before a URL mapping has matched the request, negotiate as before: the setting changes no answer Spring MVC gives a request +no URL mapping handles. Each rule of Grails' negotiation becomes: + +[cols="2,3"] +|=== +|Grails rule |With `grails.mime.negotiation.spring` + +|The `format` parameter and URL extensions select the format +|Kept, ahead of Spring MVC's strategies, and also for what Spring MVC writes for the request. An unknown format is not +acceptable, and the `all` format leaves the choice to the `Accept` header. + +|`grails.mime.use.accept.header` and `grails.mime.disable.accept.header.*` ignore the `Accept` header +|Kept, also for what Spring MVC writes for the request. + +|`text/xml` and `application/xml` are merged into one `xml` type +|Dropped: both are the `xml` format, which comes in Spring MVC's order. + +|A type with the `+xml` suffix comes ahead of `application/xml` of equal quality +|Dropped: the types come in Spring MVC's order. + +|The problem types of RFC 9457 are the `json` and `xml` formats +|Extended to any type with the `+json` or `+xml` suffix that no configured type names. + +|A request that accepts no configured type is answered as one that accepts any type, and one that names a format no +type is configured for in the first configured format +|Dropped: such a request, and one whose `Accept` header Spring cannot read, is not acceptable. +|=== + +See xref:upgrading#upgrading100x[Upgrading from Grails 9 to Grails 10] for how each answer changes. + ==== Testing Content Negotiation diff --git a/grails-doc/src/en/guide/toc.yml b/grails-doc/src/en/guide/toc.yml index 25fe7b73d95..6d49075f174 100644 --- a/grails-doc/src/en/guide/toc.yml +++ b/grails-doc/src/en/guide/toc.yml @@ -37,6 +37,7 @@ gettingStarted: developmentReloading: Development Reloading upgrading: title: Upgrading from the previous versions + upgrading100x: Upgrading from Grails 9 to Grails 10 upgrading90x: Upgrading from Grails 8 to Grails 9 upgrading81x: Upgrading from Grails 8.0 to Grails 8.1 upgrading80x: Upgrading from Grails 7 to Grails 8 diff --git a/grails-doc/src/en/guide/upgrading/upgrading100x.adoc b/grails-doc/src/en/guide/upgrading/upgrading100x.adoc new file mode 100644 index 00000000000..eecd167e672 --- /dev/null +++ b/grails-doc/src/en/guide/upgrading/upgrading100x.adoc @@ -0,0 +1,80 @@ +//// +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + +https://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +//// + +=== Upgrade Instructions for Grails 9 to Grails 10 + +This guide outlines the changes introduced in Grails 10 and the steps required to upgrade your application. + +==== 1. Opt Into Spring MVC Content Negotiation for Grails Formats + +Set `grails.mime.negotiation.spring: true` to resolve `request.format`, `response.format`, `withFormat` and the format +`respond` writes in with Spring MVC's content negotiation manager, and to have the `format` parameter, URL extensions and +the `grails.mime` settings of the `Accept` header decide what Spring MVC writes for an action too (see +<>). Grails 10 keeps the negotiation of +Grails 9 by default. + +[source,yaml] +.grails-app/conf/application.yml +---- +grails: + mime: + negotiation: + spring: true +---- + +The setting changes these answers, which the framework's tests record by running the same requests through both: + +* *The order of the `Accept` header.* Spring MVC takes a more specific type ahead of a less specific one of equal +quality, where Grails 9 kept the order of the header: `Accept: */*, application/json` is the `json` format, where it was +`all`, and `withFormat` runs the `json` block rather than its first. A type with the `+xml` suffix no longer comes ahead +of `application/xml`: `Accept: application/xml, application/atom+xml` is `xml`, where it was `atom`. +* *Types with the `+json` and `+xml` suffixes.* A type no configured type names, such as `application/merge-patch+json`, +`application/soap+xml` or, without a `hal` format, `application/hal+json`, is the `json` or `xml` format, where Grails 9 +read only the problem types of RFC 9457 so and answered any other as `all`. In `Content-Type` too: a body sent as such a +type is of the `json` format, where its `request.format` was `null`, and data binding reads it as JSON. +* *A request that accepts no configured type.* Such a request, as one that sends only `Accept: image/png`, and one whose +`Accept` header Spring cannot read, such as `Accept: application/json;q=abc`, is not acceptable: `response.format` and +`response.mimeType` are `null`, `respond` answers `406 Not Acceptable` and `withFormat` runs its `*` block, or no block, +where Grails 9 answered as for a client that accepts any type, with the `all` format, or read the header leniently. Code +that reads `response.format` or `response.mimeType` must allow for `null`. +* *The `format` parameter and URL extensions.* A format no type is configured for is not acceptable, to `respond`, which +answers `406`, and to what Spring MVC writes, where Grails 9 answered in the first configured format; `withFormat` +answers it from the `Accept` header, as for a format it has no block for. The `all` format leaves the format to the +`Accept` header: `?format=all` with `Accept: application/json` is `json`, where it was `all`. The first value of the +parameter counts. +* *What Spring MVC writes for an action.* The `format` parameter and URL extensions now decide the type of a +`ResponseEntity`, a `ProblemDetail` or another value Spring MVC writes for a request a URL mapping handles, where Spring +MVC read only the `Accept` header: `/book/show/1.json` is answered in JSON whatever the `Accept` header asks for, and +`/book/show/1.html` of an action that returns a map with `406`, since no message converter writes a map as HTML. +`grails.mime.use.accept.header: false` and `grails.mime.disable.accept.header.*` now ignore the `Accept` header for it +too. +* *Without the `all` format.* Where `grails.mime.types` does not list `all`, a request without an `Accept` header is of +the `all` format, as one that sends `Accept: */*` is, where Grails 9 gave the first configured format, so `withFormat` +runs its `*` block, or its first block, rather than the block of that format. A type of quality `0` comes after any +other, so `Accept: application/json;q=0, */*;q=0.1` is `all`, where it was `json`. `?format=all` is then an unknown +format. +* *Wildcards.* A wildcard such as `text/*` is the `all` format as before, but `withFormat` runs the first block of a +format with a type compatible with it, and `respond` answers in the first such format of the action, where Grails 9 ran +the first block and answered in the first format: `withFormat { pdf {...}; html {...} }` runs the `html` block for +`Accept: text/*`. +* *A `Content-Type` Spring cannot read.* A `Content-Type` Spring rejects, such as `application/json;charset=` or a list +of types, is of no format, where Grails 9 read it leniently as `json`. + +Other requests resolve as before, among them those of browsers and those that name one configured type. A Spring MVC +endpoint, and Spring Security's filters before a URL mapping has matched a request, negotiate as before. From c41b857df1d071ca580e6f989ef2d9b958bc7ba6 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sat, 14 Nov 2026 12:13:46 -0800 Subject: [PATCH 014/115] Offer grails.mime.negotiation.spring from Grails 9 The setting is opt-in in Grails 9, and Grails 10 makes it the default, as it does for the other migration settings in grails.config.Settings: the new API is @since 9.0, the upgrade notes are section 22 of Upgrading from Grails 8 to Grails 9, which the content negotiation guide and What's New point to, and the settings documentation gives its default as false in Grails 9 and true from Grails 10. --- .../main/groovy/grails/config/Settings.groovy | 8 +- .../en/guide/conf/config/builtInOptions.adoc | 2 +- .../src/en/guide/introduction/whatsNew.adoc | 9 ++- .../guide/theWebLayer/contentNegotiation.adoc | 5 +- grails-doc/src/en/guide/toc.yml | 1 - .../src/en/guide/upgrading/upgrading100x.adoc | 80 ------------------- .../src/en/guide/upgrading/upgrading90x.adoc | 58 ++++++++++++++ .../web/api/MimeTypesApiSupport.groovy | 2 +- .../web/mime/MimeTypesConfiguration.groovy | 2 +- ...eptHeaderContentNegotiationStrategy.groovy | 2 +- .../GrailsContentNegotiationManager.groovy | 2 +- ...tentNegotiationManagerPostProcessor.groovy | 2 +- .../GrailsContentNegotiationStrategy.groovy | 22 ++--- ...ilsFormatContentNegotiationStrategy.groovy | 2 +- .../FormatNegotiationComparisonSpec.groovy | 2 +- ...itional-spring-configuration-metadata.json | 2 +- 16 files changed, 93 insertions(+), 108 deletions(-) delete mode 100644 grails-doc/src/en/guide/upgrading/upgrading100x.adoc diff --git a/grails-core/src/main/groovy/grails/config/Settings.groovy b/grails-core/src/main/groovy/grails/config/Settings.groovy index 339a6c75bac..86833775518 100644 --- a/grails-core/src/main/groovy/grails/config/Settings.groovy +++ b/grails-core/src/main/groovy/grails/config/Settings.groovy @@ -218,15 +218,15 @@ interface Settings { * Grails keeps to what Spring MVC writes for the requests a URL mapping handles, rather than by Grails' own rules. * Defaults to {@link #MIME_NEGOTIATION_SPRING_DEFAULT}. * - * @since 10.0 + * @since 9.0 */ String MIME_NEGOTIATION_SPRING = 'grails.mime.negotiation.spring' /** - * The default of {@link #MIME_NEGOTIATION_SPRING}, as a property placeholder reads it: {@code false} in Grails 10, - * which keeps the negotiation of Grails 9. + * The default of {@link #MIME_NEGOTIATION_SPRING}, as a property placeholder reads it: {@code false} in Grails 9, + * and {@code true} from Grails 10. * - * @since 10.0 + * @since 9.0 */ String MIME_NEGOTIATION_SPRING_DEFAULT = 'false' diff --git a/grails-doc/src/en/guide/conf/config/builtInOptions.adoc b/grails-doc/src/en/guide/conf/config/builtInOptions.adoc index 7828aba7588..e82d11c9b73 100644 --- a/grails-doc/src/en/guide/conf/config/builtInOptions.adoc +++ b/grails-doc/src/en/guide/conf/config/builtInOptions.adoc @@ -34,7 +34,7 @@ On the runtime front, i.e. `grails-app/conf/application.yml`, there are quite a * `grails.views.gsp.encoding` - The file encoding used for GSP source files (default: 'utf-8'). * `grails.mime.file.extensions` - Whether to use the file extension to dictate the mime type in link:theWebLayer.html#contentNegotiation[Content Negotiation] (default: true). * `grails.mime.types` - A map of supported mime types used for link:theWebLayer.html#contentNegotiation[Content Negotiation]. -* `grails.mime.negotiation.spring` - Whether `request.format`, `response.format`, `withFormat` and the format `respond` writes in are resolved with Spring MVC's content negotiation manager, as the formats `grails.mime.types` gives the media types it resolves, rather than by the Grails rules; the `format` parameter, URL extensions and the `grails.mime` settings of the `Accept` header then also decide what Spring MVC writes for an action (default: false). See <>. +* `grails.mime.negotiation.spring` - Whether `request.format`, `response.format`, `withFormat` and the format `respond` writes in are resolved with Spring MVC's content negotiation manager, as the formats `grails.mime.types` gives the media types it resolves, rather than by the Grails rules; the `format` parameter, URL extensions and the `grails.mime` settings of the `Accept` header then also decide what Spring MVC writes for an action (default: false; true from Grails 10). See <>. * `grails.serverURL` - A string specifying the server URL portion of absolute links, including server name e.g. grails.serverURL="https://my.yourportal.com". See link:{gspTagsRef}createLink.html[createLink]. Also used by redirects. * `grails.views.gsp.layout.preprocess` - Determines whether layout preprocessing happens. Disabling this slows down page rendering, but if you need Grails' layout implementation to parse the generated HTML from a GSP view, then disabling it is the right option. Don't worry if you don't understand this advanced property: leave it set to true. * `grails.reload.excludes` and `grails.reload.includes` - Configuring these directives determines the reload behavior for project specific source files. Each directive takes a list of strings that are the class names for project source files that should be excluded from reloading behavior or included accordingly when running the application in development with the `bootRun` task. If the `grails.reload.includes` directive is configured, then only the classes in that list will be reloaded. diff --git a/grails-doc/src/en/guide/introduction/whatsNew.adoc b/grails-doc/src/en/guide/introduction/whatsNew.adoc index f74ffae4ba4..d2ecf56847a 100644 --- a/grails-doc/src/en/guide/introduction/whatsNew.adoc +++ b/grails-doc/src/en/guide/introduction/whatsNew.adoc @@ -134,7 +134,14 @@ an action annotated `@ResponseBody`, a `CompletableFuture` it returns included. selects the properties `respond` writes through Spring MVC. See xref:theWebLayer#springResponses[Returning Spring MVC Responses]. -See xref:upgrading#upgrading90x[Upgrading from Grails 8 to Grails 9], sections 4 to 11 and 13 to 16. +With `grails.mime.negotiation.spring: true`, which Grails 10 makes the default, `request.format`, `response.format`, +`withFormat` and the format `respond` writes in are resolved with Spring MVC's content negotiation manager, so that the +`spring.mvc.contentnegotiation.*` settings apply to them, and the `format` parameter, URL extensions and the +`grails.mime` settings of the `Accept` header decide what Spring MVC writes for an action too, such as a +`ResponseEntity` it returns. See xref:theWebLayer#springFormatNegotiation[Resolving Formats with Spring MVC Content +Negotiation]. + +See xref:upgrading#upgrading90x[Upgrading from Grails 8 to Grails 9], sections 4 to 11, 13 to 16 and 22. ==== Hibernate 7 Criteria Queries Can Be Extended diff --git a/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc b/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc index 797b16a7ba5..6aee23a57d9 100644 --- a/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc +++ b/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc @@ -282,7 +282,8 @@ grails: spring: true ---- -Grails {grailsMajorVersion} keeps the Grails rules by default. With the setting: +Grails {grailsMajorVersion} keeps the Grails rules by default, and Grails 10 makes the setting the default. With the +setting: * The `Accept` header is read by Spring MVC's manager, with its strategies, so the `spring.mvc.contentnegotiation.*` properties apply to the Grails formats as they do to Spring MVC, and its media types are taken in the order Spring MVC @@ -342,7 +343,7 @@ type is configured for in the first configured format |Dropped: such a request, and one whose `Accept` header Spring cannot read, is not acceptable. |=== -See xref:upgrading#upgrading100x[Upgrading from Grails 9 to Grails 10] for how each answer changes. +Section 22 of xref:upgrading#upgrading90x[Upgrading from Grails 8 to Grails 9] lists how each answer changes. ==== Testing Content Negotiation diff --git a/grails-doc/src/en/guide/toc.yml b/grails-doc/src/en/guide/toc.yml index 6d49075f174..25fe7b73d95 100644 --- a/grails-doc/src/en/guide/toc.yml +++ b/grails-doc/src/en/guide/toc.yml @@ -37,7 +37,6 @@ gettingStarted: developmentReloading: Development Reloading upgrading: title: Upgrading from the previous versions - upgrading100x: Upgrading from Grails 9 to Grails 10 upgrading90x: Upgrading from Grails 8 to Grails 9 upgrading81x: Upgrading from Grails 8.0 to Grails 8.1 upgrading80x: Upgrading from Grails 7 to Grails 8 diff --git a/grails-doc/src/en/guide/upgrading/upgrading100x.adoc b/grails-doc/src/en/guide/upgrading/upgrading100x.adoc deleted file mode 100644 index eecd167e672..00000000000 --- a/grails-doc/src/en/guide/upgrading/upgrading100x.adoc +++ /dev/null @@ -1,80 +0,0 @@ -//// -Licensed to the Apache Software Foundation (ASF) under one -or more contributor license agreements. See the NOTICE file -distributed with this work for additional information -regarding copyright ownership. The ASF licenses this file -to you under the Apache License, Version 2.0 (the -"License"); you may not use this file except in compliance -with the License. You may obtain a copy of the License at - -https://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, -software distributed under the License is distributed on an -"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -KIND, either express or implied. See the License for the -specific language governing permissions and limitations -under the License. -//// - -=== Upgrade Instructions for Grails 9 to Grails 10 - -This guide outlines the changes introduced in Grails 10 and the steps required to upgrade your application. - -==== 1. Opt Into Spring MVC Content Negotiation for Grails Formats - -Set `grails.mime.negotiation.spring: true` to resolve `request.format`, `response.format`, `withFormat` and the format -`respond` writes in with Spring MVC's content negotiation manager, and to have the `format` parameter, URL extensions and -the `grails.mime` settings of the `Accept` header decide what Spring MVC writes for an action too (see -<>). Grails 10 keeps the negotiation of -Grails 9 by default. - -[source,yaml] -.grails-app/conf/application.yml ----- -grails: - mime: - negotiation: - spring: true ----- - -The setting changes these answers, which the framework's tests record by running the same requests through both: - -* *The order of the `Accept` header.* Spring MVC takes a more specific type ahead of a less specific one of equal -quality, where Grails 9 kept the order of the header: `Accept: */*, application/json` is the `json` format, where it was -`all`, and `withFormat` runs the `json` block rather than its first. A type with the `+xml` suffix no longer comes ahead -of `application/xml`: `Accept: application/xml, application/atom+xml` is `xml`, where it was `atom`. -* *Types with the `+json` and `+xml` suffixes.* A type no configured type names, such as `application/merge-patch+json`, -`application/soap+xml` or, without a `hal` format, `application/hal+json`, is the `json` or `xml` format, where Grails 9 -read only the problem types of RFC 9457 so and answered any other as `all`. In `Content-Type` too: a body sent as such a -type is of the `json` format, where its `request.format` was `null`, and data binding reads it as JSON. -* *A request that accepts no configured type.* Such a request, as one that sends only `Accept: image/png`, and one whose -`Accept` header Spring cannot read, such as `Accept: application/json;q=abc`, is not acceptable: `response.format` and -`response.mimeType` are `null`, `respond` answers `406 Not Acceptable` and `withFormat` runs its `*` block, or no block, -where Grails 9 answered as for a client that accepts any type, with the `all` format, or read the header leniently. Code -that reads `response.format` or `response.mimeType` must allow for `null`. -* *The `format` parameter and URL extensions.* A format no type is configured for is not acceptable, to `respond`, which -answers `406`, and to what Spring MVC writes, where Grails 9 answered in the first configured format; `withFormat` -answers it from the `Accept` header, as for a format it has no block for. The `all` format leaves the format to the -`Accept` header: `?format=all` with `Accept: application/json` is `json`, where it was `all`. The first value of the -parameter counts. -* *What Spring MVC writes for an action.* The `format` parameter and URL extensions now decide the type of a -`ResponseEntity`, a `ProblemDetail` or another value Spring MVC writes for a request a URL mapping handles, where Spring -MVC read only the `Accept` header: `/book/show/1.json` is answered in JSON whatever the `Accept` header asks for, and -`/book/show/1.html` of an action that returns a map with `406`, since no message converter writes a map as HTML. -`grails.mime.use.accept.header: false` and `grails.mime.disable.accept.header.*` now ignore the `Accept` header for it -too. -* *Without the `all` format.* Where `grails.mime.types` does not list `all`, a request without an `Accept` header is of -the `all` format, as one that sends `Accept: */*` is, where Grails 9 gave the first configured format, so `withFormat` -runs its `*` block, or its first block, rather than the block of that format. A type of quality `0` comes after any -other, so `Accept: application/json;q=0, */*;q=0.1` is `all`, where it was `json`. `?format=all` is then an unknown -format. -* *Wildcards.* A wildcard such as `text/*` is the `all` format as before, but `withFormat` runs the first block of a -format with a type compatible with it, and `respond` answers in the first such format of the action, where Grails 9 ran -the first block and answered in the first format: `withFormat { pdf {...}; html {...} }` runs the `html` block for -`Accept: text/*`. -* *A `Content-Type` Spring cannot read.* A `Content-Type` Spring rejects, such as `application/json;charset=` or a list -of types, is of no format, where Grails 9 read it leniently as `json`. - -Other requests resolve as before, among them those of browsers and those that name one configured type. A Spring MVC -endpoint, and Spring Security's filters before a URL mapping has matched a request, negotiate as before. diff --git a/grails-doc/src/en/guide/upgrading/upgrading90x.adoc b/grails-doc/src/en/guide/upgrading/upgrading90x.adoc index 9b4cb68887c..dad7baaca1e 100644 --- a/grails-doc/src/en/guide/upgrading/upgrading90x.adoc +++ b/grails-doc/src/en/guide/upgrading/upgrading90x.adoc @@ -1274,3 +1274,61 @@ A property source of lower precedence that sets the deeper key now answers it. W * Code that reads a child of a string or a closure and relied on being answered with the parent reads the parent instead. * Code that read a whole list through a key ending in a whole number, such as `server.ports.0`, now gets the element; it reads the list as `server.ports`. * A typed read with a default below such a value, such as `getProperty('x.y.enabled', Boolean, true)` when `x.y` is `false`, reads the parent, or the configuration sets the deeper key. + +==== 22. Opt Into Spring MVC Content Negotiation for Grails Formats + +Set `grails.mime.negotiation.spring: true` to resolve `request.format`, `response.format`, `withFormat` and the format +`respond` writes in with Spring MVC's content negotiation manager, and to have the `format` parameter, URL extensions and +the `grails.mime` settings of the `Accept` header decide what Spring MVC writes for an action too (see +<>). Grails 9 keeps its own rules by +default, and Grails 10 makes the setting the default, so an application should check these answers before then. + +[source,yaml] +.grails-app/conf/application.yml +---- +grails: + mime: + negotiation: + spring: true +---- + +The setting changes these answers, which the framework's tests record by running the same requests both ways: + +* *The order of the `Accept` header.* Spring MVC takes a more specific type ahead of a less specific one of equal +quality, where the Grails rules keep the order of the header: `Accept: */*, application/json` is the `json` format, +where it is `all` without the setting, and `withFormat` runs the `json` block rather than its first. A type with the `+xml` suffix no longer comes ahead +of `application/xml`: `Accept: application/xml, application/atom+xml` is `xml`, where it is `atom` without the setting. +* *Types with the `+json` and `+xml` suffixes.* A type no configured type names, such as `application/merge-patch+json`, +`application/soap+xml` or, without a `hal` format, `application/hal+json`, is the `json` or `xml` format, where the +Grails rules read only the problem types of RFC 9457 so and answer any other as `all`. In `Content-Type` too: a body sent +as such a type is of the `json` format, where its `request.format` is otherwise `null`, and data binding reads it as JSON. +* *A request that accepts no configured type.* Such a request, as one that sends only `Accept: image/png`, and one whose +`Accept` header Spring cannot read, such as `Accept: application/json;q=abc`, is not acceptable: `response.format` and +`response.mimeType` are `null`, `respond` answers `406 Not Acceptable` and `withFormat` runs its `*` block, or no block, +where the Grails rules answer as for a client that accepts any type, with the `all` format, or read the header +leniently. Code that reads `response.format` or `response.mimeType` must allow for `null`. +* *The `format` parameter and URL extensions.* A format no type is configured for is not acceptable, to `respond`, which +answers `406`, and to what Spring MVC writes, where the Grails rules answer in the first configured format; +`withFormat` answers it from the `Accept` header, as for a format it has no block for. The `all` format leaves the +format to the `Accept` header: `?format=all` with `Accept: application/json` is `json`, where it is otherwise `all`. The +first value of the parameter counts. +* *What Spring MVC writes for an action.* The `format` parameter and URL extensions now decide the type of a +`ResponseEntity`, a `ProblemDetail` or another value Spring MVC writes for a request a URL mapping handles, where Spring +MVC read only the `Accept` header: `/book/show/1.json` is answered in JSON whatever the `Accept` header asks for, and +`/book/show/1.html` of an action that returns a map with `406`, since no message converter writes a map as HTML. +`grails.mime.use.accept.header: false` and `grails.mime.disable.accept.header.*` now ignore the `Accept` header for it +too. +* *Without the `all` format.* Where `grails.mime.types` does not list `all`, a request without an `Accept` header is of +the `all` format, as one that sends `Accept: */*` is, where the Grails rules give the first configured format, so +`withFormat` runs its `*` block, or its first block, rather than the block of that format. A type of quality `0` comes +after any other, so `Accept: application/json;q=0, */*;q=0.1` is `all`, where it is otherwise `json`. `?format=all` is +then an unknown format. +* *Wildcards.* A wildcard such as `text/*` is the `all` format as before, but `withFormat` runs the first block of a +format with a type compatible with it, and `respond` answers in the first such format of the action, where the Grails +rules run the first block and answer in the first format: `withFormat { pdf {...}; html {...} }` runs the `html` block for +`Accept: text/*`. +* *A `Content-Type` Spring cannot read.* A `Content-Type` Spring rejects, such as `application/json;charset=` or a list +of types, is of no format, where the Grails rules read it leniently as `json`. + +Other requests resolve as before, among them those of browsers and those that name one configured type. A Spring MVC +endpoint, and Spring Security's filters before a URL mapping has matched a request, negotiate as before. diff --git a/grails-mimetypes/src/main/groovy/org/grails/plugins/web/api/MimeTypesApiSupport.groovy b/grails-mimetypes/src/main/groovy/org/grails/plugins/web/api/MimeTypesApiSupport.groovy index 378032de564..0fd8470a2ea 100644 --- a/grails-mimetypes/src/main/groovy/org/grails/plugins/web/api/MimeTypesApiSupport.groovy +++ b/grails-mimetypes/src/main/groovy/org/grails/plugins/web/api/MimeTypesApiSupport.groovy @@ -125,7 +125,7 @@ class MimeTypesApiSupport { * @param formats the blocks, by format * @param accepted the type of the {@code all} format * @return the result of the block, or {@link #NO_FORMAT} where none is of a format compatible with the wildcard - * @since 10.0 + * @since 9.0 */ protected Object resolveAllFormat(formatProvider, LinkedHashMap formats, MimeType accepted) { if (accepted == null || accepted.extension != 'all' || accepted.name == MimeType.ALL.name || formats.containsKey('*')) { diff --git a/grails-mimetypes/src/main/groovy/org/grails/plugins/web/mime/MimeTypesConfiguration.groovy b/grails-mimetypes/src/main/groovy/org/grails/plugins/web/mime/MimeTypesConfiguration.groovy index c336b6f884a..d5393f65d9b 100644 --- a/grails-mimetypes/src/main/groovy/org/grails/plugins/web/mime/MimeTypesConfiguration.groovy +++ b/grails-mimetypes/src/main/groovy/org/grails/plugins/web/mime/MimeTypesConfiguration.groovy @@ -174,7 +174,7 @@ class MimeTypesConfiguration { * created before the other beans, and the strategy it applies them with is that of the configurer, once a request * is negotiated. * - * @since 10.0 + * @since 9.0 */ @Bean static GrailsContentNegotiationManagerPostProcessor grailsContentNegotiationManagerPostProcessor( diff --git a/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsAcceptHeaderContentNegotiationStrategy.groovy b/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsAcceptHeaderContentNegotiationStrategy.groovy index 6edfed24860..acbd45f240b 100644 --- a/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsAcceptHeaderContentNegotiationStrategy.groovy +++ b/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsAcceptHeaderContentNegotiationStrategy.groovy @@ -36,7 +36,7 @@ import org.springframework.web.context.request.NativeWebRequest * resolves for it. Any other request, such as one a Spring MVC endpoint handles, is negotiated by the strategy it * replaces. It is a {@link HeaderContentNegotiationStrategy}, so that it is found as Spring MVC's is. * - * @since 10.0 + * @since 9.0 */ @CompileStatic class GrailsAcceptHeaderContentNegotiationStrategy extends HeaderContentNegotiationStrategy { diff --git a/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationManager.groovy b/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationManager.groovy index 6c8aa49dba6..7a0cc525577 100644 --- a/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationManager.groovy +++ b/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationManager.groovy @@ -34,7 +34,7 @@ import org.springframework.web.accept.MediaTypeFileExtensionResolver * ignores it for such a request where the {@code grails.mime} settings say so. Any other request is negotiated as by * Spring MVC's manager, whose formats and file extensions it answers with. * - * @since 10.0 + * @since 9.0 */ @CompileStatic class GrailsContentNegotiationManager extends ContentNegotiationManager { diff --git a/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationManagerPostProcessor.groovy b/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationManagerPostProcessor.groovy index 2299b6d562d..463c60e50f3 100644 --- a/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationManagerPostProcessor.groovy +++ b/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationManagerPostProcessor.groovy @@ -36,7 +36,7 @@ import grails.config.Settings * a {@code ProblemDetail} an action returns or {@code respond} writes, is negotiated as Grails resolves their formats. * Without it, Spring MVC's manager is left as it is. * - * @since 10.0 + * @since 9.0 */ @CompileStatic class GrailsContentNegotiationManagerPostProcessor implements BeanPostProcessor { diff --git a/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationStrategy.groovy b/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationStrategy.groovy index 68a18d7a4ec..54c7226b527 100644 --- a/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationStrategy.groovy +++ b/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationStrategy.groovy @@ -94,7 +94,7 @@ class GrailsContentNegotiationStrategy { * {@code grails.mime.negotiation.spring}, the formats are resolved with; asked for each request, since the * strategy is created before it; or {@code null}, or one that supplies none, for one that reads the * {@code Accept} header alone, as Spring MVC's default does - * @since 10.0 + * @since 9.0 */ GrailsContentNegotiationStrategy(MimeType[] mimeTypes, Config config, Supplier contentNegotiationManager) { @@ -123,7 +123,7 @@ class GrailsContentNegotiationStrategy { /** * @return whether the formats are resolved with Spring MVC's content negotiation manager, as * {@code grails.mime.negotiation.spring} says - * @since 10.0 + * @since 9.0 */ boolean isSpringNegotiation() { return springNegotiation @@ -160,7 +160,7 @@ class GrailsContentNegotiationStrategy { * @return its first configured type; for a format that is not configured, the first configured type, as Grails 9 * answers, or, with {@code grails.mime.negotiation.spring}, {@code null}, since the request accepts no * configured type - * @since 10.0 + * @since 9.0 */ MimeType resolveFormat(Object format) { MimeType mimeType = mimeTypes.find { MimeType configured -> configured.extension == format } @@ -178,7 +178,7 @@ class GrailsContentNegotiationStrategy { * * @param webRequest the request * @return the format, or {@code null} where the request names none - * @since 10.0 + * @since 9.0 */ Object requestedFormat(GrailsWebRequest webRequest) { Object format = webRequest?.params?.get(FORMAT_PARAMETER) @@ -202,7 +202,7 @@ class GrailsContentNegotiationStrategy { * * @param request the request * @return the MIME types of its content - * @since 10.0 + * @since 9.0 */ MimeType[] resolveContentMimeTypes(HttpServletRequest request) { String header = request.contentType ?: request.getHeader(HttpHeaders.CONTENT_TYPE) @@ -230,7 +230,7 @@ class GrailsContentNegotiationStrategy { * * @param format the format * @return the media types, or {@code null} where no type of the format is configured - * @since 10.0 + * @since 9.0 */ List mediaTypesOf(String format) { List types = [] @@ -254,7 +254,7 @@ class GrailsContentNegotiationStrategy { /** * @return the media types of every configured MIME type Spring can read, which a request that names a format no type * is configured for is not acceptable in - * @since 10.0 + * @since 9.0 */ List getConfiguredMediaTypes() { return mediaTypes.findAll { it }.unique() as List @@ -263,7 +263,7 @@ class GrailsContentNegotiationStrategy { /** * @return whether the {@code Accept} header of a request is ignored, as {@code grails.mime.use.accept.header} and * {@code grails.mime.disable.accept.header.*} say - * @since 10.0 + * @since 9.0 */ boolean ignoresAcceptHeader(HttpServletRequest request) { if (!useAcceptHeader) { @@ -284,7 +284,7 @@ class GrailsContentNegotiationStrategy { * * @param request the request * @return whether a URL mapping handles it - * @since 10.0 + * @since 9.0 */ static boolean handlesRequest(HttpServletRequest request) { return request.getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE) instanceof UrlMappingInfo @@ -297,7 +297,7 @@ class GrailsContentNegotiationStrategy { * * @param request the request * @return the strategy - * @since 10.0 + * @since 9.0 */ static GrailsContentNegotiationStrategy forRequest(HttpServletRequest request) { ApplicationContext applicationContext = GrailsWebRequest.lookup()?.applicationContext @@ -387,7 +387,7 @@ class GrailsContentNegotiationStrategy { * @param accepted a type {@code response.mimeTypes} resolves * @param format a format * @return whether it accepts the format - * @since 10.0 + * @since 9.0 */ boolean acceptsFormat(MimeType accepted, String format) { if (accepted == null) { diff --git a/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsFormatContentNegotiationStrategy.groovy b/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsFormatContentNegotiationStrategy.groovy index b5e15c878ed..9db039048b1 100644 --- a/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsFormatContentNegotiationStrategy.groovy +++ b/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsFormatContentNegotiationStrategy.groovy @@ -38,7 +38,7 @@ import org.grails.web.servlet.mvc.GrailsWebRequest * leaves any other request, such as one a Spring MVC endpoint handles, to them, and so a request whose format names no * type, as the {@code all} format does. A request that names a format no type is configured for is not acceptable. * - * @since 10.0 + * @since 9.0 */ @CompileStatic class GrailsFormatContentNegotiationStrategy implements ContentNegotiationStrategy { diff --git a/grails-mimetypes/src/test/groovy/org/grails/web/mime/FormatNegotiationComparisonSpec.groovy b/grails-mimetypes/src/test/groovy/org/grails/web/mime/FormatNegotiationComparisonSpec.groovy index 8ec724d1d45..7c5e63e836b 100644 --- a/grails-mimetypes/src/test/groovy/org/grails/web/mime/FormatNegotiationComparisonSpec.groovy +++ b/grails-mimetypes/src/test/groovy/org/grails/web/mime/FormatNegotiationComparisonSpec.groovy @@ -140,7 +140,7 @@ class FormatNegotiationComparisonSpec extends Specification { [contentType: 'image/png'] ] - /** Every answer that differs, as {@code configuration | request | answer: Grails 9 -> Spring}. */ + /** Every answer that differs, as {@code configuration | request | answer: Grails rules -> Spring MVC's}. */ private static final List RECORDED = [ 'defaults | Accept: */*, application/json | response.format: all -> json', 'defaults | Accept: */*, application/json | withFormat(html, json, xml): html -> json', diff --git a/grails-web-core/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/grails-web-core/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 7a236c80de6..c3842951ce1 100644 --- a/grails-web-core/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/grails-web-core/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -135,7 +135,7 @@ { "name": "grails.mime.negotiation.spring", "type": "java.lang.Boolean", - "description": "Whether request.format, response.format, withFormat and the format respond writes in are resolved with Spring MVC's content negotiation manager, mapping the media types it resolves to the formats grails.mime.types gives them, rather than by the Grails rules. The format parameter, URL extensions and the grails.mime Accept header settings are kept, as strategies of Spring MVC's manager that also apply to what Spring MVC writes for the requests a URL mapping handles. Grails 9 negotiation stays the default in Grails 10.", + "description": "Whether request.format, response.format, withFormat and the format respond writes in are resolved with Spring MVC's content negotiation manager, mapping the media types it resolves to the formats grails.mime.types gives them, rather than by the Grails rules. The format parameter, URL extensions and the grails.mime Accept header settings are kept, as strategies of Spring MVC's manager that also apply to what Spring MVC writes for the requests a URL mapping handles. Grails 10 makes it the default.", "defaultValue": false }, { From 915151f3ffd78cf7a316face82f6d10c82c913f6 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sat, 14 Nov 2026 12:18:01 -0800 Subject: [PATCH 015/115] Match a Spring MVC endpoint an action forwards to on its own negotiation A forward an action makes is a dispatch of its own, which Spring MVC's handler mappings match after the URL mappings, while the mapping of the forwarding dispatch was still the request's best matching handler. With grails.mime.negotiation.spring, the produces condition of an endpoint was then matched on the format the action was asked for: /book/forward?format=xml to an endpoint that produces JSON answered 406 for a client that accepts JSON. The URL mappings now take a mapping an earlier dispatch left off the request when they match no handler for the dispatch. --- .../contentneg/SpringWrittenController.groovy | 4 ++++ .../SpringFormatNegotiationSpec.groovy | 9 +++++++-- .../contentneg/SpringNegotiationApi.groovy | 2 +- .../mvc/UrlMappingsHandlerMapping.groovy | 5 +++++ .../mvc/UrlMappingsHandlerMappingSpec.groovy | 20 +++++++++++++++++++ 5 files changed, 37 insertions(+), 3 deletions(-) diff --git a/grails-test-examples/app1/grails-app/controllers/functionaltests/contentneg/SpringWrittenController.groovy b/grails-test-examples/app1/grails-app/controllers/functionaltests/contentneg/SpringWrittenController.groovy index a07bf04f2b2..06398002916 100644 --- a/grails-test-examples/app1/grails-app/controllers/functionaltests/contentneg/SpringWrittenController.groovy +++ b/grails-test-examples/app1/grails-app/controllers/functionaltests/contentneg/SpringWrittenController.groovy @@ -37,4 +37,8 @@ class SpringWrittenController { def formats() { render(contentType: 'text/plain', text: "${response.format}|${request.format}") } + + def forwardToSpring() { + forward(uri: '/springNegotiation') + } } diff --git a/grails-test-examples/app1/src/integration-test/groovy/functionaltests/contentneg/SpringFormatNegotiationSpec.groovy b/grails-test-examples/app1/src/integration-test/groovy/functionaltests/contentneg/SpringFormatNegotiationSpec.groovy index e7f691c56fd..7bdea2c334e 100644 --- a/grails-test-examples/app1/src/integration-test/groovy/functionaltests/contentneg/SpringFormatNegotiationSpec.groovy +++ b/grails-test-examples/app1/src/integration-test/groovy/functionaltests/contentneg/SpringFormatNegotiationSpec.groovy @@ -73,10 +73,15 @@ class SpringFormatNegotiationSpec extends Specification implements HttpClientSup http("/springNegotiation${query}", Accept: accept).assertStatus(status) where: - description | query | accept || status - 'the format parameter is ignored' | '?format=xml' | 'application/json' || 200 + description | query | accept || status + 'the format parameter is ignored' | '?format=xml' | 'application/json' || 200 'an unknown type is not acceptable' | '' | 'image/png' || 406 } + + void 'a Spring MVC endpoint an action forwards to is matched on the Accept header, not on the format the action was asked for'() { + expect: + http('/springWritten/forwardToSpring?format=xml', Accept: 'application/json').assertStatus(200).assertJson([title: 'Grails']) + } } /** diff --git a/grails-test-examples/app1/src/main/groovy/functionaltests/contentneg/SpringNegotiationApi.groovy b/grails-test-examples/app1/src/main/groovy/functionaltests/contentneg/SpringNegotiationApi.groovy index e50b73ae05c..cdfbc687039 100644 --- a/grails-test-examples/app1/src/main/groovy/functionaltests/contentneg/SpringNegotiationApi.groovy +++ b/grails-test-examples/app1/src/main/groovy/functionaltests/contentneg/SpringNegotiationApi.groovy @@ -28,7 +28,7 @@ import org.springframework.web.bind.annotation.RestController @RestController class SpringNegotiationApi { - @GetMapping('/springNegotiation') + @GetMapping(path = '/springNegotiation', produces = 'application/json') Map negotiation() { [title: 'Grails'] } diff --git a/grails-web-url-mappings/src/main/groovy/org/grails/web/mapping/mvc/UrlMappingsHandlerMapping.groovy b/grails-web-url-mappings/src/main/groovy/org/grails/web/mapping/mvc/UrlMappingsHandlerMapping.groovy index a4a673b371a..617b7983e96 100644 --- a/grails-web-url-mappings/src/main/groovy/org/grails/web/mapping/mvc/UrlMappingsHandlerMapping.groovy +++ b/grails-web-url-mappings/src/main/groovy/org/grails/web/mapping/mvc/UrlMappingsHandlerMapping.groovy @@ -193,6 +193,11 @@ class UrlMappingsHandlerMapping extends AbstractHandlerMapping { if (handler != null) { request.setAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE, handler) } + else if (request.getAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE) instanceof UrlMappingInfo) { + // A dispatch no mapping handles, such as a forward an action makes to a Spring MVC endpoint, is not handled by + // the mapping of the dispatch that made it, whose attribute the handler mapping that matches it reads first + request.removeAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE) + } return handler } diff --git a/grails-web-url-mappings/src/test/groovy/org/grails/web/mapping/mvc/UrlMappingsHandlerMappingSpec.groovy b/grails-web-url-mappings/src/test/groovy/org/grails/web/mapping/mvc/UrlMappingsHandlerMappingSpec.groovy index 72233cada8b..85cbaeb2826 100644 --- a/grails-web-url-mappings/src/test/groovy/org/grails/web/mapping/mvc/UrlMappingsHandlerMappingSpec.groovy +++ b/grails-web-url-mappings/src/test/groovy/org/grails/web/mapping/mvc/UrlMappingsHandlerMappingSpec.groovy @@ -738,6 +738,26 @@ class UrlMappingsHandlerMappingSpec extends AbstractUrlMappingsSpec { request.getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE).is(springHandler) } + void 'a forward no mapping matches is no longer handled by the mapping of the dispatch that made it'() { + given: + def grailsApplication = new DefaultGrailsApplication(FooController) + grailsApplication.initialise() + def handler = new UrlMappingsHandlerMapping(new GrailsControllerUrlMappings(grailsApplication, getUrlMappingsHolder { + '/foo/bar'(controller: 'foo', action: 'bar') + })) + def request = GrailsWebMockUtil.bindMockWebRequest().request + request.requestURI = '/foo/bar' + handler.getHandler(request) + + when: 'the action forwards to a URI that a handler mapping of Spring MVC, which comes later, handles' + request.setAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE, '/foo/bar') + request.requestURI = '/spring/endpoint' + + then: + handler.getHandler(request) == null + request.getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE) == null + } + void 'a request no mapping matches keeps the best matching handler another handler mapping set'() { given: def handler = new UrlMappingsHandlerMapping(getUrlMappingsHolder { From a13c4890555c440dc0c327ba143683ecc6f5c93e Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sat, 14 Nov 2026 12:39:26 -0800 Subject: [PATCH 016/115] Read grails.mime.negotiation.spring from the Grails configuration only The post-processor that replaces Spring MVC's content negotiation manager read the setting from the Spring Environment, while the strategy that resolves Grails formats read it from the application's configuration. A configuration that is not the environment, as one a test builds by hand or one code changes, could then enable one half of the setting without the other: Grails formats resolved with Spring MVC's manager while what Spring MVC writes ignored the Grails rules, or the other way round. Both now read the application's configuration. --- .../web/mime/MimeTypesConfiguration.groovy | 11 ++--- ...tentNegotiationManagerPostProcessor.groovy | 16 ++++--- .../mime/SpringFormatNegotiationSpec.groovy | 44 +++++++++++++++++++ 3 files changed, 59 insertions(+), 12 deletions(-) diff --git a/grails-mimetypes/src/main/groovy/org/grails/plugins/web/mime/MimeTypesConfiguration.groovy b/grails-mimetypes/src/main/groovy/org/grails/plugins/web/mime/MimeTypesConfiguration.groovy index d5393f65d9b..ec2be2c831e 100644 --- a/grails-mimetypes/src/main/groovy/org/grails/plugins/web/mime/MimeTypesConfiguration.groovy +++ b/grails-mimetypes/src/main/groovy/org/grails/plugins/web/mime/MimeTypesConfiguration.groovy @@ -28,7 +28,6 @@ import org.springframework.beans.factory.annotation.Qualifier import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration import org.springframework.context.annotation.Primary -import org.springframework.core.env.Environment import org.springframework.web.accept.ContentNegotiationManager import grails.config.Config @@ -171,15 +170,17 @@ class MimeTypesConfiguration { /** * With {@code grails.mime.negotiation.spring}, has Spring MVC's content negotiation manager apply the * {@code grails.mime} rules Grails keeps to the requests a URL mapping handles. Static, as a post-processor is - * created before the other beans, and the strategy it applies them with is that of the configurer, once a request - * is negotiated. + * created before the other beans: it reads the setting from the application's configuration as Spring MVC's manager + * is created, as the strategy of the configurer, which it applies the rules with once a request is negotiated, reads + * it. * * @since 9.0 */ @Bean static GrailsContentNegotiationManagerPostProcessor grailsContentNegotiationManagerPostProcessor( - Environment environment, ObjectProvider grailsMimeTypesWebMvcConfigurer) { - return new GrailsContentNegotiationManagerPostProcessor(environment, () -> + ObjectProvider grailsApplication, + ObjectProvider grailsMimeTypesWebMvcConfigurer) { + return new GrailsContentNegotiationManagerPostProcessor(() -> grailsApplication.getObject().config, () -> grailsMimeTypesWebMvcConfigurer.getObject().contentNegotiationStrategy) } diff --git a/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationManagerPostProcessor.groovy b/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationManagerPostProcessor.groovy index 463c60e50f3..b5a3f11ea05 100644 --- a/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationManagerPostProcessor.groovy +++ b/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationManagerPostProcessor.groovy @@ -24,9 +24,9 @@ import groovy.transform.CompileStatic import org.springframework.beans.BeansException import org.springframework.beans.factory.config.BeanPostProcessor -import org.springframework.core.env.Environment import org.springframework.web.accept.ContentNegotiationManager +import grails.config.Config import grails.config.Settings /** @@ -34,25 +34,27 @@ import grails.config.Settings * the {@code grails.mime} rules Grails keeps to the requests a URL mapping handles (see * {@link GrailsContentNegotiationManager}), so that what Spring MVC writes for them, such as a {@code ResponseEntity} or * a {@code ProblemDetail} an action returns or {@code respond} writes, is negotiated as Grails resolves their formats. - * Without it, Spring MVC's manager is left as it is. + * Without it, Spring MVC's manager is left as it is. The setting is read from the application's configuration, which + * the strategy that resolves Grails formats reads it from too, so that the two never disagree. * * @since 9.0 */ @CompileStatic class GrailsContentNegotiationManagerPostProcessor implements BeanPostProcessor { - private final Environment environment + private final Supplier config private final Supplier grailsStrategy /** - * @param environment the environment the setting is read from + * @param config supplies the application's configuration, which the setting is read from as Spring MVC's manager is + * created, since a post-processor is created before the other beans * @param grailsStrategy supplies the strategy that resolves Grails formats, which Spring MVC's manager is created * before */ - GrailsContentNegotiationManagerPostProcessor(Environment environment, + GrailsContentNegotiationManagerPostProcessor(Supplier config, Supplier grailsStrategy) { - this.environment = environment + this.config = config this.grailsStrategy = grailsStrategy } @@ -60,7 +62,7 @@ class GrailsContentNegotiationManagerPostProcessor implements BeanPostProcessor Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (beanName == GrailsContentNegotiationStrategy.MVC_CONTENT_NEGOTIATION_MANAGER && bean instanceof ContentNegotiationManager && !(bean instanceof GrailsContentNegotiationManager) && - environment.getProperty(Settings.MIME_NEGOTIATION_SPRING, Boolean, + config.get().getProperty(Settings.MIME_NEGOTIATION_SPRING, Boolean, Boolean.valueOf(Settings.MIME_NEGOTIATION_SPRING_DEFAULT))) { return new GrailsContentNegotiationManager((ContentNegotiationManager) bean, grailsStrategy) } diff --git a/grails-mimetypes/src/test/groovy/org/grails/web/mime/SpringFormatNegotiationSpec.groovy b/grails-mimetypes/src/test/groovy/org/grails/web/mime/SpringFormatNegotiationSpec.groovy index 11915007f54..7ab8f493b76 100644 --- a/grails-mimetypes/src/test/groovy/org/grails/web/mime/SpringFormatNegotiationSpec.groovy +++ b/grails-mimetypes/src/test/groovy/org/grails/web/mime/SpringFormatNegotiationSpec.groovy @@ -24,6 +24,7 @@ import org.springframework.context.annotation.Configuration import org.springframework.context.annotation.Import import org.springframework.core.env.ConfigurableEnvironment import org.springframework.core.env.MapPropertySource +import org.springframework.core.env.MutablePropertySources import org.springframework.http.MediaType import org.springframework.mock.web.MockHttpServletRequest import org.springframework.mock.web.MockHttpServletResponse @@ -377,6 +378,28 @@ class SpringFormatNegotiationSpec extends Specification { request.mimeTypes*.name == ['image/png'] } + void "the setting is read from the application's configuration, for Spring MVC's manager as for Grails formats, where the environment says #environment"() { + given: 'a configuration of the application that is not the environment, as one a test builds by hand' + context = new AnnotationConfigWebApplicationContext() + context.servletContext = new MockServletContext() + context.environment.propertySources.addFirst(new MapPropertySource('test', [ + (Settings.MIME_NEGOTIATION_SPRING) : environment, + (SeparateConfigConfiguration.CONFIGURED): configured + ])) + context.register(SeparateConfigConfiguration) + context.refresh() + request('Accept': '*/*, application/json') + + expect: + (manager() instanceof GrailsContentNegotiationManager) == configured + GrailsWebRequest.lookup().currentResponse.format == format + + where: + environment | configured || format + false | true || 'json' + true | false || 'all' + } + void 'without the setting, the Grails 9 rules resolve the formats'() { given: start([:]) @@ -437,6 +460,27 @@ class SpringFormatNegotiationSpec extends Specification { } } + /** An application whose configuration has the setting the test gives it apart from the environment. */ + @Configuration(proxyBeanMethods = false) + @EnableWebMvc + @Import(MimeTypesConfiguration) + static class SeparateConfigConfiguration { + + static final String CONFIGURED = 'test.configured.negotiation' + + @Bean + GrailsApplication grailsApplication(ApplicationContext applicationContext, ConfigurableEnvironment environment) { + def propertySources = new MutablePropertySources() + propertySources.addFirst(new MapPropertySource('configured', + [(Settings.MIME_NEGOTIATION_SPRING): environment.getProperty(CONFIGURED, Boolean)])) + def application = new DefaultGrailsApplication() + application.config = new PropertySourcesConfig(propertySources) + application.applicationContext = applicationContext + application.mainContext = applicationContext + application + } + } + /** Enables Spring MVC's parameter negotiation, as {@code spring.mvc.contentnegotiation.*} does, where the test says. */ @Configuration(proxyBeanMethods = false) static class ParameterConfiguration implements WebMvcConfigurer { From 840fc4a56666beef8f834c144fd7f3d33adc564b Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sat, 14 Nov 2026 12:44:09 -0800 Subject: [PATCH 017/115] Test Spring MVC format negotiation in an application of its own The functional spec with grails.mime.negotiation.spring ran in app1 with @TestPropertySource, which starts a second application context in the test JVM. GORM's static API then answered for that context, so the GORM specs of app1 that ran after it failed with "No Session found for current thread". The spec moves to grails-test-examples/spring-format-negotiation, whose application.yml sets the setting, with the Spring MVC endpoint and the actions it calls; app1 keeps the spec of the Grails rules, which runs in its own context. --- .../contentneg/SpringWrittenController.groovy | 6 +-- .../init/functionaltests/Application.groovy | 11 ---- .../GrailsFormatNegotiationSpec.groovy | 50 +++++++++++++++++++ .../spring-format-negotiation/build.gradle | 42 ++++++++++++++++ .../grails-app/conf/application.yml | 32 ++++++++++++ .../grails-app/conf/logback.xml | 38 ++++++++++++++ .../SpringWrittenController.groovy | 44 ++++++++++++++++ .../UrlMappings.groovy | 30 +++++++++++ .../Application.groovy | 46 +++++++++++++++++ .../SpringFormatNegotiationSpec.groovy | 31 +----------- .../SpringNegotiationApi.groovy | 2 +- settings.gradle | 2 + 12 files changed, 288 insertions(+), 46 deletions(-) create mode 100644 grails-test-examples/app1/src/integration-test/groovy/functionaltests/contentneg/GrailsFormatNegotiationSpec.groovy create mode 100644 grails-test-examples/spring-format-negotiation/build.gradle create mode 100644 grails-test-examples/spring-format-negotiation/grails-app/conf/application.yml create mode 100644 grails-test-examples/spring-format-negotiation/grails-app/conf/logback.xml create mode 100644 grails-test-examples/spring-format-negotiation/grails-app/controllers/springformatnegotiation/SpringWrittenController.groovy create mode 100644 grails-test-examples/spring-format-negotiation/grails-app/controllers/springformatnegotiation/UrlMappings.groovy create mode 100644 grails-test-examples/spring-format-negotiation/grails-app/init/springformatnegotiation/Application.groovy rename grails-test-examples/{app1/src/integration-test/groovy/functionaltests/contentneg => spring-format-negotiation/src/integration-test/groovy/springformatnegotiation}/SpringFormatNegotiationSpec.groovy (72%) rename grails-test-examples/{app1/src/main/groovy/functionaltests/contentneg => spring-format-negotiation/src/main/groovy/springformatnegotiation}/SpringNegotiationApi.groovy (97%) diff --git a/grails-test-examples/app1/grails-app/controllers/functionaltests/contentneg/SpringWrittenController.groovy b/grails-test-examples/app1/grails-app/controllers/functionaltests/contentneg/SpringWrittenController.groovy index 06398002916..9dd265d1850 100644 --- a/grails-test-examples/app1/grails-app/controllers/functionaltests/contentneg/SpringWrittenController.groovy +++ b/grails-test-examples/app1/grails-app/controllers/functionaltests/contentneg/SpringWrittenController.groovy @@ -22,7 +22,7 @@ import org.springframework.http.ResponseEntity /** * Actions whose values Spring MVC writes, and one that answers with the formats Grails resolves for the request, which - * {@code grails.mime.negotiation.spring} negotiates alike. + * the Grails rules negotiate without {@code grails.mime.negotiation.spring}. */ class SpringWrittenController { @@ -37,8 +37,4 @@ class SpringWrittenController { def formats() { render(contentType: 'text/plain', text: "${response.format}|${request.format}") } - - def forwardToSpring() { - forward(uri: '/springNegotiation') - } } diff --git a/grails-test-examples/app1/grails-app/init/functionaltests/Application.groovy b/grails-test-examples/app1/grails-app/init/functionaltests/Application.groovy index 4019ce93a86..dcc99a5d58b 100644 --- a/grails-test-examples/app1/grails-app/init/functionaltests/Application.groovy +++ b/grails-test-examples/app1/grails-app/init/functionaltests/Application.groovy @@ -19,11 +19,8 @@ package functionaltests -import org.springframework.context.annotation.Bean - import grails.boot.GrailsApp import grails.boot.config.GrailsAutoConfiguration -import functionaltests.contentneg.SpringNegotiationApi class Application extends GrailsAutoConfiguration { static { @@ -33,12 +30,4 @@ class Application extends GrailsAutoConfiguration { static void main(String[] args) { GrailsApp.run(Application) } - - /** - * A Spring MVC endpoint beside the Grails controllers, which content negotiation leaves to Spring MVC. - */ - @Bean - SpringNegotiationApi springNegotiationApi() { - new SpringNegotiationApi() - } } diff --git a/grails-test-examples/app1/src/integration-test/groovy/functionaltests/contentneg/GrailsFormatNegotiationSpec.groovy b/grails-test-examples/app1/src/integration-test/groovy/functionaltests/contentneg/GrailsFormatNegotiationSpec.groovy new file mode 100644 index 00000000000..26073054254 --- /dev/null +++ b/grails-test-examples/app1/src/integration-test/groovy/functionaltests/contentneg/GrailsFormatNegotiationSpec.groovy @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package functionaltests.contentneg + +import spock.lang.Specification +import spock.lang.Tag + +import grails.testing.mixin.integration.Integration +import org.apache.grails.testing.http.client.HttpClientSupport + +/** + * Without {@code grails.mime.negotiation.spring}, Grails resolves formats with its own rules, and Spring MVC writes the + * values of actions as the {@code Accept} header asks. + */ +@Integration +@Tag('http-client') +class GrailsFormatNegotiationSpec extends Specification implements HttpClientSupport { + + void 'a value Spring MVC writes for an action is in the type the Accept header asks for, whatever its URL extension'() { + expect: + http('/springWritten/entity.html', Accept: '*/*').assertStatus(200).contentType.startsWith('application/json') + http('/springWritten/text.json', Accept: 'text/plain').assertStatus(200).contentType.startsWith('text/plain') + } + + void 'the formats Grails resolves follow the Grails rules: #accept'() { + expect: + http('/springWritten/formats', Accept: accept).assertStatus(200).assertEquals(formats) + + where: + accept || formats + '*/*, application/json' || 'all|all' + 'image/png' || 'all|all' + } +} diff --git a/grails-test-examples/spring-format-negotiation/build.gradle b/grails-test-examples/spring-format-negotiation/build.gradle new file mode 100644 index 00000000000..8facc4f7193 --- /dev/null +++ b/grails-test-examples/spring-format-negotiation/build.gradle @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +plugins { + id 'org.apache.grails.buildsrc.properties' + id 'org.apache.grails.buildsrc.dependency-validator' + id 'org.apache.grails.buildsrc.compile' + id 'org.apache.grails.gradle.grails-web' +} + +version = '0.1' +group = 'springformatnegotiation' + +dependencies { + + implementation platform(project(':grails-bom')) + implementation 'org.apache.grails:grails-dependencies-starter-web' + + integrationTestImplementation 'org.apache.grails:grails-testing-support-http-client' + integrationTestImplementation 'org.apache.grails:grails-testing-support-web' +} + +apply { + from rootProject.layout.projectDirectory.file('gradle/functional-test-config.gradle') + from rootProject.layout.projectDirectory.file('gradle/grails-extension-gradle-config.gradle') +} diff --git a/grails-test-examples/spring-format-negotiation/grails-app/conf/application.yml b/grails-test-examples/spring-format-negotiation/grails-app/conf/application.yml new file mode 100644 index 00000000000..4b8bc514017 --- /dev/null +++ b/grails-test-examples/spring-format-negotiation/grails-app/conf/application.yml @@ -0,0 +1,32 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +--- +grails: + profile: web + codegen: + defaultPackage: springformatnegotiation + mime: + negotiation: + spring: true +info: + app: + name: '@info.app.name@' + version: '@info.app.version@' + grailsVersion: '@info.app.grailsVersion@' +spring: + groovy: + template: + check-template-location: false diff --git a/grails-test-examples/spring-format-negotiation/grails-app/conf/logback.xml b/grails-test-examples/spring-format-negotiation/grails-app/conf/logback.xml new file mode 100644 index 00000000000..2671b7ce98d --- /dev/null +++ b/grails-test-examples/spring-format-negotiation/grails-app/conf/logback.xml @@ -0,0 +1,38 @@ + + + + + + + + + + UTF-8 + %clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wex + + + + + + + + diff --git a/grails-test-examples/spring-format-negotiation/grails-app/controllers/springformatnegotiation/SpringWrittenController.groovy b/grails-test-examples/spring-format-negotiation/grails-app/controllers/springformatnegotiation/SpringWrittenController.groovy new file mode 100644 index 00000000000..57a570aaaac --- /dev/null +++ b/grails-test-examples/spring-format-negotiation/grails-app/controllers/springformatnegotiation/SpringWrittenController.groovy @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package springformatnegotiation + +import org.springframework.http.ResponseEntity + +/** + * Actions whose values Spring MVC writes, and one that answers with the formats Grails resolves for the request, which + * {@code grails.mime.negotiation.spring} negotiates alike. + */ +class SpringWrittenController { + + def entity() { + ResponseEntity.ok([title: 'Grails']) + } + + def text() { + ResponseEntity.ok('Grails') + } + + def formats() { + render(contentType: 'text/plain', text: "${response.format}|${request.format}") + } + + def forwardToSpring() { + forward(uri: '/springNegotiation') + } +} diff --git a/grails-test-examples/spring-format-negotiation/grails-app/controllers/springformatnegotiation/UrlMappings.groovy b/grails-test-examples/spring-format-negotiation/grails-app/controllers/springformatnegotiation/UrlMappings.groovy new file mode 100644 index 00000000000..504d9cc381e --- /dev/null +++ b/grails-test-examples/spring-format-negotiation/grails-app/controllers/springformatnegotiation/UrlMappings.groovy @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package springformatnegotiation + +class UrlMappings { + + static mappings = { + "/$controller/$action?/$id?(.$format)?" { + constraints { + // apply constraints here + } + } + } +} diff --git a/grails-test-examples/spring-format-negotiation/grails-app/init/springformatnegotiation/Application.groovy b/grails-test-examples/spring-format-negotiation/grails-app/init/springformatnegotiation/Application.groovy new file mode 100644 index 00000000000..4d5a4e04800 --- /dev/null +++ b/grails-test-examples/spring-format-negotiation/grails-app/init/springformatnegotiation/Application.groovy @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package springformatnegotiation + +import groovy.transform.CompileStatic + +import org.springframework.context.annotation.Bean + +import grails.boot.GrailsApp +import grails.boot.config.GrailsAutoConfiguration + +/** + * An application that resolves Grails formats with Spring MVC's content negotiation, as + * {@code grails.mime.negotiation.spring} in its {@code application.yml} says. + */ +@CompileStatic +class Application extends GrailsAutoConfiguration { + + static void main(String[] args) { + GrailsApp.run(Application, args) + } + + /** + * A Spring MVC endpoint beside the Grails controllers, which content negotiation leaves to Spring MVC. + */ + @Bean + SpringNegotiationApi springNegotiationApi() { + new SpringNegotiationApi() + } +} diff --git a/grails-test-examples/app1/src/integration-test/groovy/functionaltests/contentneg/SpringFormatNegotiationSpec.groovy b/grails-test-examples/spring-format-negotiation/src/integration-test/groovy/springformatnegotiation/SpringFormatNegotiationSpec.groovy similarity index 72% rename from grails-test-examples/app1/src/integration-test/groovy/functionaltests/contentneg/SpringFormatNegotiationSpec.groovy rename to grails-test-examples/spring-format-negotiation/src/integration-test/groovy/springformatnegotiation/SpringFormatNegotiationSpec.groovy index 7bdea2c334e..b261db0a940 100644 --- a/grails-test-examples/app1/src/integration-test/groovy/functionaltests/contentneg/SpringFormatNegotiationSpec.groovy +++ b/grails-test-examples/spring-format-negotiation/src/integration-test/groovy/springformatnegotiation/SpringFormatNegotiationSpec.groovy @@ -16,9 +16,8 @@ * specific language governing permissions and limitations * under the License. */ -package functionaltests.contentneg +package springformatnegotiation -import org.springframework.test.context.TestPropertySource import spock.lang.Specification import spock.lang.Tag @@ -26,13 +25,12 @@ import grails.testing.mixin.integration.Integration import org.apache.grails.testing.http.client.HttpClientSupport /** - * With {@code grails.mime.negotiation.spring}, Grails formats are resolved with Spring MVC's content negotiation, and + * With {@code grails.mime.negotiation.spring}, which this application's configuration sets, Grails formats are resolved with Spring MVC's content negotiation, and * what Spring MVC writes for an action is negotiated with the format the request names, while a Spring MVC endpoint is * negotiated as before. */ @Integration @Tag('http-client') -@TestPropertySource(properties = 'grails.mime.negotiation.spring=true') class SpringFormatNegotiationSpec extends Specification implements HttpClientSupport { void 'a value Spring MVC writes for an action is in the format of its URL extension, which a map cannot be written in as HTML'() { @@ -83,28 +81,3 @@ class SpringFormatNegotiationSpec extends Specification implements HttpClientSup http('/springWritten/forwardToSpring?format=xml', Accept: 'application/json').assertStatus(200).assertJson([title: 'Grails']) } } - -/** - * Without {@code grails.mime.negotiation.spring}, Grails resolves formats with its own rules, and Spring MVC writes the - * values of actions as the {@code Accept} header asks. - */ -@Integration -@Tag('http-client') -class GrailsFormatNegotiationSpec extends Specification implements HttpClientSupport { - - void 'a value Spring MVC writes for an action is in the type the Accept header asks for, whatever its URL extension'() { - expect: - http('/springWritten/entity.html', Accept: '*/*').assertStatus(200).contentType.startsWith('application/json') - http('/springWritten/text.json', Accept: 'text/plain').assertStatus(200).contentType.startsWith('text/plain') - } - - void 'the formats Grails resolves follow the Grails rules: #accept'() { - expect: - http('/springWritten/formats', Accept: accept).assertStatus(200).assertEquals(formats) - - where: - accept || formats - '*/*, application/json' || 'all|all' - 'image/png' || 'all|all' - } -} diff --git a/grails-test-examples/app1/src/main/groovy/functionaltests/contentneg/SpringNegotiationApi.groovy b/grails-test-examples/spring-format-negotiation/src/main/groovy/springformatnegotiation/SpringNegotiationApi.groovy similarity index 97% rename from grails-test-examples/app1/src/main/groovy/functionaltests/contentneg/SpringNegotiationApi.groovy rename to grails-test-examples/spring-format-negotiation/src/main/groovy/springformatnegotiation/SpringNegotiationApi.groovy index cdfbc687039..8eda1e3bffa 100644 --- a/grails-test-examples/app1/src/main/groovy/functionaltests/contentneg/SpringNegotiationApi.groovy +++ b/grails-test-examples/spring-format-negotiation/src/main/groovy/springformatnegotiation/SpringNegotiationApi.groovy @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package functionaltests.contentneg +package springformatnegotiation import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.RestController diff --git a/settings.gradle b/settings.gradle index b14163544e0..74a4a2eee1a 100644 --- a/settings.gradle +++ b/settings.gradle @@ -576,6 +576,7 @@ include( 'grails-test-examples-datasources', 'grails-test-examples-demo33', 'grails-test-examples-enable-mvc-check', + 'grails-test-examples-spring-format-negotiation', 'grails-test-examples-exploded', 'grails-test-examples-external-configuration', 'grails-test-examples-geb', @@ -646,6 +647,7 @@ project(':grails-test-examples-issue-views-182').projectDir = file('grails-test- project(':grails-test-examples-issue-11102').projectDir = file('grails-test-examples/issue-11102') project(':grails-test-examples-demo33').projectDir = file('grails-test-examples/demo33') project(':grails-test-examples-enable-mvc-check').projectDir = file('grails-test-examples/enable-mvc-check') +project(':grails-test-examples-spring-format-negotiation').projectDir = file('grails-test-examples/spring-format-negotiation') project(':grails-test-examples-plugins-loadfirst').projectDir = file('grails-test-examples/plugins/loadfirst') project(':grails-test-examples-plugins-loadsecond').projectDir = file('grails-test-examples/plugins/loadsecond') project(':grails-test-examples-plugins-loadafter').projectDir = file('grails-test-examples/plugins/loadafter') From 1f5f19f9dfd6d3436b28cdc83a4b956c7130e43b Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sat, 14 Nov 2026 15:14:40 -0800 Subject: [PATCH 018/115] Read the all format as any type where it is not configured With grails.mime.negotiation.spring and a grails.mime.types that does not list all, a client that accepts any type resolves to the all format, which reading response.format saves as the response format. respond then resolved that saved format to no type, and answered 406: the all format is now */*, any type, as Spring MVC reads it, whether or not grails.mime.types lists it. ?format=all then leaves the format to the Accept header there too, for Grails formats and for what Spring MVC writes. --- .../GrailsContentNegotiationStrategy.groovy | 16 ++++++--- .../mime/SpringFormatNegotiationSpec.groovy | 34 ++++++++++++++++++ .../SpringFormatNegotiationRespondSpec.groovy | 36 +++++++++++++++++++ 3 files changed, 81 insertions(+), 5 deletions(-) diff --git a/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationStrategy.groovy b/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationStrategy.groovy index 54c7226b527..05555dd382d 100644 --- a/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationStrategy.groovy +++ b/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationStrategy.groovy @@ -158,15 +158,19 @@ class GrailsContentNegotiationStrategy { * * @param format the format * @return its first configured type; for a format that is not configured, the first configured type, as Grails 9 - * answers, or, with {@code grails.mime.negotiation.spring}, {@code null}, since the request accepts no - * configured type + * answers, or, with {@code grails.mime.negotiation.spring}, {@code null}, since the request accepts no configured + * type, except for the {@code all} format, which is {@code */*}, any type * @since 9.0 */ MimeType resolveFormat(Object format) { MimeType mimeType = mimeTypes.find { MimeType configured -> configured.extension == format } - if (mimeType != null || springNegotiation) { + if (mimeType != null) { return mimeType } + if (springNegotiation) { + // the all format is any type, as Spring MVC reads */*, also where grails.mime.types does not list it + return format == ALL_FORMAT ? new MimeType(MimeType.ALL.name, ALL_FORMAT) : null + } return mimeTypes.find { it } } @@ -229,7 +233,8 @@ class GrailsContentNegotiationStrategy { * suffix, as Spring MVC's JSON and XML converters write them. * * @param format the format - * @return the media types, or {@code null} where no type of the format is configured + * @return the media types, {@code */*} for the {@code all} format where no type of it is configured, or + * {@code null} where no type of another format is * @since 9.0 */ List mediaTypesOf(String format) { @@ -240,7 +245,8 @@ class GrailsContentNegotiationStrategy { } } if (types.isEmpty()) { - return null + // the all format is any type, also where grails.mime.types does not list it + return format == ALL_FORMAT ? [MediaType.ALL] : null } if (format != null && SUFFIX_FORMATS.contains(format)) { MediaType suffixed = new MediaType('application', "*+${format}".toString()) diff --git a/grails-mimetypes/src/test/groovy/org/grails/web/mime/SpringFormatNegotiationSpec.groovy b/grails-mimetypes/src/test/groovy/org/grails/web/mime/SpringFormatNegotiationSpec.groovy index 7ab8f493b76..1c67f3f7e9e 100644 --- a/grails-mimetypes/src/test/groovy/org/grails/web/mime/SpringFormatNegotiationSpec.groovy +++ b/grails-mimetypes/src/test/groovy/org/grails/web/mime/SpringFormatNegotiationSpec.groovy @@ -257,6 +257,40 @@ class SpringFormatNegotiationSpec extends Specification { } == 'html' } + void 'a client that accepts any type answers respond with any format after its format is read, #how, also where the all format is not configured'() { + given: + start((Settings.MIME_NEGOTIATION_SPRING): true, 'grails.mime.types': [json: 'application/json', html: 'text/html']) + def request = request(accept ? ['Accept': accept] : [:]) + def response = GrailsWebRequest.lookup().currentResponse + + when: 'the format is read, which saves it for the response, and then the types respond answers in' + String format = response.format + def formatAware = response.mimeTypesFormatAware.toList() + + then: + format == 'all' + formatAware*.extension == ['all'] + formatAware*.name == ['*/*'] + response.mimeType.name == '*/*' + + where: + how | accept + 'with Accept: */*' | '*/*' + 'without Accept' | null + } + + void 'a format parameter of all, where the all format is not configured, leaves the format to the Accept header'() { + given: + start((Settings.MIME_NEGOTIATION_SPRING): true, 'grails.mime.types': [json: 'application/json', html: 'text/html']) + def request = request('Accept': 'application/json') + handledBy(request, urlMapping()) + GrailsWebRequest.lookup().params.format = 'all' + + expect: + GrailsWebRequest.lookup().currentResponse.format == 'json' + manager().resolveMediaTypes(new ServletWebRequest(request)) == [MediaType.APPLICATION_JSON] + } + void 'a wildcard is of the all format, which withFormat answers with the first block compatible with it: #result for #accept'() { given: start((Settings.MIME_NEGOTIATION_SPRING): true) diff --git a/grails-test-suite-web/src/test/groovy/grails/rest/web/SpringFormatNegotiationRespondSpec.groovy b/grails-test-suite-web/src/test/groovy/grails/rest/web/SpringFormatNegotiationRespondSpec.groovy index 5a4195e05af..a6a5118f31b 100644 --- a/grails-test-suite-web/src/test/groovy/grails/rest/web/SpringFormatNegotiationRespondSpec.groovy +++ b/grails-test-suite-web/src/test/groovy/grails/rest/web/SpringFormatNegotiationRespondSpec.groovy @@ -148,6 +148,42 @@ class SpringFormatNegotiationRespondSpec extends Specification implements Contro } } +/** + * With {@code grails.mime.negotiation.spring} and a {@code grails.mime.types} that does not list the {@code all} format, + * a client that accepts any type is still answered in the action's first format, whatever read the response format first. + */ +class SpringFormatNegotiationWithoutAllRespondSpec extends Specification implements ControllerUnitTest { + + Closure doWithConfig() { + { config -> + config[Settings.WEB_RENDERING_JSON_SPRING] = true + config[Settings.MIME_NEGOTIATION_SPRING] = true + config[Settings.MIME_TYPES] = [json: 'application/json', xml: 'application/xml', html: 'text/html'] + } + } + + void 'a client that accepts any type is answered in the first format of the action after the response format is read, #how'() { + given: + if (accept) { + request.addHeader('Accept', accept) + } + + when: + String format = response.format + controller.respond([title: 'Grails'], formats: ['json', 'xml']) + + then: + format == 'all' + response.status == 200 + response.contentType.startsWith('application/json') + + where: + how | accept + 'with Accept */*' | '*/*' + 'without Accept' | null + } +} + /** * Without {@code grails.mime.negotiation.spring}, the Grails 9 rules resolve the format {@code respond} answers in. */ From f314e849f714e01ab5fa7ca7957ddbadcef19641 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sat, 14 Nov 2026 15:14:50 -0800 Subject: [PATCH 019/115] Keep a +xml type ahead of the XML type of equal quality grails.mime.negotiation.spring took the types of the Accept header in Spring MVC's order alone, so Accept: application/xml, application/atom+xml resolved to xml where the Grails rules resolve it to atom. A type with the +xml suffix of the quality of the first application/xml or text/xml now comes ahead of it, each one later in the header in front of those before it, as the Grails rules order them, for Grails formats and, for a request a URL mapping handles, for what Spring MVC writes. The comparison record loses the atom answers, and the guide and upgrade notes list the rule as kept; they also describe the all format of the previous commit. --- .../guide/theWebLayer/contentNegotiation.adoc | 10 ++++--- .../src/en/guide/upgrading/upgrading90x.adoc | 9 +++--- ...eptHeaderContentNegotiationStrategy.groovy | 13 +++++---- .../GrailsContentNegotiationStrategy.groovy | 29 ++++++++++++++++++- .../FormatNegotiationComparisonSpec.groovy | 8 ++--- .../mime/SpringFormatNegotiationSpec.groovy | 22 +++++++++++++- 6 files changed, 72 insertions(+), 19 deletions(-) diff --git a/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc b/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc index 6aee23a57d9..e939ecc2f0a 100644 --- a/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc +++ b/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc @@ -287,9 +287,10 @@ setting: * The `Accept` header is read by Spring MVC's manager, with its strategies, so the `spring.mvc.contentnegotiation.*` properties apply to the Grails formats as they do to Spring MVC, and its media types are taken in the order Spring MVC -takes them: by quality, then a more specific type ahead of a less specific one, then in the order the header lists them. +takes them: by quality, then a more specific type ahead of a less specific one, then in the order the header lists them, +except that a type with the `+xml` suffix comes ahead of the XML type of equal quality, as the Grails rules take it. `Accept: */*, application/json` is the `json` format, where the Grails rules give `all`, and -`Accept: application/xml, application/atom+xml` is `xml`, where they give `atom`. +`Accept: application/xml, application/atom+xml` is `atom`, as they give. * Each media type is the format `grails.mime.types`, or a `MimeTypeProvider` bean such as a renderer, gives it. A type with the `+json` or `+xml` suffix that no configured type names, such as `application/merge-patch+json` or `application/problem+xml`, is the `json` or the `xml` format, as Spring MVC's JSON and XML converters write such types. @@ -303,7 +304,8 @@ Spring cannot read, has no format: `response.format` and `response.mimeType` are that accepts any type, with the `all` format. * The `format` parameter and a URL extension name the format, ahead of the `Accept` header, as before. The first value of the parameter counts, as for Spring MVC; a format no type is configured for is not acceptable, where the Grails rules -answer in the first configured format; and the `all` format leaves the format to the `Accept` header. +answer in the first configured format; and the `all` format, which is any type whether or not `grails.mime.types` lists +it, leaves the format to the `Accept` header. * `grails.mime.use.accept.header` and `grails.mime.disable.accept.header.*` ignore the `Accept` header as before. * The `format` parameter, the URL extension and the `Accept` header settings also decide what Spring MVC writes for a request a URL mapping handles, so that it is in the format Grails resolves for the request: an action that returns a @@ -333,7 +335,7 @@ acceptable, and the `all` format leaves the choice to the `Accept` header. |Dropped: both are the `xml` format, which comes in Spring MVC's order. |A type with the `+xml` suffix comes ahead of `application/xml` of equal quality -|Dropped: the types come in Spring MVC's order. +|Kept, also for what Spring MVC writes for the request. |The problem types of RFC 9457 are the `json` and `xml` formats |Extended to any type with the `+json` or `+xml` suffix that no configured type names. diff --git a/grails-doc/src/en/guide/upgrading/upgrading90x.adoc b/grails-doc/src/en/guide/upgrading/upgrading90x.adoc index dad7baaca1e..678470e4f69 100644 --- a/grails-doc/src/en/guide/upgrading/upgrading90x.adoc +++ b/grails-doc/src/en/guide/upgrading/upgrading90x.adoc @@ -1296,8 +1296,9 @@ The setting changes these answers, which the framework's tests record by running * *The order of the `Accept` header.* Spring MVC takes a more specific type ahead of a less specific one of equal quality, where the Grails rules keep the order of the header: `Accept: */*, application/json` is the `json` format, -where it is `all` without the setting, and `withFormat` runs the `json` block rather than its first. A type with the `+xml` suffix no longer comes ahead -of `application/xml`: `Accept: application/xml, application/atom+xml` is `xml`, where it is `atom` without the setting. +where it is `all` without the setting, and `withFormat` runs the `json` block rather than its first. A type with the +`+xml` suffix still comes ahead of the XML type of equal quality: `Accept: application/xml, application/atom+xml` is +`atom` either way, and Spring MVC now writes what an action returns in that order too. * *Types with the `+json` and `+xml` suffixes.* A type no configured type names, such as `application/merge-patch+json`, `application/soap+xml` or, without a `hal` format, `application/hal+json`, is the `json` or `xml` format, where the Grails rules read only the problem types of RFC 9457 so and answer any other as `all`. In `Content-Type` too: a body sent @@ -1321,8 +1322,8 @@ too. * *Without the `all` format.* Where `grails.mime.types` does not list `all`, a request without an `Accept` header is of the `all` format, as one that sends `Accept: */*` is, where the Grails rules give the first configured format, so `withFormat` runs its `*` block, or its first block, rather than the block of that format. A type of quality `0` comes -after any other, so `Accept: application/json;q=0, */*;q=0.1` is `all`, where it is otherwise `json`. `?format=all` is -then an unknown format. +after any other, so `Accept: application/json;q=0, */*;q=0.1` is `all`, where it is otherwise `json`. `?format=all` +leaves the format to the `Accept` header there too, where the Grails rules give the first configured format. * *Wildcards.* A wildcard such as `text/*` is the `all` format as before, but `withFormat` runs the first block of a format with a type compatible with it, and `respond` answers in the first such format of the action, where the Grails rules run the first block and answer in the first format: `withFormat { pdf {...}; html {...} }` runs the `html` block for diff --git a/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsAcceptHeaderContentNegotiationStrategy.groovy b/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsAcceptHeaderContentNegotiationStrategy.groovy index acbd45f240b..41ece6b7506 100644 --- a/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsAcceptHeaderContentNegotiationStrategy.groovy +++ b/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsAcceptHeaderContentNegotiationStrategy.groovy @@ -32,8 +32,9 @@ import org.springframework.web.context.request.NativeWebRequest /** * The strategy of Spring MVC's content negotiation manager that reads the {@code Accept} header in place of Spring MVC's * own, and ignores it for a request a URL mapping handles where {@code grails.mime.use.accept.header} or - * {@code grails.mime.disable.accept.header.*} say so, so that what Spring MVC writes for it is in the format Grails - * resolves for it. Any other request, such as one a Spring MVC endpoint handles, is negotiated by the strategy it + * {@code grails.mime.disable.accept.header.*} say so, and otherwise takes a type with the {@code +xml} suffix ahead of the + * XML type of equal quality, as Grails does, so that what Spring MVC writes for it is in the format Grails resolves for + * it. Any other request, such as one a Spring MVC endpoint handles, is negotiated by the strategy it * replaces. It is a {@link HeaderContentNegotiationStrategy}, so that it is found as Spring MVC's is. * * @since 9.0 @@ -64,10 +65,12 @@ class GrailsAcceptHeaderContentNegotiationStrategy extends HeaderContentNegotiat @Override List resolveMediaTypes(NativeWebRequest webRequest) throws HttpMediaTypeNotAcceptableException { HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest) - if (request != null && GrailsContentNegotiationStrategy.handlesRequest(request) && - grailsStrategy.get().ignoresAcceptHeader(request)) { + boolean handled = request != null && GrailsContentNegotiationStrategy.handlesRequest(request) + if (handled && grailsStrategy.get().ignoresAcceptHeader(request)) { return MEDIA_TYPE_ALL_LIST } - return springStrategy.resolveMediaTypes(webRequest) + List mediaTypes = springStrategy.resolveMediaTypes(webRequest) + // in the order Grails reads them, so that what Spring MVC writes is in the format Grails resolves + return handled ? GrailsContentNegotiationStrategy.preferringSuffixedXml(mediaTypes) : mediaTypes } } diff --git a/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationStrategy.groovy b/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationStrategy.groovy index 05555dd382d..00b6166eecf 100644 --- a/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationStrategy.groovy +++ b/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationStrategy.groovy @@ -347,7 +347,7 @@ class GrailsContentNegotiationStrategy { } List resolved = strategy.resolveMediaTypes(webRequest) if (resolved != ContentNegotiationStrategy.MEDIA_TYPE_ALL_LIST) { - return resolved + return preferringSuffixedXml(resolved) } } } @@ -358,6 +358,33 @@ class GrailsContentNegotiationStrategy { return ContentNegotiationStrategy.MEDIA_TYPE_ALL_LIST } + /** + * Media types in the order Grails reads them, which keeps Spring MVC's order but for the types with the {@code +xml} + * suffix of the quality of the XML type, the first {@code application/xml} or {@code text/xml}: they come ahead of + * it, as the more specific types, each one later in the header in front of those before it, as Grails 9 orders them. + * + * @param mediaTypes the media types in Spring MVC's order + * @return the media types in the order Grails reads them + * @since 9.0 + */ + static List preferringSuffixedXml(List mediaTypes) { + int xml = mediaTypes.findIndexOf { MediaType mediaType -> + mediaType.type == 'application' && mediaType.subtype == 'xml' || mediaType.type == 'text' && mediaType.subtype == 'xml' + } + if (xml < 0) { + return mediaTypes + } + double quality = mediaTypes[xml].qualityValue + List ordered = new ArrayList<>(mediaTypes) + for (MediaType mediaType in mediaTypes.subList(xml + 1, mediaTypes.size())) { + if (mediaType.subtypeSuffix?.equalsIgnoreCase('xml') && mediaType.qualityValue >= quality) { + ordered.remove(mediaType) + ordered.add(xml, mediaType) + } + } + return ordered + } + /** * The configured MIME types media types stand for, in the order of the media types, each once, with the parameters, * such as the quality, of the first that stands for it; and a wildcard no type is configured with the name of, such as diff --git a/grails-mimetypes/src/test/groovy/org/grails/web/mime/FormatNegotiationComparisonSpec.groovy b/grails-mimetypes/src/test/groovy/org/grails/web/mime/FormatNegotiationComparisonSpec.groovy index 7c5e63e836b..ad7d002b1a5 100644 --- a/grails-mimetypes/src/test/groovy/org/grails/web/mime/FormatNegotiationComparisonSpec.groovy +++ b/grails-mimetypes/src/test/groovy/org/grails/web/mime/FormatNegotiationComparisonSpec.groovy @@ -155,7 +155,7 @@ class FormatNegotiationComparisonSpec extends Specification { 'defaults | Accept: application/soap+xml | withFormat(html, json, xml): html -> xml', 'defaults | Accept: application/soap+xml | withFormat(json, xml, *): * -> xml', 'defaults | Accept: application/soap+xml | withFormat(pdf, html): pdf -> null', - 'defaults | Accept: application/xml, application/atom+xml | response.format: atom -> xml', + 'defaults | Accept: application/xml, application/atom+xml | Spring MVC: [application/xml, application/atom+xml] -> [application/atom+xml, application/xml]', 'defaults | Accept: image/png | response.format: all -> null', 'defaults | Accept: image/png | withFormat(html, json, xml): html -> null', 'defaults | Accept: image/png | withFormat(pdf, html): pdf -> null', @@ -190,7 +190,7 @@ class FormatNegotiationComparisonSpec extends Specification { 'merged vendor type | Accept: application/soap+xml | withFormat(html, json, xml): html -> xml', 'merged vendor type | Accept: application/soap+xml | withFormat(json, xml, *): * -> xml', 'merged vendor type | Accept: application/soap+xml | withFormat(pdf, html): pdf -> null', - 'merged vendor type | Accept: application/xml, application/atom+xml | response.format: atom -> xml', + 'merged vendor type | Accept: application/xml, application/atom+xml | Spring MVC: [application/xml, application/atom+xml] -> [application/atom+xml, application/xml]', 'merged vendor type | Accept: image/png | response.format: all -> null', 'merged vendor type | Accept: image/png | withFormat(html, json, xml): html -> null', 'merged vendor type | Accept: image/png | withFormat(pdf, html): pdf -> null', @@ -231,6 +231,7 @@ class FormatNegotiationComparisonSpec extends Specification { 'without all | Accept: application/soap+xml | withFormat(html, json, xml): html -> xml', 'without all | Accept: application/soap+xml | withFormat(json, xml, *): * -> xml', 'without all | Accept: application/soap+xml | withFormat(pdf, html): pdf -> null', + 'without all | Accept: application/xml, application/atom+xml | Spring MVC: [application/xml, application/atom+xml] -> [application/atom+xml, application/xml]', 'without all | Accept: application/json;q=0, */*;q=0.1 | response.format: json -> all', 'without all | Accept: application/json;q=0, */*;q=0.1 | withFormat(html, json, xml): json -> html', 'without all | Accept: application/json;q=0, */*;q=0.1 | withFormat(json, xml, *): json -> *', @@ -243,10 +244,9 @@ class FormatNegotiationComparisonSpec extends Specification { 'without all | Accept: application/json;q=abc | withFormat(json, xml, *): json -> *', 'without all | Accept: application/json, format=xml | Spring MVC: [application/json] -> [text/xml, application/xml, application/*+xml]', 'without all | Accept: application/json, format=html | Spring MVC: [application/json] -> [text/html, application/xhtml+xml]', - 'without all | Accept: application/json, format=all | response.format: html -> null', + 'without all | Accept: application/json, format=all | response.format: html -> json', 'without all | Accept: application/json, format=all | withFormat(html, json, xml): html -> json', 'without all | Accept: application/json, format=all | withFormat(pdf, html): html -> null', - 'without all | Accept: application/json, format=all | Spring MVC: [application/json] -> not acceptable', 'without all | Accept: application/json, format=unknown | response.format: html -> null', 'without all | Accept: application/json, format=unknown | withFormat(html, json, xml): html -> json', 'without all | Accept: application/json, format=unknown | withFormat(pdf, html): html -> null', diff --git a/grails-mimetypes/src/test/groovy/org/grails/web/mime/SpringFormatNegotiationSpec.groovy b/grails-mimetypes/src/test/groovy/org/grails/web/mime/SpringFormatNegotiationSpec.groovy index 1c67f3f7e9e..2b57cce7138 100644 --- a/grails-mimetypes/src/test/groovy/org/grails/web/mime/SpringFormatNegotiationSpec.groovy +++ b/grails-mimetypes/src/test/groovy/org/grails/web/mime/SpringFormatNegotiationSpec.groovy @@ -222,7 +222,7 @@ class SpringFormatNegotiationSpec extends Specification { 'application/problem+json' || ['json'] 'application/vnd.api+json, application/xml' || ['json', 'xml'] 'application/soap+xml' || ['xml'] - 'application/xml, application/atom+xml' || ['xml', 'atom'] + 'application/xml, application/atom+xml' || ['atom', 'xml'] 'text/xml;q=0.5, application/xml;q=0.9, application/json' || ['json', 'xml', 'xml'] 'image/png' || [] 'application/json;q=abc' || [] @@ -291,6 +291,26 @@ class SpringFormatNegotiationSpec extends Specification { manager().resolveMediaTypes(new ServletWebRequest(request)) == [MediaType.APPLICATION_JSON] } + void 'a type with the +xml suffix comes ahead of the XML type of equal quality, for Grails and for what Spring MVC writes: #accept'() { + given: + start((Settings.MIME_NEGOTIATION_SPRING): true) + def request = request('Accept': accept) + handledBy(request, urlMapping()) + + expect: + GrailsWebRequest.lookup().currentResponse.mimeTypes*.extension == formats + manager().resolveMediaTypes(new ServletWebRequest(request))*.toString() == resolved + + where: + accept || formats | resolved + 'application/xml, application/atom+xml' || ['atom', 'xml'] | ['application/atom+xml', 'application/xml'] + 'text/xml, application/json, application/atom+xml' || ['atom', 'xml', 'json'] | ['application/atom+xml', 'text/xml', 'application/json'] + 'application/xml, application/rss+xml, application/atom+xml' || ['atom', 'rss', 'xml'] | ['application/atom+xml', 'application/rss+xml', 'application/xml'] + 'application/xml, application/atom+xml;q=0.5' || ['xml', 'atom'] | ['application/xml', 'application/atom+xml;q=0.5'] + 'application/atom+xml, application/xml' || ['atom', 'xml'] | ['application/atom+xml', 'application/xml'] + 'application/json, application/atom+xml' || ['json', 'atom'] | ['application/json', 'application/atom+xml'] + } + void 'a wildcard is of the all format, which withFormat answers with the first block compatible with it: #result for #accept'() { given: start((Settings.MIME_NEGOTIATION_SPRING): true) From 54de0e0604de03aa28032b2f8823c610087e7fe9 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sun, 15 Nov 2026 08:16:43 -0800 Subject: [PATCH 020/115] Leave ?format=all to the Accept header under Spring MVC parameter negotiation With spring.mvc.contentnegotiation.favor-parameter, Spring MVC's parameter negotiation reads the format parameter, as Grails does. Grails left the all format to the Accept header, and the parameter strategy then read the same parameter again and found all not acceptable: ?format=all with Accept: application/json answered 406 to respond and to what Spring MVC writes for the action. For a request a URL mapping handles, Grails now reads the format parameter in place of that strategy, so all is left to the Accept header; a Spring MVC endpoint still reads it with Spring MVC's parameter negotiation. --- .../guide/theWebLayer/contentNegotiation.adoc | 4 +- .../GrailsContentNegotiationManager.groovy | 33 ++++++++++++- .../GrailsContentNegotiationStrategy.groovy | 20 +++++++- .../mime/SpringFormatNegotiationSpec.groovy | 49 ++++++++++++++++++- .../SpringFormatNegotiationRespondSpec.groovy | 35 +++++++++++++ 5 files changed, 135 insertions(+), 6 deletions(-) diff --git a/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc b/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc index e939ecc2f0a..2cd642f3bf5 100644 --- a/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc +++ b/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc @@ -305,7 +305,9 @@ that accepts any type, with the `all` format. * The `format` parameter and a URL extension name the format, ahead of the `Accept` header, as before. The first value of the parameter counts, as for Spring MVC; a format no type is configured for is not acceptable, where the Grails rules answer in the first configured format; and the `all` format, which is any type whether or not `grails.mime.types` lists -it, leaves the format to the `Accept` header. +it, leaves the format to the `Accept` header. Where `spring.mvc.contentnegotiation.favor-parameter` has Spring MVC read +the `format` parameter too, Grails reads it in its place for a request a URL mapping handles, so `?format=all` is left +to the `Accept` header there as well, while a Spring MVC endpoint reads it as Spring MVC does. * `grails.mime.use.accept.header` and `grails.mime.disable.accept.header.*` ignore the `Accept` header as before. * The `format` parameter, the URL extension and the `Accept` header settings also decide what Spring MVC writes for a request a URL mapping handles, so that it is in the format Grails resolves for the request: an action that returns a diff --git a/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationManager.groovy b/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationManager.groovy index 7a0cc525577..4b3d20b79ca 100644 --- a/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationManager.groovy +++ b/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationManager.groovy @@ -22,17 +22,22 @@ import java.util.function.Supplier import groovy.transform.CompileStatic +import jakarta.servlet.http.HttpServletRequest + import org.springframework.http.MediaType +import org.springframework.web.HttpMediaTypeNotAcceptableException import org.springframework.web.accept.ContentNegotiationManager import org.springframework.web.accept.ContentNegotiationStrategy import org.springframework.web.accept.HeaderContentNegotiationStrategy import org.springframework.web.accept.MediaTypeFileExtensionResolver +import org.springframework.web.context.request.NativeWebRequest /** * Spring MVC's content negotiation manager with {@code grails.mime.negotiation.spring}: its strategies, after the one * that resolves the format a request a URL mapping handles names, and with the {@code Accept} header read by one that - * ignores it for such a request where the {@code grails.mime} settings say so. Any other request is negotiated as by - * Spring MVC's manager, whose formats and file extensions it answers with. + * ignores it for such a request where the {@code grails.mime} settings say so, and without Spring MVC's parameter + * negotiation of the {@code format} parameter, which the first strategy reads for it. Any other request is negotiated as + * by Spring MVC's manager, whose formats and file extensions it answers with. * * @since 9.0 */ @@ -57,6 +62,30 @@ class GrailsContentNegotiationManager extends ContentNegotiationManager { return springManager } + /** + * Resolves the media types of a request as Spring MVC's manager does, with its strategies in their order, but for a + * request a URL mapping handles without Spring MVC's parameter negotiation of the {@code format} parameter, which the + * first strategy has read: it would read a format Spring MVC does not know, such as {@code all}, again, and find the + * request not acceptable. + */ + @Override + List resolveMediaTypes(NativeWebRequest request) throws HttpMediaTypeNotAcceptableException { + HttpServletRequest servletRequest = request.getNativeRequest(HttpServletRequest) + if (servletRequest == null || !GrailsContentNegotiationStrategy.handlesRequest(servletRequest)) { + return super.resolveMediaTypes(request) + } + for (ContentNegotiationStrategy strategy in strategies) { + if (GrailsContentNegotiationStrategy.readsFormatParameter(strategy)) { + continue + } + List mediaTypes = strategy.resolveMediaTypes(request) + if (mediaTypes != MEDIA_TYPE_ALL_LIST) { + return mediaTypes + } + } + return MEDIA_TYPE_ALL_LIST + } + @Override void addFileExtensionResolvers(MediaTypeFileExtensionResolver... resolvers) { springManager.addFileExtensionResolvers(resolvers) diff --git a/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationStrategy.groovy b/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationStrategy.groovy index 00b6166eecf..dc3d08d3aff 100644 --- a/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationStrategy.groovy +++ b/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationStrategy.groovy @@ -35,6 +35,7 @@ import org.springframework.web.HttpMediaTypeNotAcceptableException import org.springframework.web.accept.ContentNegotiationManager import org.springframework.web.accept.ContentNegotiationStrategy import org.springframework.web.accept.HeaderContentNegotiationStrategy +import org.springframework.web.accept.ParameterContentNegotiationStrategy import org.springframework.web.context.request.ServletWebRequest import org.springframework.web.context.support.WebApplicationContextUtils import org.springframework.web.servlet.HandlerMapping @@ -296,6 +297,21 @@ class GrailsContentNegotiationStrategy { return request.getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE) instanceof UrlMappingInfo } + /** + * Whether a strategy of Spring MVC's manager reads the parameter Grails reads the format a request names from, as + * the parameter negotiation {@code spring.mvc.contentnegotiation.favor-parameter} enables does by default: Grails + * reads it on its own, ahead of Spring MVC's strategies, for the requests a URL mapping handles, so that a format + * Spring MVC does not know, such as {@code all}, is not read again. + * + * @param strategy a strategy of Spring MVC's manager + * @return whether it reads the {@code format} parameter + * @since 9.0 + */ + static boolean readsFormatParameter(ContentNegotiationStrategy strategy) { + return strategy instanceof ParameterContentNegotiationStrategy && + ((ParameterContentNegotiationStrategy) strategy).parameterName == FORMAT_PARAMETER + } + /** * The strategy that resolves the formats of a request: that of the application's {@code MimeTypesConfiguration}, or, * in a context without it, such as one a test builds by hand, one built for the request from the application's @@ -327,7 +343,7 @@ class GrailsContentNegotiationStrategy { /** * The media types Spring MVC's content negotiation manager resolves for a request, in its order, without the format - * the request names, which Grails resolves on its own, and with its {@code Accept} header ignored where the + * the request names, which Grails resolves on its own, also where Spring MVC's parameter negotiation reads it, and with its {@code Accept} header ignored where the * {@code grails.mime} settings ignore it, whether or not a URL mapping handles the request. * * @return the media types, or {@code null} where the manager finds the request not acceptable, as for an @@ -341,7 +357,7 @@ class GrailsContentNegotiationStrategy { ServletWebRequest webRequest = new ServletWebRequest(request) try { for (ContentNegotiationStrategy strategy in strategies) { - if (strategy instanceof GrailsFormatContentNegotiationStrategy || + if (strategy instanceof GrailsFormatContentNegotiationStrategy || readsFormatParameter(strategy) || (acceptHeaderIgnored && strategy instanceof HeaderContentNegotiationStrategy)) { continue } diff --git a/grails-mimetypes/src/test/groovy/org/grails/web/mime/SpringFormatNegotiationSpec.groovy b/grails-mimetypes/src/test/groovy/org/grails/web/mime/SpringFormatNegotiationSpec.groovy index 2b57cce7138..2cb13b1d2f8 100644 --- a/grails-mimetypes/src/test/groovy/org/grails/web/mime/SpringFormatNegotiationSpec.groovy +++ b/grails-mimetypes/src/test/groovy/org/grails/web/mime/SpringFormatNegotiationSpec.groovy @@ -387,6 +387,39 @@ class SpringFormatNegotiationSpec extends Specification { GrailsWebRequest.lookup().currentResponse.format == 'xml' } + void "a format parameter of all, which Spring MVC's parameter negotiation of the format parameter does not know, leaves the format to the Accept header"() { + given: 'parameter negotiation on the format parameter, as spring.mvc.contentnegotiation.favor-parameter enables it' + start((Settings.MIME_NEGOTIATION_SPRING): true, (ParameterConfiguration.ENABLED): true, (ParameterConfiguration.NAME): 'format') + def request = request('Accept': 'application/json') + request.addParameter('format', 'all') + handledBy(request, urlMapping()) + + expect: + GrailsWebRequest.lookup().currentResponse.format == 'json' + GrailsWebRequest.lookup().currentResponse.mimeTypesFormatAware*.extension == ['json'] + GrailsWebRequest.lookup().currentResponse.mimeTypes*.extension == ['json'] + manager().resolveMediaTypes(new ServletWebRequest(request)) == [MediaType.APPLICATION_JSON] + } + + void "Spring MVC's parameter negotiation of the format parameter still answers #format for an endpoint, as it does without the setting"() { + given: + start((Settings.MIME_NEGOTIATION_SPRING): true, (ParameterConfiguration.ENABLED): true, (ParameterConfiguration.NAME): 'format') + def request = request('Accept': 'application/json') + request.addParameter('format', format) + handledBy(request, endpoint()) + + when: + def resolved = resolve(request) + + then: + resolved == expected + + where: + format || expected + 'xml' || [MediaType.APPLICATION_XML] + 'all' || 'not acceptable' + } + void 'the Content-Type #contentType is the request format #format'() { given: start((Settings.MIME_NEGOTIATION_SPRING): true) @@ -475,6 +508,15 @@ class SpringFormatNegotiationSpec extends Specification { context.getBean(GrailsContentNegotiationStrategy.MVC_CONTENT_NEGOTIATION_MANAGER, ContentNegotiationManager) } + private Object resolve(MockHttpServletRequest request) { + try { + manager().resolveMediaTypes(new ServletWebRequest(request)) + } + catch (HttpMediaTypeNotAcceptableException ignored) { + 'not acceptable' + } + } + private MockHttpServletRequest request(Map headers) { def request = new MockHttpServletRequest(context.servletContext) headers.each { String name, String value -> request.addHeader(name, value) } @@ -541,16 +583,21 @@ class SpringFormatNegotiationSpec extends Specification { static final String ENABLED = 'test.parameter.negotiation' + static final String NAME = 'test.parameter.negotiation.name' + private final boolean enabled + private final String name + ParameterConfiguration(ConfigurableEnvironment environment) { enabled = environment.getProperty(ENABLED, Boolean, false) + name = environment.getProperty(NAME, 'mediaType') } @Override void configureContentNegotiation(ContentNegotiationConfigurer configurer) { if (enabled) { - configurer.favorParameter(true).parameterName('mediaType') + configurer.favorParameter(true).parameterName(name) } } } diff --git a/grails-test-suite-web/src/test/groovy/grails/rest/web/SpringFormatNegotiationRespondSpec.groovy b/grails-test-suite-web/src/test/groovy/grails/rest/web/SpringFormatNegotiationRespondSpec.groovy index a6a5118f31b..8d44f1fd709 100644 --- a/grails-test-suite-web/src/test/groovy/grails/rest/web/SpringFormatNegotiationRespondSpec.groovy +++ b/grails-test-suite-web/src/test/groovy/grails/rest/web/SpringFormatNegotiationRespondSpec.groovy @@ -184,6 +184,41 @@ class SpringFormatNegotiationWithoutAllRespondSpec extends Specification impleme } } +/** + * With {@code grails.mime.negotiation.spring} and Spring MVC's parameter negotiation of the {@code format} parameter, + * which {@code spring.mvc.contentnegotiation.favor-parameter} enables, {@code respond} answers the format the parameter + * names, and the {@code all} format with the {@code Accept} header. + */ +class SpringFormatNegotiationParameterRespondSpec extends Specification implements ControllerUnitTest { + + Closure doWithConfig() { + { config -> + config[Settings.WEB_RENDERING_JSON_SPRING] = true + config[Settings.MIME_NEGOTIATION_SPRING] = true + config['spring.mvc.contentnegotiation.favor-parameter'] = true + } + } + + void 'a format parameter of #format answers with #status #contentType'() { + given: + request.addHeader('Accept', 'application/json') + request.addParameter('format', format) + + when: + controller.respond([title: 'Grails'], formats: ['json', 'xml']) + + then: + response.status == status + (response.contentType ?: '').startsWith(contentType) + + where: + format || status | contentType + 'all' || 200 | 'application/json' + 'xml' || 200 | 'text/xml' + 'unknown' || 406 | '' + } +} + /** * Without {@code grails.mime.negotiation.spring}, the Grails 9 rules resolve the format {@code respond} answers in. */ From 641f4833c8941b4f326b7757049ebadd938fbd19 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Mon, 16 Nov 2026 18:22:34 -0800 Subject: [PATCH 021/115] Complete the Forge prompt without picocli's JLine 3 bridge The interactive Forge CLI took its Tab completion from picocli-shell-jline3, which is built for JLine 3. CommandLineCompleter makes the same picocli AutoComplete call against the JLine API directly, so the prompt completes as before and no longer depends on a bridge tied to one JLine major. --- grails-forge/grails-forge-cli/build.gradle | 5 +- .../forge/cli/CommandLineCompleter.groovy | 51 +++++++++++ .../grails/forge/cli/InteractiveShell.groovy | 4 +- .../forge/cli/CommandLineCompleterSpec.groovy | 89 +++++++++++++++++++ 4 files changed, 143 insertions(+), 6 deletions(-) create mode 100644 grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/CommandLineCompleter.groovy create mode 100644 grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/CommandLineCompleterSpec.groovy diff --git a/grails-forge/grails-forge-cli/build.gradle b/grails-forge/grails-forge-cli/build.gradle index 7e324417c1f..aab6c01bf02 100644 --- a/grails-forge/grails-forge-cli/build.gradle +++ b/grails-forge/grails-forge-cli/build.gradle @@ -98,11 +98,10 @@ dependencies { compileOnly 'org.apache.grails.views:grails-web-gsp' compileOnly 'jakarta.servlet:jakarta.servlet-api' implementation "info.picocli:picocli:$picocliVersion" - implementation "info.picocli:picocli-shell-jline3:$picocliVersion" implementation 'jakarta.inject:jakarta.inject-api' // InteractiveShell drives JLine directly, and since the ansi stream install was dropped it also - // relies on JLine's terminal for virtual-terminal processing; pin it rather than inheriting - // whatever version picocli-shell-jline3 happens to bring + // relies on JLine's terminal for virtual-terminal processing. CommandLineCompleter connects it to + // picocli's completion itself: picocli's own bridge, picocli-shell-jline3, is built for JLine 3. implementation 'org.jline:jline' implementation 'org.yaml:snakeyaml' runtimeOnly 'org.slf4j:slf4j-nop' diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/CommandLineCompleter.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/CommandLineCompleter.groovy new file mode 100644 index 00000000000..f33d061bef6 --- /dev/null +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/CommandLineCompleter.groovy @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.forge.cli + +import groovy.transform.CompileStatic + +import org.jline.reader.Candidate +import org.jline.reader.Completer +import org.jline.reader.LineReader +import org.jline.reader.ParsedLine +import picocli.AutoComplete +import picocli.CommandLine.Model.CommandSpec + +/** + * Completes the word under the cursor at the interactive prompt from the picocli commands, their + * subcommands and their options. + */ +@CompileStatic +class CommandLineCompleter implements Completer { + + private final CommandSpec spec + + CommandLineCompleter(CommandSpec spec) { + this.spec = Objects.requireNonNull(spec, 'spec') + } + + @Override + void complete(LineReader reader, ParsedLine line, List candidates) { + List suggestions = [] + AutoComplete.complete(spec, line.words().toArray(new String[0]), line.wordIndex(), 0, line.cursor(), suggestions) + suggestions.each { CharSequence suggestion -> + candidates.add(new Candidate(suggestion.toString())) + } + } +} diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/InteractiveShell.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/InteractiveShell.groovy index 7300eb20f0a..f0d56a42db2 100644 --- a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/InteractiveShell.groovy +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/InteractiveShell.groovy @@ -29,7 +29,6 @@ import org.jline.reader.impl.DefaultParser import org.jline.terminal.Terminal import org.jline.terminal.TerminalBuilder import picocli.CommandLine -import picocli.shell.jline3.PicocliJLineCompleter import java.util.function.BiFunction import java.util.function.Consumer @@ -63,11 +62,10 @@ class InteractiveShell { void start() { try { - PicocliJLineCompleter picocliCommands = new PicocliJLineCompleter(commandLine.getCommandSpec()) Terminal terminal = TerminalBuilder.terminal() LineReader reader = LineReaderBuilder.builder() .terminal(terminal) - .completer(picocliCommands) + .completer(new CommandLineCompleter(commandLine.getCommandSpec())) .parser(new DefaultParser()) .variable(LineReader.LIST_MAX, 50) // max tab completion candidates .build() diff --git a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/CommandLineCompleterSpec.groovy b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/CommandLineCompleterSpec.groovy new file mode 100644 index 00000000000..4c241cc6b7c --- /dev/null +++ b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/CommandLineCompleterSpec.groovy @@ -0,0 +1,89 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.forge.cli + +import org.jline.reader.Candidate +import org.jline.reader.LineReader +import org.jline.reader.LineReaderBuilder +import org.jline.reader.impl.DefaultParser +import org.jline.terminal.Terminal +import org.jline.terminal.TerminalBuilder +import org.springframework.context.ConfigurableApplicationContext +import picocli.CommandLine +import spock.lang.AutoCleanup +import spock.lang.Shared +import spock.lang.Specification + +class CommandLineCompleterSpec extends Specification { + + @Shared + @AutoCleanup + ConfigurableApplicationContext context = Application.builder().run() + + @Shared + CommandLine commandLine = ForgeCli.createCommandLine(context, true) + + @Shared + @AutoCleanup + Terminal terminal = TerminalBuilder.builder() + .type(Terminal.TYPE_DUMB) + .streams(new ByteArrayInputStream(new byte[0]), new ByteArrayOutputStream()) + .build() + + void 'an empty prompt offers every command and the options of the root command'() { + expect: + complete('').containsAll(['create-app', 'create-webapp', 'create-restapi', 'create-plugin', 'create-web-plugin', + '--help', '--version']) + } + + void 'a partly typed command is offered as whole words, which the prompt narrows to the typed prefix'() { + when: + List offered = complete('create-a') + + then: 'whole command names, not the rest of the word' + offered.contains('create-app') + !offered.contains('pp') + } + + void 'an option typed after a command is completed from that command, not the root command'() { + when: + List offered = complete('create-app --pro') + + then: + offered.containsAll(['--features', '--servlet', '--jdk', '--gorm']) + !offered.contains('create-plugin') + } + + void 'the completer needs a command to complete from'() { + when: + new CommandLineCompleter(null) + + then: + NullPointerException e = thrown() + e.message == 'spec' + } + + private List complete(String typed) { + LineReader reader = LineReaderBuilder.builder().terminal(terminal).build() + List candidates = [] + new CommandLineCompleter(commandLine.commandSpec) + .complete(reader, new DefaultParser().parse(typed, typed.length()), candidates) + candidates*.value() + } +} From 39cdba9685cb243814ee50bba8d82994ad4e8538 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Mon, 16 Nov 2026 18:22:34 -0800 Subject: [PATCH 022/115] Move to JLine 4, the version Groovy 6 ships jline.version tracks the JLine that groovysh is built on, but stayed at 3.30.16 when 9.0.x moved to Groovy 6, whose groovysh uses JLine 4.4.5. grails-console leaves groovysh's own JLine modules off the class path in favour of the org.jline:jline bundle, so grails shell ran groovysh 6 on JLine 3 and failed to start with ClassNotFoundException: org.jline.shell.CommandGroup, a class only JLine 4 has. The BOM now manages JLine 4.4.5. The SBOM license table drops its JLine 3 entries, which the JLine 4 correction in forcedLicenseFor replaces, and the upgrade guide describes the move. --- .../apache/grails/buildsrc/SbomPlugin.groovy | 23 ++----- .../grails/buildsrc/SbomPluginSpec.groovy | 13 +++- dependencies.gradle | 4 +- .../GroovyshApplicationContextSpec.groovy | 63 +++++++++++++++++++ .../src/en/guide/upgrading/upgrading90x.adoc | 10 +++ .../EscapingFileNameCompletorSpec.groovy | 2 +- 6 files changed, 91 insertions(+), 24 deletions(-) create mode 100644 grails-console/src/test/groovy/grails/ui/shell/support/GroovyshApplicationContextSpec.groovy diff --git a/build-logic/plugins/src/main/groovy/org/apache/grails/buildsrc/SbomPlugin.groovy b/build-logic/plugins/src/main/groovy/org/apache/grails/buildsrc/SbomPlugin.groovy index e064c884773..365dad13fa5 100644 --- a/build-logic/plugins/src/main/groovy/org/apache/grails/buildsrc/SbomPlugin.groovy +++ b/build-logic/plugins/src/main/groovy/org/apache/grails/buildsrc/SbomPlugin.groovy @@ -114,26 +114,11 @@ class SbomPlugin implements Plugin { 'pkg:maven/org.antlr/ST4@4.3.4?type=jar' : 'BSD-3-Clause', 'pkg:maven/org.antlr/antlr-runtime@3.5.3?type=jar' : 'BSD-3-Clause', // mongo-java-server declares only "The BSD License" so it maps to BSD-4-Clause for the same - // reason as the org.jline group below. https://github.com/bwaldvogel/mongo-java-server/blob/main/LICENSE + // reason as the org.jline group in forcedLicenseFor. https://github.com/bwaldvogel/mongo-java-server/blob/main/LICENSE // has three numbered clauses and no advertising clause, making it BSD-3-Clause. 'pkg:maven/de.bwaldvogel/mongo-java-server@1.47.0?type=jar' : 'BSD-3-Clause', 'pkg:maven/de.bwaldvogel/mongo-java-server-core@1.47.0?type=jar' : 'BSD-3-Clause', 'pkg:maven/de.bwaldvogel/mongo-java-server-memory-backend@1.47.0?type=jar': 'BSD-3-Clause', - // The whole org.jline group declares "The BSD License", which maps incorrectly because of - // https://github.com/CycloneDX/cyclonedx-core-java/issues/205 - the POMs point at BSD-3-Clause. - // jline.version tracks the JLine version Groovy ships, so every module resolves to one version. - // Groovy 6 may also pull JLine 4, handled by forcedLicenseFor below. - 'pkg:maven/org.jline/jansi@3.30.16?type=jar' : 'BSD-3-Clause', - 'pkg:maven/org.jline/jline@3.30.16?type=jar' : 'BSD-3-Clause', - 'pkg:maven/org.jline/jline-builtins@3.30.16?type=jar' : 'BSD-3-Clause', - 'pkg:maven/org.jline/jline-console@3.30.16?type=jar' : 'BSD-3-Clause', - 'pkg:maven/org.jline/jline-native@3.30.16?type=jar' : 'BSD-3-Clause', - 'pkg:maven/org.jline/jline-reader@3.30.16?type=jar' : 'BSD-3-Clause', - 'pkg:maven/org.jline/jline-style@3.30.16?type=jar' : 'BSD-3-Clause', - 'pkg:maven/org.jline/jline-terminal@3.30.16?type=jar' : 'BSD-3-Clause', - 'pkg:maven/org.jline/jline-terminal-jansi@3.30.16?type=jar' : 'BSD-3-Clause', - 'pkg:maven/org.jline/jline-terminal-jna@3.30.16?type=jar' : 'BSD-3-Clause', - 'pkg:maven/org.jline/jline-terminal-jni@3.30.16?type=jar' : 'BSD-3-Clause', 'pkg:maven/org.jruby/jzlib@1.1.5?type=jar' : 'BSD-3-Clause', // https://web.archive.org/web/20240822213507/http://www.jcraft.com/jzlib/LICENSE.txt shows it's a 3 clause 'pkg:maven/org.liquibase.ext/liquibase-hibernate5@4.27.0?type=jar': 'Apache-2.0', // maps incorrectly because of https://github.com/liquibase/liquibase/issues/2445 & the base pom does not define a license 'pkg:maven/org.json/json@20260719?type=jar' : 'Public-Domain', // required due to jedis, https://issues.apache.org/jira/browse/LEGAL-666 approves this usage @@ -147,8 +132,10 @@ class SbomPlugin implements Plugin { if (LICENSE_MAPPING.containsKey(bomRef)) { return LICENSE_MAPPING[bomRef] } - // JLine 4 LICENSE.txt confirms BSD-3-Clause, but CycloneDX misreports it as BSD-4-Clause - // (cyclonedx-core-java#205). This is limited to the org.jline Maven jar family resolved via Groovy 6. + // The whole org.jline group declares "The BSD License", which CycloneDX misreports as BSD-4-Clause + // (cyclonedx-core-java#205); JLine 4's LICENSE.txt confirms BSD-3-Clause. jline.version tracks the + // JLine version Groovy 6 ships, so every module resolves to one JLine 4 version, and this is + // limited to that org.jline Maven jar family. if (bomRef.matches('pkg:maven/org\\.jline/[^/@?]+@4\\.[^?]+\\?type=jar')) { return 'BSD-3-Clause' } diff --git a/build-logic/plugins/src/test/groovy/org/apache/grails/buildsrc/SbomPluginSpec.groovy b/build-logic/plugins/src/test/groovy/org/apache/grails/buildsrc/SbomPluginSpec.groovy index 0e3b047f441..6e37adf8b48 100644 --- a/build-logic/plugins/src/test/groovy/org/apache/grails/buildsrc/SbomPluginSpec.groovy +++ b/build-logic/plugins/src/test/groovy/org/apache/grails/buildsrc/SbomPluginSpec.groovy @@ -110,8 +110,14 @@ class SbomPluginSpec extends Specification { void "JLine 4 Maven jars use the BSD-3-Clause correction for new versions"() { expect: - SbomPlugin.pickLicense(LOGGER, 'grails-console', 'grails-console', - 'pkg:maven/org.jline/jansi@4.3.1?type=jar', bsd4Choice()).id == 'BSD-3-Clause' + SbomPlugin.pickLicense(LOGGER, 'grails-console', 'grails-console', bomRef, bsd4Choice()).id == 'BSD-3-Clause' + + where: + bomRef << [ + 'pkg:maven/org.jline/jansi@4.3.1?type=jar', + 'pkg:maven/org.jline/jline@4.4.5?type=jar', + 'pkg:maven/org.jline/jline-shell@4.4.5?type=jar' + ] } void "org.json resolved through Jedis uses the public domain license approved in LEGAL-666"() { @@ -134,7 +140,8 @@ class SbomPluginSpec extends Specification { where: bomRef << [ 'pkg:maven/com.example/jansi@4.3.1?type=jar', - 'pkg:maven/org.jline/jansi@3.3.1?type=jar' + 'pkg:maven/org.jline/jansi@3.3.1?type=jar', + 'pkg:maven/org.jline/jline@3.30.16?type=jar' ] } diff --git a/dependencies.gradle b/dependencies.gradle index 452d0e4844e..21ffee22cdd 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -34,8 +34,8 @@ ext { 'grails-publish-plugin.version' : '1.0.0', 'javaparser-core.version' : '3.28.2', // Tracks the JLine version Groovy ships (groovy-groovysh), so the framework and groovysh - // share one terminal stack - keep in step with groovy.version. - 'jline.version' : '3.30.16', + // share one terminal stack - keep in step with groovy.version. Groovy 6 moved to JLine 4. + 'jline.version' : '4.4.5', 'jna.version' : '5.19.1', 'maven-model.version' : '3.9.16', 'objenesis.version' : '3.6', diff --git a/grails-console/src/test/groovy/grails/ui/shell/support/GroovyshApplicationContextSpec.groovy b/grails-console/src/test/groovy/grails/ui/shell/support/GroovyshApplicationContextSpec.groovy new file mode 100644 index 00000000000..aecd7e4bc84 --- /dev/null +++ b/grails-console/src/test/groovy/grails/ui/shell/support/GroovyshApplicationContextSpec.groovy @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information + * regarding copyright ownership. The ASF licenses this file to You under + * the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package grails.ui.shell.support + +import java.nio.file.Path + +import spock.lang.Specification +import spock.lang.TempDir + +import grails.core.DefaultGrailsApplication +import grails.core.GrailsApplication + +/** + * Starts the Groovy shell {@code grails shell} opens, on the class path the console module ships. + */ +class GroovyshApplicationContextSpec extends Specification { + + @TempDir + Path userHome + + String originalUserHome + + void setup() { + // groovysh keeps its history and settings under the user's home directory + originalUserHome = System.getProperty('user.home') + System.setProperty('user.home', userHome.toString()) + } + + void cleanup() { + System.setProperty('user.home', originalUserHome) + } + + void 'the shell starts with the application in its binding and ends when its input ends'() { + given: 'an application context with a Grails application, as grails shell starts it' + def context = new GroovyshApplicationContext() + context.registerBean(GrailsApplication.APPLICATION_ID, GrailsApplication, { new DefaultGrailsApplication() }) + + when: 'the context refreshes, which starts the shell on the test JVM terminal' + context.refresh() + + then: 'the shell runs and returns without a JLine class missing from the class path' + noExceptionThrown() + context.active + + cleanup: + context.close() + } +} diff --git a/grails-doc/src/en/guide/upgrading/upgrading90x.adoc b/grails-doc/src/en/guide/upgrading/upgrading90x.adoc index 8a769c10e22..3a93c5bc019 100644 --- a/grails-doc/src/en/guide/upgrading/upgrading90x.adoc +++ b/grails-doc/src/en/guide/upgrading/upgrading90x.adoc @@ -1453,3 +1453,13 @@ With `cacheUsers` enabled, the Spring Security plugin's user cache now stores us * Code that changed a user it read from `userCache` and expected the cache to see the change puts the user back with `userCache.putUserInCache`. * To store users by reference, as Grails 8 did, set `cacheConfig` on the `userCache` bean to a `MutableConfiguration` with `setStoreByValue(false)`. + +==== 25. JLine 4 + +Grails 9 manages JLine 4 (`org.jline:jline` 4.4.5), the version Groovy 6's `groovysh` is built on, in place of JLine 3. JLine is the terminal library behind the interactive prompts of the `grails` command line tools and of `grails shell`, and behind `GrailsConsole`, which application and plugin commands write to. The line reader, terminal and completer APIs keep their `org.jline` packages, and the JLine code in Grails itself compiled against JLine 4 unchanged. + +===== Required Actions + +* Recompile code of your own that uses JLine, such as an `org.jline.reader.Completer` for a command, against JLine 4. +* JLine 4 has no JNA or Jansi terminal provider. Remove a dependency on `org.jline:jline-terminal-jna` or `org.jline:jline-terminal-jansi`: neither has a JLine 4 version, and JLine finds the terminal through its remaining providers. +* A picocli command line that reads its input with JLine through picocli's `info.picocli:picocli-shell-jline3` gets JLine 4 from the Grails BOM, while that bridge is built for JLine 3. Use JLine's own picocli bridge, `org.jline:jline-picocli`, at the JLine version the Grails BOM manages. diff --git a/grails-shell-cli/src/test/groovy/org/grails/cli/interactive/completers/EscapingFileNameCompletorSpec.groovy b/grails-shell-cli/src/test/groovy/org/grails/cli/interactive/completers/EscapingFileNameCompletorSpec.groovy index a6994af6530..1cebcb5b1a2 100644 --- a/grails-shell-cli/src/test/groovy/org/grails/cli/interactive/completers/EscapingFileNameCompletorSpec.groovy +++ b/grails-shell-cli/src/test/groovy/org/grails/cli/interactive/completers/EscapingFileNameCompletorSpec.groovy @@ -33,7 +33,7 @@ import java.nio.file.Files import java.nio.file.Path /** - * Tests for EscapingFileNameCompletor which extends JLine 3's FileNameCompleter + * Tests for EscapingFileNameCompletor which extends JLine's FileNameCompleter * and escapes whitespace in file path completions. */ class EscapingFileNameCompletorSpec extends Specification { From 301e4886c442ff30ff33a93200e57d4dd9921730 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Mon, 16 Nov 2026 21:31:26 -0800 Subject: [PATCH 023/115] Let commands describe their arguments and options, and write them out @CommandArgument and @CommandOption describe the positional arguments and the options of an ApplicationCommand, so a command line tool can show them in the command's help, check them and complete them. They only describe the command, which still reads its command line as before. ApplicationCommandManifestWriter writes every command the registry finds on the classpath, with its description, arguments and options, to a JSON file that tools read without loading the commands themselves. The writer reads the annotations with getAnnotation rather than getAnnotationsByType: the static compiler links getAnnotationsByType with an Object[] return type the JDK method does not have, so the call fails with NoSuchMethodError. --- .../ApplicationCommandManifestWriter.groovy | 97 ++++++++++ .../grails/core/cli/CommandArgument.groovy | 71 +++++++ .../grails/core/cli/CommandArguments.groovy | 41 ++++ .../grails/core/cli/CommandOption.groovy | 71 +++++++ .../grails/core/cli/CommandOptions.groovy | 41 ++++ ...pplicationCommandManifestWriterSpec.groovy | 180 ++++++++++++++++++ 6 files changed, 501 insertions(+) create mode 100644 grails-core/src/cli/groovy/org/apache/grails/core/cli/ApplicationCommandManifestWriter.groovy create mode 100644 grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandArgument.groovy create mode 100644 grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandArguments.groovy create mode 100644 grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandOption.groovy create mode 100644 grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandOptions.groovy create mode 100644 grails-core/src/test-cli/groovy/org/apache/grails/core/cli/ApplicationCommandManifestWriterSpec.groovy diff --git a/grails-core/src/cli/groovy/org/apache/grails/core/cli/ApplicationCommandManifestWriter.groovy b/grails-core/src/cli/groovy/org/apache/grails/core/cli/ApplicationCommandManifestWriter.groovy new file mode 100644 index 00000000000..e42de0d9208 --- /dev/null +++ b/grails-core/src/cli/groovy/org/apache/grails/core/cli/ApplicationCommandManifestWriter.groovy @@ -0,0 +1,97 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.grails.core.cli + +import groovy.json.JsonOutput +import groovy.transform.CompileStatic + +/** + * Writes the commands an application can run, with their descriptions, arguments and options, to a + * JSON file that command line tools read to offer and complete those commands without loading them. + * + *

The commands are those {@code runCommand} runs: every {@link ApplicationCommand} the + * {@link ApplicationContextCommandRegistry} finds on the classpath of the process, which the Grails + * Gradle plugin makes the application's classpath plus its {@code grailsCli} configuration.

+ * + * @since 9.0 + */ +@CompileStatic +final class ApplicationCommandManifestWriter { + + /** + * The version of the file's format, written to the file, raised when a reader could misread a + * file of the new format. + */ + static final int FORMAT_VERSION = 1 + + private ApplicationCommandManifestWriter() { + } + + /** + * Writes the manifest of the commands on the classpath. + * + * @param args the path of the file to write + */ + static void main(String[] args) { + if (args.length != 1) { + throw new IllegalArgumentException('Expected the path of the manifest file to write as the only argument') + } + write(new ApplicationContextCommandRegistry().findCommands(), new File(args[0])) + } + + /** + * Writes the manifest of the given commands, sorted by name. + * + * @param commands the commands to describe + * @param file the file to write, whose directory is created when it does not exist + */ + static void write(Collection commands, File file) { + List> described = commands + .sort(false) { ApplicationCommand command -> command.name } + .collect { ApplicationCommand command -> describe(command) } + file.parentFile?.mkdirs() + file.setText(JsonOutput.prettyPrint(JsonOutput.toJson([version: FORMAT_VERSION, commands: described])), 'UTF-8') + } + + private static Map describe(ApplicationCommand command) { + Class type = command.class + List> arguments = declaredArguments(type).collect { CommandArgument argument -> + [name: argument.name(), description: argument.description(), required: argument.required(), + values: argument.values().toList()] as Map + } + List> options = declaredOptions(type).collect { CommandOption option -> + [name: option.name(), description: option.description(), flag: option.flag(), + values: option.values().toList()] as Map + } + [name: command.name, description: command.description ?: '', arguments: arguments, options: options] as Map + } + + // Class#getAnnotationsByType would read both forms at once, but the static compiler links it with an + // Object[] return type, which the JDK method does not have, so the call fails with NoSuchMethodError. + // getAnnotation returns a single annotation and links correctly. + private static List declaredArguments(Class type) { + CommandArgument single = type.getAnnotation(CommandArgument) + single ? [single] : (type.getAnnotation(CommandArguments)?.value()?.toList() ?: []) + } + + private static List declaredOptions(Class type) { + CommandOption single = type.getAnnotation(CommandOption) + single ? [single] : (type.getAnnotation(CommandOptions)?.value()?.toList() ?: []) + } +} diff --git a/grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandArgument.groovy b/grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandArgument.groovy new file mode 100644 index 00000000000..71964dd81bf --- /dev/null +++ b/grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandArgument.groovy @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.grails.core.cli + +import java.lang.annotation.Documented +import java.lang.annotation.ElementType +import java.lang.annotation.Repeatable +import java.lang.annotation.Retention +import java.lang.annotation.RetentionPolicy +import java.lang.annotation.Target + +/** + * Describes one positional argument of an {@link ApplicationCommand}, so that command line tools can + * show it in the command's help and complete it. + * + *

Declare one annotation per argument, in the order the arguments are given on the command line:

+ * + *
+ * @CommandArgument(name = 'package', description = 'The package of the domain classes')
+ * @CommandArgument(name = 'user-class-name', description = 'The name of the user class')
+ * class SetupUsersCommand implements GrailsApplicationCommand { ... }
+ * 
+ * + *

The annotation only describes the argument. The command still reads its value from the command + * line, for example through {@code args} of {@link GrailsApplicationCommand}. A command that declares + * no argument accepts any arguments.

+ * + * @since 9.0 + */ +@Documented +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +@Repeatable(CommandArguments) +@interface CommandArgument { + + /** + * @return the name of the argument, shown in the command's help + */ + String name() + + /** + * @return the description of the argument, shown in the command's help + */ + String description() default '' + + /** + * @return whether the command needs the argument + */ + boolean required() default true + + /** + * @return the values offered when the argument is completed; none when the argument takes any value + */ + String[] values() default [] +} diff --git a/grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandArguments.groovy b/grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandArguments.groovy new file mode 100644 index 00000000000..00dc375cbf0 --- /dev/null +++ b/grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandArguments.groovy @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.grails.core.cli + +import java.lang.annotation.Documented +import java.lang.annotation.ElementType +import java.lang.annotation.Retention +import java.lang.annotation.RetentionPolicy +import java.lang.annotation.Target + +/** + * Holds the {@link CommandArgument} annotations of a command that declares more than one. + * + * @since 9.0 + */ +@Documented +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +@interface CommandArguments { + + /** + * @return the arguments of the command, in the order they are given on the command line + */ + CommandArgument[] value() +} diff --git a/grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandOption.groovy b/grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandOption.groovy new file mode 100644 index 00000000000..e680a0cb0cf --- /dev/null +++ b/grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandOption.groovy @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.grails.core.cli + +import java.lang.annotation.Documented +import java.lang.annotation.ElementType +import java.lang.annotation.Repeatable +import java.lang.annotation.Retention +import java.lang.annotation.RetentionPolicy +import java.lang.annotation.Target + +/** + * Describes one option of an {@link ApplicationCommand}, so that command line tools can show it in the + * command's help and complete it. + * + *

An option is given as {@code --name=value}, or as {@code --name} when it is a {@link #flag()}:

+ * + *
+ * @CommandOption(name = 'group-class-name', description = 'Also generate role group classes with this name')
+ * @CommandOption(name = 'ui-only', flag = true, description = 'Configure the plugin without generating classes')
+ * class SetupUsersCommand implements GrailsApplicationCommand { ... }
+ * 
+ * + *

The annotation only describes the option. The command still reads it from the command line, for + * example through {@code executionContext.commandLine}. A command that declares no option accepts any + * options.

+ * + * @since 9.0 + */ +@Documented +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +@Repeatable(CommandOptions) +@interface CommandOption { + + /** + * @return the name of the option, without the leading {@code --} + */ + String name() + + /** + * @return the description of the option, shown in the command's help + */ + String description() default '' + + /** + * @return whether the option is a flag, given without a value + */ + boolean flag() default false + + /** + * @return the values offered when the option's value is completed; none when it takes any value + */ + String[] values() default [] +} diff --git a/grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandOptions.groovy b/grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandOptions.groovy new file mode 100644 index 00000000000..d9c37db45c2 --- /dev/null +++ b/grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandOptions.groovy @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.grails.core.cli + +import java.lang.annotation.Documented +import java.lang.annotation.ElementType +import java.lang.annotation.Retention +import java.lang.annotation.RetentionPolicy +import java.lang.annotation.Target + +/** + * Holds the {@link CommandOption} annotations of a command that declares more than one. + * + * @since 9.0 + */ +@Documented +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +@interface CommandOptions { + + /** + * @return the options of the command + */ + CommandOption[] value() +} diff --git a/grails-core/src/test-cli/groovy/org/apache/grails/core/cli/ApplicationCommandManifestWriterSpec.groovy b/grails-core/src/test-cli/groovy/org/apache/grails/core/cli/ApplicationCommandManifestWriterSpec.groovy new file mode 100644 index 00000000000..0f2da11f7fd --- /dev/null +++ b/grails-core/src/test-cli/groovy/org/apache/grails/core/cli/ApplicationCommandManifestWriterSpec.groovy @@ -0,0 +1,180 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.grails.core.cli + +import java.nio.file.Path + +import groovy.json.JsonSlurper + +import spock.lang.Specification +import spock.lang.TempDir + +class ApplicationCommandManifestWriterSpec extends Specification { + + @TempDir + Path tempDir + + private ClassLoader originalContextClassLoader + private URLClassLoader factoryClassLoader + + void setup() { + originalContextClassLoader = Thread.currentThread().contextClassLoader + } + + void cleanup() { + Thread.currentThread().contextClassLoader = originalContextClassLoader + factoryClassLoader?.close() + } + + void 'the manifest lists the commands on the classpath, sorted by name, with their descriptions'() { + given: + declareCommands(UndescribedManifestCommand, DescribedManifestCommand) + + when: + Map manifest = writeManifest() + + then: + manifest.version == ApplicationCommandManifestWriter.FORMAT_VERSION + + and: 'every command the registry finds, in name order' + List names = manifest.commands*.name + names == names.toSorted() + names.containsAll(['described-manifest', 'undescribed-manifest']) + + and: + with(command(manifest, 'described-manifest')) { + description == 'Sets up the users of the application' + } + + and: "a command without a description of its own is described by the trait's default, its name" + with(command(manifest, 'undescribed-manifest')) { + description == 'undescribed-manifest' + arguments == [] + options == [] + } + } + + void 'the manifest describes the declared arguments in the order they are given and the declared options'() { + given: + declareCommands(DescribedManifestCommand) + + when: + Map described = command(writeManifest(), 'described-manifest') + + then: + described.arguments == [ + [name: 'package', description: 'The package of the domain classes', required: true, values: []], + [name: 'user-class-name', description: 'The name of the user class', required: false, values: []] + ] + + and: + described.options == [ + [name: 'groupClassName', description: 'Also generate role group classes', flag: false, values: []], + [name: 'uiOnly', description: 'Configure without generating classes', flag: true, values: []], + [name: 'format', description: '', flag: false, values: ['json', 'yaml']] + ] + } + + void 'a command may declare a single argument and a single option'() { + given: + declareCommands(SingleDeclarationManifestCommand) + + when: + Map described = command(writeManifest(), 'single-declaration-manifest') + + then: + described.arguments == [[name: 'domain-class', description: '', required: true, values: ['Book', 'Author']]] + described.options == [[name: 'force', description: '', flag: true, values: []]] + } + + void 'the directory of the manifest is created when it does not exist'() { + given: + declareCommands(UndescribedManifestCommand) + File manifestFile = tempDir.resolve('build/grails/commands.json').toFile() + + when: + ApplicationCommandManifestWriter.main(manifestFile.path) + + then: + manifestFile.isFile() + } + + void 'the writer needs the path of the file to write'() { + when: + ApplicationCommandManifestWriter.main(args as String[]) + + then: + IllegalArgumentException e = thrown() + e.message == 'Expected the path of the manifest file to write as the only argument' + + where: + args << [[], ['one.json', 'two.json']] + } + + private void declareCommands(Class... commandTypes) { + File metaInf = tempDir.resolve('classes/META-INF').toFile() + assert metaInf.mkdirs() + new File(metaInf, 'grails-cli.factories').text = "${ApplicationCommand.name}=${commandTypes*.name.join(',')}" + factoryClassLoader = new URLClassLoader([tempDir.resolve('classes').toUri().toURL()] as URL[], getClass().classLoader) + Thread.currentThread().contextClassLoader = factoryClassLoader + } + + private Map writeManifest() { + File manifestFile = tempDir.resolve('commands.json').toFile() + ApplicationCommandManifestWriter.main(manifestFile.path) + (Map) new JsonSlurper().parse(manifestFile) + } + + private static Map command(Map manifest, String name) { + (Map) manifest.commands.find { it.name == name } + } +} + +class UndescribedManifestCommand implements ApplicationCommand { + + @Override + boolean handle(ExecutionContext executionContext) { + true + } +} + +@CommandArgument(name = 'package', description = 'The package of the domain classes') +@CommandArgument(name = 'user-class-name', description = 'The name of the user class', required = false) +@CommandOption(name = 'groupClassName', description = 'Also generate role group classes') +@CommandOption(name = 'uiOnly', description = 'Configure without generating classes', flag = true) +@CommandOption(name = 'format', values = ['json', 'yaml']) +class DescribedManifestCommand implements ApplicationCommand { + + final String description = 'Sets up the users of the application' + + @Override + boolean handle(ExecutionContext executionContext) { + true + } +} + +@CommandArgument(name = 'domain-class', values = ['Book', 'Author']) +@CommandOption(name = 'force', flag = true) +class SingleDeclarationManifestCommand implements ApplicationCommand { + + @Override + boolean handle(ExecutionContext executionContext) { + true + } +} From 062ab3af0f2678b05dc05dda742c0a61ba98d5f9 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Mon, 16 Nov 2026 21:31:26 -0800 Subject: [PATCH 024/115] Add the generateCommandManifest task The task writes the commands runCommand can run to build/grails/commands.json, running the grails-core-cli writer on runCommand's classpath without starting the application. It stays up to date while that classpath is unchanged. --- .../ApplicationCommandManifestTask.groovy | 67 +++++++++++++++++ .../commands/GrailsCliGradlePlugin.groovy | 32 ++++++++ .../ApplicationCommandManifestTaskSpec.groovy | 75 +++++++++++++++++++ .../command-manifest/app/build.gradle | 8 ++ .../app/src/main/resources/runtime-marker.txt | 1 + .../command-manifest/gradle.properties | 3 + .../manifest-writer/build.gradle | 3 + .../cli/ApplicationCommandManifestWriter.java | 45 +++++++++++ .../command-manifest/settings.gradle | 3 + 9 files changed, 237 insertions(+) create mode 100644 grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTask.groovy create mode 100644 grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTaskSpec.groovy create mode 100644 grails-gradle/plugins/src/test/resources/test-projects/command-manifest/app/build.gradle create mode 100644 grails-gradle/plugins/src/test/resources/test-projects/command-manifest/app/src/main/resources/runtime-marker.txt create mode 100644 grails-gradle/plugins/src/test/resources/test-projects/command-manifest/gradle.properties create mode 100644 grails-gradle/plugins/src/test/resources/test-projects/command-manifest/manifest-writer/build.gradle create mode 100644 grails-gradle/plugins/src/test/resources/test-projects/command-manifest/manifest-writer/src/main/java/org/apache/grails/core/cli/ApplicationCommandManifestWriter.java create mode 100644 grails-gradle/plugins/src/test/resources/test-projects/command-manifest/settings.gradle diff --git a/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTask.groovy b/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTask.groovy new file mode 100644 index 00000000000..d7ec4840407 --- /dev/null +++ b/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTask.groovy @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.gradle.plugin.commands + +import groovy.transform.CompileStatic + +import org.gradle.api.file.RegularFileProperty +import org.gradle.api.tasks.JavaExec +import org.gradle.api.tasks.OutputFile +import org.gradle.api.tasks.TaskAction +import org.gradle.work.DisableCachingByDefault + +import javax.inject.Inject + +/** + * Writes the commands the application can run with {@code runCommand}, with their descriptions, + * arguments and options, to a JSON file. Command line tools such as the Grails Forge CLI read the file + * to offer those commands and complete them without loading the application's classpath themselves. + * + *

Runs on the classpath {@code runCommand} uses, so the file lists exactly the commands + * {@code runCommand} can run. The task is up to date while that classpath is unchanged.

+ * + * @since 9.0 + */ +@CompileStatic +@DisableCachingByDefault(because = 'Reading the command metadata is cheaper than restoring it from the build cache') +abstract class ApplicationCommandManifestTask extends JavaExec { + + /** The class in the {@code grails-core-cli} artifact that writes the file */ + public static final String MANIFEST_WRITER_CLASS = 'org.apache.grails.core.cli.ApplicationCommandManifestWriter' + + @Inject + ApplicationCommandManifestTask() { + mainClass.set(MANIFEST_WRITER_CLASS) + } + + /** + * @return the JSON file the commands are written to + */ + @OutputFile + abstract RegularFileProperty getManifestFile() + + @Override + @TaskAction + void exec() { + // the file is the task's output, so it is given to the writer here rather than as an + // argument input, which would make the absolute path part of the task's inputs + args(manifestFile.get().asFile.absolutePath) + super.exec() + } +} diff --git a/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/GrailsCliGradlePlugin.groovy b/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/GrailsCliGradlePlugin.groovy index a5e23fdcbe9..1fb28d31362 100644 --- a/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/GrailsCliGradlePlugin.groovy +++ b/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/GrailsCliGradlePlugin.groovy @@ -105,6 +105,15 @@ class GrailsCliGradlePlugin implements Plugin { /** The internal probe configuration used to detect companion `-cli` modules */ public static final String GRAILS_CLI_DETECT_CONFIGURATION = 'grailsCliDetect' + /** + * The task that writes the commands {@code runCommand} can run to {@link #COMMAND_MANIFEST_PATH}, + * which the Grails Forge CLI reads to offer them. + */ + public static final String COMMAND_MANIFEST_TASK = 'generateCommandManifest' + + /** Where {@link #COMMAND_MANIFEST_TASK} writes the commands, relative to the build directory */ + public static final String COMMAND_MANIFEST_PATH = 'grails/commands.json' + @Override void apply(Project project) { // self-sufficient when applied standalone: the auto-provisioning behavior is configured @@ -122,6 +131,8 @@ class GrailsCliGradlePlugin implements Plugin { configureRunScript(project) configureRunCommand(project) + + configureCommandManifest(project) } /** @@ -609,4 +620,25 @@ class GrailsCliGradlePlugin implements Plugin { } } + /** + * Registers {@link #COMMAND_MANIFEST_TASK}, which describes the commands of {@code runCommand}'s + * classpath without starting the application. + */ + @CompileDynamic + protected void configureCommandManifest(Project project) { + if (!project.tasks.names.contains(COMMAND_MANIFEST_TASK)) { + def manifestTask = project.tasks.register(COMMAND_MANIFEST_TASK, ApplicationCommandManifestTask) + manifestTask.configure { + it.description = 'Writes the Grails commands of the application, which the Grails Forge CLI offers' + it.manifestFile.convention(project.layout.buildDirectory.file(COMMAND_MANIFEST_PATH)) + } + project.afterEvaluate { + manifestTask.configure { + SourceSet mainSourceSet = SourceSets.findMainSourceSet(project) + it.classpath = mainSourceSet.runtimeClasspath + project.configurations.getByName(GRAILS_CLI_CLASSPATH_CONFIGURATION) + } + } + } + } + } diff --git a/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTaskSpec.groovy b/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTaskSpec.groovy new file mode 100644 index 00000000000..081ee5bbf63 --- /dev/null +++ b/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTaskSpec.groovy @@ -0,0 +1,75 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.gradle.plugin.commands + +import java.nio.file.Path + +import org.gradle.testkit.runner.TaskOutcome + +import org.grails.gradle.plugin.core.GradleSpecification + +class ApplicationCommandManifestTaskSpec extends GradleSpecification { + + void 'the manifest is written by the grails-core-cli writer on the classpath runCommand uses'() { + given: + def runner = setupTestResourceProject('command-manifest') + Path manifest = runner.projectDir.toPath().resolve('app/build/grails/commands.json') + + when: + def result = executeTask(':app:generateCommandManifest') + + then: + assertTaskSuccess('generateCommandManifest', result) + + and: 'the writer was given only the file to write, which is in the build directory' + Map written = read(manifest) + written.arguments == '1' + Path.of(written.file).toRealPath() == manifest.toRealPath() + + and: "the application's own classes and resources are on the classpath next to its grailsCli dependencies" + written.runtimeMarker == 'from the application' + } + + void 'the manifest is up to date until the classpath changes'() { + given: + def runner = setupTestResourceProject('command-manifest') + executeTask(':app:generateCommandManifest') + + when: + def unchanged = executeTask(':app:generateCommandManifest') + + then: + unchanged.task(':app:generateCommandManifest').outcome == TaskOutcome.UP_TO_DATE + + when: 'a resource of the application changes' + runner.projectDir.toPath().resolve('app/src/main/resources/runtime-marker.txt').toFile().text = 'changed' + def changed = executeTask(':app:generateCommandManifest') + + then: + changed.task(':app:generateCommandManifest').outcome == TaskOutcome.SUCCESS + read(runner.projectDir.toPath().resolve('app/build/grails/commands.json')).runtimeMarker == 'changed' + } + + private static Map read(Path file) { + file.toFile().readLines().collectEntries { String line -> + int separator = line.indexOf('=') + [(line.substring(0, separator)): line.substring(separator + 1)] + } + } +} diff --git a/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/app/build.gradle b/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/app/build.gradle new file mode 100644 index 00000000000..178e89eb151 --- /dev/null +++ b/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/app/build.gradle @@ -0,0 +1,8 @@ +plugins { + id 'java' + id 'org.apache.grails.gradle.grails-cli' +} + +dependencies { + grailsCli project(':manifest-writer') +} diff --git a/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/app/src/main/resources/runtime-marker.txt b/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/app/src/main/resources/runtime-marker.txt new file mode 100644 index 00000000000..3c4f04f6f47 --- /dev/null +++ b/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/app/src/main/resources/runtime-marker.txt @@ -0,0 +1 @@ +from the application diff --git a/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/gradle.properties b/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/gradle.properties new file mode 100644 index 00000000000..d1e27431f11 --- /dev/null +++ b/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/gradle.properties @@ -0,0 +1,3 @@ +version=1.0.0 +group=org.example.test +grailsCliAutoProvision=false diff --git a/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/manifest-writer/build.gradle b/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/manifest-writer/build.gradle new file mode 100644 index 00000000000..075ba3d5638 --- /dev/null +++ b/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/manifest-writer/build.gradle @@ -0,0 +1,3 @@ +plugins { + id 'java' +} diff --git a/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/manifest-writer/src/main/java/org/apache/grails/core/cli/ApplicationCommandManifestWriter.java b/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/manifest-writer/src/main/java/org/apache/grails/core/cli/ApplicationCommandManifestWriter.java new file mode 100644 index 00000000000..6808741cb7b --- /dev/null +++ b/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/manifest-writer/src/main/java/org/apache/grails/core/cli/ApplicationCommandManifestWriter.java @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.grails.core.cli; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; + +/** + * Stands in for the writer of grails-core-cli: records the arguments it was given and what its + * classpath held, so the spec can check what the manifest task runs it with. + */ +public final class ApplicationCommandManifestWriter { + + private ApplicationCommandManifestWriter() { + } + + public static void main(String[] args) throws IOException { + String runtimeMarker; + try (InputStream input = ApplicationCommandManifestWriter.class.getClassLoader().getResourceAsStream("runtime-marker.txt")) { + runtimeMarker = input == null ? "absent" : new String(input.readAllBytes(), StandardCharsets.UTF_8).trim(); + } + Path file = Path.of(args[0]); + Files.createDirectories(file.getParent()); + Files.writeString(file, "arguments=" + args.length + "\nfile=" + file + "\nruntimeMarker=" + runtimeMarker + "\n"); + } +} diff --git a/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/settings.gradle b/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/settings.gradle new file mode 100644 index 00000000000..5ca8892f9d9 --- /dev/null +++ b/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/settings.gradle @@ -0,0 +1,3 @@ +rootProject.name = 'command-manifest' + +include 'manifest-writer', 'app' From f0de78130d4dc005aaa35665f868381c7350406b Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Mon, 16 Nov 2026 21:31:26 -0800 Subject: [PATCH 025/115] Offer the commands of the project in the Forge CLI Inside a Grails project, the Forge CLI offers the commands of the application and of its plugins next to its own, with their help and their completion at the prompt, and runs them through the project's runCommand task. It reads them from the manifest generateCommandManifest writes, and runs that task first when the manifest is missing or older than the build files or grails-app/commands. The Forge CLI's own commands never wait for the build, and keep their names over a project command of the same name. Declared arguments and options are checked and completed; a command that declares none takes any and passes them on. --- grails-forge/grails-forge-cli/build.gradle | 1 + .../org/grails/forge/cli/ForgeCli.groovy | 41 +- .../forge/cli/command/ProjectCommand.groovy | 166 ++++++++ .../forge/cli/command/ProjectCommands.groovy | 131 +++++++ .../forge/cli/command/ProjectGradle.groovy | 95 +++++ .../forge/cli/ProjectCommandsSpec.groovy | 361 ++++++++++++++++++ 6 files changed, 794 insertions(+), 1 deletion(-) create mode 100644 grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommand.groovy create mode 100644 grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy create mode 100644 grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectGradle.groovy create mode 100644 grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy diff --git a/grails-forge/grails-forge-cli/build.gradle b/grails-forge/grails-forge-cli/build.gradle index aab6c01bf02..3aa89fa82db 100644 --- a/grails-forge/grails-forge-cli/build.gradle +++ b/grails-forge/grails-forge-cli/build.gradle @@ -90,6 +90,7 @@ dependencies { implementation 'org.apache.grails:grails-core' implementation 'org.apache.grails.bootstrap:grails-bootstrap' // grails.codegen.model, for add-property implementation 'org.apache.groovy:groovy' + implementation 'org.apache.groovy:groovy-json' // reads the command manifest of a project // renders the compiled pages, without a web request implementation 'org.apache.grails.views:grails-gsp-core' diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/ForgeCli.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/ForgeCli.groovy index 9a1025c3c8a..34b69a0bdb7 100644 --- a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/ForgeCli.groovy +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/ForgeCli.groovy @@ -32,7 +32,10 @@ import org.grails.forge.cli.command.CreateInterceptorCommand import org.grails.forge.cli.command.CreateJobCommand import org.grails.forge.cli.command.CreateServiceCommand import org.grails.forge.cli.command.CreateTagLibCommand +import org.grails.forge.cli.command.ProjectCommand +import org.grails.forge.cli.command.ProjectCommands import org.grails.forge.io.ConsoleOutput +import org.grails.forge.io.FileSystemOutputHandler /** * Runs the Forge CLI against the Grails application {@link Application} starts: builds the picocli @@ -94,7 +97,13 @@ final class ForgeCli { */ static int execute(ApplicationContext context, String... args) { boolean noOpConsole = args.length > 0 && args[0].startsWith('update-cli-config') - return createCommandLine(context, noOpConsole).execute(args) + CommandLine commandLine = createForgeCommandLine(context, noOpConsole) + // the commands of the project are read only when the command line may name one of them, so + // the Forge CLI's own commands never wait for the project's build + if (args.length == 0 || !commandLine.subcommands.containsKey(args[0])) { + addProjectCommands(commandLine) + } + return commandLine.execute(args) } /** @@ -105,7 +114,17 @@ final class ForgeCli { new InteractiveShell(commandLine, { String[] commandArgs -> execute(context, commandArgs) }, EXCEPTION_HANDLER).start() } + /** + * Builds the command line with every command it offers in the current directory: the Forge CLI's + * own, and the commands of the Grails project there, if any. + */ static CommandLine createCommandLine(ApplicationContext context, boolean noOpConsole) { + CommandLine commandLine = createForgeCommandLine(context, noOpConsole) + addProjectCommands(commandLine) + return commandLine + } + + private static CommandLine createForgeCommandLine(ApplicationContext context, boolean noOpConsole) { ForgeCommand root = context.getBean(ForgeCommand) CommandLine commandLine = new CommandLine(root, new GrailsPicocliFactory(context)) commandLine.executionExceptionHandler = { Exception ex, CommandLine failed, CommandLine.ParseResult parseResult -> @@ -130,4 +149,24 @@ final class ForgeCli { return commandLine } + /** + * Adds the commands of the Grails project in the current directory, which run through the project's + * build. A command named like one of the Forge CLI's own is left out: the Forge CLI's command runs. + */ + private static void addProjectCommands(CommandLine commandLine) { + File projectDir + try { + projectDir = FileSystemOutputHandler.getDefaultBaseDirectory() + } + catch (IOException ignored) { + return + } + ConsoleOutput console = (ForgeCommand) commandLine.getCommand() + for (ProjectCommand command : new ProjectCommands(projectDir).load(console)) { + if (!commandLine.subcommands.containsKey(command.name)) { + commandLine.addSubcommand(command.name, command.commandSpec) + } + } + } + } diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommand.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommand.groovy new file mode 100644 index 00000000000..5fd79c0e28a --- /dev/null +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommand.groovy @@ -0,0 +1,166 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.forge.cli.command + +import java.util.concurrent.Callable + +import groovy.transform.CompileStatic + +import picocli.CommandLine +import picocli.CommandLine.Model.CommandSpec +import picocli.CommandLine.Model.OptionSpec +import picocli.CommandLine.Model.PositionalParamSpec + +/** + * A command of the project, from the command manifest the project's build writes: one of the + * application's own commands or a command of one of its plugins. It is offered and completed like + * the Forge CLI's own commands, and runs through the project's {@code runCommand} Gradle task. + * + *

Arguments and options the command declares are checked and completed. A command that declares + * no arguments takes any, and one that declares no options takes any options, so a command that does + * not describe itself still runs with whatever it is given.

+ */ +@CompileStatic +class ProjectCommand extends BaseCommand implements Callable { + + /** The task that runs a command of the project */ + static final String RUN_COMMAND_TASK = 'runCommand' + + private static final String PASS_THROUGH_LABEL = 'ARGS' + + final String name + + private final ProjectGradle gradle + private final CommandSpec commandSpec + + /** + * @param command the command, as described in the manifest + * @param gradle runs the project's build + */ + ProjectCommand(Map command, ProjectGradle gradle) { + this.name = command.name as String + this.gradle = gradle + this.commandSpec = createSpec(command) + this.spec = commandSpec + } + + /** + * @return the picocli model of the command, which is what the command line runs and completes + */ + CommandSpec getCommandSpec() { + commandSpec + } + + @Override + Integer call() throws Exception { + List commandArguments = [name] + commandArguments.addAll(givenArguments()) + gradle.run([RUN_COMMAND_TASK, "-Pargs=${join(commandArguments)}".toString()]) + } + + private List givenArguments() { + CommandLine.ParseResult parsed = commandSpec.commandLine().parseResult + List given = [] + for (PositionalParamSpec positional : commandSpec.positionalParameters()) { + if (parsed.hasMatchedPositional(positional)) { + given.addAll(positional.stringValues()) + } + } + for (OptionSpec option : commandSpec.options()) { + if (option.usageHelp() || option.versionHelp() || !parsed.hasMatchedOption(option)) { + continue + } + given << (option.arity().max() == 0 + ? option.longestName() + : "${option.longestName()}=${option.stringValues().first()}".toString()) + } + given.addAll(parsed.unmatched()) + given + } + + private CommandSpec createSpec(Map command) { + CommandSpec created = CommandSpec.wrapWithoutInspection(this) + created.name(name) + created.addOption(OptionSpec.builder('-h', '--help') + .usageHelp(true) + .description('Show this help message and exit.') + .build()) + String description = command.description as String + if (description) { + created.usageMessage().description(description) + } + + List> arguments = (command.arguments ?: []) as List> + arguments.eachWithIndex { Map argument, int index -> + boolean required = argument.required != false + created.addPositional(PositionalParamSpec.builder() + .index(String.valueOf(index)) + .paramLabel(argument.name as String) + .description((argument.description ?: '') as String) + .arity(required ? '1' : '0..1') + .required(required) + .type(String) + .completionCandidates(candidates(argument)) + .build()) + } + if (!arguments) { + created.addPositional(PositionalParamSpec.builder() + .index('0..*') + .paramLabel(PASS_THROUGH_LABEL) + .description('The arguments of the command') + .arity('0..*') + .type(String[]) + .build()) + } + + List> options = (command.options ?: []) as List> + for (Map option : options) { + boolean flag = option.flag == true + created.addOption(OptionSpec.builder("--${option.name}".toString()) + .description((option.description ?: '') as String) + .arity(flag ? '0' : '1') + .type(flag ? boolean : String) + .paramLabel((option.name as String).toUpperCase(Locale.ENGLISH)) + .completionCandidates(candidates(option)) + .build()) + } + if (!options) { + created.parser().unmatchedArgumentsAllowed(true) + } + created + } + + private static Iterable candidates(Map declaration) { + List values = (declaration.values ?: []) as List + values ? values.asImmutable() : null + } + + /** + * Joins the arguments into the one {@code args} property {@code runCommand} splits again, quoting + * those that contain spaces or quotes. + */ + private static String join(List arguments) { + arguments.collect { String argument -> + if (argument && !(argument =~ /[\s"']/)) { + return argument + } + argument.contains('"') && !argument.contains("'") ? "'${argument}'" : "\"${argument}\"" + }.join(' ') + } +} diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy new file mode 100644 index 00000000000..b0b17881277 --- /dev/null +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy @@ -0,0 +1,131 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.forge.cli.command + +import groovy.json.JsonException +import groovy.json.JsonSlurper +import groovy.transform.CompileStatic + +import org.grails.forge.io.ConsoleOutput + +/** + * Finds the commands of the Grails project in a directory: those of the application and of its + * plugins, which {@code runCommand} runs. + * + *

The project's build describes them in a manifest, written by the Grails Gradle plugin's + * {@code generateCommandManifest} task. The manifest is read as long as it is newer than the files + * that decide the project's dependencies and its own commands; otherwise the task is run first, so + * a plugin added to the build brings its commands with it.

+ */ +@CompileStatic +class ProjectCommands { + + /** The task of the Grails Gradle plugin that writes the manifest */ + static final String MANIFEST_TASK = 'generateCommandManifest' + + /** Where the task writes the manifest, relative to the project directory */ + static final String MANIFEST_PATH = 'build/grails/commands.json' + + private static final List BUILD_FILES = ['build.gradle', 'build.gradle.kts', 'settings.gradle', + 'settings.gradle.kts', 'gradle.properties', + 'gradle/libs.versions.toml'] + + /** Where an application keeps its own commands */ + private static final String COMMAND_SOURCES = 'grails-app/commands' + + private final File projectDir + private final ProjectGradle gradle + + ProjectCommands(File projectDir) { + this(projectDir, new ProjectGradle(projectDir)) + } + + ProjectCommands(File projectDir, ProjectGradle gradle) { + this.projectDir = projectDir + this.gradle = gradle + } + + /** + * @return whether the directory holds a Grails project built with Gradle + */ + boolean isGrailsProject() { + new File(projectDir, 'grails-app').isDirectory() && + (new File(projectDir, 'build.gradle').isFile() || new File(projectDir, 'build.gradle.kts').isFile()) + } + + /** + * Reads the commands of the project, after updating the manifest when it is missing or older than + * the project's build or its commands. + * + * @param console where to say that the build is being run, and why the commands could not be read + * @return the commands, or none when the directory holds no Grails project or the build failed + */ + List load(ConsoleOutput console) { + if (!grailsProject) { + return [] + } + File manifest = new File(projectDir, MANIFEST_PATH) + if (outdated(manifest)) { + console.out('Reading the commands of the project from its build...') + int exitCode = gradle.runSilently([MANIFEST_TASK]) + if (exitCode != 0 || !manifest.isFile()) { + console.warning("Could not read the commands of the project. Run its ${MANIFEST_TASK} Gradle task to see why.") + return [] + } + } + read(manifest, console) + } + + private List read(File manifest, ConsoleOutput console) { + Map parsed + try { + parsed = (Map) new JsonSlurper().parse(manifest, 'UTF-8') + } + catch (JsonException | ClassCastException e) { + console.warning("Could not read the commands of the project from ${manifest}: ${e.message}") + return [] + } + List> commands = (parsed.commands ?: []) as List> + commands.collect { Map command -> new ProjectCommand(command, gradle) } + } + + private boolean outdated(File manifest) { + if (!manifest.isFile()) { + return true + } + long written = manifest.lastModified() + List buildFiles = (BUILD_FILES.collect { new File(projectDir, it) } + + BUILD_FILES.collect { new File(gradle.rootDir, it) }).unique() + buildFiles.any { File file -> file.lastModified() > written } || + newerFile(new File(projectDir, COMMAND_SOURCES), written) + } + + private static boolean newerFile(File dir, long than) { + if (!dir.isDirectory()) { + return false + } + boolean newer = false + dir.eachFileRecurse { File file -> + if (!newer && file.isFile() && file.lastModified() > than) { + newer = true + } + } + newer + } +} diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectGradle.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectGradle.groovy new file mode 100644 index 00000000000..653df4e2d00 --- /dev/null +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectGradle.groovy @@ -0,0 +1,95 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.forge.cli.command + +import groovy.transform.CompileStatic + +/** + * Runs Gradle for a project: through the project's Gradle wrapper, found in the project directory or + * the build's root above it, or through {@code gradle} on the path when the project has no wrapper. + */ +@CompileStatic +class ProjectGradle { + + private static final boolean WINDOWS = System.getProperty('os.name', '').toLowerCase(Locale.ENGLISH).contains('win') + + private final File projectDir + private final File wrapper + + ProjectGradle(File projectDir) { + this.projectDir = projectDir + this.wrapper = findWrapper(projectDir) + } + + /** + * @return the root directory of the build the project belongs to: the directory of its Gradle + * wrapper, or the project directory when it has none + */ + File getRootDir() { + wrapper?.parentFile ?: projectDir + } + + /** + * Runs Gradle in the project quietly, with its output shown. + * + * @param arguments the tasks and arguments to run + * @return the exit code of Gradle + */ + int run(List arguments) { + start(arguments) { ProcessBuilder builder -> builder.inheritIO() } + } + + /** + * Runs Gradle in the project quietly, with its output hidden. + * + * @param arguments the tasks and arguments to run + * @return the exit code of Gradle + */ + int runSilently(List arguments) { + start(arguments) { ProcessBuilder builder -> + builder.redirectOutput(ProcessBuilder.Redirect.DISCARD).redirectError(ProcessBuilder.Redirect.DISCARD) + } + } + + private int start(List arguments, Closure redirect) { + List command = [wrapper?.path ?: (WINDOWS ? 'gradle.bat' : 'gradle'), + '-p', projectDir.path, '-q', '--console=plain'] + command.addAll(arguments) + ProcessBuilder builder = new ProcessBuilder(command).directory(projectDir) + redirect.call(builder) + try { + return builder.start().waitFor() + } + catch (InterruptedException e) { + Thread.currentThread().interrupt() + throw new IOException("Gradle was interrupted while running ${arguments}", e) + } + } + + private static File findWrapper(File projectDir) { + String name = WINDOWS ? 'gradlew.bat' : 'gradlew' + for (File dir = projectDir; dir != null; dir = dir.parentFile) { + File candidate = new File(dir, name) + if (candidate.isFile()) { + return candidate + } + } + null + } +} diff --git a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy new file mode 100644 index 00000000000..e15a271a534 --- /dev/null +++ b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy @@ -0,0 +1,361 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.forge.cli + +import java.nio.file.Files +import java.nio.file.attribute.PosixFilePermission + +import groovy.json.JsonOutput + +import org.jline.reader.Candidate +import org.jline.reader.LineReader +import org.jline.reader.LineReaderBuilder +import org.jline.reader.Parser +import org.jline.reader.impl.DefaultParser +import org.jline.terminal.Terminal +import org.jline.terminal.TerminalBuilder +import org.springframework.context.ConfigurableApplicationContext +import picocli.CommandLine + +import org.grails.forge.cli.command.CreateAppCommand +import spock.lang.AutoCleanup +import spock.lang.IgnoreIf +import spock.lang.Shared +import spock.lang.Specification + +// the project's build is a shell script standing in for the Gradle wrapper +@IgnoreIf({ os.windows }) +class ProjectCommandsSpec extends Specification { + + private static final Map SECURITY_QUICKSTART = [ + name : 's2-quickstart', + description: 'Creates domain classes and updates config settings for the Spring Security plugin', + arguments : [ + [name: 'package', description: 'The package of the domain classes', required: false, values: []], + [name: 'user-class-name', description: 'The name of the user class', required: false, values: []], + [name: 'role-class-name', description: 'The name of the role class', required: false, values: []] + ], + options : [ + [name: 'groupClassName', description: 'Also generate role group classes', flag: false, values: []], + [name: 'uiOnly', description: 'Configure without generating classes', flag: true, values: []] + ] + ] + + private static final Map CREATE_TOKEN = [ + name : 's2-create-persistent-token', + description: 'Creates a persistent token domain class', + arguments : [[name: 'domain-class-name', description: 'The name of the domain class', required: true, values: []]], + options : [[name: 'format', description: 'The format', flag: false, values: ['json', 'yaml']]] + ] + + private static final Map URL_MAPPINGS_REPORT = [ + name: 'url-mappings-report', description: 'url-mappings-report', arguments: [], options: [] + ] + + private static final Map PLUGIN_CREATE_APP = [ + name: 'create-app', description: 'A plugin command named like a Forge command', arguments: [], options: [] + ] + + @Shared + @AutoCleanup + ConfigurableApplicationContext context = Application.builder().run() + + @Shared + @AutoCleanup + Terminal terminal = TerminalBuilder.builder() + .type(Terminal.TYPE_DUMB) + .streams(new ByteArrayInputStream(new byte[0]), new ByteArrayOutputStream()) + .build() + + File dir = Files.createTempDirectory('forge-project-commands').toFile().canonicalFile + File manifest = new File(dir, 'build/grails/commands.json') + ByteArrayOutputStream output = new ByteArrayOutputStream() + + private String previousUserDir + private PrintStream previousOut + private PrintStream previousErr + + void setup() { + previousUserDir = System.getProperty('user.dir') + System.setProperty('user.dir', dir.path) + previousOut = System.out + previousErr = System.err + PrintStream captured = new PrintStream(output, true) + System.out = captured + System.err = captured + assert new File(dir, 'grails-app/commands').mkdirs() + new File(dir, 'build.gradle').text = "plugins { id 'org.apache.grails.gradle.grails-web' }\n" + writeGradleWrapper() + } + + void cleanup() { + System.out = previousOut + System.err = previousErr + System.setProperty('user.dir', previousUserDir) + dir.deleteDir() + } + + void 'the commands of a project without a manifest are read from its build first'() { + given: + buildWrites(SECURITY_QUICKSTART, URL_MAPPINGS_REPORT) + + when: + CommandLine commandLine = ForgeCli.createCommandLine(context, false) + + then: "the build is run once, quietly, in the project directory" + gradleInvocations() == [['-p', dir.path, '-q', '--console=plain', 'generateCommandManifest']] + output.toString().contains('Reading the commands of the project from its build...') + + and: "the project's commands are offered next to the Forge CLI's own" + commandLine.subcommands.keySet().containsAll(['create-app', 's2-quickstart', 'url-mappings-report']) + } + + void 'a manifest newer than the build is read without running the build'() { + given: + writeManifest(SECURITY_QUICKSTART) + + when: + CommandLine commandLine = ForgeCli.createCommandLine(context, false) + + then: + commandLine.subcommands.containsKey('s2-quickstart') + gradleInvocations() == [] + } + + void 'the manifest is written again when #changed changed after it'() { + given: + writeManifest(URL_MAPPINGS_REPORT) + buildWrites(SECURITY_QUICKSTART) + File changedFile = new File(dir, changed) + changedFile.parentFile.mkdirs() + changedFile.text = 'changed' + changedFile.setLastModified(manifest.lastModified() + 10_000) + + when: + CommandLine commandLine = ForgeCli.createCommandLine(context, false) + + then: + gradleInvocations()*.last() == ['generateCommandManifest'] + commandLine.subcommands.containsKey('s2-quickstart') + + where: + changed << ['build.gradle', 'settings.gradle', 'gradle.properties', 'gradle/libs.versions.toml', + 'grails-app/commands/example/HelloCommand.groovy'] + } + + void 'a directory without a Grails project has no project commands and runs no build'() { + given: + new File(dir, 'grails-app').deleteDir() + buildWrites(SECURITY_QUICKSTART) + + when: + CommandLine commandLine = ForgeCli.createCommandLine(context, false) + + then: + !commandLine.subcommands.containsKey('s2-quickstart') + gradleInvocations() == [] + } + + void "the Forge CLI's own commands run without reading the project's commands"() { + given: + buildWrites(SECURITY_QUICKSTART) + + expect: + ForgeCli.execute(context, 'create-app', '--help') == 0 + gradleInvocations() == [] + } + + void "a project command named like one of the Forge CLI's commands does not replace it"() { + given: + writeManifest(PLUGIN_CREATE_APP, URL_MAPPINGS_REPORT) + + when: + CommandLine commandLine = ForgeCli.createCommandLine(context, false) + + then: + commandLine.subcommands['create-app'].commandSpec.userObject() instanceof CreateAppCommand + commandLine.subcommands.containsKey('url-mappings-report') + } + + void 'a project command runs through runCommand with its arguments, then its options'() { + given: + writeManifest(SECURITY_QUICKSTART) + + when: + int exitCode = ForgeCli.execute(context, 's2-quickstart', '--uiOnly', 'com.example', 'User', 'Role', + '--groupClassName=RoleGroup') + + then: + exitCode == 0 + gradleInvocations() == [['-p', dir.path, '-q', '--console=plain', 'runCommand', + '-Pargs=s2-quickstart com.example User Role --groupClassName=RoleGroup --uiOnly']] + } + + void 'an option value given as a separate argument is passed as one option'() { + given: + writeManifest(SECURITY_QUICKSTART) + + when: + ForgeCli.execute(context, 's2-quickstart', 'com.example', 'User', 'Role', '--groupClassName', 'RoleGroup') + + then: + gradleInvocations()*.last() == ['-Pargs=s2-quickstart com.example User Role --groupClassName=RoleGroup'] + } + + void 'a command that describes no arguments or options passes on whatever it is given, quoting what needs it'() { + given: + writeManifest(URL_MAPPINGS_REPORT) + + when: + ForgeCli.execute(context, 'url-mappings-report', 'first', 'two words', '--format=json', 'say "hi"', '') + + then: 'the arguments in the order given, then the options it does not know' + gradleInvocations()*.last() == ['-Pargs=url-mappings-report first "two words" \'say "hi"\' "" --format=json'] + } + + void 'the exit code of the command is the exit code of the build'() { + given: + writeManifest(URL_MAPPINGS_REPORT) + new File(dir, 'gradle-exit-code').text = '3' + + expect: + ForgeCli.execute(context, 'url-mappings-report') == 3 + } + + void 'a missing required argument or an undeclared option is reported without running the build'() { + given: + writeManifest(CREATE_TOKEN, SECURITY_QUICKSTART) + + expect: + ForgeCli.execute(context, arguments as String[]) != 0 + gradleInvocations() == [] + + where: + arguments << [['s2-create-persistent-token'], ['s2-quickstart', '--unknown']] + } + + void 'the help of a project command shows what it declares'() { + given: + writeManifest(SECURITY_QUICKSTART) + + when: + ForgeCli.execute(context, 's2-quickstart', '--help') + + then: 'the usage, as picocli wraps it, read as one line' + String help = output.toString().split(/\s+/).join(' ') + help.contains('Creates domain classes and updates config settings for the Spring Security plugin') + help.contains('[package] [user-class-name] [role-class-name]') + help.contains('The package of the domain classes') + help.contains('--groupClassName=GROUPCLASSNAME') + help.contains('--uiOnly') + gradleInvocations() == [] + } + + void 'a build that cannot write the manifest is reported, and the Forge CLI still runs'() { + given: + new File(dir, 'gradle-exit-code').text = '1' + + when: + CommandLine commandLine = ForgeCli.createCommandLine(context, false) + + then: + output.toString().contains('Could not read the commands of the project. Run its generateCommandManifest Gradle task to see why.') + commandLine.subcommands.containsKey('create-app') + !commandLine.subcommands.containsKey('s2-quickstart') + } + + void 'a manifest that is not JSON is reported'() { + given: + manifest.parentFile.mkdirs() + manifest.text = 'not json' + + when: + CommandLine commandLine = ForgeCli.createCommandLine(context, false) + + then: + output.toString().contains("Could not read the commands of the project from ${manifest}") + commandLine.subcommands.containsKey('create-app') + } + + void 'the prompt completes project commands, their options and the values they declare'() { + given: + writeManifest(SECURITY_QUICKSTART, CREATE_TOKEN) + CommandLine commandLine = ForgeCli.createCommandLine(context, true) + + expect: + complete(commandLine, 's2-').containsAll(['s2-quickstart', 's2-create-persistent-token']) + complete(commandLine, 's2-quickstart --').containsAll(['--groupClassName', '--uiOnly', '--help']) + complete(commandLine, 's2-create-persistent-token --format ').containsAll(['json', 'yaml']) + } + + private void buildWrites(Map... commands) { + new File(dir, 'manifest-source.json').text = JsonOutput.toJson([version: 1, commands: commands.toList()]) + } + + private void writeManifest(Map... commands) { + manifest.parentFile.mkdirs() + manifest.text = JsonOutput.toJson([version: 1, commands: commands.toList()]) + manifest.setLastModified(System.currentTimeMillis() + 10_000) + } + + private List> gradleInvocations() { + File log = new File(dir, 'gradle-invocations.txt') + if (!log.isFile()) { + return [] + } + List> invocations = [] + List current = [] + for (String line : log.readLines()) { + if (line == '--') { + invocations << current + current = [] + } else { + current << line + } + } + invocations + } + + private void writeGradleWrapper() { + File wrapper = new File(dir, 'gradlew') + wrapper.text = '''\ + #!/bin/sh + dir=$(cd "$(dirname "$0")" && pwd) + { for argument in "$@"; do printf '%s\\n' "$argument"; done; echo '--'; } >> "$dir/gradle-invocations.txt" + case " $* " in + *" generateCommandManifest "*) + if [ -f "$dir/manifest-source.json" ]; then + mkdir -p "$dir/build/grails" && cp "$dir/manifest-source.json" "$dir/build/grails/commands.json" + fi;; + esac + if [ -f "$dir/gradle-exit-code" ]; then exit "$(cat "$dir/gradle-exit-code")"; fi + exit 0 + '''.stripIndent() + Files.setPosixFilePermissions(wrapper.toPath(), [PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE, + PosixFilePermission.OWNER_EXECUTE] as Set) + } + + private List complete(CommandLine commandLine, String typed) { + LineReader reader = LineReaderBuilder.builder().terminal(terminal).build() + List candidates = [] + new CommandLineCompleter(commandLine.commandSpec) + .complete(reader, new DefaultParser().parse(typed, typed.length(), Parser.ParseContext.COMPLETE), candidates) + candidates*.value() + } +} From 3904a1af4a9d18815f0255efc6661814750f3adc Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Mon, 16 Nov 2026 21:31:26 -0800 Subject: [PATCH 026/115] Describe the arguments and options of the Spring Security commands s2-quickstart declares its four optional class name arguments and its groupClassName, uiOnly and salt options, and the s2-create-* commands the domain class name they need, so the Forge CLI checks and completes them. The s2-create-* commands also get a description of their own instead of their name. --- .../cli/S2CreatePersistentTokenCommand.groovy | 5 ++ .../S2CreateRoleHierarchyEntryCommand.groovy | 5 ++ .../security/cli/S2QuickstartCommand.groovy | 12 ++++ .../cli/SecurityCommandManifestSpec.groovy | 70 +++++++++++++++++++ 4 files changed, 92 insertions(+) create mode 100644 grails-spring-security/plugin/src/test-cli/groovy/org/apache/grails/security/cli/SecurityCommandManifestSpec.groovy diff --git a/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2CreatePersistentTokenCommand.groovy b/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2CreatePersistentTokenCommand.groovy index 615db6fc799..02f4280ea65 100644 --- a/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2CreatePersistentTokenCommand.groovy +++ b/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2CreatePersistentTokenCommand.groovy @@ -18,6 +18,7 @@ package org.apache.grails.security.cli import grails.build.logging.ConsoleLogger import grails.build.logging.GrailsConsole import grails.codegen.model.Model +import org.apache.grails.core.cli.CommandArgument import org.apache.grails.core.cli.GrailsApplicationCommand import groovy.transform.CompileStatic @@ -29,9 +30,13 @@ import groovy.transform.CompileStatic * * @since 6.0.0 */ +@CommandArgument(name = 'domain-class-name', + description = 'The fully qualified name of the persistent token domain class, e.g. com.yourapp.PersistentLogin') @CompileStatic class S2CreatePersistentTokenCommand implements GrailsApplicationCommand, CommandLineHelper, SkipBootstrap { + String description = 'Creates a persistent token domain class for remember-me logins.' + @Delegate ConsoleLogger consoleLogger = GrailsConsole.getInstance() diff --git a/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2CreateRoleHierarchyEntryCommand.groovy b/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2CreateRoleHierarchyEntryCommand.groovy index 1411817642e..620b92333a5 100644 --- a/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2CreateRoleHierarchyEntryCommand.groovy +++ b/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2CreateRoleHierarchyEntryCommand.groovy @@ -18,6 +18,7 @@ package org.apache.grails.security.cli import grails.build.logging.ConsoleLogger import grails.build.logging.GrailsConsole import grails.codegen.model.Model +import org.apache.grails.core.cli.CommandArgument import org.apache.grails.core.cli.GrailsApplicationCommand import groovy.transform.CompileStatic @@ -28,9 +29,13 @@ import groovy.transform.CompileStatic * * @since 6.0.0 */ +@CommandArgument(name = 'domain-class-name', + description = 'The fully qualified name of the role hierarchy domain class, e.g. com.yourapp.RoleHierarchyEntry') @CompileStatic class S2CreateRoleHierarchyEntryCommand implements GrailsApplicationCommand, CommandLineHelper, SkipBootstrap { + String description = 'Creates a domain class for a persistent role hierarchy.' + private static final String USAGE_MESSAGE = ''' ./grailsw s2-create-role-hierarchy-entry [DOMAIN CLASS NAME] diff --git a/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2QuickstartCommand.groovy b/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2QuickstartCommand.groovy index 9a7cab86840..a2d5b0ed5a4 100644 --- a/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2QuickstartCommand.groovy +++ b/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2QuickstartCommand.groovy @@ -18,6 +18,8 @@ package org.apache.grails.security.cli import grails.build.logging.ConsoleLogger import grails.build.logging.GrailsConsole import grails.codegen.model.Model +import org.apache.grails.core.cli.CommandArgument +import org.apache.grails.core.cli.CommandOption import org.apache.grails.core.cli.GrailsApplicationCommand import groovy.transform.CompileStatic @@ -34,6 +36,16 @@ import groovy.transform.CompileStatic * * @since 6.0.0 */ +@CommandArgument(name = 'domain-class-package', required = false, + description = 'The package of the domain classes') +@CommandArgument(name = 'user-class-name', required = false, description = 'The name of the user class') +@CommandArgument(name = 'role-class-name', required = false, description = 'The name of the role class') +@CommandArgument(name = 'requestmap-class-name', required = false, + description = 'The name of the request map class, when request maps secure the URLs') +@CommandOption(name = 'groupClassName', description = 'Also creates role group classes with this name') +@CommandOption(name = 'uiOnly', flag = true, + description = 'Configures the plugin without creating domain classes, as LDAP or CAS authentication needs') +@CommandOption(name = 'salt', flag = true, description = 'Creates a password encoder listener that salts the passwords') @CompileStatic class S2QuickstartCommand implements GrailsApplicationCommand, CommandLineHelper, SkipBootstrap { diff --git a/grails-spring-security/plugin/src/test-cli/groovy/org/apache/grails/security/cli/SecurityCommandManifestSpec.groovy b/grails-spring-security/plugin/src/test-cli/groovy/org/apache/grails/security/cli/SecurityCommandManifestSpec.groovy new file mode 100644 index 00000000000..fed2aad8d1a --- /dev/null +++ b/grails-spring-security/plugin/src/test-cli/groovy/org/apache/grails/security/cli/SecurityCommandManifestSpec.groovy @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.grails.security.cli + +import java.nio.file.Path + +import groovy.json.JsonSlurper + +import org.apache.grails.core.cli.ApplicationCommandManifestWriter +import spock.lang.Specification +import spock.lang.TempDir + +class SecurityCommandManifestSpec extends Specification { + + @TempDir + Path tempDir + + void 's2-quickstart describes its arguments and options'() { + when: + Map quickstart = command('s2-quickstart') + + then: + quickstart.description == 'Creates domain classes and updates config settings for the Spring Security plugin.' + quickstart.arguments*.name == ['domain-class-package', 'user-class-name', 'role-class-name', 'requestmap-class-name'] + quickstart.arguments*.required == [false, false, false, false] + + and: 'the options the command reads' + quickstart.options.collectEntries { [(it.name): it.flag] } == [groupClassName: false, uiOnly: true, salt: true] + } + + void '#name takes the domain class name it creates'() { + when: + Map described = command(name) + + then: + described.description == description + described.arguments*.name == ['domain-class-name'] + described.arguments*.required == [true] + described.options == [] + + where: + name | description + 's2-create-persistent-token' | 'Creates a persistent token domain class for remember-me logins.' + 's2-create-role-hierarchy-entry' | 'Creates a domain class for a persistent role hierarchy.' + } + + private Map command(String name) { + File manifest = tempDir.resolve('commands.json').toFile() + ApplicationCommandManifestWriter.main(manifest.path) + Map found = (Map) ((Map) new JsonSlurper().parse(manifest)).commands.find { it.name == name } + assert found : "${name} is not in the manifest" + found + } +} From 382bf414e3496f1ecd6c1e7b6158976d6a5d4f61 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Mon, 16 Nov 2026 21:31:26 -0800 Subject: [PATCH 027/115] Document project commands in the Forge CLI The Forge CLI section explains which commands of a project it offers, how it learns them from generateCommandManifest and when it runs that task. Creating Custom Commands shows @CommandArgument and @CommandOption, and What's New introduces the feature. --- grails-doc/src/en/guide/commandLine.adoc | 37 +++++++++++++ .../commandLine/creatingCustomCommands.adoc | 53 +++++++++++++++++++ .../src/en/guide/introduction/whatsNew.adoc | 22 ++++++++ 3 files changed, 112 insertions(+) diff --git a/grails-doc/src/en/guide/commandLine.adoc b/grails-doc/src/en/guide/commandLine.adoc index b66755c6cd0..4cfc6290cac 100644 --- a/grails-doc/src/en/guide/commandLine.adoc +++ b/grails-doc/src/en/guide/commandLine.adoc @@ -195,6 +195,43 @@ features: This "grails-forge-cli.yml" configuration sets the default values for various aspects of the Grails web application, including the application type, default package, test framework, source language, build tool, GORM implementation, servlet implementation, and a list of enabled features. +[[projectCommands]] +=== Commands of the Project + +Inside a Grails project, the Grails Forge CLI also offers the commands of the application and of its +plugins: the commands in `grails-app/commands` and those every plugin of the build brings with it, such +as `s2-quickstart` from the Spring Security plugin or the `dbm-*` commands of database migration. They +are listed in `grails-forge-cli --help`, and the interactive prompt completes their names, the options +they declare and the values those options take: + +[source,shell] +---- +$ grails-forge-cli +grails> s2- +s2-create-persistent-token s2-create-role-hierarchy-entry s2-quickstart +grails> s2-quickstart com.example User Role -- +--groupClassName --help --salt --uiOnly +---- + +Each command shows its usage with `--help`, and runs through the project's `runCommand` Gradle task, +as `./gradlew runCommand -Pargs="s2-quickstart com.example User Role"` would run it. The Forge CLI's own +commands keep their name: a project command named like one of them is not offered. + +The Forge CLI learns the commands from the project's build: the Grails Gradle plugin's +`generateCommandManifest` task writes them to `build/grails/commands.json`. The Forge CLI runs the task +when the file is missing, or older than the project's build files (`build.gradle`, `settings.gradle`, +`gradle.properties` and `gradle/libs.versions.toml`) or its `grails-app/commands`, so a plugin added to +the build brings its commands the next time a project command is asked for. Running one of the Forge +CLI's own commands, such as `create-controller`, never runs the build. When the task fails, the Forge +CLI says so and offers only its own commands; run `./gradlew generateCommandManifest` to see why. + +A command that describes its arguments and options, as xref:commandLine#creatingCustomCommands[Creating Custom Commands] +shows, has them checked and completed. A command that does not takes any arguments and options and +passes them on unchanged. + +The `grails` command of an SDKMAN! installation starts the profile CLI by default. Start the Forge CLI +with `grails-forge-cli`, with `grails -t forge`, or by default with `GRAILS_PREFERRED_SHELL=forge`. + === Grails Default Package Configuration The project's default package is determined based on the project's name. For instance, running the following command: diff --git a/grails-doc/src/en/guide/commandLine/creatingCustomCommands.adoc b/grails-doc/src/en/guide/commandLine/creatingCustomCommands.adoc index 79f00b6a598..00c92586c8a 100644 --- a/grails-doc/src/en/guide/commandLine/creatingCustomCommands.adoc +++ b/grails-doc/src/en/guide/commandLine/creatingCustomCommands.adoc @@ -214,6 +214,51 @@ Now you can run the `greet` command with arguments and options: This allows you to create more versatile and interactive custom commands for your Grails application. +**Describing Arguments and Options** + +A command can describe the arguments and options it takes with the +link:{api}org/apache/grails/core/cli/CommandArgument.html[CommandArgument] and +link:{api}org/apache/grails/core/cli/CommandOption.html[CommandOption] annotations. Tools that offer the +commands of a project, such as the xref:commandLine#projectCommands[Grails Forge CLI], show them in the +command's help, check them and complete them. Declare one `@CommandArgument` per argument, in the order +the arguments are given, and one `@CommandOption` per option: + +[source,groovy] +---- +// grails-app/commands/com/example/GreetCommand.groovy +package com.example + +import org.apache.grails.core.cli.CommandArgument +import org.apache.grails.core.cli.CommandOption +import org.apache.grails.core.cli.GrailsApplicationCommand + +@CommandArgument(name = 'name', required = false, description = 'Who to greet') +@CommandOption(name = 'loud', flag = true, description = 'Greet in capitals') +@CommandOption(name = 'language', values = ['en', 'de', 'fr'], description = 'The language of the greeting') +class GreetCommand implements GrailsApplicationCommand { + + String getDescription() { + 'Greet the user with options' + } + + boolean handle() { + String name = args ? args[0] : 'user' + boolean loud = executionContext.commandLine.hasOption('loud') + String language = executionContext.commandLine.optionValue('language') ?: 'en' + // ... + true + } +} +---- + +An argument is required unless it declares `required = false`. An option is given as `--name=value`, or +as `--name` when it is a `flag`. `values` lists the values offered when the argument or option is +completed. The annotations only describe the command: it still reads its arguments and options from the +command line as before. + +A command that declares no `@CommandArgument` takes any arguments, and one that declares no +`@CommandOption` takes any options, so a command without the annotations works as it always has. + In summary, creating custom commands in Grails using the `GrailsApplicationCommand` trait is a powerful way to extend your application's functionality beyond the web interface. You can define the command's name, description, and logic, and then execute it from the command line, optionally passing arguments and options as needed. **Using the `executionContext` in the Grails Custom Commands** @@ -376,6 +421,14 @@ dependencies { # or, via the automatically registered per-command task: ./gradlew auditReport ---- ++ +Inside the application, the xref:commandLine#projectCommands[Grails Forge CLI] offers and completes the +command as well, and runs it through `runCommand`: ++ +[source,shell] +---- +grails-forge-cli audit-report +---- If an application disables the automatic discovery (`grails { cliAutoProvision = false }`), it declares the companion explicitly instead: diff --git a/grails-doc/src/en/guide/introduction/whatsNew.adoc b/grails-doc/src/en/guide/introduction/whatsNew.adoc index 7918c538eaa..fa59390f2a4 100644 --- a/grails-doc/src/en/guide/introduction/whatsNew.adoc +++ b/grails-doc/src/en/guide/introduction/whatsNew.adoc @@ -369,6 +369,28 @@ breakdown. The Grails Data plugin itself (`grails-data-hibernate5` or `grails-data-hibernate7`) is an application dependency only: generated builds no longer place it on the `buildscript` classpath or in `buildSrc`, where Gradle's own Groovy would run the compile-time transformations the plugin carries while compiling the build scripts. See the xref:upgrading#upgrading80x[upgrade guide] for the equivalent change in an existing build. +==== Plugin Commands in the Grails Forge CLI + +Inside a Grails project, the Grails Forge CLI now offers the commands of the application and of its +plugins next to its own, with Tab completion at its prompt. Adding the Spring Security plugin to the +build is enough to type `s2-quickstart`: + +[source,shell] +---- +$ grails-forge-cli +grails> s2-quickstart com.example User Role --groupClassName=RoleGroup +---- + +The Forge CLI reads the commands from a manifest that the project's build writes with the new +`generateCommandManifest` task, so it never loads the project's classpath itself, and runs a command +through the project's `runCommand` task. A command describes the arguments and options it takes with +the new `@CommandArgument` and `@CommandOption` annotations, which the Forge CLI shows in the command's +help, checks and completes. Commands without them, such as those of plugins built for an earlier +release, are offered too and take any arguments. + +See xref:commandLine#projectCommands[Commands of the Project] and +xref:commandLine#creatingCustomCommands[Creating Custom Commands]. + ==== GSP Tag Library Improvements Grails {grailsMajorVersion} continues the move toward method-based TagLib handlers while preserving compatibility with existing closure-based tags. From b9c7f03086a0bbe2aadb13bbab4a6501a7cfe4fd Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Mon, 16 Nov 2026 21:36:39 -0800 Subject: [PATCH 028/115] Run only the project's own build, and only when a project command is asked for The Gradle wrapper was looked for in every directory above the project, so a gradlew in a shared parent directory, outside the project's build, would have been run, and without a wrapper gradle on the path was. Only the project's own wrapper or the one at the root of its build, the first directory above it with a settings file, runs now; a build without one offers no project commands. --help and the other options no longer run the build either. They list the project's commands from the manifest the build last wrote, and the build runs only for the interactive prompt or when a command the Forge CLI does not have is named. --- grails-doc/src/en/guide/commandLine.adoc | 17 ++- .../org/grails/forge/cli/ForgeCli.groovy | 17 +-- .../forge/cli/command/ProjectCommands.groovy | 19 ++++ .../forge/cli/command/ProjectGradle.groovy | 33 ++++-- .../forge/cli/ProjectCommandsSpec.groovy | 103 ++++++++++++++++-- 5 files changed, 162 insertions(+), 27 deletions(-) diff --git a/grails-doc/src/en/guide/commandLine.adoc b/grails-doc/src/en/guide/commandLine.adoc index 4cfc6290cac..134e5b266e1 100644 --- a/grails-doc/src/en/guide/commandLine.adoc +++ b/grails-doc/src/en/guide/commandLine.adoc @@ -200,9 +200,8 @@ This "grails-forge-cli.yml" configuration sets the default values for various as Inside a Grails project, the Grails Forge CLI also offers the commands of the application and of its plugins: the commands in `grails-app/commands` and those every plugin of the build brings with it, such -as `s2-quickstart` from the Spring Security plugin or the `dbm-*` commands of database migration. They -are listed in `grails-forge-cli --help`, and the interactive prompt completes their names, the options -they declare and the values those options take: +as `s2-quickstart` from the Spring Security plugin or the `dbm-*` commands of database migration. The +interactive prompt completes their names, the options they declare and the values those options take: [source,shell] ---- @@ -221,9 +220,15 @@ The Forge CLI learns the commands from the project's build: the Grails Gradle pl `generateCommandManifest` task writes them to `build/grails/commands.json`. The Forge CLI runs the task when the file is missing, or older than the project's build files (`build.gradle`, `settings.gradle`, `gradle.properties` and `gradle/libs.versions.toml`) or its `grails-app/commands`, so a plugin added to -the build brings its commands the next time a project command is asked for. Running one of the Forge -CLI's own commands, such as `create-controller`, never runs the build. When the task fails, the Forge -CLI says so and offers only its own commands; run `./gradlew generateCommandManifest` to see why. +the build brings its commands the next time a project command is asked for. The build runs only when the +interactive prompt starts or a command the Forge CLI does not have is named: its own commands, such as +`create-controller`, and its options, such as `--help`, never run it. `grails-forge-cli --help` lists the +project's commands from the file the build last wrote. When the task fails, the Forge CLI says so and +offers only its own commands; run `./gradlew generateCommandManifest` to see why. + +The build runs through the Gradle wrapper of the project, or of the build it belongs to: the wrapper in +the first directory above it with a `settings.gradle` file. A project whose build has no wrapper has no +commands offered, and no `gradle` found on the path is run instead. A command that describes its arguments and options, as xref:commandLine#creatingCustomCommands[Creating Custom Commands] shows, has them checked and completed. A command that does not takes any arguments and options and diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/ForgeCli.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/ForgeCli.groovy index 34b69a0bdb7..0290ef49637 100644 --- a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/ForgeCli.groovy +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/ForgeCli.groovy @@ -98,10 +98,11 @@ final class ForgeCli { static int execute(ApplicationContext context, String... args) { boolean noOpConsole = args.length > 0 && args[0].startsWith('update-cli-config') CommandLine commandLine = createForgeCommandLine(context, noOpConsole) - // the commands of the project are read only when the command line may name one of them, so - // the Forge CLI's own commands never wait for the project's build - if (args.length == 0 || !commandLine.subcommands.containsKey(args[0])) { - addProjectCommands(commandLine) + // the project's build runs only when the command line names a command that is not the Forge CLI's: + // its own commands never wait for the build, and its options, such as --help, list the project's + // commands from a manifest the build already wrote + if (args.length > 0 && !commandLine.subcommands.containsKey(args[0])) { + addProjectCommands(commandLine, !args[0].startsWith('-')) } return commandLine.execute(args) } @@ -120,7 +121,7 @@ final class ForgeCli { */ static CommandLine createCommandLine(ApplicationContext context, boolean noOpConsole) { CommandLine commandLine = createForgeCommandLine(context, noOpConsole) - addProjectCommands(commandLine) + addProjectCommands(commandLine, true) return commandLine } @@ -152,8 +153,10 @@ final class ForgeCli { /** * Adds the commands of the Grails project in the current directory, which run through the project's * build. A command named like one of the Forge CLI's own is left out: the Forge CLI's command runs. + * + * @param runBuild whether the project's build may run to update the commands */ - private static void addProjectCommands(CommandLine commandLine) { + private static void addProjectCommands(CommandLine commandLine, boolean runBuild) { File projectDir try { projectDir = FileSystemOutputHandler.getDefaultBaseDirectory() @@ -162,7 +165,7 @@ final class ForgeCli { return } ConsoleOutput console = (ForgeCommand) commandLine.getCommand() - for (ProjectCommand command : new ProjectCommands(projectDir).load(console)) { + for (ProjectCommand command : new ProjectCommands(projectDir).load(console, runBuild)) { if (!commandLine.subcommands.containsKey(command.name)) { commandLine.addSubcommand(command.name, command.commandSpec) } diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy index b0b17881277..f77e1b17948 100644 --- a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy @@ -77,10 +77,29 @@ class ProjectCommands { * @return the commands, or none when the directory holds no Grails project or the build failed */ List load(ConsoleOutput console) { + load(console, true) + } + + /** + * Reads the commands of the project. + * + * @param console where to say that the build is being run, and why the commands could not be read + * @param runBuild whether the manifest may be updated by running the build; when not, a manifest + * that is there is read as it is, and none means no commands + * @return the commands, or none when the directory holds no Grails project or the build failed + */ + List load(ConsoleOutput console, boolean runBuild) { if (!grailsProject) { return [] } + if (!gradle.available) { + console.warning('The build of the project has no Gradle wrapper, so its commands are not offered.') + return [] + } File manifest = new File(projectDir, MANIFEST_PATH) + if (!runBuild) { + return manifest.isFile() ? read(manifest, console) : [] + } if (outdated(manifest)) { console.out('Reading the commands of the project from its build...') int exitCode = gradle.runSilently([MANIFEST_TASK]) diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectGradle.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectGradle.groovy index 653df4e2d00..62390184e18 100644 --- a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectGradle.groovy +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectGradle.groovy @@ -21,14 +21,18 @@ package org.grails.forge.cli.command import groovy.transform.CompileStatic /** - * Runs Gradle for a project: through the project's Gradle wrapper, found in the project directory or - * the build's root above it, or through {@code gradle} on the path when the project has no wrapper. + * Runs Gradle for a project through the Gradle wrapper of its build: the wrapper in the project + * directory, or the one in the root directory of the build the project belongs to, the first + * directory above it with a settings file. No wrapper outside the build is ever run, and neither is + * a {@code gradle} found on the path. */ @CompileStatic class ProjectGradle { private static final boolean WINDOWS = System.getProperty('os.name', '').toLowerCase(Locale.ENGLISH).contains('win') + private static final List SETTINGS_FILES = ['settings.gradle', 'settings.gradle.kts'] + private final File projectDir private final File wrapper @@ -37,6 +41,13 @@ class ProjectGradle { this.wrapper = findWrapper(projectDir) } + /** + * @return whether the project's build has a Gradle wrapper to run + */ + boolean isAvailable() { + wrapper != null + } + /** * @return the root directory of the build the project belongs to: the directory of its Gradle * wrapper, or the project directory when it has none @@ -68,8 +79,10 @@ class ProjectGradle { } private int start(List arguments, Closure redirect) { - List command = [wrapper?.path ?: (WINDOWS ? 'gradle.bat' : 'gradle'), - '-p', projectDir.path, '-q', '--console=plain'] + if (wrapper == null) { + throw new IOException("The build of ${projectDir} has no Gradle wrapper") + } + List command = [wrapper.path, '-p', projectDir.path, '-q', '--console=plain'] command.addAll(arguments) ProcessBuilder builder = new ProcessBuilder(command).directory(projectDir) redirect.call(builder) @@ -84,10 +97,16 @@ class ProjectGradle { private static File findWrapper(File projectDir) { String name = WINDOWS ? 'gradlew.bat' : 'gradlew' + File own = new File(projectDir, name) + if (own.isFile()) { + return own + } + // the build's root is the first directory with a settings file, as Gradle finds it; a wrapper + // further up belongs to something else for (File dir = projectDir; dir != null; dir = dir.parentFile) { - File candidate = new File(dir, name) - if (candidate.isFile()) { - return candidate + if (SETTINGS_FILES.any { new File(dir, it).isFile() }) { + File rootWrapper = new File(dir, name) + return rootWrapper.isFile() ? rootWrapper : null } } null diff --git a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy index e15a271a534..31ea0208b26 100644 --- a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy +++ b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy @@ -193,6 +193,35 @@ class ProjectCommandsSpec extends Specification { commandLine.subcommands.containsKey('url-mappings-report') } + void '--help lists the commands the build already wrote, even when they are outdated, without running the build'() { + given: + writeManifest(SECURITY_QUICKSTART) + new File(dir, 'build.gradle').setLastModified(manifest.lastModified() + 10_000) + buildWrites(URL_MAPPINGS_REPORT) + + when: + int exitCode = ForgeCli.execute(context, '--help') + + then: + exitCode == 0 + output.toString().contains('s2-quickstart') + !output.toString().contains('url-mappings-report') + gradleInvocations() == [] + } + + void '--help without a manifest lists only the Forge CLI commands and runs no build'() { + given: + buildWrites(SECURITY_QUICKSTART) + + when: + ForgeCli.execute(context, '--help') + + then: + output.toString().contains('create-app') + !output.toString().contains('s2-quickstart') + gradleInvocations() == [] + } + void 'a project command runs through runCommand with its arguments, then its options'() { given: writeManifest(SECURITY_QUICKSTART) @@ -304,18 +333,78 @@ class ProjectCommandsSpec extends Specification { complete(commandLine, 's2-create-persistent-token --format ').containsAll(['json', 'yaml']) } + void 'a project of a multi-project build runs the wrapper at the root of the build'() { + given: 'the project in a subdirectory of a build whose wrapper and settings are at its root' + File project = moveProjectInto('app') + new File(dir, 'settings.gradle').text = "include 'app'\n" + writeManifest(new File(project, 'build/grails/commands.json'), URL_MAPPINGS_REPORT) + + when: + int exitCode = ForgeCli.execute(context, 'url-mappings-report') + + then: + exitCode == 0 + gradleInvocations() == [['-p', project.path, '-q', '--console=plain', 'runCommand', '-Pargs=url-mappings-report']] + } + + void 'a wrapper above the root of the build is never run'() { + given: 'a project with a settings file of its own but no wrapper, inside a directory that has one' + File project = moveProjectInto('app') + new File(project, 'settings.gradle').text = '' + writeManifest(new File(project, 'build/grails/commands.json'), URL_MAPPINGS_REPORT) + + when: + CommandLine commandLine = ForgeCli.createCommandLine(context, false) + + then: + !commandLine.subcommands.containsKey('url-mappings-report') + output.toString().contains('The build of the project has no Gradle wrapper, so its commands are not offered.') + gradleInvocations() == [] + } + + void 'a wrapper above a project without a settings file is never run'() { + given: + File project = moveProjectInto('app') + buildWrites(URL_MAPPINGS_REPORT) + + when: + CommandLine commandLine = ForgeCli.createCommandLine(context, false) + + then: + !commandLine.subcommands.containsKey('url-mappings-report') + gradleInvocations() == [] + !new File(project, 'build/grails/commands.json').exists() + } + + /** + * Moves the project into a subdirectory of the directory that keeps the Gradle wrapper, and makes + * it the current directory. + */ + private File moveProjectInto(String name) { + File project = new File(dir, name) + assert project.mkdirs() + assert new File(dir, 'grails-app').renameTo(new File(project, 'grails-app')) + assert new File(dir, 'build.gradle').renameTo(new File(project, 'build.gradle')) + System.setProperty('user.dir', project.path) + project + } + private void buildWrites(Map... commands) { new File(dir, 'manifest-source.json').text = JsonOutput.toJson([version: 1, commands: commands.toList()]) } private void writeManifest(Map... commands) { - manifest.parentFile.mkdirs() - manifest.text = JsonOutput.toJson([version: 1, commands: commands.toList()]) - manifest.setLastModified(System.currentTimeMillis() + 10_000) + writeManifest(manifest, commands) + } + + private static void writeManifest(File file, Map... commands) { + file.parentFile.mkdirs() + file.text = JsonOutput.toJson([version: 1, commands: commands.toList()]) + file.setLastModified(System.currentTimeMillis() + 10_000) } - private List> gradleInvocations() { - File log = new File(dir, 'gradle-invocations.txt') + private List> gradleInvocations(File wrapperDir = dir) { + File log = new File(wrapperDir, 'gradle-invocations.txt') if (!log.isFile()) { return [] } @@ -332,8 +421,8 @@ class ProjectCommandsSpec extends Specification { invocations } - private void writeGradleWrapper() { - File wrapper = new File(dir, 'gradlew') + private void writeGradleWrapper(File wrapperDir = dir) { + File wrapper = new File(wrapperDir, 'gradlew') wrapper.text = '''\ #!/bin/sh dir=$(cd "$(dirname "$0")" && pwd) From dddcec509a13097a6b8322b10885b8ae57515ea4 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sat, 14 Nov 2026 09:47:49 -0800 Subject: [PATCH 029/115] Run the events of a save through another in-memory datastore The in-memory datastore of unit tests builds a datastore for each other connection, and for each tenant with a database or a schema of its own, but registered no event listeners for them. Their sessions publish their events with that datastore as the source, which only its own listeners handle, so a save through one, such as book.secondary.save(), ran no domain class events and set no timestamps. Each now registers its own. --- .../mapping/simple/SimpleMapDatastore.java | 7 + .../SimpleMapConnectionEventsSpec.groovy | 137 ++++++++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 grails-data-simple/src/test/groovy/org/grails/datastore/mapping/simple/SimpleMapConnectionEventsSpec.groovy diff --git a/grails-data-simple/src/main/groovy/org/grails/datastore/mapping/simple/SimpleMapDatastore.java b/grails-data-simple/src/main/groovy/org/grails/datastore/mapping/simple/SimpleMapDatastore.java index 08c4ef2c4b8..647dfa56f91 100644 --- a/grails-data-simple/src/main/groovy/org/grails/datastore/mapping/simple/SimpleMapDatastore.java +++ b/grails-data-simple/src/main/groovy/org/grails/datastore/mapping/simple/SimpleMapDatastore.java @@ -123,6 +123,10 @@ public SimpleMapDatastore(ConnectionSources, ConnectionSourceSe childDatastore = new SimpleMapDatastore(singletonConnectionSources, mappingContext, eventPublisher) { @Override protected GormEnhancer initialize(ConnectionSourceSettings settings) { + // The parent's enhancer serves this connection's entities, but the sessions of this + // connection publish their events with this datastore as the source, and only + // listeners of its own handle those. + registerEventListeners(eventPublisher); return null; } @@ -466,6 +470,9 @@ public void addTenantForSchema(String schemaName) { childDatastore = new SimpleMapDatastore(singletonConnectionSources, mappingContext, eventPublisher) { @Override protected GormEnhancer initialize(ConnectionSourceSettings settings) { + // As for a connection's datastore, the schema's sessions publish their events with this + // datastore as the source. + registerEventListeners(eventPublisher); return null; } }; diff --git a/grails-data-simple/src/test/groovy/org/grails/datastore/mapping/simple/SimpleMapConnectionEventsSpec.groovy b/grails-data-simple/src/test/groovy/org/grails/datastore/mapping/simple/SimpleMapConnectionEventsSpec.groovy new file mode 100644 index 00000000000..b72d2404dbf --- /dev/null +++ b/grails-data-simple/src/test/groovy/org/grails/datastore/mapping/simple/SimpleMapConnectionEventsSpec.groovy @@ -0,0 +1,137 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.datastore.mapping.simple + +import spock.lang.AutoCleanup +import spock.lang.Specification +import spock.util.environment.RestoreSystemProperties + +import grails.gorm.MultiTenant +import grails.gorm.annotation.Entity +import grails.gorm.multitenancy.Tenants +import org.grails.datastore.mapping.config.Settings +import org.grails.datastore.mapping.core.DatastoreUtils +import org.grails.datastore.mapping.core.connections.ConnectionSource +import org.grails.datastore.mapping.multitenancy.MultiTenancySettings +import org.grails.datastore.mapping.multitenancy.resolvers.SystemPropertyTenantResolver + +/** + * A save through the datastore of another connection, of a tenant's database or of a schema added at runtime runs + * the domain class events and sets the timestamps, as a save through the default connection does. + */ +@RestoreSystemProperties +class SimpleMapConnectionEventsSpec extends Specification { + + @AutoCleanup + SimpleMapDatastore datastore + + void 'a save through another connection runs the events and sets the timestamps'() { + given: + datastore = new SimpleMapDatastore(['secondary'], ConnectionNote) + ConnectionNote note = new ConnectionNote(text: 'draft') + + when: + ConnectionNote.secondary.withTransaction { + note.secondary.save(flush: true) + } + + then: + ConnectionNote.secondary.withTransaction { ConnectionNote.secondary.count() } == 1 + note.text == 'DRAFT' + note.dateCreated != null + note.lastUpdated != null + } + + void 'a save for a tenant with a database of its own runs the events and sets the timestamps'() { + given: + datastore = new SimpleMapDatastore( + DatastoreUtils.createPropertyResolver([ + (Settings.SETTING_MULTI_TENANCY_MODE) : MultiTenancySettings.MultiTenancyMode.DATABASE, + (Settings.SETTING_MULTI_TENANT_RESOLVER): new SystemPropertyTenantResolver() + ]), + [ConnectionSource.DEFAULT, 'foo'], + TenantEventNote + ) + TenantEventNote note = new TenantEventNote(text: 'draft') + + when: + Tenants.withId('foo') { + note.save(flush: true) + } + + then: + Tenants.withId('foo') { TenantEventNote.count() } == 1 + note.text == 'DRAFT' + note.dateCreated != null + note.lastUpdated != null + } + + void 'a save for a tenant with a schema added at runtime runs the events and sets the timestamps'() { + given: + datastore = new SimpleMapDatastore( + DatastoreUtils.createPropertyResolver([ + (Settings.SETTING_MULTI_TENANCY_MODE) : MultiTenancySettings.MultiTenancyMode.SCHEMA, + (Settings.SETTING_MULTI_TENANT_RESOLVER): new SystemPropertyTenantResolver() + ]), + [ConnectionSource.DEFAULT], + TenantEventNote + ) + datastore.addTenantForSchema('foo') + TenantEventNote note = new TenantEventNote(text: 'draft') + + when: + Tenants.withId('foo') { + note.save(flush: true) + } + + then: + Tenants.withId('foo') { TenantEventNote.count() } == 1 + note.text == 'DRAFT' + note.dateCreated != null + note.lastUpdated != null + } +} + +@Entity +class ConnectionNote { + + String text + Date dateCreated + Date lastUpdated + + def beforeInsert() { + text = text.toUpperCase() + } + + static mapping = { + datasource 'ALL' + } +} + +@Entity +class TenantEventNote implements MultiTenant { + + String text + Date dateCreated + Date lastUpdated + + def beforeInsert() { + text = text.toUpperCase() + } +} From 2c3fc8a97367ee2b26121ff077b4ce64bb0c8d42 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sat, 14 Nov 2026 09:48:06 -0800 Subject: [PATCH 030/115] Name the tenant in withTenant without resolving the current one MultiTenant.withTenant(tenantId) and withTenant(tenantId) { } looked up the class's API through findStaticApi, which resolves the current tenant first. With a database or a schema per tenant and no current tenant, that threw TenantNotFoundException although the call names the tenant. They now start from the class's own API, as eachTenant already did. --- .../database/DatabasePerTenantSpec.groovy | 17 +++++++++++++++++ .../main/groovy/grails/gorm/MultiTenant.groovy | 7 +++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/grails-datamapping-core-test/src/test/groovy/grails/gorm/services/multitenancy/database/DatabasePerTenantSpec.groovy b/grails-datamapping-core-test/src/test/groovy/grails/gorm/services/multitenancy/database/DatabasePerTenantSpec.groovy index cf8ebfd3e94..3da1d1ac41c 100644 --- a/grails-datamapping-core-test/src/test/groovy/grails/gorm/services/multitenancy/database/DatabasePerTenantSpec.groovy +++ b/grails-datamapping-core-test/src/test/groovy/grails/gorm/services/multitenancy/database/DatabasePerTenantSpec.groovy @@ -23,6 +23,7 @@ import spock.lang.Shared import spock.lang.Specification import spock.util.environment.RestoreSystemProperties +import grails.gorm.multitenancy.Tenants import org.grails.datastore.mapping.config.Settings import org.grails.datastore.mapping.core.DatastoreUtils import org.grails.datastore.mapping.core.connections.ConnectionSource @@ -81,6 +82,22 @@ class DatabasePerTenantSpec extends Specification { bookService.countBooks() == 2 bookDataService.countBooks() == 2 } + + void 'withTenant reaches the named tenant when no tenant is current'() { + given: 'a book in the foo database and none in bar' + Tenants.withId('foo') { + new Book(title: 'The Stand').save(flush: true) + } + + and: 'no current tenant' + System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, '') + + expect: + Book.withTenant('foo') { Book.count() } == 1 + Book.withTenant('bar') { Book.count() } == 0 + Book.withTenant('foo').count() == 1 + Book.withTenant('bar').count() == 0 + } } diff --git a/grails-datamapping-core/src/main/groovy/grails/gorm/MultiTenant.groovy b/grails-datamapping-core/src/main/groovy/grails/gorm/MultiTenant.groovy index 195b0ba33f5..d0fe5f412b3 100644 --- a/grails-datamapping-core/src/main/groovy/grails/gorm/MultiTenant.groovy +++ b/grails-datamapping-core/src/main/groovy/grails/gorm/MultiTenant.groovy @@ -43,7 +43,9 @@ trait MultiTenant extends Entity { */ @Generated static T withTenant(Serializable tenantId, Closure callable) { - GormRegistry.instance.findStaticApi((Class) this).withTenant(tenantId, callable) + // The tenant is named here, so the lookup must not resolve the current one: findStaticApi does, and for a + // database or schema per tenant it throws TenantNotFoundException when no tenant is current. + GormRegistry.instance.getStaticApi((Class) this).withTenant(tenantId, callable) } /** @@ -68,6 +70,7 @@ trait MultiTenant extends Entity { */ @Generated static GormAllOperations withTenant(Serializable tenantId) { - (GormAllOperations) GormRegistry.instance.findStaticApi((Class) this).withTenant(tenantId) + // The tenant is named here too, so the current one is not resolved; see withTenant(Serializable, Closure). + (GormAllOperations) GormRegistry.instance.getStaticApi((Class) this).withTenant(tenantId) } } From 7ccb109e2cb036129c6018633959daff2d9652d0 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sat, 14 Nov 2026 09:48:06 -0800 Subject: [PATCH 031/115] Look up the auto-timestamp listener of a domain class from the class Every datastore has an AutoTimestampEventListener of its own, and so do every connection of a datastore with several and every tenant with a database or a schema of its own. A listener only handles the saves made through its own datastore, so with several of them the autoTimestampEventListener bean, which is the default connection's, cannot turn off the timestamps of a class saved through another one. Car.autoTimestampEventListener now returns the listener of the connection that a save of Car made from the same place goes to: the class's default connection, the connection of a running Car.secondary.withTransaction { } block or @Transactional(connection = ...) method, or the current tenant's for a multi-tenant class. Car.secondary.autoTimestampEventListener names another connection. GormStaticApi.getAutoTimestampEventListener() does the lookup and throws IllegalStateException for a datastore without such a listener. Datastores expose their listener through the new AutoTimestampingDatastore interface, which the Hibernate 7, Hibernate 5, MongoDB, Neo4j and in-memory datastores implement. The Hibernate guides' withoutLastUpdated(Foo, Bar) { } example called an overload that does not exist and now passes a list. The upgrade guide notes that unit tests now run the events of saves through other connections and tenants. Fixes #14509 --- .../src/main/docs/stepByStep.adoc | 2 + .../hibernate/AbstractHibernateDatastore.java | 4 +- .../eventsAutoTimestamping.adoc | 30 ++- .../orm/hibernate/HibernateDatastore.java | 5 +- ...utoTimestampEventListenerTenantSpec.groovy | 159 +++++++++++++++ .../eventsAutoTimestamping.adoc | 30 ++- .../mapping/mongo/MongoDatastore.java | 4 +- .../datastore/gorm/neo4j/Neo4jDatastore.java | 4 +- .../mapping/simple/SimpleMapDatastore.java | 12 +- .../SimpleMapAutoTimestampListenerSpec.groovy | 192 ++++++++++++++++++ .../gorm/CustomAutoTimestampSpec.groovy | 6 +- .../grails/datastore/gorm/GormEntity.groovy | 28 +++ .../datastore/gorm/GormStaticApi.groovy | 23 +++ .../events/AutoTimestampingDatastore.java | 38 ++++ .../datastore/gorm/GormStaticApiSpec.groovy | 42 ++++ .../AllConnectionsTimestampNote.groovy | 35 ++++ .../DefaultConnectionTimestampNote.groovy | 31 +++ .../SecondaryConnectionTimestampNote.groovy | 35 ++++ .../testing/tck/domains/TimestampNote.groovy | 31 +++ ...utoTimestampEventListenerLookupSpec.groovy | 58 ++++++ ...ampEventListenerMultiDataSourceSpec.groovy | 114 +++++++++++ .../src/en/guide/introduction/whatsNew.adoc | 17 ++ .../src/en/guide/upgrading/upgrading90x.adoc | 8 + 23 files changed, 892 insertions(+), 16 deletions(-) create mode 100644 grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/connections/AutoTimestampEventListenerTenantSpec.groovy create mode 100644 grails-data-simple/src/test/groovy/org/grails/datastore/mapping/simple/SimpleMapAutoTimestampListenerSpec.groovy create mode 100644 grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/events/AutoTimestampingDatastore.java create mode 100644 grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/domains/AllConnectionsTimestampNote.groovy create mode 100644 grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/domains/DefaultConnectionTimestampNote.groovy create mode 100644 grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/domains/SecondaryConnectionTimestampNote.groovy create mode 100644 grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/domains/TimestampNote.groovy create mode 100644 grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/tests/AutoTimestampEventListenerLookupSpec.groovy create mode 100644 grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/tests/AutoTimestampEventListenerMultiDataSourceSpec.groovy diff --git a/grails-data-docs/guide-developer/src/main/docs/stepByStep.adoc b/grails-data-docs/guide-developer/src/main/docs/stepByStep.adoc index 38902dc4831..3f0d77cddc3 100644 --- a/grails-data-docs/guide-developer/src/main/docs/stepByStep.adoc +++ b/grails-data-docs/guide-developer/src/main/docs/stepByStep.adoc @@ -142,6 +142,8 @@ class Setup { } ---- +A datastore that sets timestamps keeps the `AutoTimestampEventListener` it registers for itself, implements `AutoTimestampingDatastore` and returns that listener from `getAutoTimestampEventListener()`, so that `Book.autoTimestampEventListener` on a domain class finds it. The TCK's `AutoTimestampEventListenerLookupSpec` checks this. + Then in `src/test/groovy` create test suite class to allow running tests in IDE (without this you won't be able to run TCK tests from the IDE). Example test suite: [source,groovy] diff --git a/grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/AbstractHibernateDatastore.java b/grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/AbstractHibernateDatastore.java index b50cacb41ef..8a9d2b3bc41 100644 --- a/grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/AbstractHibernateDatastore.java +++ b/grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/AbstractHibernateDatastore.java @@ -49,6 +49,7 @@ import grails.gorm.multitenancy.Tenants; import org.grails.datastore.gorm.events.AutoTimestampEventListener; +import org.grails.datastore.gorm.events.AutoTimestampingDatastore; import org.grails.datastore.gorm.jdbc.schema.DefaultSchemaHandler; import org.grails.datastore.gorm.jdbc.schema.SchemaHandler; import org.grails.datastore.gorm.validation.registry.support.ValidatorRegistries; @@ -84,7 +85,7 @@ * @author Graeme Rocher * @since 2.0 */ -public abstract class AbstractHibernateDatastore extends AbstractDatastore implements ApplicationContextAware, Settings, SchemaMultiTenantCapableDatastore, TransactionCapableDatastore, Closeable, MessageSourceAware, MultipleConnectionSourceCapableDatastore { +public abstract class AbstractHibernateDatastore extends AbstractDatastore implements ApplicationContextAware, Settings, SchemaMultiTenantCapableDatastore, TransactionCapableDatastore, Closeable, MessageSourceAware, MultipleConnectionSourceCapableDatastore, AutoTimestampingDatastore { public static final String CONFIG_PROPERTY_CACHE_QUERIES = "grails.hibernate.cache.queries"; public static final String CONFIG_PROPERTY_OSIV_READONLY = "grails.hibernate.osiv.readonly"; @@ -301,6 +302,7 @@ public AbstractHibernateEventListener getEventTriggeringInterceptor() { /** * @return The event listener that populates lastUpdated and dateCreated */ + @Override public AutoTimestampEventListener getAutoTimestampEventListener() { return autoTimestampEventListener; } diff --git a/grails-data-hibernate5/docs/src/docs/asciidoc/advancedGORMFeatures/eventsAutoTimestamping.adoc b/grails-data-hibernate5/docs/src/docs/asciidoc/advancedGORMFeatures/eventsAutoTimestamping.adoc index aa77c905a0f..376e270d441 100644 --- a/grails-data-hibernate5/docs/src/docs/asciidoc/advancedGORMFeatures/eventsAutoTimestamping.adoc +++ b/grails-data-hibernate5/docs/src/docs/asciidoc/advancedGORMFeatures/eventsAutoTimestamping.adoc @@ -381,21 +381,45 @@ WARNING: If you have `nullable: false` constraints on either `dateCreated` or `l It is also possible to disable the automatic timestamping temporarily. This is most typically done in the case of a test where you need to define values for the `dateCreated` or `lastUpdated` in the past. It may also be useful for importing old data from other systems where you would like to keep the current values of the timestamps. -Timestamps can be temporarily disabled for all domains, a specified list of domains, or a single domain. To get started, you need to get a reference to the `AutoTimestampEventListener`. If you already have access to the datastore, you can execute the `getAutoTimestampEventListener` method. If you don't have access to the datastore, inject the `autoTimestampEventListener` bean. +Timestamps can be temporarily disabled for all domains, a specified list of domains, or a single domain. To get started, you need a reference to the `AutoTimestampEventListener` that handles the domain class, which the domain class answers with: + +[source,groovy] +---- +Foo.autoTimestampEventListener.withoutTimestamps { + new Foo(dateCreated: new Date() - 2, lastUpdated: new Date() - 1).save(flush: true) +} +---- + +Each datastore has a listener of its own, and so does each data source of an application with several and each tenant with a database or schema of its own. A listener only handles the saves made through its own datastore, so turning timestamps off on one leaves the saves to another data source timestamped. `Foo.autoTimestampEventListener` is the listener of the data source that a save of `Foo` made from the same place goes to: its default data source, the one a `Foo.secondary.withTransaction { }` block or a method annotated `@Transactional(connection = 'secondary')` routes it to, or the current tenant's for a multi-tenant class. For another data source of a class mapped to several, name the data source, as in `Foo.secondary.autoTimestampEventListener`. + +[source,groovy] +---- +// Car is mapped to the default data source and Person to the 'people' data source +Car.autoTimestampEventListener.withoutTimestamps { + car.save(flush: true) +} +Person.autoTimestampEventListener.withoutTimestamps { + person.save(flush: true) +} +---- + +The `autoTimestampEventListener` bean, and the `getAutoTimestampEventListener` method of the datastore bean, return the listener of the default data source. Once you have a reference to the event listener, you can execute `withoutDateCreated`, `withoutLastUpdated`, or `withoutTimestamps`. The `withoutTimestamps` method will temporarily disable both `dateCreated` and `lastUpdated`. -Example: +Example, where `Foo`, `Bar` and `FooBar` share a data source: [source,groovy] ---- +def autoTimestampEventListener = Foo.autoTimestampEventListener + //Only the dateCreated property handling will be disabled for only the Foo domain autoTimestampEventListener.withoutDateCreated(Foo) { new Foo(dateCreated: new Date() - 1).save(flush: true) } //Only the lastUpdated property handling will be disabled for only the Foo and Bar domains -autoTimestampEventListener.withoutLastUpdated(Foo, Bar) { +autoTimestampEventListener.withoutLastUpdated([Foo, Bar]) { new Foo(lastUpdated: new Date() - 1, bar: new Bar(lastUpdated: new Date() + 1)).save(flush: true) } diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateDatastore.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateDatastore.java index 71345e0f034..86da125485a 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateDatastore.java +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateDatastore.java @@ -79,6 +79,7 @@ import grails.gorm.multitenancy.Tenants; import org.grails.datastore.gorm.GormRegistry; import org.grails.datastore.gorm.events.AutoTimestampEventListener; +import org.grails.datastore.gorm.events.AutoTimestampingDatastore; import org.grails.datastore.gorm.events.ConfigurableApplicationContextEventPublisher; import org.grails.datastore.gorm.events.ConfigurableApplicationEventPublisher; import org.grails.datastore.gorm.events.DefaultApplicationEventPublisher; @@ -151,7 +152,8 @@ public class HibernateDatastore extends AbstractDatastore TransactionCapableDatastore, Closeable, MessageSourceAware, - MultipleConnectionSourceCapableDatastore { + MultipleConnectionSourceCapableDatastore, + AutoTimestampingDatastore { private static final Logger LOG = LoggerFactory.getLogger(HibernateDatastore.class); /** @@ -1013,6 +1015,7 @@ public HibernateEventListener getEventTriggeringInterceptor() { return eventTriggeringInterceptor; } + @Override public AutoTimestampEventListener getAutoTimestampEventListener() { return autoTimestampEventListener; } diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/connections/AutoTimestampEventListenerTenantSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/connections/AutoTimestampEventListenerTenantSpec.groovy new file mode 100644 index 00000000000..5a1a09e5254 --- /dev/null +++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/connections/AutoTimestampEventListenerTenantSpec.groovy @@ -0,0 +1,159 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.orm.hibernate.connections + +import org.hibernate.dialect.H2Dialect +import spock.lang.AutoCleanup +import spock.lang.Issue +import spock.lang.Specification +import spock.util.environment.RestoreSystemProperties + +import grails.gorm.MultiTenant +import grails.gorm.annotation.Entity +import grails.gorm.multitenancy.Tenants +import org.grails.datastore.gorm.events.AutoTimestampEventListener +import org.grails.datastore.mapping.core.DatastoreUtils +import org.grails.datastore.mapping.multitenancy.AllTenantsResolver +import org.grails.datastore.mapping.multitenancy.resolvers.SystemPropertyTenantResolver +import org.grails.orm.hibernate.HibernateDatastore + +/** + * A tenant with a database or a schema of its own has an auto-timestamp listener of its own, and a multi-tenant + * domain class answers with the current tenant's. + */ +@Issue('https://github.com/apache/grails-core/issues/14509') +@RestoreSystemProperties +class AutoTimestampEventListenerTenantSpec extends Specification { + + private static final Date PAST = new Date(110_000_000_000L) + + @AutoCleanup + HibernateDatastore datastore + + void setup() { + System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, '') + } + + void 'a tenant with a database of its own answers with the listener of that database'() { + given: + datastore = databasePerTenant() + AutoTimestampEventListener books = listenerOf('books') + AutoTimestampEventListener moreBooks = listenerOf('moreBooks') + + expect: + !books.is(moreBooks) + !books.is(datastore.autoTimestampEventListener) + Tenants.withId('books') { TenantTimestampNote.autoTimestampEventListener }.is(books) + Tenants.withId('moreBooks') { TenantTimestampNote.autoTimestampEventListener }.is(moreBooks) + TenantTimestampNote.withTenant('moreBooks').autoTimestampEventListener.is(moreBooks) + + when: 'the current tenant comes from the resolver' + System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, 'moreBooks') + + then: + TenantTimestampNote.autoTimestampEventListener.is(moreBooks) + } + + void 'the listener of the current tenant turns off the timestamps of its saves, and another tenant\'s does not'() { + given: + datastore = databasePerTenant() + System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, 'books') + TenantTimestampNote kept = new TenantTimestampNote(text: 'kept', dateCreated: PAST, lastUpdated: PAST) + TenantTimestampNote stamped = new TenantTimestampNote(text: 'stamped', dateCreated: PAST, lastUpdated: PAST) + + when: + TenantTimestampNote.withTransaction { + TenantTimestampNote.autoTimestampEventListener.withoutTimestamps { + kept.save(flush: true) + } + listenerOf('moreBooks').withoutTimestamps { + stamped.save(flush: true) + } + } + + then: + TenantTimestampNote.withNewTransaction { TenantTimestampNote.get(kept.id).dateCreated } == PAST + TenantTimestampNote.withNewTransaction { TenantTimestampNote.get(kept.id).lastUpdated } == PAST + TenantTimestampNote.withNewTransaction { TenantTimestampNote.get(stamped.id).dateCreated } != PAST + TenantTimestampNote.withNewTransaction { TenantTimestampNote.get(stamped.id).lastUpdated } != PAST + } + + void 'a tenant with a schema of its own answers with the listener of that schema'() { + given: + datastore = new HibernateDatastore(DatastoreUtils.createPropertyResolver([ + 'grails.gorm.multiTenancy.mode' : 'SCHEMA', + 'grails.gorm.multiTenancy.tenantResolverClass': TwoSchemaResolver, + 'dataSource.url' : 'jdbc:h2:mem:timestampSchemaDB;LOCK_TIMEOUT=10000', + 'dataSource.dbCreate' : 'create-drop', + 'dataSource.dialect' : H2Dialect.name, + 'hibernate.hbm2ddl.auto' : 'create' + ]), TenantTimestampNote) + AutoTimestampEventListener books = listenerOf('books') + AutoTimestampEventListener moreBooks = listenerOf('moreBooks') + TenantTimestampNote kept = new TenantTimestampNote(text: 'kept', dateCreated: PAST, lastUpdated: PAST) + + when: + Tenants.withId('books') { + TenantTimestampNote.withTransaction { + TenantTimestampNote.autoTimestampEventListener.withoutTimestamps { + kept.save(flush: true) + } + } + } + + then: + !books.is(moreBooks) + Tenants.withId('books') { TenantTimestampNote.autoTimestampEventListener }.is(books) + TenantTimestampNote.withTenant('moreBooks').autoTimestampEventListener.is(moreBooks) + Tenants.withId('books') { TenantTimestampNote.withNewTransaction { TenantTimestampNote.get(kept.id).dateCreated } } == PAST + } + + private HibernateDatastore databasePerTenant() { + new HibernateDatastore(DatastoreUtils.createPropertyResolver([ + 'grails.gorm.multiTenancy.mode' : 'DATABASE', + 'grails.gorm.multiTenancy.tenantResolverClass': SystemPropertyTenantResolver, + 'dataSource.url' : 'jdbc:h2:mem:timestampDefaultDB;LOCK_TIMEOUT=10000', + 'dataSource.dbCreate' : 'create-drop', + 'dataSource.dialect' : H2Dialect.name, + 'hibernate.hbm2ddl.auto' : 'create', + 'dataSources.books' : [url: 'jdbc:h2:mem:timestampBooksDB;LOCK_TIMEOUT=10000'], + 'dataSources.moreBooks' : [url: 'jdbc:h2:mem:timestampMoreBooksDB;LOCK_TIMEOUT=10000'] + ]), TenantTimestampNote) + } + + private AutoTimestampEventListener listenerOf(String connectionName) { + datastore.getDatastoreForConnection(connectionName).autoTimestampEventListener + } + + static class TwoSchemaResolver extends SystemPropertyTenantResolver implements AllTenantsResolver { + + @Override + Iterable resolveTenantIds() { + ['books', 'moreBooks'] + } + } +} + +@Entity +class TenantTimestampNote implements MultiTenant { + + String text + Date dateCreated + Date lastUpdated +} diff --git a/grails-data-hibernate7/docs/src/docs/asciidoc/advancedGORMFeatures/eventsAutoTimestamping.adoc b/grails-data-hibernate7/docs/src/docs/asciidoc/advancedGORMFeatures/eventsAutoTimestamping.adoc index df5a9c418c2..dfa0b282015 100644 --- a/grails-data-hibernate7/docs/src/docs/asciidoc/advancedGORMFeatures/eventsAutoTimestamping.adoc +++ b/grails-data-hibernate7/docs/src/docs/asciidoc/advancedGORMFeatures/eventsAutoTimestamping.adoc @@ -387,21 +387,45 @@ WARNING: If you have `nullable: false` constraints on either `dateCreated` or `l It is also possible to disable the automatic timestamping temporarily. This is most typically done in the case of a test where you need to define values for the `dateCreated` or `lastUpdated` in the past. It may also be useful for importing old data from other systems where you would like to keep the current values of the timestamps. -Timestamps can be temporarily disabled for all domains, a specified list of domains, or a single domain. To get started, you need to get a reference to the `AutoTimestampEventListener`. If you already have access to the datastore, you can execute the `getAutoTimestampEventListener` method. If you don't have access to the datastore, inject the `autoTimestampEventListener` bean. +Timestamps can be temporarily disabled for all domains, a specified list of domains, or a single domain. To get started, you need a reference to the `AutoTimestampEventListener` that handles the domain class, which the domain class answers with: + +[source,groovy] +---- +Foo.autoTimestampEventListener.withoutTimestamps { + new Foo(dateCreated: new Date() - 2, lastUpdated: new Date() - 1).save(flush: true) +} +---- + +Each datastore has a listener of its own, and so does each data source of an application with several and each tenant with a database or schema of its own. A listener only handles the saves made through its own datastore, so turning timestamps off on one leaves the saves to another data source timestamped. `Foo.autoTimestampEventListener` is the listener of the data source that a save of `Foo` made from the same place goes to: its default data source, the one a `Foo.secondary.withTransaction { }` block or a method annotated `@Transactional(connection = 'secondary')` routes it to, or the current tenant's for a multi-tenant class. For another data source of a class mapped to several, name the data source, as in `Foo.secondary.autoTimestampEventListener`. + +[source,groovy] +---- +// Car is mapped to the default data source and Person to the 'people' data source +Car.autoTimestampEventListener.withoutTimestamps { + car.save(flush: true) +} +Person.autoTimestampEventListener.withoutTimestamps { + person.save(flush: true) +} +---- + +The `autoTimestampEventListener` bean, and the `getAutoTimestampEventListener` method of the datastore bean, return the listener of the default data source. Once you have a reference to the event listener, you can execute `withoutDateCreated`, `withoutLastUpdated`, or `withoutTimestamps`. The `withoutTimestamps` method will temporarily disable both `dateCreated` and `lastUpdated`. -Example: +Example, where `Foo`, `Bar` and `FooBar` share a data source: [source,groovy] ---- +def autoTimestampEventListener = Foo.autoTimestampEventListener + //Only the dateCreated property handling will be disabled for only the Foo domain autoTimestampEventListener.withoutDateCreated(Foo) { new Foo(dateCreated: new Date() - 1).save(flush: true) } //Only the lastUpdated property handling will be disabled for only the Foo and Bar domains -autoTimestampEventListener.withoutLastUpdated(Foo, Bar) { +autoTimestampEventListener.withoutLastUpdated([Foo, Bar]) { new Foo(lastUpdated: new Date() - 1, bar: new Bar(lastUpdated: new Date() + 1)).save(flush: true) } diff --git a/grails-data-mongodb/core/src/main/groovy/org/grails/datastore/mapping/mongo/MongoDatastore.java b/grails-data-mongodb/core/src/main/groovy/org/grails/datastore/mapping/mongo/MongoDatastore.java index 6136b9292f6..14e8db781fe 100644 --- a/grails-data-mongodb/core/src/main/groovy/org/grails/datastore/mapping/mongo/MongoDatastore.java +++ b/grails-data-mongodb/core/src/main/groovy/org/grails/datastore/mapping/mongo/MongoDatastore.java @@ -86,6 +86,7 @@ import org.grails.datastore.gorm.GormInstanceApi; import org.grails.datastore.gorm.GormValidationApi; import org.grails.datastore.gorm.events.AutoTimestampEventListener; +import org.grails.datastore.gorm.events.AutoTimestampingDatastore; import org.grails.datastore.gorm.events.ConfigurableApplicationEventPublisher; import org.grails.datastore.gorm.events.DefaultApplicationEventPublisher; import org.grails.datastore.gorm.events.DomainEventListener; @@ -147,7 +148,7 @@ * @author Graeme Rocher * @since 1.0 */ -public class MongoDatastore extends AbstractDatastore implements MappingContext.Listener, Closeable, StatelessDatastore, MultipleConnectionSourceCapableDatastore, MultiTenantCapableDatastore, TransactionCapableDatastore, SmartLifecycle { +public class MongoDatastore extends AbstractDatastore implements MappingContext.Listener, Closeable, StatelessDatastore, MultipleConnectionSourceCapableDatastore, MultiTenantCapableDatastore, TransactionCapableDatastore, SmartLifecycle, AutoTimestampingDatastore { public static final String SETTING_DATABASE_NAME = MongoSettings.SETTING_DATABASE_NAME; public static final String SETTING_CONNECTION_STRING = MongoSettings.SETTING_CONNECTION_STRING; @@ -3102,6 +3103,7 @@ public Codec get(Class clazz, CodecRegistry registry) { } } + @Override public AutoTimestampEventListener getAutoTimestampEventListener() { return this.autoTimestampEventListener; } diff --git a/grails-data-neo4j/core/src/main/groovy/org/grails/datastore/gorm/neo4j/Neo4jDatastore.java b/grails-data-neo4j/core/src/main/groovy/org/grails/datastore/gorm/neo4j/Neo4jDatastore.java index 8b124f0a2d6..261cea92c89 100644 --- a/grails-data-neo4j/core/src/main/groovy/org/grails/datastore/gorm/neo4j/Neo4jDatastore.java +++ b/grails-data-neo4j/core/src/main/groovy/org/grails/datastore/gorm/neo4j/Neo4jDatastore.java @@ -47,6 +47,7 @@ import org.apache.grails.datastore.core.querytail.QueryTailSettings; import org.grails.datastore.gorm.GormEnhancer; import org.grails.datastore.gorm.events.AutoTimestampEventListener; +import org.grails.datastore.gorm.events.AutoTimestampingDatastore; import org.grails.datastore.gorm.events.ConfigurableApplicationEventPublisher; import org.grails.datastore.gorm.events.DefaultApplicationEventPublisher; import org.grails.datastore.gorm.events.DomainEventListener; @@ -97,7 +98,7 @@ * * @since 1.0 */ -public class Neo4jDatastore extends AbstractDatastore implements Closeable, StatelessDatastore, GraphDatastore, Settings, MultipleConnectionSourceCapableDatastore, MultiTenantCapableDatastore, MessageSourceAware, TransactionCapableDatastore { +public class Neo4jDatastore extends AbstractDatastore implements Closeable, StatelessDatastore, GraphDatastore, Settings, MultipleConnectionSourceCapableDatastore, MultiTenantCapableDatastore, MessageSourceAware, TransactionCapableDatastore, AutoTimestampingDatastore { private static final Logger log = LoggerFactory.getLogger(Neo4jDatastore.class); protected final Driver boltDriver; @@ -611,6 +612,7 @@ public void setMessageSource(MessageSource messageSource) { } } + @Override public AutoTimestampEventListener getAutoTimestampEventListener() { return this.autoTimestampEventListener; } diff --git a/grails-data-simple/src/main/groovy/org/grails/datastore/mapping/simple/SimpleMapDatastore.java b/grails-data-simple/src/main/groovy/org/grails/datastore/mapping/simple/SimpleMapDatastore.java index 647dfa56f91..295722acc40 100644 --- a/grails-data-simple/src/main/groovy/org/grails/datastore/mapping/simple/SimpleMapDatastore.java +++ b/grails-data-simple/src/main/groovy/org/grails/datastore/mapping/simple/SimpleMapDatastore.java @@ -40,6 +40,7 @@ import org.grails.datastore.gorm.GormStaticApi; import org.grails.datastore.gorm.GormValidationApi; import org.grails.datastore.gorm.events.AutoTimestampEventListener; +import org.grails.datastore.gorm.events.AutoTimestampingDatastore; import org.grails.datastore.gorm.events.ConfigurableApplicationContextEventPublisher; import org.grails.datastore.gorm.events.ConfigurableApplicationEventPublisher; import org.grails.datastore.gorm.events.DefaultApplicationEventPublisher; @@ -81,7 +82,7 @@ * @since 1.0 */ @SuppressWarnings("rawtypes") -public class SimpleMapDatastore extends AbstractDatastore implements Closeable, TransactionCapableDatastore, SingleSessionCapableDatastore, SchemaMultiTenantCapableDatastore, ConnectionSourceSettings>, ConnectionSourcesProvider, ConnectionSourceSettings> { +public class SimpleMapDatastore extends AbstractDatastore implements Closeable, TransactionCapableDatastore, SingleSessionCapableDatastore, SchemaMultiTenantCapableDatastore, ConnectionSourceSettings>, ConnectionSourcesProvider, ConnectionSourceSettings>, AutoTimestampingDatastore { private final Map inmemoryData; private final TenantResolver tenantResolver; protected final GormEnhancer gormEnhancer; @@ -94,6 +95,7 @@ public class SimpleMapDatastore extends AbstractDatastore implements Closeable, protected final Map datastoresByConnectionSource = new LinkedHashMap<>(); protected final boolean failOnError; private final SimpleMapQueryTail queryTail; + private AutoTimestampEventListener autoTimestampEventListener; public SimpleMapDatastore(ConnectionSources, ConnectionSourceSettings> connectionSources, MappingContext mappingContext, ConfigurableApplicationEventPublisher eventPublisher) { super(mappingContext); @@ -296,7 +298,8 @@ private SimpleMapDatastore getDatastoreForQualifier(Class cls, String qua protected void registerEventListeners(ConfigurableApplicationEventPublisher eventPublisher) { eventPublisher.addApplicationListener(new DomainEventListener(this)); - eventPublisher.addApplicationListener(new AutoTimestampEventListener(this)); + autoTimestampEventListener = new AutoTimestampEventListener(this); + eventPublisher.addApplicationListener(autoTimestampEventListener); if (multiTenancyMode == MultiTenancySettings.MultiTenancyMode.DISCRIMINATOR) { eventPublisher.addApplicationListener(new MultiTenantEventListener(this)); } @@ -316,6 +319,11 @@ public ApplicationEventPublisher getApplicationEventPublisher() { return this.eventPublisher; } + @Override + public AutoTimestampEventListener getAutoTimestampEventListener() { + return autoTimestampEventListener; + } + /** * @return the query-tail capture of this datastore's connection source, or null when capture is not enabled * @since 9.0 diff --git a/grails-data-simple/src/test/groovy/org/grails/datastore/mapping/simple/SimpleMapAutoTimestampListenerSpec.groovy b/grails-data-simple/src/test/groovy/org/grails/datastore/mapping/simple/SimpleMapAutoTimestampListenerSpec.groovy new file mode 100644 index 00000000000..3e5c9a111ac --- /dev/null +++ b/grails-data-simple/src/test/groovy/org/grails/datastore/mapping/simple/SimpleMapAutoTimestampListenerSpec.groovy @@ -0,0 +1,192 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.datastore.mapping.simple + +import spock.lang.AutoCleanup +import spock.lang.Specification +import spock.util.environment.RestoreSystemProperties + +import grails.gorm.MultiTenant +import grails.gorm.annotation.Entity +import grails.gorm.multitenancy.Tenants +import grails.gorm.transactions.Transactional +import org.grails.datastore.gorm.events.AutoTimestampEventListener +import org.grails.datastore.mapping.config.Settings +import org.grails.datastore.mapping.core.DatastoreUtils +import org.grails.datastore.mapping.core.connections.ConnectionSource +import org.grails.datastore.mapping.multitenancy.MultiTenancySettings +import org.grails.datastore.mapping.multitenancy.resolvers.SystemPropertyTenantResolver + +/** + * The listener a domain class answers with is the one of the datastore its saves go through. The in-memory + * datastore keeps the unqualified operations of every domain class on the datastore of the default connection, and + * an explicit connection, or a tenant's database or schema, on its own. + */ +@RestoreSystemProperties +class SimpleMapAutoTimestampListenerSpec extends Specification { + + private static final Date PAST = new Date(110_000_000_000L) + + @AutoCleanup + SimpleMapDatastore datastore + + void 'a domain class answers with the listener of the datastore that saves it'() { + given: + datastore = new SimpleMapDatastore(['secondary'], ListenerNote, SecondaryListenerNote) + SimpleMapDatastore secondary = (SimpleMapDatastore) datastore.getDatastoreForConnection('secondary') + + expect: 'unqualified operations stay on the default connection, whatever the class is mapped to' + ListenerNote.autoTimestampEventListener.is(datastore.autoTimestampEventListener) + SecondaryListenerNote.autoTimestampEventListener.is(datastore.autoTimestampEventListener) + + and: 'an explicit connection has a listener of its own' + ListenerNote.secondary.autoTimestampEventListener.is(secondary.autoTimestampEventListener) + SecondaryListenerNote.secondary.autoTimestampEventListener.is(secondary.autoTimestampEventListener) + !secondary.autoTimestampEventListener.is(datastore.autoTimestampEventListener) + } + + void 'a transaction for a connection routes the unqualified lookup to that connection'() { + given: + datastore = new SimpleMapDatastore(['secondary'], ListenerNote) + SimpleMapDatastore secondary = (SimpleMapDatastore) datastore.getDatastoreForConnection('secondary') + + expect: + ListenerNote.secondary.withTransaction { ListenerNote.autoTimestampEventListener }.is(secondary.autoTimestampEventListener) + new SecondaryListenerLookup().lookUp().is(secondary.autoTimestampEventListener) + ListenerNote.autoTimestampEventListener.is(datastore.autoTimestampEventListener) + } + + void 'only the listener of the connection that saves an instance turns its timestamps off'() { + given: + datastore = new SimpleMapDatastore(['secondary'], ListenerNote) + def defaultListener = ListenerNote.autoTimestampEventListener + def secondaryListener = ListenerNote.secondary.autoTimestampEventListener + ListenerNote kept = new ListenerNote(text: 'kept', dateCreated: PAST, lastUpdated: PAST) + ListenerNote overwritten = new ListenerNote(text: 'overwritten', dateCreated: PAST, lastUpdated: PAST) + + when: + ListenerNote.secondary.withTransaction { + secondaryListener.withoutTimestamps { + kept.secondary.save(flush: true) + } + defaultListener.withoutTimestamps { + overwritten.secondary.save(flush: true) + } + } + + then: + kept.dateCreated == PAST + kept.lastUpdated == PAST + overwritten.dateCreated != PAST + overwritten.lastUpdated != PAST + } + + void 'a tenant with a database of its own answers with the listener of that database'() { + given: + datastore = new SimpleMapDatastore( + DatastoreUtils.createPropertyResolver([ + (Settings.SETTING_MULTI_TENANCY_MODE) : MultiTenancySettings.MultiTenancyMode.DATABASE, + (Settings.SETTING_MULTI_TENANT_RESOLVER): new SystemPropertyTenantResolver() + ]), + [ConnectionSource.DEFAULT, 'foo', 'bar'], + TenantListenerNote + ) + def foo = datastore.getDatastoreForConnection('foo').autoTimestampEventListener + def bar = datastore.getDatastoreForConnection('bar').autoTimestampEventListener + + expect: + !foo.is(bar) + Tenants.withId('foo') { TenantListenerNote.autoTimestampEventListener }.is(foo) + Tenants.withId('bar') { TenantListenerNote.autoTimestampEventListener }.is(bar) + TenantListenerNote.withTenant('bar').autoTimestampEventListener.is(bar) + + when: 'the current tenant comes from the resolver' + System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, 'foo') + + then: + TenantListenerNote.autoTimestampEventListener.is(foo) + } + + void 'a tenant with a schema added at runtime answers with the listener of that schema'() { + given: + datastore = new SimpleMapDatastore( + DatastoreUtils.createPropertyResolver([ + (Settings.SETTING_MULTI_TENANCY_MODE) : MultiTenancySettings.MultiTenancyMode.SCHEMA, + (Settings.SETTING_MULTI_TENANT_RESOLVER): new SystemPropertyTenantResolver() + ]), + [ConnectionSource.DEFAULT], + TenantListenerNote + ) + datastore.addTenantForSchema('foo') + TenantListenerNote note = new TenantListenerNote(text: 'kept', dateCreated: PAST, lastUpdated: PAST) + + when: + Tenants.withId('foo') { + TenantListenerNote.autoTimestampEventListener.withoutTimestamps { + note.save(flush: true) + } + } + + then: + Tenants.withId('foo') { TenantListenerNote.autoTimestampEventListener }.is(datastore.getDatastoreForConnection('foo').autoTimestampEventListener) + TenantListenerNote.withTenant('foo').autoTimestampEventListener.is(datastore.getDatastoreForConnection('foo').autoTimestampEventListener) + note.dateCreated == PAST + note.lastUpdated == PAST + } +} + +@Entity +class ListenerNote { + + String text + Date dateCreated + Date lastUpdated + + static mapping = { + datasource 'ALL' + } +} + +class SecondaryListenerLookup { + + @Transactional(connection = 'secondary') + AutoTimestampEventListener lookUp() { + ListenerNote.autoTimestampEventListener + } +} + +@Entity +class SecondaryListenerNote { + + String text + Date dateCreated + Date lastUpdated + + static mapping = { + datasource 'secondary' + } +} + +@Entity +class TenantListenerNote implements MultiTenant { + + String text + Date dateCreated + Date lastUpdated +} diff --git a/grails-datamapping-core-test/src/test/groovy/org/grails/datastore/gorm/CustomAutoTimestampSpec.groovy b/grails-datamapping-core-test/src/test/groovy/org/grails/datastore/gorm/CustomAutoTimestampSpec.groovy index d720cea1932..5fe21047b5d 100644 --- a/grails-datamapping-core-test/src/test/groovy/org/grails/datastore/gorm/CustomAutoTimestampSpec.groovy +++ b/grails-datamapping-core-test/src/test/groovy/org/grails/datastore/gorm/CustomAutoTimestampSpec.groovy @@ -93,8 +93,7 @@ class CustomAutoTimestampSpec extends GrailsDataTckSpec implements GormValidateable, DirtyCheckable, GormEntityApi getGormDynamicFinders() { currentGormStaticApi().gormDynamicFinders } + + /** + * Returns the listener that sets this class's timestamp and auditor properties. Each datastore has a listener of + * its own, and so does each connection and each tenant with a database or schema of its own, so the + * {@code autoTimestampEventListener} bean may not be the one that handles this class. This one is the listener of + * the connection that a {@code save()} made from the same place goes to: the class's default connection, the + * connection of a running {@code Car.secondary.withTransaction { }} block or + * {@code @Transactional(connection = 'secondary')} method, or the current tenant's for a multi-tenant class. + * + *
+     * Car.autoTimestampEventListener.withoutTimestamps {
+     *     car.save(flush: true)
+     * }
+     * 
+ * + * For another connection of a class mapped to several, ask that connection, as in + * {@code Car.secondary.autoTimestampEventListener}. + * + * @return the listener that handles this class + * @throws IllegalStateException when the class's datastore has no such listener + * @since 9.0 + */ + @Generated + static AutoTimestampEventListener getAutoTimestampEventListener() { + currentGormStaticApi().autoTimestampEventListener + } + /** * * @param callable Callable closure containing detached criteria definition diff --git a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/GormStaticApi.groovy b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/GormStaticApi.groovy index 849fafdad9e..b016f1af4e2 100644 --- a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/GormStaticApi.groovy +++ b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/GormStaticApi.groovy @@ -38,6 +38,8 @@ import grails.gorm.api.GormStaticOperations import grails.gorm.multitenancy.CurrentTenantHolder import grails.gorm.multitenancy.Tenants import grails.gorm.transactions.GrailsTransactionTemplate +import org.grails.datastore.gorm.events.AutoTimestampEventListener +import org.grails.datastore.gorm.events.AutoTimestampingDatastore import org.grails.datastore.gorm.finders.FinderMethod import org.grails.datastore.gorm.internal.RefreshLockArguments import org.grails.datastore.gorm.transactions.DefaultTransactionTemplateFactory @@ -168,6 +170,27 @@ class GormStaticApi extends AbstractGormApi implements GormAllOperations forQualifier(String qualifier) { Datastore ds = getDatastore() DatastoreResolver resolver = new DatastoreResolver() { diff --git a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/events/AutoTimestampingDatastore.java b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/events/AutoTimestampingDatastore.java new file mode 100644 index 00000000000..1ab197088f9 --- /dev/null +++ b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/events/AutoTimestampingDatastore.java @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.datastore.gorm.events; + +/** + * A datastore that sets the timestamp and auditor properties of the entities it saves through an + * {@link AutoTimestampEventListener} of its own. Every datastore has its own listener, and so does every connection + * of a datastore with several, and a listener only handles the entities saved through its own datastore. Turning + * timestamps off on one listener therefore leaves the entities saved through any other datastore or connection + * untouched. + * + * @since 9.0 + */ +public interface AutoTimestampingDatastore { + + /** + * @return the listener that sets the timestamps of the entities this datastore saves, or {@code null} when this + * datastore sets none + */ + AutoTimestampEventListener getAutoTimestampEventListener(); + +} diff --git a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/GormStaticApiSpec.groovy b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/GormStaticApiSpec.groovy index ff8f6bb2174..cad395c1fea 100644 --- a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/GormStaticApiSpec.groovy +++ b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/GormStaticApiSpec.groovy @@ -28,6 +28,7 @@ import grails.gorm.api.GormAllOperations import grails.gorm.api.GormInstanceOperations import grails.gorm.api.GormStaticOperations import grails.gorm.multitenancy.Tenants +import org.grails.datastore.gorm.events.AutoTimestampingDatastore import org.grails.datastore.gorm.events.DefaultApplicationEventPublisher import org.grails.datastore.mapping.config.Settings import org.grails.datastore.mapping.core.Datastore @@ -159,6 +160,47 @@ class GormStaticApiSpec extends Specification { api.getGormDynamicFinders() == [finder] } + void "getAutoTimestampEventListener returns the listener of the api's datastore"() { + given: + def api = new GormStaticApi(GormStaticApiThing, datastore, []) + + expect: + api.autoTimestampEventListener != null + api.autoTimestampEventListener.is(datastore.autoTimestampEventListener) + GormStaticApiThing.autoTimestampEventListener.is(datastore.autoTimestampEventListener) + } + + void "getAutoTimestampEventListener rejects a datastore that does not set timestamps through a listener"() { + given: + def plainDatastore = Stub(Datastore) { + getMappingContext() >> datastore.mappingContext + } + def api = new GormStaticApi(GormStaticApiThing, plainDatastore, []) + + when: + api.autoTimestampEventListener + + then: + IllegalStateException e = thrown() + e.message.contains(GormStaticApiThing.name) + } + + void "getAutoTimestampEventListener rejects an auto-timestamping datastore that has no listener"() { + given: + def listenerlessDatastore = Stub(Datastore, additionalInterfaces: [AutoTimestampingDatastore]) { + getMappingContext() >> datastore.mappingContext + getAutoTimestampEventListener() >> null + } + def api = new GormStaticApi(GormStaticApiThing, listenerlessDatastore, []) + + when: + api.autoTimestampEventListener + + then: + IllegalStateException e = thrown() + e.message.contains(GormStaticApiThing.name) + } + void "forQualifier creates a new static api bound to the requested qualifier"() { given: def api = new GormStaticApi(GormStaticApiThing, datastore, []) diff --git a/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/domains/AllConnectionsTimestampNote.groovy b/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/domains/AllConnectionsTimestampNote.groovy new file mode 100644 index 00000000000..86a9e74f0ba --- /dev/null +++ b/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/domains/AllConnectionsTimestampNote.groovy @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.grails.data.testing.tck.domains + +import grails.persistence.Entity + +@Entity +class AllConnectionsTimestampNote { + + Long id + Long version + String text + Date dateCreated + Date lastUpdated + + static mapping = { + datasource 'ALL' + } +} diff --git a/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/domains/DefaultConnectionTimestampNote.groovy b/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/domains/DefaultConnectionTimestampNote.groovy new file mode 100644 index 00000000000..60634a1d53a --- /dev/null +++ b/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/domains/DefaultConnectionTimestampNote.groovy @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.grails.data.testing.tck.domains + +import grails.persistence.Entity + +@Entity +class DefaultConnectionTimestampNote { + + Long id + Long version + String text + Date dateCreated + Date lastUpdated +} diff --git a/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/domains/SecondaryConnectionTimestampNote.groovy b/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/domains/SecondaryConnectionTimestampNote.groovy new file mode 100644 index 00000000000..14e86b3ea6b --- /dev/null +++ b/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/domains/SecondaryConnectionTimestampNote.groovy @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.grails.data.testing.tck.domains + +import grails.persistence.Entity + +@Entity +class SecondaryConnectionTimestampNote { + + Long id + Long version + String text + Date dateCreated + Date lastUpdated + + static mapping = { + datasource 'secondary' + } +} diff --git a/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/domains/TimestampNote.groovy b/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/domains/TimestampNote.groovy new file mode 100644 index 00000000000..7a12f16c99a --- /dev/null +++ b/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/domains/TimestampNote.groovy @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.grails.data.testing.tck.domains + +import grails.persistence.Entity + +@Entity +class TimestampNote { + + Long id + Long version + String text + Date dateCreated + Date lastUpdated +} diff --git a/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/tests/AutoTimestampEventListenerLookupSpec.groovy b/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/tests/AutoTimestampEventListenerLookupSpec.groovy new file mode 100644 index 00000000000..57e5ff3358a --- /dev/null +++ b/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/tests/AutoTimestampEventListenerLookupSpec.groovy @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.grails.data.testing.tck.tests + +import org.apache.grails.data.testing.tck.base.GrailsDataTckSpec +import org.apache.grails.data.testing.tck.domains.TimestampNote +import org.grails.datastore.gorm.events.AutoTimestampingDatastore + +class AutoTimestampEventListenerLookupSpec extends GrailsDataTckSpec { + + private static final Date PAST = new Date(110_000_000_000L) + + void setupSpec() { + manager.registerDomainClasses(TimestampNote) + } + + void 'a domain class answers with the listener of its datastore'() { + expect: + manager.session.datastore instanceof AutoTimestampingDatastore + TimestampNote.autoTimestampEventListener != null + TimestampNote.autoTimestampEventListener.is(((AutoTimestampingDatastore) manager.session.datastore).autoTimestampEventListener) + } + + void 'the listener of a domain class turns its timestamps off'() { + given: + TimestampNote kept = new TimestampNote(text: 'kept', dateCreated: PAST, lastUpdated: PAST) + TimestampNote stamped = new TimestampNote(text: 'stamped', dateCreated: PAST, lastUpdated: PAST) + + when: + TimestampNote.autoTimestampEventListener.withoutTimestamps { + kept.save(flush: true) + } + stamped.save(flush: true) + manager.session.clear() + + then: + TimestampNote.get(kept.id).dateCreated == PAST + TimestampNote.get(kept.id).lastUpdated == PAST + TimestampNote.get(stamped.id).dateCreated != PAST + TimestampNote.get(stamped.id).lastUpdated != PAST + } +} diff --git a/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/tests/AutoTimestampEventListenerMultiDataSourceSpec.groovy b/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/tests/AutoTimestampEventListenerMultiDataSourceSpec.groovy new file mode 100644 index 00000000000..143025714d6 --- /dev/null +++ b/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/tests/AutoTimestampEventListenerMultiDataSourceSpec.groovy @@ -0,0 +1,114 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.grails.data.testing.tck.tests + +import spock.lang.Issue +import spock.lang.Requires + +import org.apache.grails.data.testing.tck.base.GrailsDataTckSpec +import org.apache.grails.data.testing.tck.domains.AllConnectionsTimestampNote +import org.apache.grails.data.testing.tck.domains.DefaultConnectionTimestampNote +import org.apache.grails.data.testing.tck.domains.SecondaryConnectionTimestampNote + +/** + * Every connection has an auto-timestamp listener of its own, and a domain class answers with the listener of the + * connection its saves go through. + */ +@Issue('https://github.com/apache/grails-core/issues/14509') +@Requires({ instance.manager?.supportsMultipleDataSources() }) +class AutoTimestampEventListenerMultiDataSourceSpec extends GrailsDataTckSpec { + + private static final Date PAST = new Date(110_000_000_000L) + + void setup() { + manager.setupMultiDataSource(DefaultConnectionTimestampNote, SecondaryConnectionTimestampNote, AllConnectionsTimestampNote) + } + + void cleanup() { + manager.cleanupMultiDataSource() + } + + void 'each connection answers with a listener of its own'() { + given: + def defaultListener = DefaultConnectionTimestampNote.autoTimestampEventListener + def secondaryListener = SecondaryConnectionTimestampNote.autoTimestampEventListener + + expect: + !defaultListener.is(secondaryListener) + AllConnectionsTimestampNote.autoTimestampEventListener.is(defaultListener) + AllConnectionsTimestampNote.secondary.autoTimestampEventListener.is(secondaryListener) + AllConnectionsTimestampNote.secondary.withTransaction { AllConnectionsTimestampNote.autoTimestampEventListener }.is(secondaryListener) + } + + void 'the listener of each domain class turns off the timestamps of its own saves'() { + given: + DefaultConnectionTimestampNote car = new DefaultConnectionTimestampNote(text: 'car', dateCreated: PAST, lastUpdated: PAST) + SecondaryConnectionTimestampNote person = new SecondaryConnectionTimestampNote(text: 'person', dateCreated: PAST, lastUpdated: PAST) + + when: + DefaultConnectionTimestampNote.withNewTransaction { + DefaultConnectionTimestampNote.autoTimestampEventListener.withoutTimestamps { + car.save(flush: true) + } + } + SecondaryConnectionTimestampNote.withNewTransaction { + SecondaryConnectionTimestampNote.autoTimestampEventListener.withoutTimestamps { + person.save(flush: true) + } + } + + then: + DefaultConnectionTimestampNote.withNewTransaction { DefaultConnectionTimestampNote.get(car.id).dateCreated } == PAST + DefaultConnectionTimestampNote.withNewTransaction { DefaultConnectionTimestampNote.get(car.id).lastUpdated } == PAST + SecondaryConnectionTimestampNote.withNewTransaction { SecondaryConnectionTimestampNote.get(person.id).dateCreated } == PAST + SecondaryConnectionTimestampNote.withNewTransaction { SecondaryConnectionTimestampNote.get(person.id).lastUpdated } == PAST + } + + void 'the listener of one connection does not reach a save through another'() { + given: + SecondaryConnectionTimestampNote person = new SecondaryConnectionTimestampNote(text: 'person', dateCreated: PAST, lastUpdated: PAST) + + when: + SecondaryConnectionTimestampNote.withNewTransaction { + DefaultConnectionTimestampNote.autoTimestampEventListener.withoutTimestamps { + person.save(flush: true) + } + } + + then: + SecondaryConnectionTimestampNote.withNewTransaction { SecondaryConnectionTimestampNote.get(person.id).dateCreated } != PAST + SecondaryConnectionTimestampNote.withNewTransaction { SecondaryConnectionTimestampNote.get(person.id).lastUpdated } != PAST + } + + void 'a domain class on several connections turns off the timestamps of the connection it names'() { + given: + AllConnectionsTimestampNote note = new AllConnectionsTimestampNote(text: 'shared', dateCreated: PAST, lastUpdated: PAST) + + when: + AllConnectionsTimestampNote.secondary.withNewTransaction { + AllConnectionsTimestampNote.secondary.autoTimestampEventListener.withoutTimestamps { + note.secondary.save(flush: true) + } + } + + then: + AllConnectionsTimestampNote.secondary.withNewTransaction { AllConnectionsTimestampNote.secondary.get(note.id).dateCreated } == PAST + AllConnectionsTimestampNote.secondary.withNewTransaction { AllConnectionsTimestampNote.secondary.get(note.id).lastUpdated } == PAST + } +} diff --git a/grails-doc/src/en/guide/introduction/whatsNew.adoc b/grails-doc/src/en/guide/introduction/whatsNew.adoc index 7918c538eaa..f210153d8d0 100644 --- a/grails-doc/src/en/guide/introduction/whatsNew.adoc +++ b/grails-doc/src/en/guide/introduction/whatsNew.adoc @@ -593,6 +593,23 @@ GORM for Hibernate 7 now starts tracking changes to an entity when it is persist As part of this change, persistence event listeners on Hibernate 7 receive a `PersistEvent` (`eventType` `Persist`) for every explicit or flush-time cascaded persist and a `MergeEvent` (`eventType` `Merge`) for every merge. Neither event can be cancelled. Hibernate 5 continues to publish `SaveOrUpdateEvent` when an entity is saved, and publishes no event for a merge. A listener that switches over `event.eventType` with a default branch that throws should handle the new values. See the Events and Auto Timestamping section of the GORM for link:{hibernate7Guide}index.html#eventsAutoTimestamping[Hibernate 7] guide. +==== A Domain Class Answers with Its Auto-Timestamp Listener + +Every datastore has an `AutoTimestampEventListener` of its own, and so does every data source of an application with several and every tenant with a database or schema of its own. A listener only handles the saves made through its own datastore, so the `autoTimestampEventListener` bean, which belongs to the default data source, cannot turn off the timestamps of a domain class saved to another one. A domain class now answers with the listener that handles its saves: + +[source,groovy] +---- +// Car is mapped to the default data source and Person to the 'people' data source +Car.autoTimestampEventListener.withoutTimestamps { + car.save(flush: true) +} +Person.autoTimestampEventListener.withoutTimestamps { + person.save(flush: true) +} +---- + +The lookup follows the data source a save made from the same place goes to: inside a `Car.secondary.withTransaction { }` block, or a method annotated `@Transactional(connection = 'secondary')`, it is the `secondary` data source's listener, and for a multi-tenant class it is the current tenant's. `Car.secondary.autoTimestampEventListener` names another data source of a class mapped to several. This works with GORM for Hibernate 7, Hibernate 5, MongoDB and Neo4j, and in unit tests. See the Events and Auto Timestamping section of the GORM for link:{hibernate7Guide}index.html#eventsAutoTimestamping[Hibernate 7] guide. + ==== Default Security Response Headers Servlet web applications now send a baseline set of browser hardening headers on every response: diff --git a/grails-doc/src/en/guide/upgrading/upgrading90x.adoc b/grails-doc/src/en/guide/upgrading/upgrading90x.adoc index 8a769c10e22..d18a587f1ef 100644 --- a/grails-doc/src/en/guide/upgrading/upgrading90x.adoc +++ b/grails-doc/src/en/guide/upgrading/upgrading90x.adoc @@ -1453,3 +1453,11 @@ With `cacheUsers` enabled, the Spring Security plugin's user cache now stores us * Code that changed a user it read from `userCache` and expected the cache to see the change puts the user back with `userCache.putUserInCache`. * To store users by reference, as Grails 8 did, set `cacheConfig` on the `userCache` bean to a `MutableConfiguration` with `setStoreByValue(false)`. +==== 25. Unit Tests Run Domain Class Events for Every Data Source and Tenant + +In a unit test that implements `DataTest` or `DomainUnitTest`, a save through a data source other than the default one, as `book.secondary.save()` makes, and a save for a tenant with a database or a schema of its own, now run the domain class's events, such as `beforeInsert`, and set its `dateCreated` and `lastUpdated` properties, as they do in the application. In Grails 8 they ran neither. + +===== Required Actions + +* A test that expected such a save to keep the `dateCreated` and `lastUpdated` it was given saves inside a `withoutTimestamps { }` block of that data source's listener, such as `Book.secondary.autoTimestampEventListener`. +* A test that expected a domain class event not to run on such a save expects what the event does. From 147b14471c4c1bbcd20fef73397cd6a0809d49fa Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sat, 14 Nov 2026 10:26:31 -0800 Subject: [PATCH 032/115] Answer with the listener of the datastore that saves the class Inside a tenant block, GORM's generic API saves an instance of a class that is not multi-tenant through the tenant's datastore: execute() routes to the datastore of a bound tenant. The accessor took the datastore the API was made for, the default one, so turning its timestamps off did not reach the save. GormStaticApi now asks a session of execute()'s routing for its datastore. Hibernate saves through the datastore its API is bound to, inside a tenant block as well, so its static APIs keep answering with that datastore's listener. --- .../AbstractHibernateGormStaticApi.groovy | 10 + ...utoTimestampEventListenerTenantSpec.groovy | 188 ++++++++++++++++++ .../hibernate/HibernateGormStaticApi.groovy | 10 + ...utoTimestampEventListenerTenantSpec.groovy | 31 ++- .../SimpleMapAutoTimestampListenerSpec.groovy | 27 +++ .../datastore/gorm/GormStaticApi.groovy | 23 ++- .../datastore/gorm/GormStaticApiSpec.groovy | 16 +- 7 files changed, 291 insertions(+), 14 deletions(-) create mode 100644 grails-data-hibernate5/core/src/test/groovy/org/grails/orm/hibernate/connections/AutoTimestampEventListenerTenantSpec.groovy diff --git a/grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/AbstractHibernateGormStaticApi.groovy b/grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/AbstractHibernateGormStaticApi.groovy index 39047af8249..093220fb287 100644 --- a/grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/AbstractHibernateGormStaticApi.groovy +++ b/grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/AbstractHibernateGormStaticApi.groovy @@ -41,6 +41,7 @@ import org.springframework.core.convert.ConversionService import org.springframework.transaction.PlatformTransactionManager import org.grails.datastore.gorm.GormStaticApi +import org.grails.datastore.gorm.events.AutoTimestampEventListener import org.grails.datastore.gorm.finders.DynamicFinder import org.grails.datastore.gorm.finders.FinderMethod import org.grails.datastore.mapping.proxy.ProxyHandler @@ -93,6 +94,15 @@ abstract class AbstractHibernateGormStaticApi extends GormStaticApi { return hibernateTemplate } + /** + * Hibernate saves an instance through the datastore this API is bound to, inside a tenant block as well, so this + * is that datastore's listener. + */ + @Override + AutoTimestampEventListener getAutoTimestampEventListener() { + autoTimestampEventListenerOf(getDatastore()) + } + @Override T withNewSession(Closure callable) { AbstractHibernateDatastore hibernateDatastore = (AbstractHibernateDatastore) datastore diff --git a/grails-data-hibernate5/core/src/test/groovy/org/grails/orm/hibernate/connections/AutoTimestampEventListenerTenantSpec.groovy b/grails-data-hibernate5/core/src/test/groovy/org/grails/orm/hibernate/connections/AutoTimestampEventListenerTenantSpec.groovy new file mode 100644 index 00000000000..42c7c15edda --- /dev/null +++ b/grails-data-hibernate5/core/src/test/groovy/org/grails/orm/hibernate/connections/AutoTimestampEventListenerTenantSpec.groovy @@ -0,0 +1,188 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.orm.hibernate.connections + +import org.hibernate.dialect.H2Dialect +import spock.lang.AutoCleanup +import spock.lang.Issue +import spock.lang.Specification +import spock.util.environment.RestoreSystemProperties + +import grails.gorm.MultiTenant +import grails.gorm.annotation.Entity +import grails.gorm.multitenancy.Tenants +import org.grails.datastore.gorm.events.AutoTimestampEventListener +import org.grails.datastore.mapping.core.DatastoreUtils +import org.grails.datastore.mapping.multitenancy.AllTenantsResolver +import org.grails.datastore.mapping.multitenancy.resolvers.SystemPropertyTenantResolver +import org.grails.orm.hibernate.HibernateDatastore + +/** + * A tenant with a database or a schema of its own has an auto-timestamp listener of its own, and a multi-tenant + * domain class answers with the current tenant's. + */ +@Issue('https://github.com/apache/grails-core/issues/14509') +@RestoreSystemProperties +class AutoTimestampEventListenerTenantSpec extends Specification { + + private static final Date PAST = new Date(110_000_000_000L) + + @AutoCleanup + HibernateDatastore datastore + + void setup() { + System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, '') + } + + void 'a tenant with a database of its own answers with the listener of that database'() { + given: + datastore = databasePerTenant() + AutoTimestampEventListener books = listenerOf('books') + AutoTimestampEventListener moreBooks = listenerOf('moreBooks') + + expect: + !books.is(moreBooks) + !books.is(datastore.autoTimestampEventListener) + Tenants.withId('books') { TenantTimestampNote.autoTimestampEventListener }.is(books) + Tenants.withId('moreBooks') { TenantTimestampNote.autoTimestampEventListener }.is(moreBooks) + TenantTimestampNote.withTenant('moreBooks').autoTimestampEventListener.is(moreBooks) + + when: 'the current tenant comes from the resolver' + System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, 'moreBooks') + + then: + TenantTimestampNote.autoTimestampEventListener.is(moreBooks) + } + + void 'the listener of the current tenant turns off the timestamps of its saves, and another tenant\'s does not'() { + given: + datastore = databasePerTenant() + System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, 'books') + TenantTimestampNote kept = new TenantTimestampNote(text: 'kept', dateCreated: PAST, lastUpdated: PAST) + TenantTimestampNote stamped = new TenantTimestampNote(text: 'stamped', dateCreated: PAST, lastUpdated: PAST) + + when: + TenantTimestampNote.withTransaction { + TenantTimestampNote.autoTimestampEventListener.withoutTimestamps { + kept.save(flush: true) + } + listenerOf('moreBooks').withoutTimestamps { + stamped.save(flush: true) + } + } + + then: + TenantTimestampNote.withNewTransaction { TenantTimestampNote.get(kept.id).dateCreated } == PAST + TenantTimestampNote.withNewTransaction { TenantTimestampNote.get(kept.id).lastUpdated } == PAST + TenantTimestampNote.withNewTransaction { TenantTimestampNote.get(stamped.id).dateCreated } != PAST + TenantTimestampNote.withNewTransaction { TenantTimestampNote.get(stamped.id).lastUpdated } != PAST + } + + void 'a class that is not multi-tenant answers inside a tenant block with the listener of the default database'() { + given: 'Hibernate saves a class that is not multi-tenant to the default database inside a tenant block too' + datastore = databasePerTenant() + PlainTimestampNote kept = new PlainTimestampNote(text: 'kept', dateCreated: PAST, lastUpdated: PAST) + + when: + def listener = Tenants.withId('books') { + PlainTimestampNote.withTransaction { + PlainTimestampNote.autoTimestampEventListener.withoutTimestamps { + kept.save(flush: true) + } + } + PlainTimestampNote.autoTimestampEventListener + } + + then: + listener.is(datastore.autoTimestampEventListener) + PlainTimestampNote.withNewTransaction { PlainTimestampNote.get(kept.id).dateCreated } == PAST + PlainTimestampNote.withNewTransaction { PlainTimestampNote.get(kept.id).lastUpdated } == PAST + } + + void 'a tenant with a schema of its own answers with the listener of that schema'() { + given: + datastore = new HibernateDatastore(DatastoreUtils.createPropertyResolver([ + 'grails.gorm.multiTenancy.mode' : 'SCHEMA', + 'grails.gorm.multiTenancy.tenantResolverClass': TwoSchemaResolver, + 'dataSource.url' : 'jdbc:h2:mem:timestampSchemaDB;LOCK_TIMEOUT=10000', + 'dataSource.dbCreate' : 'create-drop', + 'dataSource.dialect' : H2Dialect.name, + 'hibernate.hbm2ddl.auto' : 'create' + ]), TenantTimestampNote) + AutoTimestampEventListener books = listenerOf('books') + AutoTimestampEventListener moreBooks = listenerOf('moreBooks') + TenantTimestampNote kept = new TenantTimestampNote(text: 'kept', dateCreated: PAST, lastUpdated: PAST) + + when: + Tenants.withId('books') { + TenantTimestampNote.withTransaction { + TenantTimestampNote.autoTimestampEventListener.withoutTimestamps { + kept.save(flush: true) + } + } + } + + then: + !books.is(moreBooks) + Tenants.withId('books') { TenantTimestampNote.autoTimestampEventListener }.is(books) + TenantTimestampNote.withTenant('moreBooks').autoTimestampEventListener.is(moreBooks) + Tenants.withId('books') { TenantTimestampNote.withNewTransaction { TenantTimestampNote.get(kept.id).dateCreated } } == PAST + } + + private HibernateDatastore databasePerTenant() { + new HibernateDatastore(DatastoreUtils.createPropertyResolver([ + 'grails.gorm.multiTenancy.mode' : 'DATABASE', + 'grails.gorm.multiTenancy.tenantResolverClass': SystemPropertyTenantResolver, + 'dataSource.url' : 'jdbc:h2:mem:timestampDefaultDB;LOCK_TIMEOUT=10000', + 'dataSource.dbCreate' : 'create-drop', + 'dataSource.dialect' : H2Dialect.name, + 'hibernate.hbm2ddl.auto' : 'create', + 'dataSources.books' : [url: 'jdbc:h2:mem:timestampBooksDB;LOCK_TIMEOUT=10000'], + 'dataSources.moreBooks' : [url: 'jdbc:h2:mem:timestampMoreBooksDB;LOCK_TIMEOUT=10000'] + ]), TenantTimestampNote, PlainTimestampNote) + } + + private AutoTimestampEventListener listenerOf(String connectionName) { + datastore.getDatastoreForConnection(connectionName).autoTimestampEventListener + } + + static class TwoSchemaResolver extends SystemPropertyTenantResolver implements AllTenantsResolver { + + @Override + Iterable resolveTenantIds() { + ['books', 'moreBooks'] + } + } +} + +@Entity +class TenantTimestampNote implements MultiTenant { + + String text + Date dateCreated + Date lastUpdated +} + +@Entity +class PlainTimestampNote { + + String text + Date dateCreated + Date lastUpdated +} diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateGormStaticApi.groovy b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateGormStaticApi.groovy index 9f2358e309b..aca747fbb36 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateGormStaticApi.groovy +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateGormStaticApi.groovy @@ -59,6 +59,7 @@ import org.springframework.transaction.PlatformTransactionManager import grails.orm.HibernateCriteriaBuilder import grails.gorm.DetachedCriteria import org.grails.datastore.gorm.GormStaticApi +import org.grails.datastore.gorm.events.AutoTimestampEventListener import org.grails.datastore.gorm.finders.FinderMethod import org.grails.datastore.gorm.internal.RefreshLockArguments import org.grails.datastore.mapping.core.connections.ConnectionSource @@ -134,6 +135,15 @@ class HibernateGormStaticApi extends GormStaticApi { (GormStaticApi) HibernateGormEnhancer.findStaticApi(persistentClass, qualifier) } + /** + * Hibernate saves an instance through the datastore this API is bound to, inside a tenant block as well, so this + * is that datastore's listener. + */ + @Override + AutoTimestampEventListener getAutoTimestampEventListener() { + autoTimestampEventListenerOf(getDatastore()) + } + @Override DetachedCriteria where(Closure callable) { new HibernateDetachedCriteria(persistentClass).build((Closure) callable) diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/connections/AutoTimestampEventListenerTenantSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/connections/AutoTimestampEventListenerTenantSpec.groovy index 5a1a09e5254..42c7c15edda 100644 --- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/connections/AutoTimestampEventListenerTenantSpec.groovy +++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/connections/AutoTimestampEventListenerTenantSpec.groovy @@ -94,6 +94,27 @@ class AutoTimestampEventListenerTenantSpec extends Specification { TenantTimestampNote.withNewTransaction { TenantTimestampNote.get(stamped.id).lastUpdated } != PAST } + void 'a class that is not multi-tenant answers inside a tenant block with the listener of the default database'() { + given: 'Hibernate saves a class that is not multi-tenant to the default database inside a tenant block too' + datastore = databasePerTenant() + PlainTimestampNote kept = new PlainTimestampNote(text: 'kept', dateCreated: PAST, lastUpdated: PAST) + + when: + def listener = Tenants.withId('books') { + PlainTimestampNote.withTransaction { + PlainTimestampNote.autoTimestampEventListener.withoutTimestamps { + kept.save(flush: true) + } + } + PlainTimestampNote.autoTimestampEventListener + } + + then: + listener.is(datastore.autoTimestampEventListener) + PlainTimestampNote.withNewTransaction { PlainTimestampNote.get(kept.id).dateCreated } == PAST + PlainTimestampNote.withNewTransaction { PlainTimestampNote.get(kept.id).lastUpdated } == PAST + } + void 'a tenant with a schema of its own answers with the listener of that schema'() { given: datastore = new HibernateDatastore(DatastoreUtils.createPropertyResolver([ @@ -134,7 +155,7 @@ class AutoTimestampEventListenerTenantSpec extends Specification { 'hibernate.hbm2ddl.auto' : 'create', 'dataSources.books' : [url: 'jdbc:h2:mem:timestampBooksDB;LOCK_TIMEOUT=10000'], 'dataSources.moreBooks' : [url: 'jdbc:h2:mem:timestampMoreBooksDB;LOCK_TIMEOUT=10000'] - ]), TenantTimestampNote) + ]), TenantTimestampNote, PlainTimestampNote) } private AutoTimestampEventListener listenerOf(String connectionName) { @@ -157,3 +178,11 @@ class TenantTimestampNote implements MultiTenant { Date dateCreated Date lastUpdated } + +@Entity +class PlainTimestampNote { + + String text + Date dateCreated + Date lastUpdated +} diff --git a/grails-data-simple/src/test/groovy/org/grails/datastore/mapping/simple/SimpleMapAutoTimestampListenerSpec.groovy b/grails-data-simple/src/test/groovy/org/grails/datastore/mapping/simple/SimpleMapAutoTimestampListenerSpec.groovy index 3e5c9a111ac..2d3a8230486 100644 --- a/grails-data-simple/src/test/groovy/org/grails/datastore/mapping/simple/SimpleMapAutoTimestampListenerSpec.groovy +++ b/grails-data-simple/src/test/groovy/org/grails/datastore/mapping/simple/SimpleMapAutoTimestampListenerSpec.groovy @@ -123,6 +123,33 @@ class SimpleMapAutoTimestampListenerSpec extends Specification { TenantListenerNote.autoTimestampEventListener.is(foo) } + void 'a class that is not multi-tenant answers inside a tenant block with the listener of the datastore that saves it'() { + given: 'the in-memory datastore runs the operations of every class inside a tenant block on that tenant\'s database' + datastore = new SimpleMapDatastore( + DatastoreUtils.createPropertyResolver([ + (Settings.SETTING_MULTI_TENANCY_MODE) : MultiTenancySettings.MultiTenancyMode.DATABASE, + (Settings.SETTING_MULTI_TENANT_RESOLVER): new SystemPropertyTenantResolver() + ]), + [ConnectionSource.DEFAULT, 'foo'], + TenantListenerNote, ListenerNote + ) + ListenerNote note = new ListenerNote(text: 'kept', dateCreated: PAST, lastUpdated: PAST) + + when: + def listener = Tenants.withId('foo') { + ListenerNote.autoTimestampEventListener.withoutTimestamps { + note.save(flush: true) + } + ListenerNote.autoTimestampEventListener + } + + then: + Tenants.withId('foo') { ListenerNote.count() } == 1 + listener.is(datastore.getDatastoreForConnection('foo').autoTimestampEventListener) + note.dateCreated == PAST + note.lastUpdated == PAST + } + void 'a tenant with a schema added at runtime answers with the listener of that schema'() { given: datastore = new SimpleMapDatastore( diff --git a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/GormStaticApi.groovy b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/GormStaticApi.groovy index b016f1af4e2..be5f030301d 100644 --- a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/GormStaticApi.groovy +++ b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/GormStaticApi.groovy @@ -171,17 +171,28 @@ class GormStaticApi extends AbstractGormApi implements GormAllOperations session.datastore } as SessionCallback)) + } + + /** + * @param ds the datastore that saves the instances of the persistent class + * @return the auto-timestamp listener of the datastore + * @throws IllegalStateException when the datastore has no such listener + * @since 9.0 + */ + protected AutoTimestampEventListener autoTimestampEventListenerOf(Datastore ds) { AutoTimestampEventListener listener = ds instanceof AutoTimestampingDatastore ? ((AutoTimestampingDatastore) ds).autoTimestampEventListener : null diff --git a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/GormStaticApiSpec.groovy b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/GormStaticApiSpec.groovy index cad395c1fea..d3c7bf12fef 100644 --- a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/GormStaticApiSpec.groovy +++ b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/GormStaticApiSpec.groovy @@ -172,9 +172,9 @@ class GormStaticApiSpec extends Specification { void "getAutoTimestampEventListener rejects a datastore that does not set timestamps through a listener"() { given: - def plainDatastore = Stub(Datastore) { - getMappingContext() >> datastore.mappingContext - } + Datastore plainDatastore = Stub(Datastore) + plainDatastore.getMappingContext() >> datastore.mappingContext + plainDatastore.connect() >> Stub(Session) { getDatastore() >> plainDatastore } def api = new GormStaticApi(GormStaticApiThing, plainDatastore, []) when: @@ -183,14 +183,15 @@ class GormStaticApiSpec extends Specification { then: IllegalStateException e = thrown() e.message.contains(GormStaticApiThing.name) + e.message.contains(plainDatastore.toString()) } void "getAutoTimestampEventListener rejects an auto-timestamping datastore that has no listener"() { given: - def listenerlessDatastore = Stub(Datastore, additionalInterfaces: [AutoTimestampingDatastore]) { - getMappingContext() >> datastore.mappingContext - getAutoTimestampEventListener() >> null - } + Datastore listenerlessDatastore = Stub(Datastore, additionalInterfaces: [AutoTimestampingDatastore]) + listenerlessDatastore.getMappingContext() >> datastore.mappingContext + listenerlessDatastore.connect() >> Stub(Session) { getDatastore() >> listenerlessDatastore } + ((AutoTimestampingDatastore) listenerlessDatastore).getAutoTimestampEventListener() >> null def api = new GormStaticApi(GormStaticApiThing, listenerlessDatastore, []) when: @@ -199,6 +200,7 @@ class GormStaticApiSpec extends Specification { then: IllegalStateException e = thrown() e.message.contains(GormStaticApiThing.name) + e.message.contains(listenerlessDatastore.toString()) } void "forQualifier creates a new static api bound to the requested qualifier"() { From cc0e03f5de9a78d44c151a892d270f70f58c8f4b Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sat, 14 Nov 2026 10:27:48 -0800 Subject: [PATCH 033/115] Keep the tenant handling of the in-memory datastore's other connections With a discriminator column for the tenant and several connections, the datastore of every other connection now also registered a tenant filter of its own. Tenants.withId binds the tenant for the datastore of the default connection, which that filter does not see, so a query through another connection inside Tenants.withId threw TenantNotFoundException, or filtered by the resolver's tenant. The datastores of the other connections, and of schemas, register only the listeners that run the domain class events and set the timestamps, so their tenant handling is what it was. --- .../mapping/simple/SimpleMapDatastore.java | 23 ++++++++--- .../SimpleMapConnectionEventsSpec.groovy | 38 +++++++++++++++++++ 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/grails-data-simple/src/main/groovy/org/grails/datastore/mapping/simple/SimpleMapDatastore.java b/grails-data-simple/src/main/groovy/org/grails/datastore/mapping/simple/SimpleMapDatastore.java index 295722acc40..6fd79554901 100644 --- a/grails-data-simple/src/main/groovy/org/grails/datastore/mapping/simple/SimpleMapDatastore.java +++ b/grails-data-simple/src/main/groovy/org/grails/datastore/mapping/simple/SimpleMapDatastore.java @@ -128,7 +128,7 @@ protected GormEnhancer initialize(ConnectionSourceSettings settings) { // The parent's enhancer serves this connection's entities, but the sessions of this // connection publish their events with this datastore as the source, and only // listeners of its own handle those. - registerEventListeners(eventPublisher); + registerEntityEventListeners(eventPublisher); return null; } @@ -297,14 +297,27 @@ private SimpleMapDatastore getDatastoreForQualifier(Class cls, String qua } protected void registerEventListeners(ConfigurableApplicationEventPublisher eventPublisher) { - eventPublisher.addApplicationListener(new DomainEventListener(this)); - autoTimestampEventListener = new AutoTimestampEventListener(this); - eventPublisher.addApplicationListener(autoTimestampEventListener); + registerEntityEventListeners(eventPublisher); if (multiTenancyMode == MultiTenancySettings.MultiTenancyMode.DISCRIMINATOR) { eventPublisher.addApplicationListener(new MultiTenantEventListener(this)); } } + /** + * Registers the listeners that run the domain class events and set the timestamps of the entities saved through + * this datastore. The datastore of another connection, or of a schema, registers only these: the tenant of a + * discriminator column is bound for the datastore of the default connection, and a tenant filter of its own + * would not see it. + * + * @param eventPublisher The publisher of this datastore's events + * @since 9.0 + */ + protected void registerEntityEventListeners(ConfigurableApplicationEventPublisher eventPublisher) { + eventPublisher.addApplicationListener(new DomainEventListener(this)); + autoTimestampEventListener = new AutoTimestampEventListener(this); + eventPublisher.addApplicationListener(autoTimestampEventListener); + } + public Map getIndices() { return indices; } @@ -480,7 +493,7 @@ public void addTenantForSchema(String schemaName) { protected GormEnhancer initialize(ConnectionSourceSettings settings) { // As for a connection's datastore, the schema's sessions publish their events with this // datastore as the source. - registerEventListeners(eventPublisher); + registerEntityEventListeners(eventPublisher); return null; } }; diff --git a/grails-data-simple/src/test/groovy/org/grails/datastore/mapping/simple/SimpleMapConnectionEventsSpec.groovy b/grails-data-simple/src/test/groovy/org/grails/datastore/mapping/simple/SimpleMapConnectionEventsSpec.groovy index b72d2404dbf..d64964cd47e 100644 --- a/grails-data-simple/src/test/groovy/org/grails/datastore/mapping/simple/SimpleMapConnectionEventsSpec.groovy +++ b/grails-data-simple/src/test/groovy/org/grails/datastore/mapping/simple/SimpleMapConnectionEventsSpec.groovy @@ -58,6 +58,32 @@ class SimpleMapConnectionEventsSpec extends Specification { note.lastUpdated != null } + void 'with a discriminator column, another connection keeps the tenant handling it had and sets the timestamps'() { + given: 'no tenant from the resolver' + System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, '') + datastore = new SimpleMapDatastore( + DatastoreUtils.createPropertyResolver([ + (Settings.SETTING_MULTI_TENANCY_MODE) : MultiTenancySettings.MultiTenancyMode.DISCRIMINATOR, + (Settings.SETTING_MULTI_TENANT_RESOLVER): new SystemPropertyTenantResolver() + ]), + [ConnectionSource.DEFAULT, 'secondary'], + DiscriminatorNote + ) + DiscriminatorNote note = new DiscriminatorNote(text: 'draft', tenantId: 't1') + + when: + List found = Tenants.withId('t1') { + DiscriminatorNote.secondary.withTransaction { + note.secondary.save(flush: true) + DiscriminatorNote.secondary.list() + } + } + + then: + found*.text == ['draft'] + note.dateCreated != null + } + void 'a save for a tenant with a database of its own runs the events and sets the timestamps'() { given: datastore = new SimpleMapDatastore( @@ -108,6 +134,18 @@ class SimpleMapConnectionEventsSpec extends Specification { } } +@Entity +class DiscriminatorNote implements MultiTenant { + + String text + String tenantId + Date dateCreated + + static mapping = { + datasource 'ALL' + } +} + @Entity class ConnectionNote { From 394485041a3feb501f6def4d29f33c08623cbcb9 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sat, 14 Nov 2026 10:29:32 -0800 Subject: [PATCH 034/115] Let a listener be added while the default publisher publishes an event DefaultApplicationEventPublisher iterated a plain list, so a listener added while an event was being dispatched, as a datastore adds those of a schema it adds at runtime, failed the dispatch with a ConcurrentModificationException. The list is now copied on write; a listener added during a dispatch receives the next event. --- .../DefaultApplicationEventPublisher.groovy | 6 +- ...efaultApplicationEventPublisherSpec.groovy | 145 +++--------------- 2 files changed, 23 insertions(+), 128 deletions(-) diff --git a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/events/DefaultApplicationEventPublisher.groovy b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/events/DefaultApplicationEventPublisher.groovy index b1d534a60ae..4533712620d 100644 --- a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/events/DefaultApplicationEventPublisher.groovy +++ b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/events/DefaultApplicationEventPublisher.groovy @@ -19,6 +19,8 @@ package org.grails.datastore.gorm.events +import java.util.concurrent.CopyOnWriteArrayList + import groovy.transform.CompileStatic import org.springframework.context.ApplicationEvent @@ -35,7 +37,9 @@ import org.springframework.context.event.SmartApplicationListener @CompileStatic class DefaultApplicationEventPublisher implements ConfigurableApplicationEventPublisher { - private List applicationListeners = [] + // Copied on write: a datastore adds the listeners of a schema it adds at runtime, which can happen while an + // event is being published on this thread or another one. A listener added then receives the next event. + private final List applicationListeners = new CopyOnWriteArrayList<>() @Override void publishEvent(ApplicationEvent event) { diff --git a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/events/DefaultApplicationEventPublisherSpec.groovy b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/events/DefaultApplicationEventPublisherSpec.groovy index 2c4e5fc689d..815737c16ed 100644 --- a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/events/DefaultApplicationEventPublisherSpec.groovy +++ b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/events/DefaultApplicationEventPublisherSpec.groovy @@ -16,148 +16,39 @@ * specific language governing permissions and limitations * under the License. */ - package org.grails.datastore.gorm.events import spock.lang.Specification import org.springframework.context.ApplicationEvent import org.springframework.context.ApplicationListener -import org.springframework.context.PayloadApplicationEvent -import org.springframework.context.event.SmartApplicationListener class DefaultApplicationEventPublisherSpec extends Specification { - DefaultApplicationEventPublisher publisher = new DefaultApplicationEventPublisher() - - void "publishEvent(ApplicationEvent) does nothing when no listeners are registered"() { - given: - ApplicationEvent event = new PayloadApplicationEvent<>(this, 'payload') - - when: - publisher.publishEvent(event) - - then: - noExceptionThrown() - } - - void "publishEvent(Object) does nothing when no listeners are registered"() { - when: - publisher.publishEvent('a plain payload') - - then: - noExceptionThrown() - } - - void "publishEvent notifies a plain ApplicationListener regardless of event or source type"() { - given: - ApplicationListener listener = Mock(ApplicationListener) - publisher.addApplicationListener(listener) - ApplicationEvent event = new PayloadApplicationEvent<>(this, 'payload') - - when: - publisher.publishEvent(event) - - then: - 1 * listener.onApplicationEvent(event) - } - - void "publishEvent notifies every registered listener in order"() { - given: - ApplicationListener first = Mock(ApplicationListener) - ApplicationListener second = Mock(ApplicationListener) - publisher.addApplicationListener(first) - publisher.addApplicationListener(second) - ApplicationEvent event = new PayloadApplicationEvent<>(this, 'payload') - - when: - publisher.publishEvent(event) - - then: - 1 * first.onApplicationEvent(event) - 1 * second.onApplicationEvent(event) - } - - void "publishEvent notifies a SmartApplicationListener when it supports both the event type and source type"() { - given: - SmartApplicationListener listener = Mock(SmartApplicationListener) { - supportsEventType(_) >> true - supportsSourceType(_) >> true - } - publisher.addApplicationListener(listener) - ApplicationEvent event = new PayloadApplicationEvent<>(this, 'payload') + void 'a listener can add another listener while an event is published'() { + given: 'a listener that adds one, as a datastore that adds a schema while it saves does' + DefaultApplicationEventPublisher publisher = new DefaultApplicationEventPublisher() + List received = [] + ApplicationListener added = { ApplicationEvent event -> received << "added:${event.source}".toString() } as ApplicationListener + publisher.addApplicationListener({ ApplicationEvent event -> + received << "adding:${event.source}".toString() + if (event.source == 'first') { + publisher.addApplicationListener(added) + } + } as ApplicationListener) when: - publisher.publishEvent(event) + publisher.publishEvent(new TestEvent('first')) + publisher.publishEvent(new TestEvent('second')) - then: - 1 * listener.onApplicationEvent(event) + then: 'the added listener receives the events published after it was added' + received == ['adding:first', 'adding:second', 'added:second'] } - void "publishEvent skips a SmartApplicationListener that does not support the event type"() { - given: - SmartApplicationListener listener = Mock(SmartApplicationListener) { - supportsEventType(_) >> false - } - publisher.addApplicationListener(listener) - ApplicationEvent event = new PayloadApplicationEvent<>(this, 'payload') - - when: - publisher.publishEvent(event) - - then: - 0 * listener.onApplicationEvent(_) - } + static class TestEvent extends ApplicationEvent { - void "publishEvent skips a SmartApplicationListener that supports the event type but not the source type"() { - given: - SmartApplicationListener listener = Mock(SmartApplicationListener) { - supportsEventType(_) >> true - supportsSourceType(_) >> false + TestEvent(Object source) { + super(source) } - publisher.addApplicationListener(listener) - ApplicationEvent event = new PayloadApplicationEvent<>(this, 'payload') - - when: - publisher.publishEvent(event) - - then: - 0 * listener.onApplicationEvent(_) - } - - void "publishEvent(Object) wraps the payload in a PayloadApplicationEvent sourced from the publisher itself"() { - given: - ApplicationListener listener = Mock(ApplicationListener) - publisher.addApplicationListener(listener) - - when: - publisher.publishEvent('a plain payload') - - then: - 1 * listener.onApplicationEvent({ ApplicationEvent e -> - e instanceof PayloadApplicationEvent && - e.source.is(publisher) && - e.payload == 'a plain payload' - }) - } - - void "publishEvent(Object) applies the same SmartApplicationListener filtering as publishEvent(ApplicationEvent)"() { - given: - SmartApplicationListener supported = Mock(SmartApplicationListener) { - supportsEventType(_) >> true - supportsSourceType(_) >> true - } - SmartApplicationListener unsupported = Mock(SmartApplicationListener) { - supportsEventType(_) >> false - } - publisher.addApplicationListener(supported) - publisher.addApplicationListener(unsupported) - - when: - publisher.publishEvent('a plain payload') - - then: - 1 * supported.onApplicationEvent(_) - 0 * unsupported.onApplicationEvent(_) } } From f8f5d43864a0a73f4636ff3d7c46296c213a7016 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sat, 14 Nov 2026 10:31:52 -0800 Subject: [PATCH 035/115] Run the events of a save through another Neo4j connection Neo4jDatastore builds a datastore for each other connection, each with an AutoTimestampEventListener of its own, but only the default connection's datastore registered its listeners. The sessions of another connection publish their events with that connection's datastore as the source, which only its own listeners handle, so a save through it ran no domain class events and set no timestamps, and its listener, which a domain class now answers with, did nothing. The datastore of each other connection now registers the listeners that run the domain class events and set the timestamps, and keeps the tenant handling it had. --- .../datastore/gorm/neo4j/Neo4jDatastore.java | 21 ++++++- .../gorm/tests/NamedConnectionsSpec.groovy | 63 ++++++++++++++++++- 2 files changed, 81 insertions(+), 3 deletions(-) diff --git a/grails-data-neo4j/core/src/main/groovy/org/grails/datastore/gorm/neo4j/Neo4jDatastore.java b/grails-data-neo4j/core/src/main/groovy/org/grails/datastore/gorm/neo4j/Neo4jDatastore.java index 261cea92c89..9b33a326161 100644 --- a/grails-data-neo4j/core/src/main/groovy/org/grails/datastore/gorm/neo4j/Neo4jDatastore.java +++ b/grails-data-neo4j/core/src/main/groovy/org/grails/datastore/gorm/neo4j/Neo4jDatastore.java @@ -152,6 +152,10 @@ public Neo4jDatastore(ConnectionSources c childDatastore = new Neo4jDatastore(singletonConnectionSources, mappingContext, eventPublisher) { @Override protected GormEnhancer initialize(Neo4jConnectionSourceSettings settings) { + // The parent's enhancer serves this connection's entities, but the sessions of this + // connection publish their events with this datastore as the source, and only + // listeners of its own handle those. + registerEntityEventListeners(eventPublisher); return null; } }; @@ -433,13 +437,26 @@ public Neo4jMappingContext getMappingContext() { } protected void registerEventListeners(ConfigurableApplicationEventPublisher eventPublisher) { - eventPublisher.addApplicationListener(new DomainEventListener(this)); - eventPublisher.addApplicationListener(autoTimestampEventListener); + registerEntityEventListeners(eventPublisher); if (multiTenancyMode == MultiTenancySettings.MultiTenancyMode.DISCRIMINATOR) { eventPublisher.addApplicationListener(new MultiTenantEventListener(this)); } } + /** + * Registers the listeners that run the domain class events and set the timestamps of the entities saved through + * this datastore. The datastore of another connection registers only these: the tenant of a discriminator + * property is bound for the datastore of the default connection, and a tenant filter of its own would not see + * it. + * + * @param eventPublisher The publisher of this datastore's events + * @since 9.0 + */ + protected void registerEntityEventListeners(ConfigurableApplicationEventPublisher eventPublisher) { + eventPublisher.addApplicationListener(new DomainEventListener(this)); + eventPublisher.addApplicationListener(autoTimestampEventListener); + } + protected GormEnhancer initialize(Neo4jConnectionSourceSettings settings) { registerEventListeners(this.eventPublisher); diff --git a/grails-data-neo4j/core/src/test/groovy/grails/gorm/tests/NamedConnectionsSpec.groovy b/grails-data-neo4j/core/src/test/groovy/grails/gorm/tests/NamedConnectionsSpec.groovy index c8dd2af853f..3ce100158be 100644 --- a/grails-data-neo4j/core/src/test/groovy/grails/gorm/tests/NamedConnectionsSpec.groovy +++ b/grails-data-neo4j/core/src/test/groovy/grails/gorm/tests/NamedConnectionsSpec.groovy @@ -63,12 +63,56 @@ class NamedConnectionsSpec extends Specification { (Settings.SETTING_NEO4J_URL) : "bolt://localhost:${defaultPort}".toString(), (Settings.SETTING_NEO4J_BUILD_INDEX) : false, (Settings.SETTING_CONNECTIONS) : [other: [url: "bolt://localhost:${otherPort}".toString()]] - ], RoutedCompany) + ], RoutedCompany, TimestampedCompany) } void cleanup() { RoutedCompany.withTransaction { RoutedCompany.list()*.delete(flush: true) } RoutedCompany.other.withTransaction { RoutedCompany.other.list().each { RoutedCompany.other.delete(it, [flush: true]) } } + TimestampedCompany.other.withTransaction { TimestampedCompany.other.list().each { TimestampedCompany.other.delete(it, [flush: true]) } } + } + + void "test a save through a named connection runs the events and sets the timestamps"() { + given: + def company = new TimestampedCompany(name: 'draft') + + when: + TimestampedCompany.other.withTransaction { + TimestampedCompany.other.save(company, [flush: true]) + } + + then: + TimestampedCompany.other.count() == 1 + company.name == 'DRAFT' + company.dateCreated != null + company.lastUpdated != null + } + + void "test the listener of a named connection turns off the timestamps of the saves through it"() { + given: + Date past = new Date(110_000_000_000L) + def kept = new TimestampedCompany(name: 'kept', dateCreated: past, lastUpdated: past) + def stamped = new TimestampedCompany(name: 'stamped', dateCreated: past, lastUpdated: past) + def defaultListener = TimestampedCompany.autoTimestampEventListener + def otherListener = TimestampedCompany.other.autoTimestampEventListener + + when: + TimestampedCompany.other.withTransaction { + otherListener.withoutTimestamps { + TimestampedCompany.other.save(kept, [flush: true]) + } + defaultListener.withoutTimestamps { + TimestampedCompany.other.save(stamped, [flush: true]) + } + } + + then: + !otherListener.is(defaultListener) + otherListener.is(datastore.getDatastoreForConnection('other').autoTimestampEventListener) + kept.dateCreated == past + kept.lastUpdated == past + stamped.dateCreated != past + stamped.lastUpdated != past } void "test an entity is saved through a named connection to that connection's server"() { @@ -139,6 +183,23 @@ class RoutedCompanyService { } } +@Entity +class TimestampedCompany implements Neo4jEntity { + + Long id + String name + Date dateCreated + Date lastUpdated + + def beforeInsert() { + name = name.toUpperCase() + } + + static mapping = { + datasource 'ALL' + } +} + @Entity class RoutedCompany implements Neo4jEntity { Long id From 039e4ad6a516aaf726391fab221a4e3c91473a0c Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sat, 14 Nov 2026 10:42:40 -0800 Subject: [PATCH 036/115] Give the listener of every connection the auditor and the settings Only the default connection's AutoTimestampEventListener is a Spring bean, so only it was given the AuditorAware of the application context and the @Value settings autoTimestampInsertOverwrite and autoTimestampCacheAnnotations. The listener of every other connection, and of a tenant's database or schema, set no auditor properties and ignored both settings. The in-memory datastore's listeners in unit tests ignored both settings too. A listener now reads both settings from the configuration of its datastore when it is created, and one that is given neither an auditor nor an application context looks the AuditorAware up, once the context is active, in the application context of its datastore or of the datastore's event publisher, which the datastores of every connection share. --- ...mestampEventListenerConnectionsSpec.groovy | 130 ++++++++++++++++++ ...mestampEventListenerConnectionsSpec.groovy | 125 +++++++++++++++++ .../SimpleMapAutoTimestampListenerSpec.groovy | 29 ++++ .../events/AutoTimestampEventListener.java | 92 ++++++++++++- .../AutoTimestampEventListenerSpec.groovy | 69 +++++++++- 5 files changed, 437 insertions(+), 8 deletions(-) create mode 100644 grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/connections/AutoTimestampEventListenerConnectionsSpec.groovy create mode 100644 grails-data-mongodb/core/src/test/groovy/org/grails/datastore/gorm/mongo/connections/AutoTimestampEventListenerConnectionsSpec.groovy diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/connections/AutoTimestampEventListenerConnectionsSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/connections/AutoTimestampEventListenerConnectionsSpec.groovy new file mode 100644 index 00000000000..df43d5b119f --- /dev/null +++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/connections/AutoTimestampEventListenerConnectionsSpec.groovy @@ -0,0 +1,130 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.orm.hibernate.connections + +import java.util.function.Supplier + +import org.hibernate.dialect.H2Dialect +import spock.lang.AutoCleanup +import spock.lang.Specification + +import org.springframework.context.support.GenericApplicationContext + +import grails.gorm.annotation.CreatedBy +import grails.gorm.annotation.Entity +import grails.gorm.annotation.LastModifiedBy +import org.grails.datastore.gorm.timestamp.AuditorAware +import org.grails.datastore.mapping.config.Settings +import org.grails.datastore.mapping.core.DatastoreUtils +import org.grails.orm.hibernate.HibernateDatastore + +/** + * The listener of every connection, not only that of the default one, sets the auditor properties and follows the + * auto-timestamp settings of the configuration. + */ +class AutoTimestampEventListenerConnectionsSpec extends Specification { + + private static final Date PAST = new Date(110_000_000_000L) + + @AutoCleanup + GenericApplicationContext context + + @AutoCleanup + HibernateDatastore datastore + + void 'every connection sets the auditor properties from the application context of the datastore'() { + given: + context = new GenericApplicationContext() + context.registerBean(AuditorAware, { { -> Optional.of('auditor') } as AuditorAware } as Supplier) + context.refresh() + datastore = new HibernateDatastore(DatastoreUtils.createPropertyResolver(configuration()), AuditedConnectionNote) + datastore.setApplicationContext(context) + AuditedConnectionNote onDefault = new AuditedConnectionNote(text: 'default') + AuditedConnectionNote onSecondary = new AuditedConnectionNote(text: 'secondary') + + when: + AuditedConnectionNote.withTransaction { + onDefault.save(flush: true) + } + AuditedConnectionNote.secondary.withTransaction { + onSecondary.secondary.save(flush: true) + } + + then: + onDefault.createdBy == 'auditor' + onDefault.lastModifiedBy == 'auditor' + onSecondary.createdBy == 'auditor' + onSecondary.lastModifiedBy == 'auditor' + } + + void 'every connection keeps a given timestamp when the configuration turns overwriting off'() { + given: + datastore = new HibernateDatastore(DatastoreUtils.createPropertyResolver(configuration() + [ + (Settings.SETTING_AUTO_TIMESTAMP_INSERT_OVERWRITE): false + ]), AuditedConnectionNote) + AuditedConnectionNote onDefault = new AuditedConnectionNote(text: 'default', dateCreated: PAST) + AuditedConnectionNote onSecondary = new AuditedConnectionNote(text: 'secondary', dateCreated: PAST) + + when: + AuditedConnectionNote.withTransaction { + onDefault.save(flush: true) + } + AuditedConnectionNote.secondary.withTransaction { + onSecondary.secondary.save(flush: true) + } + + then: + AuditedConnectionNote.withNewTransaction { AuditedConnectionNote.get(onDefault.id).dateCreated } == PAST + AuditedConnectionNote.secondary.withNewTransaction { AuditedConnectionNote.secondary.get(onSecondary.id).dateCreated } == PAST + onSecondary.lastUpdated != null + } + + private static Map configuration() { + [ + 'dataSource.url' : 'jdbc:h2:mem:timestampConnectionsDefaultDB;LOCK_TIMEOUT=10000', + 'dataSource.dbCreate' : 'create-drop', + 'dataSource.dialect' : H2Dialect.name, + 'hibernate.hbm2ddl.auto': 'create', + 'dataSources.secondary' : [url: 'jdbc:h2:mem:timestampConnectionsSecondaryDB;LOCK_TIMEOUT=10000'] + ] as Map + } +} + +@Entity +class AuditedConnectionNote { + + String text + Date dateCreated + Date lastUpdated + + @CreatedBy + String createdBy + + @LastModifiedBy + String lastModifiedBy + + static constraints = { + createdBy nullable: true + lastModifiedBy nullable: true + } + + static mapping = { + datasource 'ALL' + } +} diff --git a/grails-data-mongodb/core/src/test/groovy/org/grails/datastore/gorm/mongo/connections/AutoTimestampEventListenerConnectionsSpec.groovy b/grails-data-mongodb/core/src/test/groovy/org/grails/datastore/gorm/mongo/connections/AutoTimestampEventListenerConnectionsSpec.groovy new file mode 100644 index 00000000000..a8f62c78267 --- /dev/null +++ b/grails-data-mongodb/core/src/test/groovy/org/grails/datastore/gorm/mongo/connections/AutoTimestampEventListenerConnectionsSpec.groovy @@ -0,0 +1,125 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.datastore.gorm.mongo.connections + +import java.util.function.Supplier + +import spock.lang.AutoCleanup + +import org.springframework.context.support.GenericApplicationContext + +import grails.gorm.annotation.CreatedBy +import grails.gorm.annotation.Entity +import grails.gorm.annotation.LastModifiedBy +import org.apache.grails.testing.mongo.AutoStartedMongoSpec +import org.grails.datastore.gorm.events.ConfigurableApplicationContextEventPublisher +import org.grails.datastore.gorm.timestamp.AuditorAware +import org.grails.datastore.mapping.config.Settings +import org.grails.datastore.mapping.mongo.MongoDatastore + +/** + * The listener of every connection, not only that of the default one, sets the auditor properties from the + * application context of the datastore's event publisher, follows the auto-timestamp settings of the configuration, + * and is the one a domain class answers with. + */ +class AutoTimestampEventListenerConnectionsSpec extends AutoStartedMongoSpec { + + private static final Date PAST = new Date(110_000_000_000L) + + @AutoCleanup + GenericApplicationContext context + + @AutoCleanup + MongoDatastore datastore + + @Override + boolean shouldInitializeDatastore() { + false + } + + void cleanup() { + datastore?.mongoClient?.listDatabaseNames()?.findAll { String name -> name.startsWith('timestampConnections') } + ?.each { String name -> datastore.mongoClient.getDatabase(name).drop() } + } + + void 'every connection sets the auditor properties from the application context of the publisher'() { + given: + context = new GenericApplicationContext() + context.registerBean(AuditorAware, { { -> Optional.of('auditor') } as AuditorAware } as Supplier) + context.refresh() + datastore = new MongoDatastore(configuration(), new ConfigurableApplicationContextEventPublisher(context), AuditedMongoNote) + AuditedMongoNote onDefault = new AuditedMongoNote(text: 'default') + AuditedMongoNote onOther = new AuditedMongoNote(text: 'other') + + when: + onDefault.save(flush: true) + AuditedMongoNote.other.withSession { + AuditedMongoNote.other.save(onOther, [flush: true]) + } + + then: + AuditedMongoNote.other.withSession { AuditedMongoNote.other.count() } == 1 + onDefault.createdBy == 'auditor' + onOther.createdBy == 'auditor' + onOther.lastModifiedBy == 'auditor' + !AuditedMongoNote.other.autoTimestampEventListener.is(AuditedMongoNote.autoTimestampEventListener) + } + + void 'every connection keeps a given timestamp when the configuration turns overwriting off'() { + given: + datastore = new MongoDatastore(configuration() + [(Settings.SETTING_AUTO_TIMESTAMP_INSERT_OVERWRITE): false], AuditedMongoNote) + AuditedMongoNote onOther = new AuditedMongoNote(text: 'other', dateCreated: PAST) + + when: + AuditedMongoNote.other.withSession { + AuditedMongoNote.other.save(onOther, [flush: true]) + } + + then: + AuditedMongoNote.other.withNewSession { AuditedMongoNote.other.get(onOther.id).dateCreated } == PAST + onOther.lastUpdated != null + } + + private Map configuration() { + [ + 'grails.mongodb.url' : "mongodb://${mongoHost}:${mongoPort}/timestampConnectionsDefault".toString(), + 'grails.mongodb.connections': [ + other: [url: "mongodb://${mongoHost}:${mongoPort}/timestampConnectionsOther".toString()] + ] + ] as Map + } +} + +@Entity +class AuditedMongoNote { + + String text + Date dateCreated + Date lastUpdated + + @CreatedBy + String createdBy + + @LastModifiedBy + String lastModifiedBy + + static mapping = { + connections 'default', 'other' + } +} diff --git a/grails-data-simple/src/test/groovy/org/grails/datastore/mapping/simple/SimpleMapAutoTimestampListenerSpec.groovy b/grails-data-simple/src/test/groovy/org/grails/datastore/mapping/simple/SimpleMapAutoTimestampListenerSpec.groovy index 2d3a8230486..e17eee4e135 100644 --- a/grails-data-simple/src/test/groovy/org/grails/datastore/mapping/simple/SimpleMapAutoTimestampListenerSpec.groovy +++ b/grails-data-simple/src/test/groovy/org/grails/datastore/mapping/simple/SimpleMapAutoTimestampListenerSpec.groovy @@ -97,6 +97,35 @@ class SimpleMapAutoTimestampListenerSpec extends Specification { overwritten.lastUpdated != PAST } + void 'every connection follows the auto-timestamp settings of the configuration'() { + given: + datastore = new SimpleMapDatastore( + DatastoreUtils.createPropertyResolver([ + (Settings.SETTING_AUTO_TIMESTAMP_INSERT_OVERWRITE) : false, + (Settings.SETTING_AUTO_TIMESTAMP_CACHE_ANNOTATIONS): false + ]), + ['secondary'], + ListenerNote + ) + ListenerNote onDefault = new ListenerNote(text: 'default', dateCreated: PAST) + ListenerNote onSecondary = new ListenerNote(text: 'secondary', dateCreated: PAST) + + when: + ListenerNote.withTransaction { + onDefault.save(flush: true) + } + ListenerNote.secondary.withTransaction { + onSecondary.secondary.save(flush: true) + } + + then: + onDefault.dateCreated == PAST + onSecondary.dateCreated == PAST + onSecondary.lastUpdated != null + !ListenerNote.autoTimestampEventListener.cacheAutoTimestampAnnotations + !ListenerNote.secondary.autoTimestampEventListener.cacheAutoTimestampAnnotations + } + void 'a tenant with a database of its own answers with the listener of that database'() { given: datastore = new SimpleMapDatastore( diff --git a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/events/AutoTimestampEventListener.java b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/events/AutoTimestampEventListener.java index 957eefb8a7c..f301b72abe4 100644 --- a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/events/AutoTimestampEventListener.java +++ b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/events/AutoTimestampEventListener.java @@ -38,6 +38,9 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationEvent; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.core.env.PropertyResolver; import org.grails.datastore.gorm.timestamp.AuditorAware; import org.grails.datastore.gorm.timestamp.DefaultTimestampProvider; @@ -45,7 +48,10 @@ import org.grails.datastore.mapping.config.AuditMetadataType; import org.grails.datastore.mapping.config.Entity; import org.grails.datastore.mapping.config.Settings; +import org.grails.datastore.mapping.core.AbstractDatastore; import org.grails.datastore.mapping.core.Datastore; +import org.grails.datastore.mapping.core.connections.ConnectionSources; +import org.grails.datastore.mapping.core.connections.ConnectionSourcesProvider; import org.grails.datastore.mapping.engine.EntityAccess; import org.grails.datastore.mapping.engine.event.AbstractPersistenceEvent; import org.grails.datastore.mapping.engine.event.AbstractPersistenceEventListener; @@ -89,11 +95,27 @@ public class AutoTimestampEventListener extends AbstractPersistenceEventListener private final ThreadLocal disabledLastUpdated = new ThreadLocal<>(); private TimestampProvider timestampProvider = new DefaultTimestampProvider(); - private AuditorAware auditorAware; + private volatile AuditorAware auditorAware; + // Whether auditorAware holds the outcome of a lookup, or a value that was set; until then it is looked up from + // the application context of the datastore when an entity is saved. + private volatile boolean auditorAwareResolved; + + /** + * Creates the listener of the datastore, which reads {@link Settings#SETTING_AUTO_TIMESTAMP_INSERT_OVERWRITE} and + * {@link Settings#SETTING_AUTO_TIMESTAMP_CACHE_ANNOTATIONS} from the datastore's configuration. Unless an + * {@link AuditorAware} is set, or the listener is given an application context, it looks the auditor up in the + * application context of the datastore when an entity is first saved. + * + * @param datastore The datastore whose entities the listener handles + */ public AutoTimestampEventListener(final Datastore datastore) { super(datastore); + // Only a listener that is a Spring bean gets the @Value settings injected, and that is the listener of the + // default connection only; the listener of every other connection, and of a tenant's database or schema, + // takes them from the configuration of its datastore. + applySettings(datastore); MappingContext mappingContext = datastore.getMappingContext(); initForMappingContext(mappingContext); } @@ -106,9 +128,50 @@ protected AutoTimestampEventListener(final MappingContext mappingContext) { @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + this.auditorAware = lookUpAuditorAware(applicationContext); + this.auditorAwareResolved = true; + } + + private void applySettings(Datastore datastore) { + if (!(datastore instanceof ConnectionSourcesProvider connectionSourcesProvider)) { + return; + } + ConnectionSources connectionSources = connectionSourcesProvider.getConnectionSources(); + PropertyResolver configuration = connectionSources != null ? connectionSources.getBaseConfiguration() : null; + if (configuration != null) { + insertOverwrite = configuration.getProperty(Settings.SETTING_AUTO_TIMESTAMP_INSERT_OVERWRITE, Boolean.class, insertOverwrite); + cacheAutoTimestampAnnotations = configuration.getProperty(Settings.SETTING_AUTO_TIMESTAMP_CACHE_ANNOTATIONS, Boolean.class, cacheAutoTimestampAnnotations); + } + } + + private static AuditorAware lookUpAuditorAware(ApplicationContext applicationContext) { try { - this.auditorAware = applicationContext.getBean(AuditorAware.class); - } catch (BeansException ignore) {} + return applicationContext.getBean(AuditorAware.class); + } catch (BeansException ignore) { + return null; + } + } + + /** + * @return the application context of the datastore, once it is active, from the datastore or else from its + * event publisher, or {@code null} + */ + private ApplicationContext activeApplicationContextOfDatastore() { + ApplicationContext applicationContext = null; + if (datastore instanceof AbstractDatastore abstractDatastore) { + applicationContext = abstractDatastore.getApplicationContext(); + } + if (applicationContext == null && datastore != null) { + ApplicationEventPublisher publisher = datastore.getApplicationEventPublisher(); + if (publisher instanceof ConfigurableApplicationContextEventPublisher contextEventPublisher) { + applicationContext = contextEventPublisher.getApplicationContext(); + } + } + if (applicationContext instanceof ConfigurableApplicationContext configurableApplicationContext && + !configurableApplicationContext.isActive()) { + return null; + } + return applicationContext; } protected void initForMappingContext(MappingContext mappingContext) { @@ -163,8 +226,9 @@ public boolean beforeInsert(PersistentEntity entity, EntityAccess ea) { } // Handle auditor fields - if (auditorAware != null) { - Optional currentAuditor = auditorAware.getCurrentAuditor(); + AuditorAware currentAuditorAware = getAuditorAware(); + if (currentAuditorAware != null) { + Optional currentAuditor = currentAuditorAware.getCurrentAuditor(); if (currentAuditor.isPresent()) { Object auditor = currentAuditor.get(); @@ -209,8 +273,9 @@ public boolean beforeUpdate(PersistentEntity entity, EntityAccess ea) { } // Handle auditor fields - if (auditorAware != null) { - Optional currentAuditor = auditorAware.getCurrentAuditor(); + AuditorAware currentAuditorAware = getAuditorAware(); + if (currentAuditorAware != null) { + Optional currentAuditor = currentAuditorAware.getCurrentAuditor(); if (currentAuditor.isPresent()) { Object auditor = currentAuditor.get(); @@ -307,12 +372,25 @@ public void setTimestampProvider(TimestampProvider timestampProvider) { this.timestampProvider = timestampProvider; } + /** + * @return the auditor that the auditor properties are set from: the one that was set, the one of the application + * context the listener was given, or else the one of the datastore's application context, which is looked up + * once that context is active; {@code null} when there is none + */ public AuditorAware getAuditorAware() { + if (!auditorAwareResolved) { + ApplicationContext applicationContext = activeApplicationContextOfDatastore(); + if (applicationContext != null) { + auditorAware = lookUpAuditorAware(applicationContext); + auditorAwareResolved = true; + } + } return auditorAware; } public void setAuditorAware(AuditorAware auditorAware) { this.auditorAware = auditorAware; + this.auditorAwareResolved = true; } private static boolean isDisabled(final ThreadLocal disabledTimestamps, final String entityName) { diff --git a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/timestamp/AutoTimestampEventListenerSpec.groovy b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/timestamp/AutoTimestampEventListenerSpec.groovy index 743cbe2346f..cf45f3a200d 100644 --- a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/timestamp/AutoTimestampEventListenerSpec.groovy +++ b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/timestamp/AutoTimestampEventListenerSpec.groovy @@ -26,13 +26,17 @@ import java.util.concurrent.ExecutorService import java.util.concurrent.Executors import java.util.concurrent.Future import java.util.concurrent.TimeUnit +import java.util.function.Supplier import groovy.transform.InheritConstructors import spock.lang.Specification +import org.springframework.context.support.GenericApplicationContext + import grails.gorm.annotation.CreatedBy import grails.gorm.annotation.LastModifiedBy import org.grails.datastore.gorm.events.AutoTimestampEventListener +import org.grails.datastore.gorm.events.ConfigurableApplicationContextEventPublisher import org.grails.datastore.mapping.config.Property import org.grails.datastore.mapping.core.Datastore import org.grails.datastore.mapping.engine.EntityAccess @@ -633,6 +637,65 @@ class AutoTimestampEventListenerSpec extends Specification { updated == [lastModifiedBy: 'testUser'] } + void "a listener that is not given an auditor looks it up in the application context of its datastore once that is active"() { + given: 'the publisher of the datastore, which the datastores of every connection share, holds the context' + AuditorAware contextAuditor = { -> Optional.of('contextUser') } as AuditorAware + GenericApplicationContext context = new GenericApplicationContext() + context.registerBean(AuditorAware, { contextAuditor } as Supplier) + TestEventListener contextListener = new TestEventListener(Stub(Datastore) { + getMappingContext() >> null + getApplicationEventPublisher() >> new ConfigurableApplicationContextEventPublisher(context) + }) + + expect: 'no auditor while the context is not refreshed' + contextListener.auditorAware == null + + when: + context.refresh() + contextListener.persistentEntityAdded(auditedEntity()) + + then: + contextListener.auditorAware.is(contextAuditor) + appliedOnInsert(contextListener, Audited) == [createdBy: 'contextUser', lastModifiedBy: 'contextUser'] + + cleanup: + context.close() + } + + void "an auditor that is set, or found in the application context given to the listener, is not looked up again"() { + given: + AuditorAware setAuditor = { -> Optional.of('setUser') } as AuditorAware + AuditorAware givenAuditor = { -> Optional.of('givenUser') } as AuditorAware + GenericApplicationContext datastoreContext = new GenericApplicationContext() + datastoreContext.registerBean(AuditorAware, { { -> Optional.of('datastoreUser') } as AuditorAware } as Supplier) + datastoreContext.refresh() + GenericApplicationContext givenContext = new GenericApplicationContext() + givenContext.registerBean(AuditorAware, { givenAuditor } as Supplier) + givenContext.refresh() + Datastore datastore = Stub(Datastore) { + getMappingContext() >> null + getApplicationEventPublisher() >> new ConfigurableApplicationContextEventPublisher(datastoreContext) + } + TestEventListener withSetAuditor = new TestEventListener(datastore) + TestEventListener withGivenContext = new TestEventListener(datastore) + TestEventListener withEmptyContext = new TestEventListener(datastore) + GenericApplicationContext emptyContext = new GenericApplicationContext() + emptyContext.refresh() + + when: + withSetAuditor.auditorAware = setAuditor + withGivenContext.applicationContext = givenContext + withEmptyContext.applicationContext = emptyContext + + then: + withSetAuditor.auditorAware.is(setAuditor) + withGivenContext.auditorAware.is(givenAuditor) + withEmptyContext.auditorAware == null + + cleanup: + [datastoreContext, givenContext, emptyContext]*.close() + } + private PersistentEntity auditedEntity() { PersistentEntity entity = null PersistentProperty createdBy = auditedProperty('createdBy') { -> entity } @@ -660,8 +723,12 @@ class AutoTimestampEventListenerSpec extends Specification { } private Map appliedOnInsert(Class clazz) { + appliedOnInsert(listener, clazz) + } + + private static Map appliedOnInsert(AutoTimestampEventListener eventListener, Class clazz) { Map applied = new ConcurrentHashMap<>() - listener.beforeInsert(entityFor(clazz), recordingAccess(applied)) + eventListener.beforeInsert(entityFor(clazz), recordingAccess(applied)) applied } From be2e1f6e6962569ee7734f6c87e6181a26730169 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sat, 14 Nov 2026 10:45:13 -0800 Subject: [PATCH 037/115] Keep the multi-connection TCK spec true of a single-session datastore The spec compared the listener of a class mapped only to the secondary connection with the default one, and saved such a class unqualified. A datastore with a single session, as the in-memory one of unit tests, keeps the unqualified operations of such a class on the default connection, so both were the default listener there. The comparisons and the cross-connection save now use the class mapped to every connection and name the secondary connection. --- ...ampEventListenerMultiDataSourceSpec.groovy | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/tests/AutoTimestampEventListenerMultiDataSourceSpec.groovy b/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/tests/AutoTimestampEventListenerMultiDataSourceSpec.groovy index 143025714d6..83b030e56cc 100644 --- a/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/tests/AutoTimestampEventListenerMultiDataSourceSpec.groovy +++ b/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/tests/AutoTimestampEventListenerMultiDataSourceSpec.groovy @@ -28,7 +28,9 @@ import org.apache.grails.data.testing.tck.domains.SecondaryConnectionTimestampNo /** * Every connection has an auto-timestamp listener of its own, and a domain class answers with the listener of the - * connection its saves go through. + * connection its saves go through. Which connection that is for the unqualified operations of a class mapped only to + * another connection is the datastore's own: a datastore with a single session, as the in-memory one of unit tests, + * keeps them on the default connection. */ @Issue('https://github.com/apache/grails-core/issues/14509') @Requires({ instance.manager?.supportsMultipleDataSources() }) @@ -46,13 +48,12 @@ class AutoTimestampEventListenerMultiDataSourceSpec extends GrailsDataTckSpec { void 'each connection answers with a listener of its own'() { given: - def defaultListener = DefaultConnectionTimestampNote.autoTimestampEventListener - def secondaryListener = SecondaryConnectionTimestampNote.autoTimestampEventListener + def defaultListener = AllConnectionsTimestampNote.autoTimestampEventListener + def secondaryListener = AllConnectionsTimestampNote.secondary.autoTimestampEventListener expect: !defaultListener.is(secondaryListener) - AllConnectionsTimestampNote.autoTimestampEventListener.is(defaultListener) - AllConnectionsTimestampNote.secondary.autoTimestampEventListener.is(secondaryListener) + DefaultConnectionTimestampNote.autoTimestampEventListener.is(defaultListener) AllConnectionsTimestampNote.secondary.withTransaction { AllConnectionsTimestampNote.autoTimestampEventListener }.is(secondaryListener) } @@ -82,18 +83,19 @@ class AutoTimestampEventListenerMultiDataSourceSpec extends GrailsDataTckSpec { void 'the listener of one connection does not reach a save through another'() { given: - SecondaryConnectionTimestampNote person = new SecondaryConnectionTimestampNote(text: 'person', dateCreated: PAST, lastUpdated: PAST) + def defaultListener = AllConnectionsTimestampNote.autoTimestampEventListener + AllConnectionsTimestampNote note = new AllConnectionsTimestampNote(text: 'shared', dateCreated: PAST, lastUpdated: PAST) when: - SecondaryConnectionTimestampNote.withNewTransaction { - DefaultConnectionTimestampNote.autoTimestampEventListener.withoutTimestamps { - person.save(flush: true) + AllConnectionsTimestampNote.secondary.withNewTransaction { + defaultListener.withoutTimestamps { + note.secondary.save(flush: true) } } then: - SecondaryConnectionTimestampNote.withNewTransaction { SecondaryConnectionTimestampNote.get(person.id).dateCreated } != PAST - SecondaryConnectionTimestampNote.withNewTransaction { SecondaryConnectionTimestampNote.get(person.id).lastUpdated } != PAST + AllConnectionsTimestampNote.secondary.withNewTransaction { AllConnectionsTimestampNote.secondary.get(note.id).dateCreated } != PAST + AllConnectionsTimestampNote.secondary.withNewTransaction { AllConnectionsTimestampNote.secondary.get(note.id).lastUpdated } != PAST } void 'a domain class on several connections turns off the timestamps of the connection it names'() { From 309b9b44f7a262f17b93e73e8992d2e676e736df Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sat, 14 Nov 2026 12:18:15 -0800 Subject: [PATCH 038/115] Document the listener property's name clash and the implementer's side A domain class that declares its own autoTimestampEventListener property no longer compiles, or hides the static one when it is typed AutoTimestampEventListener; upgrade note 26 says to rename it. The implementer guide's sample registered a listener the datastore did not keep, which AutoTimestampEventListenerLookupSpec now fails; it registers the datastore's own listener and says that a datastore registers the listeners of each of its connections. --- .../guide-developer/src/main/docs/stepByStep.adoc | 4 ++-- grails-doc/src/en/guide/upgrading/upgrading90x.adoc | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/grails-data-docs/guide-developer/src/main/docs/stepByStep.adoc b/grails-data-docs/guide-developer/src/main/docs/stepByStep.adoc index 3f0d77cddc3..23ff5aa9df2 100644 --- a/grails-data-docs/guide-developer/src/main/docs/stepByStep.adoc +++ b/grails-data-docs/guide-developer/src/main/docs/stepByStep.adoc @@ -135,14 +135,14 @@ class Setup { xyz.mappingContext.addMappingContextListener({ e -> enhancer.enhance e } as MappingContext.Listener) xyz.applicationContext.addApplicationListener new DomainEventListener(xyz) - xyz.applicationContext.addApplicationListener new AutoTimestampEventListener(xyz) + xyz.applicationContext.addApplicationListener xyz.autoTimestampEventListener xyz.connect() } } ---- -A datastore that sets timestamps keeps the `AutoTimestampEventListener` it registers for itself, implements `AutoTimestampingDatastore` and returns that listener from `getAutoTimestampEventListener()`, so that `Book.autoTimestampEventListener` on a domain class finds it. The TCK's `AutoTimestampEventListenerLookupSpec` checks this. +Here `XyzDatastore` implements `AutoTimestampingDatastore`: it creates its `AutoTimestampEventListener` once, as `new AutoTimestampEventListener(this)`, and returns it from `getAutoTimestampEventListener()`, so that `Book.autoTimestampEventListener` on a domain class finds the listener that handles the class's saves. The TCK's `AutoTimestampEventListenerLookupSpec` checks this. A datastore that builds a datastore for each of its connections registers the listeners of each one, because a listener only handles the events whose source is its own datastore. Then in `src/test/groovy` create test suite class to allow running tests in IDE (without this you won't be able to run TCK tests from the IDE). Example test suite: diff --git a/grails-doc/src/en/guide/upgrading/upgrading90x.adoc b/grails-doc/src/en/guide/upgrading/upgrading90x.adoc index d18a587f1ef..c42e710c723 100644 --- a/grails-doc/src/en/guide/upgrading/upgrading90x.adoc +++ b/grails-doc/src/en/guide/upgrading/upgrading90x.adoc @@ -1461,3 +1461,11 @@ In a unit test that implements `DataTest` or `DomainUnitTest`, a save through a * A test that expected such a save to keep the `dateCreated` and `lastUpdated` it was given saves inside a `withoutTimestamps { }` block of that data source's listener, such as `Book.secondary.autoTimestampEventListener`. * A test that expected a domain class event not to run on such a save expects what the event does. + +==== 26. A Domain Class Has an `autoTimestampEventListener` Property + +Every domain class now has a static `autoTimestampEventListener` property, the listener that sets its timestamp and auditor properties (see the Events and Auto Timestamping section of the GORM for link:{hibernate7Guide}index.html#eventsAutoTimestamping[Hibernate 7] guide). A domain class that declares a property of that name of another type no longer compiles: the compiler reports that the return type of its `getAutoTimestampEventListener()` is incompatible with the one in `GormEntity`. A property of that name typed `AutoTimestampEventListener`, such as an injected bean, compiles but hides the static property, so `Book.autoTimestampEventListener` throws `MissingPropertyException`. + +===== Required Actions + +* Rename a property of a domain class named `autoTimestampEventListener`. Code that used an injected `autoTimestampEventListener` bean in a domain class reads `Book.autoTimestampEventListener` instead, which is the listener of the data source the class's saves go to. From 6f0c59d72d61414a5a9205b794618f95b51ce17c Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sat, 14 Nov 2026 12:20:00 -0800 Subject: [PATCH 039/115] Keep the default connection's listener first in the mapping context The in-memory datastore builds the datastores of its other connections before its own listener, and each of them now has a listener, which registers itself with the mapping context they share. Code that found the listener as the first AutoTimestampEventListener of the mapping context, the workaround from #14509, then found another connection's, which does not reach the saves the datastore keeps on the default connection. The default connection's listener is now created ahead of them. --- .../datastore/mapping/simple/SimpleMapDatastore.java | 6 ++++-- .../simple/SimpleMapAutoTimestampListenerSpec.groovy | 8 ++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/grails-data-simple/src/main/groovy/org/grails/datastore/mapping/simple/SimpleMapDatastore.java b/grails-data-simple/src/main/groovy/org/grails/datastore/mapping/simple/SimpleMapDatastore.java index 6fd79554901..5e78a4d0893 100644 --- a/grails-data-simple/src/main/groovy/org/grails/datastore/mapping/simple/SimpleMapDatastore.java +++ b/grails-data-simple/src/main/groovy/org/grails/datastore/mapping/simple/SimpleMapDatastore.java @@ -95,7 +95,7 @@ public class SimpleMapDatastore extends AbstractDatastore implements Closeable, protected final Map datastoresByConnectionSource = new LinkedHashMap<>(); protected final boolean failOnError; private final SimpleMapQueryTail queryTail; - private AutoTimestampEventListener autoTimestampEventListener; + private final AutoTimestampEventListener autoTimestampEventListener; public SimpleMapDatastore(ConnectionSources, ConnectionSourceSettings> connectionSources, MappingContext mappingContext, ConfigurableApplicationEventPublisher eventPublisher) { super(mappingContext); @@ -111,6 +111,9 @@ public SimpleMapDatastore(ConnectionSources, ConnectionSourceSe this.tenantResolver = multiTenancy.getTenantResolver(); PropertyResolver config = connectionSources.getBaseConfiguration(); this.failOnError = config.getProperty(Settings.SETTING_FAIL_ON_ERROR, Boolean.class, false); + // Created ahead of the datastores of the other connections, so that it stays the first one the shared mapping + // context lists, as it was when only this datastore had a listener. + this.autoTimestampEventListener = new AutoTimestampEventListener(this); if (!(connectionSources instanceof SingletonConnectionSources)) { Iterable, ConnectionSourceSettings>> allConnectionSources = connectionSources.getAllConnectionSources(); @@ -314,7 +317,6 @@ protected void registerEventListeners(ConfigurableApplicationEventPublisher even */ protected void registerEntityEventListeners(ConfigurableApplicationEventPublisher eventPublisher) { eventPublisher.addApplicationListener(new DomainEventListener(this)); - autoTimestampEventListener = new AutoTimestampEventListener(this); eventPublisher.addApplicationListener(autoTimestampEventListener); } diff --git a/grails-data-simple/src/test/groovy/org/grails/datastore/mapping/simple/SimpleMapAutoTimestampListenerSpec.groovy b/grails-data-simple/src/test/groovy/org/grails/datastore/mapping/simple/SimpleMapAutoTimestampListenerSpec.groovy index e17eee4e135..b91f0b14701 100644 --- a/grails-data-simple/src/test/groovy/org/grails/datastore/mapping/simple/SimpleMapAutoTimestampListenerSpec.groovy +++ b/grails-data-simple/src/test/groovy/org/grails/datastore/mapping/simple/SimpleMapAutoTimestampListenerSpec.groovy @@ -61,6 +61,14 @@ class SimpleMapAutoTimestampListenerSpec extends Specification { !secondary.autoTimestampEventListener.is(datastore.autoTimestampEventListener) } + void 'the first auto-timestamp listener of the mapping context is still the default connection\'s'() { + given: 'code that looked the listener up through the mapping context, before domain classes answered with it' + datastore = new SimpleMapDatastore(['secondary', 'tertiary'], ListenerNote) + + expect: + datastore.mappingContext.eventListeners.find { it instanceof AutoTimestampEventListener }.is(datastore.autoTimestampEventListener) + } + void 'a transaction for a connection routes the unqualified lookup to that connection'() { given: datastore = new SimpleMapDatastore(['secondary'], ListenerNote) From 5c7c1fcdd468622267365bdfa3072c44c25b823e Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Tue, 17 Nov 2026 07:33:38 -0800 Subject: [PATCH 040/115] Note what saves through other data sources now do in the upgrade guide Saves through a data source other than the default one now set the auditor properties and follow the auto-timestamp settings, and with Neo4j run the domain class events. Upgrade note 27 lists the three changes for an application coming from 8. --- grails-doc/src/en/guide/upgrading/upgrading90x.adoc | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/grails-doc/src/en/guide/upgrading/upgrading90x.adoc b/grails-doc/src/en/guide/upgrading/upgrading90x.adoc index c42e710c723..d918d5486c1 100644 --- a/grails-doc/src/en/guide/upgrading/upgrading90x.adoc +++ b/grails-doc/src/en/guide/upgrading/upgrading90x.adoc @@ -1469,3 +1469,15 @@ Every domain class now has a static `autoTimestampEventListener` property, the l ===== Required Actions * Rename a property of a domain class named `autoTimestampEventListener`. Code that used an injected `autoTimestampEventListener` bean in a domain class reads `Book.autoTimestampEventListener` instead, which is the listener of the data source the class's saves go to. + +==== 27. Every Data Source Sets the Auditor Properties and Follows the Auto-Timestamp Settings + +In an application with several data sources, or with a database or a schema per tenant, a save through a data source other than the default one: + +* sets the `@CreatedBy` and `@LastModifiedBy` properties from the application's `AuditorAware`. In Grails 8 only a save through the default data source set them. +* follows `grails.gorm.events.autoTimestampInsertOverwrite` and `grails.gorm.autoTimestampCacheAnnotations`. In Grails 8 it overwrote a `dateCreated` or `lastUpdated` that an instance was saved with, whatever `autoTimestampInsertOverwrite` said. +* with GORM for Neo4j, runs the domain class's events, such as `beforeInsert`, and sets `dateCreated` and `lastUpdated`. In Grails 8 a save through another Neo4j connection ran neither. + +===== Required Actions + +For most applications **no changes are required**. Check the domain classes saved through a data source other than the default one whose auditor properties, given timestamps or, with Neo4j, events were expected to be left alone. From 706a68c2d8a98973df2aa3789b51eaf4b1455700 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sat, 14 Nov 2026 12:31:31 -0800 Subject: [PATCH 041/115] Check the plain class's listener against the datastore holding its row The feature for a class that is not multi-tenant, saved inside a tenant block, asserted that the save went to the tenant's datastore. Where it goes is the datastore's routing, not what the accessor promises, so the feature now checks that the timestamps were turned off for the save, by the listener of whichever datastore holds the saved instance. --- .../SimpleMapAutoTimestampListenerSpec.groovy | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/grails-data-simple/src/test/groovy/org/grails/datastore/mapping/simple/SimpleMapAutoTimestampListenerSpec.groovy b/grails-data-simple/src/test/groovy/org/grails/datastore/mapping/simple/SimpleMapAutoTimestampListenerSpec.groovy index b91f0b14701..6b588ece3c2 100644 --- a/grails-data-simple/src/test/groovy/org/grails/datastore/mapping/simple/SimpleMapAutoTimestampListenerSpec.groovy +++ b/grails-data-simple/src/test/groovy/org/grails/datastore/mapping/simple/SimpleMapAutoTimestampListenerSpec.groovy @@ -29,6 +29,8 @@ import grails.gorm.transactions.Transactional import org.grails.datastore.gorm.events.AutoTimestampEventListener import org.grails.datastore.mapping.config.Settings import org.grails.datastore.mapping.core.DatastoreUtils +import org.grails.datastore.mapping.core.Session +import org.grails.datastore.mapping.core.SessionCallback import org.grails.datastore.mapping.core.connections.ConnectionSource import org.grails.datastore.mapping.multitenancy.MultiTenancySettings import org.grails.datastore.mapping.multitenancy.resolvers.SystemPropertyTenantResolver @@ -161,7 +163,7 @@ class SimpleMapAutoTimestampListenerSpec extends Specification { } void 'a class that is not multi-tenant answers inside a tenant block with the listener of the datastore that saves it'() { - given: 'the in-memory datastore runs the operations of every class inside a tenant block on that tenant\'s database' + given: datastore = new SimpleMapDatastore( DatastoreUtils.createPropertyResolver([ (Settings.SETTING_MULTI_TENANCY_MODE) : MultiTenancySettings.MultiTenancyMode.DATABASE, @@ -180,11 +182,17 @@ class SimpleMapAutoTimestampListenerSpec extends Specification { ListenerNote.autoTimestampEventListener } - then: - Tenants.withId('foo') { ListenerNote.count() } == 1 - listener.is(datastore.getDatastoreForConnection('foo').autoTimestampEventListener) + then: 'the timestamps were turned off for the save' note.dateCreated == PAST note.lastUpdated == PAST + + and: 'by the listener of the datastore that holds the saved instance' + SimpleMapDatastore foo = (SimpleMapDatastore) datastore.getDatastoreForConnection('foo') + List holding = [datastore, foo].findAll { SimpleMapDatastore ds -> + DatastoreUtils.execute(ds, { Session session -> session.retrieve(ListenerNote, note.id) } as SessionCallback) != null + } + holding.size() == 1 + listener.is(holding.first().autoTimestampEventListener) } void 'a tenant with a schema added at runtime answers with the listener of that schema'() { From d49753b96dc41180f0b6cd5d6a3604325adadcf1 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sat, 14 Nov 2026 15:14:58 -0800 Subject: [PATCH 042/115] Declare the listener getter on the public static operations type Book.withTenant(id) returns GormAllOperations, which did not declare getAutoTimestampEventListener(), so statically compiled code could not call it there; only the concrete GormStaticApi had it. It is now a default method of GormStaticOperations, statically compiled so that an implementation that is not a Groovy object can call it, which throws UnsupportedOperationException for an implementation that does not override it. GormStaticApi overrides it and TenantDelegatingGormOperations delegates under its tenant. --- .../SimpleMapAutoTimestampListenerSpec.groovy | 14 ++++++++++++++ .../gorm/api/GormStaticOperations.groovy | 16 ++++++++++++++++ .../datastore/gorm/GormStaticApi.groovy | 1 + .../TenantDelegatingGormOperations.groovy | 8 ++++++++ .../datastore/gorm/GormStaticApiSpec.groovy | 19 +++++++++++++++++++ .../TenantDelegatingGormOperationsSpec.groovy | 16 ++++++++++++++++ 6 files changed, 74 insertions(+) diff --git a/grails-data-simple/src/test/groovy/org/grails/datastore/mapping/simple/SimpleMapAutoTimestampListenerSpec.groovy b/grails-data-simple/src/test/groovy/org/grails/datastore/mapping/simple/SimpleMapAutoTimestampListenerSpec.groovy index 6b588ece3c2..ae068f97e71 100644 --- a/grails-data-simple/src/test/groovy/org/grails/datastore/mapping/simple/SimpleMapAutoTimestampListenerSpec.groovy +++ b/grails-data-simple/src/test/groovy/org/grails/datastore/mapping/simple/SimpleMapAutoTimestampListenerSpec.groovy @@ -18,6 +18,8 @@ */ package org.grails.datastore.mapping.simple +import groovy.transform.CompileStatic + import spock.lang.AutoCleanup import spock.lang.Specification import spock.util.environment.RestoreSystemProperties @@ -155,6 +157,10 @@ class SimpleMapAutoTimestampListenerSpec extends Specification { Tenants.withId('bar') { TenantListenerNote.autoTimestampEventListener }.is(bar) TenantListenerNote.withTenant('bar').autoTimestampEventListener.is(bar) + and: 'statically compiled code reaches it through the operations withTenant returns' + StaticTenantListenerLookup.forTenant('foo').is(foo) + StaticTenantListenerLookup.forTenant('bar').is(bar) + when: 'the current tenant comes from the resolver' System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, 'foo') @@ -235,6 +241,14 @@ class ListenerNote { } } +@CompileStatic +class StaticTenantListenerLookup { + + static AutoTimestampEventListener forTenant(String tenantId) { + TenantListenerNote.withTenant(tenantId).autoTimestampEventListener + } +} + class SecondaryListenerLookup { @Transactional(connection = 'secondary') diff --git a/grails-datamapping-core/src/main/groovy/grails/gorm/api/GormStaticOperations.groovy b/grails-datamapping-core/src/main/groovy/grails/gorm/api/GormStaticOperations.groovy index b8de3e30a2f..f4eff110baf 100644 --- a/grails-datamapping-core/src/main/groovy/grails/gorm/api/GormStaticOperations.groovy +++ b/grails-datamapping-core/src/main/groovy/grails/gorm/api/GormStaticOperations.groovy @@ -28,6 +28,7 @@ import jakarta.persistence.LockModeType import org.springframework.transaction.TransactionDefinition import grails.gorm.DetachedCriteria +import org.grails.datastore.gorm.events.AutoTimestampEventListener import org.grails.datastore.gorm.finders.FinderMethod import org.grails.datastore.gorm.internal.RefreshLockArguments import org.grails.datastore.mapping.model.PersistentEntity @@ -52,6 +53,21 @@ interface GormStaticOperations { */ List getGormDynamicFinders() + /** + * Returns the listener that sets the timestamp and auditor properties of an instance saved through these + * operations, as {@code Book.withTenant('acme').autoTimestampEventListener} returns the tenant's. Each datastore + * has a listener of its own, and so does each connection and each tenant with a database or schema of its own, + * and a listener only handles the saves made through its own datastore. + * + * @return the listener that handles a save through these operations + * @throws UnsupportedOperationException when these operations cannot look the listener up + * @since 9.0 + */ + @CompileStatic + default AutoTimestampEventListener getAutoTimestampEventListener() { + throw new UnsupportedOperationException("[${getClass().name}] cannot look up the auto-timestamp event listener") + } + /** * * @param callable Callable closure containing detached criteria definition diff --git a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/GormStaticApi.groovy b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/GormStaticApi.groovy index be5f030301d..fee3b11e7c3 100644 --- a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/GormStaticApi.groovy +++ b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/GormStaticApi.groovy @@ -180,6 +180,7 @@ class GormStaticApi extends AbstractGormApi implements GormAllOperations implements GormAllOperations { return allOperations.gormDynamicFinders } + @Override + AutoTimestampEventListener getAutoTimestampEventListener() { + Tenants.withId(requireMultiTenantCapableDatastore(), tenantId) { + allOperations.autoTimestampEventListener + } + } + @Override DetachedCriteria where(Closure callable) { Tenants.withId(requireMultiTenantCapableDatastore(), tenantId) { diff --git a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/GormStaticApiSpec.groovy b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/GormStaticApiSpec.groovy index d3c7bf12fef..19867d02105 100644 --- a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/GormStaticApiSpec.groovy +++ b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/GormStaticApiSpec.groovy @@ -170,6 +170,25 @@ class GormStaticApiSpec extends Specification { GormStaticApiThing.autoTimestampEventListener.is(datastore.autoTimestampEventListener) } + void "the default auto-timestamp listener of the static operations contract says it cannot be looked up"() { + given: 'an implementation that provides nothing beyond the interface defaults' + GormStaticOperations operations = (GormStaticOperations) Proxy.newProxyInstance( + GormStaticOperations.classLoader, [GormStaticOperations] as Class[], + { Object proxy, Method method, Object[] methodArgs -> + if (method.isDefault()) { + return InvocationHandler.invokeDefault(proxy, method, methodArgs) + } + throw new AssertionError("unexpected call of ${method.name}") + } as InvocationHandler) + + when: + operations.autoTimestampEventListener + + then: + UnsupportedOperationException e = thrown() + e.message.contains('cannot look up the auto-timestamp event listener') + } + void "getAutoTimestampEventListener rejects a datastore that does not set timestamps through a listener"() { given: Datastore plainDatastore = Stub(Datastore) diff --git a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/multitenancy/TenantDelegatingGormOperationsSpec.groovy b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/multitenancy/TenantDelegatingGormOperationsSpec.groovy index a7fd2af87ea..d5200e97e38 100644 --- a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/multitenancy/TenantDelegatingGormOperationsSpec.groovy +++ b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/multitenancy/TenantDelegatingGormOperationsSpec.groovy @@ -20,7 +20,9 @@ package org.grails.datastore.gorm.multitenancy import grails.gorm.api.GormAllOperations import grails.gorm.multitenancy.Tenants +import org.grails.datastore.gorm.events.AutoTimestampEventListener import org.grails.datastore.mapping.core.Datastore +import org.grails.datastore.mapping.keyvalue.mapping.config.KeyValueMappingContext import org.grails.datastore.mapping.multitenancy.MultiTenancySettings import org.grails.datastore.mapping.multitenancy.MultiTenantCapableDatastore import org.springframework.transaction.TransactionDefinition @@ -74,6 +76,20 @@ class TenantDelegatingGormOperationsSpec extends Specification { 0 * delegate.save(_, _) } + void "getAutoTimestampEventListener delegates to the wrapped operations under the bound tenant"() { + given: + def delegate = Mock(GormAllOperations) + def ops = buildOperations(delegate) + def listener = new AutoTimestampEventListener(Stub(Datastore) { getMappingContext() >> new KeyValueMappingContext('test') }) + + when: + def result = ops.autoTimestampEventListener + + then: + 1 * delegate.getAutoTimestampEventListener() >> listener + result.is(listener) + } + void "deleteAll() delegates to the wrapped operations under the bound tenant"() { given: def delegate = Mock(GormAllOperations) From a291dc4cc4ee9b71c21c3736f83f5bcfec1e19a9 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sun, 15 Nov 2026 08:15:25 -0800 Subject: [PATCH 043/115] Keep a listener's configured auditor when the context has none Spring sets a listener bean's auditorAware property and then calls setApplicationContext. Since the per-connection auditor change, that call assigned the context's lookup unconditionally, so a context without an AuditorAware bean replaced the configured auditor with null and the created-by and last-modified-by properties stayed empty. It now replaces the configured auditor only with one the context has, as before. --- .../events/AutoTimestampEventListener.java | 7 +++- .../AutoTimestampEventListenerSpec.groovy | 41 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/events/AutoTimestampEventListener.java b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/events/AutoTimestampEventListener.java index f301b72abe4..a3a4483b6e5 100644 --- a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/events/AutoTimestampEventListener.java +++ b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/events/AutoTimestampEventListener.java @@ -128,7 +128,12 @@ protected AutoTimestampEventListener(final MappingContext mappingContext) { @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { - this.auditorAware = lookUpAuditorAware(applicationContext); + // Spring calls this after it has set the bean's properties, so an auditor the listener was configured with + // is kept when the context has none of its own. + AuditorAware contextAuditorAware = lookUpAuditorAware(applicationContext); + if (contextAuditorAware != null) { + this.auditorAware = contextAuditorAware; + } this.auditorAwareResolved = true; } diff --git a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/timestamp/AutoTimestampEventListenerSpec.groovy b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/timestamp/AutoTimestampEventListenerSpec.groovy index cf45f3a200d..ee0505927d6 100644 --- a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/timestamp/AutoTimestampEventListenerSpec.groovy +++ b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/timestamp/AutoTimestampEventListenerSpec.groovy @@ -31,6 +31,8 @@ import java.util.function.Supplier import groovy.transform.InheritConstructors import spock.lang.Specification +import org.springframework.beans.factory.config.BeanDefinition +import org.springframework.beans.factory.config.BeanDefinitionCustomizer import org.springframework.context.support.GenericApplicationContext import grails.gorm.annotation.CreatedBy @@ -696,6 +698,45 @@ class AutoTimestampEventListenerSpec extends Specification { [datastoreContext, givenContext, emptyContext]*.close() } + void "a listener bean keeps the auditor it is configured with when the application context has none"() { + given: 'Spring sets the bean\'s auditorAware property and then gives it the application context' + AuditorAware configuredAuditor = { -> Optional.of('configuredUser') } as AuditorAware + GenericApplicationContext context = new GenericApplicationContext() + context.registerBean('listener', TestEventListener, { new TestEventListener(Stub(Datastore) { getMappingContext() >> null }) } as Supplier, + { BeanDefinition definition -> definition.propertyValues.add('auditorAware', configuredAuditor) } as BeanDefinitionCustomizer) + + when: + context.refresh() + TestEventListener bean = context.getBean(TestEventListener) + bean.persistentEntityAdded(auditedEntity()) + + then: + bean.auditorAware.is(configuredAuditor) + appliedOnInsert(bean, Audited) == [createdBy: 'configuredUser', lastModifiedBy: 'configuredUser'] + + cleanup: + context.close() + } + + void "a listener bean takes the auditor of the application context over the one it is configured with"() { + given: + AuditorAware configuredAuditor = { -> Optional.of('configuredUser') } as AuditorAware + AuditorAware contextAuditor = { -> Optional.of('contextUser') } as AuditorAware + GenericApplicationContext context = new GenericApplicationContext() + context.registerBean(AuditorAware, { contextAuditor } as Supplier) + context.registerBean('listener', TestEventListener, { new TestEventListener(Stub(Datastore) { getMappingContext() >> null }) } as Supplier, + { BeanDefinition definition -> definition.propertyValues.add('auditorAware', configuredAuditor) } as BeanDefinitionCustomizer) + + when: + context.refresh() + + then: + context.getBean(TestEventListener).auditorAware.is(contextAuditor) + + cleanup: + context.close() + } + private PersistentEntity auditedEntity() { PersistentEntity entity = null PersistentProperty createdBy = auditedProperty('createdBy') { -> entity } From 86d558e6d2907cdc36630e28423419f2ec502e3e Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Tue, 17 Nov 2026 07:29:50 -0800 Subject: [PATCH 044/115] Restore the publisher spec's features that adding one deleted f84a7fdff7 replaced DefaultApplicationEventPublisherSpec with its one new feature, deleting the nine that covered dispatch without listeners, the order of notification, the event-type and source-type filtering of a SmartApplicationListener and the payload wrapping of publishEvent(Object). They are back, and the new feature sits next to them. --- ...efaultApplicationEventPublisherSpec.groovy | 137 +++++++++++++++++- 1 file changed, 136 insertions(+), 1 deletion(-) diff --git a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/events/DefaultApplicationEventPublisherSpec.groovy b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/events/DefaultApplicationEventPublisherSpec.groovy index 815737c16ed..6f6d2cf1ae9 100644 --- a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/events/DefaultApplicationEventPublisherSpec.groovy +++ b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/events/DefaultApplicationEventPublisherSpec.groovy @@ -16,18 +16,153 @@ * specific language governing permissions and limitations * under the License. */ + package org.grails.datastore.gorm.events import spock.lang.Specification import org.springframework.context.ApplicationEvent import org.springframework.context.ApplicationListener +import org.springframework.context.PayloadApplicationEvent +import org.springframework.context.event.SmartApplicationListener class DefaultApplicationEventPublisherSpec extends Specification { + DefaultApplicationEventPublisher publisher = new DefaultApplicationEventPublisher() + + void "publishEvent(ApplicationEvent) does nothing when no listeners are registered"() { + given: + ApplicationEvent event = new PayloadApplicationEvent<>(this, 'payload') + + when: + publisher.publishEvent(event) + + then: + noExceptionThrown() + } + + void "publishEvent(Object) does nothing when no listeners are registered"() { + when: + publisher.publishEvent('a plain payload') + + then: + noExceptionThrown() + } + + void "publishEvent notifies a plain ApplicationListener regardless of event or source type"() { + given: + ApplicationListener listener = Mock(ApplicationListener) + publisher.addApplicationListener(listener) + ApplicationEvent event = new PayloadApplicationEvent<>(this, 'payload') + + when: + publisher.publishEvent(event) + + then: + 1 * listener.onApplicationEvent(event) + } + + void "publishEvent notifies every registered listener in order"() { + given: + ApplicationListener first = Mock(ApplicationListener) + ApplicationListener second = Mock(ApplicationListener) + publisher.addApplicationListener(first) + publisher.addApplicationListener(second) + ApplicationEvent event = new PayloadApplicationEvent<>(this, 'payload') + + when: + publisher.publishEvent(event) + + then: + 1 * first.onApplicationEvent(event) + 1 * second.onApplicationEvent(event) + } + + void "publishEvent notifies a SmartApplicationListener when it supports both the event type and source type"() { + given: + SmartApplicationListener listener = Mock(SmartApplicationListener) { + supportsEventType(_) >> true + supportsSourceType(_) >> true + } + publisher.addApplicationListener(listener) + ApplicationEvent event = new PayloadApplicationEvent<>(this, 'payload') + + when: + publisher.publishEvent(event) + + then: + 1 * listener.onApplicationEvent(event) + } + + void "publishEvent skips a SmartApplicationListener that does not support the event type"() { + given: + SmartApplicationListener listener = Mock(SmartApplicationListener) { + supportsEventType(_) >> false + } + publisher.addApplicationListener(listener) + ApplicationEvent event = new PayloadApplicationEvent<>(this, 'payload') + + when: + publisher.publishEvent(event) + + then: + 0 * listener.onApplicationEvent(_) + } + + void "publishEvent skips a SmartApplicationListener that supports the event type but not the source type"() { + given: + SmartApplicationListener listener = Mock(SmartApplicationListener) { + supportsEventType(_) >> true + supportsSourceType(_) >> false + } + publisher.addApplicationListener(listener) + ApplicationEvent event = new PayloadApplicationEvent<>(this, 'payload') + + when: + publisher.publishEvent(event) + + then: + 0 * listener.onApplicationEvent(_) + } + + void "publishEvent(Object) wraps the payload in a PayloadApplicationEvent sourced from the publisher itself"() { + given: + ApplicationListener listener = Mock(ApplicationListener) + publisher.addApplicationListener(listener) + + when: + publisher.publishEvent('a plain payload') + + then: + 1 * listener.onApplicationEvent({ ApplicationEvent e -> + e instanceof PayloadApplicationEvent && + e.source.is(publisher) && + e.payload == 'a plain payload' + }) + } + + void "publishEvent(Object) applies the same SmartApplicationListener filtering as publishEvent(ApplicationEvent)"() { + given: + SmartApplicationListener supported = Mock(SmartApplicationListener) { + supportsEventType(_) >> true + supportsSourceType(_) >> true + } + SmartApplicationListener unsupported = Mock(SmartApplicationListener) { + supportsEventType(_) >> false + } + publisher.addApplicationListener(supported) + publisher.addApplicationListener(unsupported) + + when: + publisher.publishEvent('a plain payload') + + then: + 1 * supported.onApplicationEvent(_) + 0 * unsupported.onApplicationEvent(_) + } + void 'a listener can add another listener while an event is published'() { given: 'a listener that adds one, as a datastore that adds a schema while it saves does' - DefaultApplicationEventPublisher publisher = new DefaultApplicationEventPublisher() List received = [] ApplicationListener added = { ApplicationEvent event -> received << "added:${event.source}".toString() } as ApplicationListener publisher.addApplicationListener({ ApplicationEvent event -> From 4abc0f5d2b870807205b911e2d77c0139e82881b Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Tue, 17 Nov 2026 07:34:32 -0800 Subject: [PATCH 045/115] Cover the unit tests' default data source and correct the examples Upgrade note 25 also covers the in-memory datastore of unit tests following the two auto-timestamp settings on its default data source. What's New uses a class mapped to several data sources for the connection examples, and the trait's getter documents TenantNotFoundException. --- .../src/main/groovy/org/grails/datastore/gorm/GormEntity.groovy | 2 ++ grails-doc/src/en/guide/introduction/whatsNew.adoc | 2 +- grails-doc/src/en/guide/upgrading/upgrading90x.adoc | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/GormEntity.groovy b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/GormEntity.groovy index 6636a5f4793..d60b763dff9 100644 --- a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/GormEntity.groovy +++ b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/GormEntity.groovy @@ -527,6 +527,8 @@ trait GormEntity implements GormValidateable, DirtyCheckable, GormEntityApi Date: Tue, 17 Nov 2026 07:29:50 -0800 Subject: [PATCH 046/115] Check the listener lookup in a method transactional on a connection A TCK data service annotated @Transactional(connection = 'secondary') answers with the secondary connection's listener, and turning timestamps off on it reaches a save the method makes. @Transactional scopes every class mapped to the connection, a different path from the withTransaction block the spec already checked. --- .../TimestampNoteConnectionService.groovy | 40 +++++++++++++++++++ ...ampEventListenerMultiDataSourceSpec.groovy | 16 ++++++++ 2 files changed, 56 insertions(+) create mode 100644 grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/domains/TimestampNoteConnectionService.groovy diff --git a/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/domains/TimestampNoteConnectionService.groovy b/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/domains/TimestampNoteConnectionService.groovy new file mode 100644 index 00000000000..8e912082212 --- /dev/null +++ b/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/domains/TimestampNoteConnectionService.groovy @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.grails.data.testing.tck.domains + +import grails.gorm.services.Service +import grails.gorm.transactions.Transactional +import org.grails.datastore.gorm.events.AutoTimestampEventListener + +@Service(AllConnectionsTimestampNote) +@Transactional(connection = 'secondary') +abstract class TimestampNoteConnectionService { + + AutoTimestampEventListener listener() { + AllConnectionsTimestampNote.autoTimestampEventListener + } + + AllConnectionsTimestampNote saveWithoutTimestamps(String text, Date timestamp) { + AllConnectionsTimestampNote note = new AllConnectionsTimestampNote(text: text, dateCreated: timestamp, lastUpdated: timestamp) + AllConnectionsTimestampNote.autoTimestampEventListener.withoutTimestamps { + note.save(flush: true) + } + note + } +} diff --git a/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/tests/AutoTimestampEventListenerMultiDataSourceSpec.groovy b/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/tests/AutoTimestampEventListenerMultiDataSourceSpec.groovy index 83b030e56cc..a335f0e2781 100644 --- a/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/tests/AutoTimestampEventListenerMultiDataSourceSpec.groovy +++ b/grails-datamapping-tck/src/main/groovy/org/apache/grails/data/testing/tck/tests/AutoTimestampEventListenerMultiDataSourceSpec.groovy @@ -25,6 +25,7 @@ import org.apache.grails.data.testing.tck.base.GrailsDataTckSpec import org.apache.grails.data.testing.tck.domains.AllConnectionsTimestampNote import org.apache.grails.data.testing.tck.domains.DefaultConnectionTimestampNote import org.apache.grails.data.testing.tck.domains.SecondaryConnectionTimestampNote +import org.apache.grails.data.testing.tck.domains.TimestampNoteConnectionService /** * Every connection has an auto-timestamp listener of its own, and a domain class answers with the listener of the @@ -57,6 +58,21 @@ class AutoTimestampEventListenerMultiDataSourceSpec extends GrailsDataTckSpec { AllConnectionsTimestampNote.secondary.withTransaction { AllConnectionsTimestampNote.autoTimestampEventListener }.is(secondaryListener) } + void 'a method transactional on a connection answers with that connection\'s listener'() { + given: + TimestampNoteConnectionService service = manager.getServiceForConnection(TimestampNoteConnectionService, 'secondary') + def secondaryListener = AllConnectionsTimestampNote.secondary.autoTimestampEventListener + + when: + AllConnectionsTimestampNote note = service.saveWithoutTimestamps('transactional', PAST) + + then: + service.listener().is(secondaryListener) + !service.listener().is(AllConnectionsTimestampNote.autoTimestampEventListener) + AllConnectionsTimestampNote.secondary.withNewTransaction { AllConnectionsTimestampNote.secondary.get(note.id).dateCreated } == PAST + AllConnectionsTimestampNote.secondary.withNewTransaction { AllConnectionsTimestampNote.secondary.get(note.id).lastUpdated } == PAST + } + void 'the listener of each domain class turns off the timestamps of its own saves'() { given: DefaultConnectionTimestampNote car = new DefaultConnectionTimestampNote(text: 'car', dateCreated: PAST, lastUpdated: PAST) From e0b9887da09f7bd1e95c380ac9f12bcb9508e280 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Tue, 17 Nov 2026 07:44:14 -0800 Subject: [PATCH 047/115] Name the JLine APIs JLine 4 removed, and keep picocli-shell-jline3 optional to move off --- .agents/skills/mono-repo-integration/SKILL.md | 2 +- grails-doc/src/en/guide/upgrading/upgrading90x.adoc | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.agents/skills/mono-repo-integration/SKILL.md b/.agents/skills/mono-repo-integration/SKILL.md index 1540da96f95..e73bcf8dd65 100644 --- a/.agents/skills/mono-repo-integration/SKILL.md +++ b/.agents/skills/mono-repo-integration/SKILL.md @@ -140,7 +140,7 @@ The apps were physically moved to `grails-test-examples//...` back in Init - Remove now-empty imported folders. - Match property/naming style to existing conventions (e.g. `xxxVersion`). - **CodeNarc**: the imported source will almost certainly trip `codeStyle` (wildcard imports, tabs, `if(`, unnecessary GStrings/semicolons, brace spacing, missing trailing newline, class-starts-with-blank-line). Fix mechanically until `:grails-:codenarcMain` passes; test-example apps don't apply `grails-code-style`, so they aren't style-checked. -- **SBOM (`cyclonedxBom`)**: a merged plugin pulls transitives the SBOM plugin can't auto-resolve a license for, failing with "Could not determine License id for dependency: …". Trace it (`./gradlew :grails-:dependencyInsight --dependency --configuration runtimeClasspath`) to confirm what pulls it and that the license is ASF-acceptable, then add a mapping to `LICENSE_MAPPING` (and a `LICENSES` entry if the license has no SPDX id) in `build-logic/plugins/.../SbomPlugin.groovy` — the same place `jline`/`sitemesh` are mapped. (Redis: `org.json:json` via `jedis`, relicensed to Public Domain → mapped.) +- **SBOM (`cyclonedxBom`)**: a merged plugin pulls transitives the SBOM plugin can't auto-resolve a license for, failing with "Could not determine License id for dependency: …". Trace it (`./gradlew :grails-:dependencyInsight --dependency --configuration runtimeClasspath`) to confirm what pulls it and that the license is ASF-acceptable, then add a mapping to `LICENSE_MAPPING` (and a `LICENSES` entry if the license has no SPDX id) in `build-logic/plugins/.../SbomPlugin.groovy` — the same place `sitemesh` is mapped. (Redis: `org.json:json` via `jedis`, relicensed to Public Domain → mapped.) - **RAT** noise: a local working copy often has stale `bin/`/`build/` artifacts that `rat` flags; filter the report to your new files (excluding `build/`/`bin/`) to confirm the contribution itself is clean. - **Missing ASF license headers on imported files**: the plugin source usually already has Apache headers, but the imported **example/test app** files (controllers, GSP views, assets, config, i18n) frequently do not, and `rat` flags them. Add headers with the per-filetype scripts in `etc/bin/` — `add-license-groovy-java.groovy`, `add-license-gsp.groovy`, `add-license-css.groovy`, `add-license-js.groovy`, `add-license-yml.groovy`, `add-license-properties.groovy`, etc. — each takes a target path and recurses (skipping files that already have the marker): `groovy etc/bin/add-license-.groovy grails-test-examples/`. There is **no** `add-license-xml`; add the XML comment header (e.g. to `logback-spring.xml`/`logback-test.xml`) by hand, matching an existing example app's format. - **Gradle can't run in the sandbox here** (wrapper needs `~/.gradle`; signing needs the 1Password agent) — run every `./gradlew` with the sandbox disabled, and `export GRADLE_OPTS="-Dorg.gradle.jvmargs=-Xmx4G"`. diff --git a/grails-doc/src/en/guide/upgrading/upgrading90x.adoc b/grails-doc/src/en/guide/upgrading/upgrading90x.adoc index 3a93c5bc019..27107942913 100644 --- a/grails-doc/src/en/guide/upgrading/upgrading90x.adoc +++ b/grails-doc/src/en/guide/upgrading/upgrading90x.adoc @@ -1456,10 +1456,11 @@ With `cacheUsers` enabled, the Spring Security plugin's user cache now stores us ==== 25. JLine 4 -Grails 9 manages JLine 4 (`org.jline:jline` 4.4.5), the version Groovy 6's `groovysh` is built on, in place of JLine 3. JLine is the terminal library behind the interactive prompts of the `grails` command line tools and of `grails shell`, and behind `GrailsConsole`, which application and plugin commands write to. The line reader, terminal and completer APIs keep their `org.jline` packages, and the JLine code in Grails itself compiled against JLine 4 unchanged. +Grails 9 manages JLine 4 (`org.jline:jline`), the line Groovy 6's `groovysh` is built on, in place of JLine 3. The link:{versionsRef}Grails%20BOM.html[Grails BOM dependency table] has the exact version. JLine is the terminal library behind the interactive prompts of the `grails` command line tools and of `grails shell`, and behind `GrailsConsole`, which application and plugin commands write to. The line reader, terminal and completer APIs keep their `org.jline` packages. ===== Required Actions -* Recompile code of your own that uses JLine, such as an `org.jline.reader.Completer` for a command, against JLine 4. +* Recompile code of your own that uses JLine, such as an `org.jline.reader.Completer` for a command, against JLine 4. JLine 4 removed APIs that JLine 3 had deprecated, so code that uses them no longer compiles. For example, use `org.jline.builtins.Completers.FileNameCompleter` in place of `org.jline.reader.impl.completer.FileNameCompleter`, and `Terminal.inputEncoding()` and `outputEncoding()` in place of `stdinEncoding()`, `stdoutEncoding()` and `stderrEncoding()`. `TerminalBuilder.codepage(int)` and the `TerminalBuilder.PROP_JNA` and `PROP_JANSI` constants are gone too. * JLine 4 has no JNA or Jansi terminal provider. Remove a dependency on `org.jline:jline-terminal-jna` or `org.jline:jline-terminal-jansi`: neither has a JLine 4 version, and JLine finds the terminal through its remaining providers. -* A picocli command line that reads its input with JLine through picocli's `info.picocli:picocli-shell-jline3` gets JLine 4 from the Grails BOM, while that bridge is built for JLine 3. Use JLine's own picocli bridge, `org.jline:jline-picocli`, at the JLine version the Grails BOM manages. + +picocli's `info.picocli:picocli-shell-jline3` runs on JLine 4, but picocli has no JLine 4 version of it, and its `PicocliCommands` implements `org.jline.console.CommandRegistry`, which JLine 4 deprecates. JLine's own picocli bridge, `org.jline:jline-picocli`, is built for JLine 4. To move to it, use it at the JLine version the Grails BOM manages. From b60b0b7da3ef5eb8d2fdbb4100e4fa002efebdee Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Tue, 17 Nov 2026 07:44:14 -0800 Subject: [PATCH 048/115] Complete a real option in the completer test, and restore user.home with @RestoreSystemProperties --- .../shell/support/GroovyshApplicationContextSpec.groovy | 9 ++------- .../groovy/org/grails/forge/cli/InteractiveShell.groovy | 2 +- .../org/grails/forge/cli/CommandLineCompleterSpec.groovy | 2 +- 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/grails-console/src/test/groovy/grails/ui/shell/support/GroovyshApplicationContextSpec.groovy b/grails-console/src/test/groovy/grails/ui/shell/support/GroovyshApplicationContextSpec.groovy index aecd7e4bc84..9ac2f09dc0e 100644 --- a/grails-console/src/test/groovy/grails/ui/shell/support/GroovyshApplicationContextSpec.groovy +++ b/grails-console/src/test/groovy/grails/ui/shell/support/GroovyshApplicationContextSpec.groovy @@ -21,6 +21,7 @@ import java.nio.file.Path import spock.lang.Specification import spock.lang.TempDir +import spock.util.environment.RestoreSystemProperties import grails.core.DefaultGrailsApplication import grails.core.GrailsApplication @@ -28,23 +29,17 @@ import grails.core.GrailsApplication /** * Starts the Groovy shell {@code grails shell} opens, on the class path the console module ships. */ +@RestoreSystemProperties class GroovyshApplicationContextSpec extends Specification { @TempDir Path userHome - String originalUserHome - void setup() { // groovysh keeps its history and settings under the user's home directory - originalUserHome = System.getProperty('user.home') System.setProperty('user.home', userHome.toString()) } - void cleanup() { - System.setProperty('user.home', originalUserHome) - } - void 'the shell starts with the application in its binding and ends when its input ends'() { given: 'an application context with a Grails application, as grails shell starts it' def context = new GroovyshApplicationContext() diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/InteractiveShell.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/InteractiveShell.groovy index f0d56a42db2..8d86582208c 100644 --- a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/InteractiveShell.groovy +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/InteractiveShell.groovy @@ -65,7 +65,7 @@ class InteractiveShell { Terminal terminal = TerminalBuilder.terminal() LineReader reader = LineReaderBuilder.builder() .terminal(terminal) - .completer(new CommandLineCompleter(commandLine.getCommandSpec())) + .completer(new CommandLineCompleter(commandLine.commandSpec)) .parser(new DefaultParser()) .variable(LineReader.LIST_MAX, 50) // max tab completion candidates .build() diff --git a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/CommandLineCompleterSpec.groovy b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/CommandLineCompleterSpec.groovy index 4c241cc6b7c..2139ee616aa 100644 --- a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/CommandLineCompleterSpec.groovy +++ b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/CommandLineCompleterSpec.groovy @@ -63,7 +63,7 @@ class CommandLineCompleterSpec extends Specification { void 'an option typed after a command is completed from that command, not the root command'() { when: - List offered = complete('create-app --pro') + List offered = complete('create-app --fe') then: offered.containsAll(['--features', '--servlet', '--jdk', '--gorm']) From 4b56419494b3a364dcc85f79e0bd1e0d4fb06b84 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Tue, 17 Nov 2026 07:59:33 -0800 Subject: [PATCH 049/115] Point What's New at the renumbered upgrade section --- grails-doc/src/en/guide/introduction/whatsNew.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/grails-doc/src/en/guide/introduction/whatsNew.adoc b/grails-doc/src/en/guide/introduction/whatsNew.adoc index 83708c70168..7d20d354787 100644 --- a/grails-doc/src/en/guide/introduction/whatsNew.adoc +++ b/grails-doc/src/en/guide/introduction/whatsNew.adoc @@ -174,7 +174,7 @@ With `grails.mime.negotiation.spring: true`, which Grails 10 makes the default, `ResponseEntity` it returns. See xref:theWebLayer#springFormatNegotiation[Resolving Formats with Spring MVC Content Negotiation]. -See xref:upgrading#upgrading90x[Upgrading from Grails 8 to Grails 9], sections 4 to 11, 13 to 16 and 22. +See xref:upgrading#upgrading90x[Upgrading from Grails 8 to Grails 9], sections 4 to 11, 13 to 16 and 25. ==== Hibernate 7 Criteria Queries Can Be Extended From e71bc8fadd16ae8ee80c1f5944380ea6be6d46ba Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Tue, 17 Nov 2026 08:36:42 -0800 Subject: [PATCH 050/115] Let runCommand read its arguments from a file, and generateCommandManifest write where it is told --- .../commands/GrailsCliGradlePlugin.groovy | 79 ++++++++++++++---- .../ApplicationCommandManifestTaskSpec.groovy | 14 ++++ .../commands/CommandArgumentsSpec.groovy | 81 +++++++++++++++++++ 3 files changed, 158 insertions(+), 16 deletions(-) create mode 100644 grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/CommandArgumentsSpec.groovy diff --git a/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/GrailsCliGradlePlugin.groovy b/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/GrailsCliGradlePlugin.groovy index 1fb28d31362..da51a244ba8 100644 --- a/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/GrailsCliGradlePlugin.groovy +++ b/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/GrailsCliGradlePlugin.groovy @@ -20,6 +20,8 @@ package org.grails.gradle.plugin.commands import java.util.jar.JarFile +import groovy.json.JsonException +import groovy.json.JsonSlurper import groovy.transform.CompileDynamic import groovy.transform.CompileStatic import groovy.transform.PackageScope @@ -36,6 +38,7 @@ import org.gradle.api.artifacts.component.ModuleComponentIdentifier import org.gradle.api.artifacts.component.ProjectComponentIdentifier import org.gradle.api.artifacts.result.ResolvedArtifactResult import org.gradle.api.artifacts.result.ResolvedComponentResult +import org.gradle.api.file.Directory import org.gradle.api.file.FileCollection import org.gradle.api.provider.Provider import org.gradle.api.tasks.JavaExec @@ -114,6 +117,24 @@ class GrailsCliGradlePlugin implements Plugin { /** Where {@link #COMMAND_MANIFEST_TASK} writes the commands, relative to the build directory */ public static final String COMMAND_MANIFEST_PATH = 'grails/commands.json' + /** + * Set this project property to the path of a file, relative to the project directory, to have + * {@link #COMMAND_MANIFEST_TASK} write the commands there instead of {@link #COMMAND_MANIFEST_PATH}. + */ + public static final String COMMAND_MANIFEST_PROPERTY = 'grailsCommandManifest' + + /** + * The project property with the arguments of a command task, such as {@code runCommand}, as one + * command line that is split into arguments as a shell would. + */ + public static final String ARGS_PROPERTY = 'args' + + /** + * The project property with the path of a JSON file holding the arguments of a command task as an + * array of strings, each passed on unchanged. They follow those of {@link #ARGS_PROPERTY}. + */ + public static final String ARGS_FILE_PROPERTY = 'argsFile' + @Override void apply(Project project) { // self-sufficient when applied standalone: the auto-provisioning behavior is configured @@ -392,6 +413,40 @@ class GrailsCliGradlePlugin implements Plugin { * @param taskName the name of the task that requires the main class * @return the name of the main class */ + /** + * Returns the arguments a command task passes to its command: those of {@link #ARGS_PROPERTY}, split + * as a shell would, then those of the file {@link #ARGS_FILE_PROPERTY} names, unchanged. + * + * @param project the project the command task belongs to + * @return the arguments, empty when neither property is set + */ + static List commandArguments(Project project) { + List arguments = [] + def args = project.findProperty(ARGS_PROPERTY) + if (args) { + arguments.addAll(CommandLineParser.translateCommandline(args as String)) + } + def argsFile = project.findProperty(ARGS_FILE_PROPERTY) + if (argsFile) { + String content = project.providers.fileContents(project.layout.projectDirectory.file(argsFile as String)).asText.orNull + if (content == null) { + throw new GradleException("The file of the '${ARGS_FILE_PROPERTY}' property, ${argsFile}, does not exist.") + } + Object parsed + try { + parsed = new JsonSlurper().parseText(content) + } + catch (JsonException e) { + throw new GradleException("The file of the '${ARGS_FILE_PROPERTY}' property, ${argsFile}, is not JSON: ${e.message}", e) + } + if (!(parsed instanceof List) || ((List) parsed).any { !(it instanceof String) }) { + throw new GradleException("The file of the '${ARGS_FILE_PROPERTY}' property, ${argsFile}, must hold a JSON array of strings.") + } + arguments.addAll((List) parsed) + } + arguments + } + @PackageScope static String requireMainClass(Provider mainClass, String taskName) { def mainClassName = mainClass.orNull @@ -433,11 +488,7 @@ class GrailsCliGradlePlugin implements Plugin { it.classpath = fileCollection it.command = commandName it.systemProperty(Environment.KEY, System.getProperty(Environment.KEY, Environment.DEVELOPMENT.getName())) - List args = [] - def otherArgs = project.findProperty('args') - if (otherArgs) { - args.addAll(CommandLineParser.translateCommandline(otherArgs as String)) - } + List args = new ArrayList(GrailsCliGradlePlugin.commandArguments(project)) def appClassProvider = GrailsGradlePlugin.getMainClassProvider(project) @@ -569,11 +620,7 @@ class GrailsCliGradlePlugin implements Plugin { // with Grails' plugin management and bean wiring when running CLI scripts via Gradle it.systemProperty('spring.devtools.restart.enabled', 'false') - List args = [] - def otherArgs = project.findProperty('args') - if (otherArgs) { - args.addAll(CommandLineParser.translateCommandline(otherArgs as String)) - } + List args = new ArrayList(GrailsCliGradlePlugin.commandArguments(project)) def appClassProvider = GrailsGradlePlugin.getMainClassProvider(project) @@ -602,11 +649,7 @@ class GrailsCliGradlePlugin implements Plugin { // with Grails' plugin management and bean wiring when running CLI commands via Gradle it.systemProperty('spring.devtools.restart.enabled', 'false') - List args = [] - def otherArgs = project.findProperty('args') - if (otherArgs) { - args.addAll(CommandLineParser.translateCommandline(otherArgs as String)) - } + List args = new ArrayList(GrailsCliGradlePlugin.commandArguments(project)) def appClassProvider = GrailsGradlePlugin.getMainClassProvider(project) @@ -628,9 +671,13 @@ class GrailsCliGradlePlugin implements Plugin { protected void configureCommandManifest(Project project) { if (!project.tasks.names.contains(COMMAND_MANIFEST_TASK)) { def manifestTask = project.tasks.register(COMMAND_MANIFEST_TASK, ApplicationCommandManifestTask) + // the configuration cache cannot keep the project, so the mapping holds only its directory + Directory projectDirectory = project.layout.projectDirectory manifestTask.configure { it.description = 'Writes the Grails commands of the application, which the Grails Forge CLI offers' - it.manifestFile.convention(project.layout.buildDirectory.file(COMMAND_MANIFEST_PATH)) + it.manifestFile.convention(project.providers.gradleProperty(COMMAND_MANIFEST_PROPERTY) + .map { String path -> projectDirectory.file(path) } + .orElse(project.layout.buildDirectory.file(COMMAND_MANIFEST_PATH))) } project.afterEvaluate { manifestTask.configure { diff --git a/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTaskSpec.groovy b/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTaskSpec.groovy index 081ee5bbf63..ff3d1cc3a90 100644 --- a/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTaskSpec.groovy +++ b/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTaskSpec.groovy @@ -66,6 +66,20 @@ class ApplicationCommandManifestTaskSpec extends GradleSpecification { read(runner.projectDir.toPath().resolve('app/build/grails/commands.json')).runtimeMarker == 'changed' } + void 'the manifest is written where the grailsCommandManifest property says'() { + given: + def runner = setupTestResourceProject('command-manifest') + Path manifest = runner.projectDir.toPath().resolve('app/elsewhere/commands.json') + + when: + def result = executeTask(':app:generateCommandManifest', ['-PgrailsCommandManifest=elsewhere/commands.json']) + + then: + assertTaskSuccess('generateCommandManifest', result) + Path.of(read(manifest).file).toRealPath() == manifest.toRealPath() + !runner.projectDir.toPath().resolve('app/build/grails/commands.json').toFile().exists() + } + private static Map read(Path file) { file.toFile().readLines().collectEntries { String line -> int separator = line.indexOf('=') diff --git a/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/CommandArgumentsSpec.groovy b/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/CommandArgumentsSpec.groovy new file mode 100644 index 00000000000..544a1272422 --- /dev/null +++ b/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/CommandArgumentsSpec.groovy @@ -0,0 +1,81 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.gradle.plugin.commands + +import groovy.json.JsonOutput + +import org.gradle.api.GradleException +import org.gradle.api.Project +import org.gradle.testfixtures.ProjectBuilder +import spock.lang.Specification +import spock.lang.TempDir + +class CommandArgumentsSpec extends Specification { + + @TempDir + File projectDir + + void 'without arguments a command gets none'() { + expect: + GrailsCliGradlePlugin.commandArguments(project([:])) == [] + } + + void 'the args property is split as a shell would split it'() { + expect: + GrailsCliGradlePlugin.commandArguments(project(args: 'url-mappings-report "two words" \'say "hi"\'')) == + ['url-mappings-report', 'two words', 'say "hi"'] + } + + void 'the arguments of the args file are passed on unchanged, after those of the args property'() { + given: 'arguments a command line could not carry without quoting' + List arguments = ['two words', 'it\'s "x" y', '', '%PATH% & | <>', '--format', 'json'] + new File(projectDir, 'arguments.json').text = JsonOutput.toJson(arguments) + + expect: + GrailsCliGradlePlugin.commandArguments(project(args: 'url-mappings-report', argsFile: 'arguments.json')) == + ['url-mappings-report'] + arguments + } + + void 'an args file that #problem is reported'() { + given: + if (content != null) { + new File(projectDir, 'arguments.json').text = content + } + + when: + GrailsCliGradlePlugin.commandArguments(project(argsFile: 'arguments.json')) + + then: + GradleException e = thrown() + e.message.contains(message) + + where: + problem | content || message + 'does not exist' | null || 'does not exist' + 'is not JSON' | 'not json' || 'is not JSON' + 'is not an array of strings' | '["a", 1]' || 'must hold a JSON array of strings' + 'is an object' | '{"a": "b"}' || 'must hold a JSON array of strings' + } + + private Project project(Map properties) { + Project project = ProjectBuilder.builder().withProjectDir(projectDir).build() + properties.each { String name, String value -> project.extensions.extraProperties.set(name, value) } + project + } +} From 64f6e79d45e88192adda7e32d3996d881772a4cd Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Tue, 17 Nov 2026 08:36:42 -0800 Subject: [PATCH 051/115] Keep the Forge CLI working when the project's build or manifest is not as expected, and pass commands their arguments as typed --- .../forge/cli/CommandLineCompleter.groovy | 17 +- .../org/grails/forge/cli/ForgeCli.groovy | 36 ++- .../forge/cli/command/ProjectCommand.groovy | 79 ++++-- .../forge/cli/command/ProjectCommands.groovy | 135 +++++++--- .../forge/cli/command/ProjectGradle.groovy | 6 + .../forge/cli/ProjectCommandsSpec.groovy | 238 ++++++++++++++++-- 6 files changed, 436 insertions(+), 75 deletions(-) diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/CommandLineCompleter.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/CommandLineCompleter.groovy index f33d061bef6..88559b5de89 100644 --- a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/CommandLineCompleter.groovy +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/CommandLineCompleter.groovy @@ -42,10 +42,23 @@ class CommandLineCompleter implements Completer { @Override void complete(LineReader reader, ParsedLine line, List candidates) { + List words = new ArrayList<>(line.words()) + int wordIndex = line.wordIndex() + String word = wordIndex < words.size() ? words[wordIndex] : '' + int separator = word.indexOf('=') + // picocli completes the value of an option only as a word of its own, so --name=value is completed + // as --name value and the values are offered with the option in front of them + String prefix = '' + if (word.startsWith('--') && separator > 0) { + prefix = word.substring(0, separator + 1) + words.set(wordIndex, word.substring(0, separator)) + words.add(wordIndex + 1, word.substring(separator + 1)) + wordIndex++ + } List suggestions = [] - AutoComplete.complete(spec, line.words().toArray(new String[0]), line.wordIndex(), 0, line.cursor(), suggestions) + AutoComplete.complete(spec, words.toArray(new String[0]), wordIndex, 0, line.cursor(), suggestions) suggestions.each { CharSequence suggestion -> - candidates.add(new Candidate(suggestion.toString())) + candidates.add(new Candidate(prefix + suggestion)) } } } diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/ForgeCli.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/ForgeCli.groovy index 0290ef49637..6e215431a69 100644 --- a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/ForgeCli.groovy +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/ForgeCli.groovy @@ -23,6 +23,7 @@ import groovy.transform.CompileStatic import org.springframework.context.ApplicationContext import org.springframework.context.ConfigurableApplicationContext import picocli.CommandLine +import picocli.CommandLine.Model.OptionSpec import org.grails.forge.cli.command.AddPropertyCommand import org.grails.forge.cli.command.BaseCommand import org.grails.forge.cli.command.CodeGenCommand @@ -99,14 +100,41 @@ final class ForgeCli { boolean noOpConsole = args.length > 0 && args[0].startsWith('update-cli-config') CommandLine commandLine = createForgeCommandLine(context, noOpConsole) // the project's build runs only when the command line names a command that is not the Forge CLI's: - // its own commands never wait for the build, and its options, such as --help, list the project's - // commands from a manifest the build already wrote - if (args.length > 0 && !commandLine.subcommands.containsKey(args[0])) { - addProjectCommands(commandLine, !args[0].startsWith('-')) + // its own commands never wait for the build, and its options alone, such as --help, list the + // project's commands from a manifest the build already wrote + String command = commandName(commandLine, args) + if (command == null) { + if (args.length > 0) { + addProjectCommands(commandLine, false) + } + } + else if (!commandLine.subcommands.containsKey(command)) { + addProjectCommands(commandLine, true) } return commandLine.execute(args) } + /** + * The command a command line names: its first word that is neither one of the Forge CLI's options + * nor the value of one. + */ + private static String commandName(CommandLine commandLine, String... args) { + for (int i = 0; i < args.length; i++) { + String word = args[i] + if (word == '--') { + return i + 1 < args.length ? args[i + 1] : null + } + if (!word.startsWith('-')) { + return word + } + OptionSpec option = commandLine.commandSpec.findOption(word) + if (option != null && option.arity().min() > 0) { + i++ + } + } + null + } + /** * Runs the interactive shell on a running context until the user leaves it. */ diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommand.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommand.groovy index 5fd79c0e28a..28d33b414df 100644 --- a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommand.groovy +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommand.groovy @@ -20,6 +20,7 @@ package org.grails.forge.cli.command import java.util.concurrent.Callable +import groovy.json.JsonOutput import groovy.transform.CompileStatic import picocli.CommandLine @@ -33,8 +34,13 @@ import picocli.CommandLine.Model.PositionalParamSpec * the Forge CLI's own commands, and runs through the project's {@code runCommand} Gradle task. * *

Arguments and options the command declares are checked and completed. A command that declares - * no arguments takes any, and one that declares no options takes any options, so a command that does - * not describe itself still runs with whatever it is given.

+ * no arguments takes any, and one that declares no options takes any options and is given the words of + * its command line in the order they were typed, so a command that does not describe itself runs as it + * does through {@code runCommand}.

+ * + *

The arguments reach {@code runCommand} in a file, so they need no quoting for the shell or the + * wrapper script. {@code -D} sets a system property of the build, such as {@code -Dgrails.env=test}, + * which {@code runCommand} passes on to the command.

*/ @CompileStatic class ProjectCommand extends BaseCommand implements Callable { @@ -42,12 +48,19 @@ class ProjectCommand extends BaseCommand implements Callable { /** The task that runs a command of the project */ static final String RUN_COMMAND_TASK = 'runCommand' + /** The project property {@code runCommand} reads the arguments of the command from */ + static final String ARGS_FILE_PROPERTY = 'argsFile' + private static final String PASS_THROUGH_LABEL = 'ARGS' + private static final String SYSTEM_PROPERTY_OPTION = '-D' + final String name private final ProjectGradle gradle private final CommandSpec commandSpec + private final OptionSpec systemProperties + private final boolean declaresOptions /** * @param command the command, as described in the manifest @@ -56,6 +69,13 @@ class ProjectCommand extends BaseCommand implements Callable { ProjectCommand(Map command, ProjectGradle gradle) { this.name = command.name as String this.gradle = gradle + this.declaresOptions = command.options as boolean + this.systemProperties = OptionSpec.builder(SYSTEM_PROPERTY_OPTION) + .paramLabel('=') + .type(Map) + .auxiliaryTypes(String, String) + .description('Sets a system property of the build, such as -Dgrails.env=test to run the command in the test environment.') + .build() this.commandSpec = createSpec(command) this.spec = commandSpec } @@ -71,11 +91,28 @@ class ProjectCommand extends BaseCommand implements Callable { Integer call() throws Exception { List commandArguments = [name] commandArguments.addAll(givenArguments()) - gradle.run([RUN_COMMAND_TASK, "-Pargs=${join(commandArguments)}".toString()]) + File argumentsFile = File.createTempFile('grails-command-arguments', '.json') + try { + argumentsFile.setText(JsonOutput.toJson(commandArguments), 'UTF-8') + List gradleArguments = givenSystemProperties() + gradleArguments << RUN_COMMAND_TASK << "-P${ARGS_FILE_PROPERTY}=${argumentsFile.absolutePath}".toString() + return gradle.run(gradleArguments) + } + finally { + argumentsFile.delete() + } + } + + private List givenSystemProperties() { + Map given = (systemProperties.getValue() ?: [:]) as Map + given.collect { String property, String value -> "${SYSTEM_PROPERTY_OPTION}${property}=${value}".toString() } } private List givenArguments() { CommandLine.ParseResult parsed = commandSpec.commandLine().parseResult + if (!declaresOptions) { + return typedArguments(parsed.originalArgs()) + } List given = [] for (PositionalParamSpec positional : commandSpec.positionalParameters()) { if (parsed.hasMatchedPositional(positional)) { @@ -83,7 +120,7 @@ class ProjectCommand extends BaseCommand implements Callable { } } for (OptionSpec option : commandSpec.options()) { - if (option.usageHelp() || option.versionHelp() || !parsed.hasMatchedOption(option)) { + if (option.usageHelp() || option.versionHelp() || option.is(systemProperties) || !parsed.hasMatchedOption(option)) { continue } given << (option.arity().max() == 0 @@ -94,6 +131,26 @@ class ProjectCommand extends BaseCommand implements Callable { given } + /** + * The words typed after the command's name, without those of {@code -D}: picocli cannot tell which + * word of an undeclared option is its value, so the command gets them in their order, as it does from + * {@code runCommand}. + */ + private List typedArguments(List commandLine) { + List typed = [] + List afterName = commandLine.subList(commandLine.indexOf(name) + 1, commandLine.size()) + for (int i = 0; i < afterName.size(); i++) { + String word = afterName[i] + if (word == SYSTEM_PROPERTY_OPTION) { + i++ + } + else if (!word.startsWith(SYSTEM_PROPERTY_OPTION)) { + typed << word + } + } + typed + } + private CommandSpec createSpec(Map command) { CommandSpec created = CommandSpec.wrapWithoutInspection(this) created.name(name) @@ -101,6 +158,7 @@ class ProjectCommand extends BaseCommand implements Callable { .usageHelp(true) .description('Show this help message and exit.') .build()) + created.addOption(systemProperties) String description = command.description as String if (description) { created.usageMessage().description(description) @@ -150,17 +208,4 @@ class ProjectCommand extends BaseCommand implements Callable { List values = (declaration.values ?: []) as List values ? values.asImmutable() : null } - - /** - * Joins the arguments into the one {@code args} property {@code runCommand} splits again, quoting - * those that contain spaces or quotes. - */ - private static String join(List arguments) { - arguments.collect { String argument -> - if (argument && !(argument =~ /[\s"']/)) { - return argument - } - argument.contains('"') && !argument.contains("'") ? "'${argument}'" : "\"${argument}\"" - }.join(' ') - } } diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy index f77e1b17948..5fdf50d7fa9 100644 --- a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy @@ -30,8 +30,9 @@ import org.grails.forge.io.ConsoleOutput * *

The project's build describes them in a manifest, written by the Grails Gradle plugin's * {@code generateCommandManifest} task. The manifest is read as long as it is newer than the files - * that decide the project's dependencies and its own commands; otherwise the task is run first, so - * a plugin added to the build brings its commands with it.

+ * that decide the project's build and its own commands; otherwise the task is run first, so a plugin + * added to the build brings its commands with it. When the build fails, the commands of the last + * manifest are kept, and the build is not run for them again until one of those files changes.

*/ @CompileStatic class ProjectCommands { @@ -39,12 +40,25 @@ class ProjectCommands { /** The task of the Grails Gradle plugin that writes the manifest */ static final String MANIFEST_TASK = 'generateCommandManifest' - /** Where the task writes the manifest, relative to the project directory */ + /** The project property that tells the task where to write the manifest */ + static final String MANIFEST_PROPERTY = 'grailsCommandManifest' + + /** Where the manifest is kept, relative to the project directory */ static final String MANIFEST_PATH = 'build/grails/commands.json' - private static final List BUILD_FILES = ['build.gradle', 'build.gradle.kts', 'settings.gradle', - 'settings.gradle.kts', 'gradle.properties', - 'gradle/libs.versions.toml'] + /** Kept next to the manifest when the build could not write it, so the build is not run again for nothing */ + static final String FAILED_PATH = 'build/grails/commands.failed' + + /** The newest version of the manifest's format this CLI reads */ + static final int FORMAT_VERSION = 1 + + private static final List BUILD_FILE_SUFFIXES = ['.gradle', '.gradle.kts', '.properties', '.toml'] + + /** Directories of the build whose files change the build: scripts applied from them, and build logic */ + private static final List BUILD_DIRS = ['gradle', 'buildSrc'] + + /** Directories under the build directories that the build writes, rather than reads */ + private static final List OUTPUT_DIRS = ['build', '.gradle'] /** Where an application keeps its own commands */ private static final String COMMAND_SOURCES = 'grails-app/commands' @@ -74,7 +88,7 @@ class ProjectCommands { * the project's build or its commands. * * @param console where to say that the build is being run, and why the commands could not be read - * @return the commands, or none when the directory holds no Grails project or the build failed + * @return the commands, or none when the directory holds no Grails project or none could be read */ List load(ConsoleOutput console) { load(console, true) @@ -86,7 +100,7 @@ class ProjectCommands { * @param console where to say that the build is being run, and why the commands could not be read * @param runBuild whether the manifest may be updated by running the build; when not, a manifest * that is there is read as it is, and none means no commands - * @return the commands, or none when the directory holds no Grails project or the build failed + * @return the commands, or none when the directory holds no Grails project or none could be read */ List load(ConsoleOutput console, boolean runBuild) { if (!grailsProject) { @@ -97,54 +111,101 @@ class ProjectCommands { return [] } File manifest = new File(projectDir, MANIFEST_PATH) - if (!runBuild) { - return manifest.isFile() ? read(manifest, console) : [] + if (runBuild && outdated(manifest)) { + update(manifest, console) } - if (outdated(manifest)) { - console.out('Reading the commands of the project from its build...') - int exitCode = gradle.runSilently([MANIFEST_TASK]) - if (exitCode != 0 || !manifest.isFile()) { - console.warning("Could not read the commands of the project. Run its ${MANIFEST_TASK} Gradle task to see why.") - return [] - } + manifest.isFile() ? read(manifest, console) : [] + } + + private void update(File manifest, ConsoleOutput console) { + File failed = new File(projectDir, FAILED_PATH) + console.out('Reading the commands of the project from its build...') + String problem + try { + int exitCode = gradle.runSilently([MANIFEST_TASK, "-P${MANIFEST_PROPERTY}=${MANIFEST_PATH}".toString()]) + problem = exitCode == 0 ? null : "its build failed with exit code ${exitCode}" + } + catch (IOException e) { + problem = e.message } - read(manifest, console) + if (problem == null && manifest.isFile()) { + // the task is up to date when the classpath is, without writing the manifest again + manifest.setLastModified(System.currentTimeMillis()) + failed.delete() + return + } + failed.parentFile.mkdirs() + failed.setText((problem ?: 'its build wrote no manifest') + '\n', 'UTF-8') + String kept = manifest.isFile() ? 'The commands it listed before are offered' : 'Its commands are not offered' + console.warning("Could not read the commands of the project: ${problem ?: 'its build wrote no manifest'}. ${kept}, " + + 'and the build is not run for them again until a build file changes. A project of Grails 9 or ' + + "later lists them with its ${MANIFEST_TASK} Gradle task; run it to see what went wrong.") } private List read(File manifest, ConsoleOutput console) { - Map parsed try { - parsed = (Map) new JsonSlurper().parse(manifest, 'UTF-8') + Object parsed = new JsonSlurper().parse(manifest, 'UTF-8') + if (!(parsed instanceof Map)) { + throw new IllegalArgumentException('it is not a JSON object') + } + Map content = (Map) parsed + Object version = content.version + if (!(version instanceof Integer)) { + throw new IllegalArgumentException('it has no format version') + } + if ((int) version > FORMAT_VERSION) { + throw new IllegalArgumentException("it was written in format ${version}, by a newer Grails, and this " + + "Grails Forge CLI reads up to format ${FORMAT_VERSION}") + } + Object commands = content.commands + if (!(commands instanceof List)) { + throw new IllegalArgumentException('it has no list of commands') + } + return ((List) commands).collect { Object command -> create(command) } } - catch (JsonException | ClassCastException e) { - console.warning("Could not read the commands of the project from ${manifest}: ${e.message}") + catch (JsonException | RuntimeException e) { + console.warning("Could not read the commands of the project from ${manifest}, so they are not offered: ${e.message}") return [] } - List> commands = (parsed.commands ?: []) as List> - commands.collect { Map command -> new ProjectCommand(command, gradle) } + } + + private ProjectCommand create(Object command) { + if (!(command instanceof Map) || !(((Map) command).name instanceof String) || !((Map) command).name) { + throw new IllegalArgumentException("a command has no name: ${command}") + } + new ProjectCommand((Map) command, gradle) } private boolean outdated(File manifest) { - if (!manifest.isFile()) { + File failed = new File(projectDir, FAILED_PATH) + long lastRead = Math.max(manifest.isFile() ? manifest.lastModified() : 0L, failed.isFile() ? failed.lastModified() : 0L) + if (lastRead == 0L) { return true } - long written = manifest.lastModified() - List buildFiles = (BUILD_FILES.collect { new File(projectDir, it) } + - BUILD_FILES.collect { new File(gradle.rootDir, it) }).unique() - buildFiles.any { File file -> file.lastModified() > written } || - newerFile(new File(projectDir, COMMAND_SOURCES), written) + ([projectDir, gradle.rootDir] as LinkedHashSet).any { File dir -> changedBuild(dir, lastRead) } || + changed(new File(projectDir, COMMAND_SOURCES), lastRead) + } + + private static boolean changedBuild(File dir, long since) { + dir.listFiles()?.any { File file -> + file.isFile() && BUILD_FILE_SUFFIXES.any { String suffix -> file.name.endsWith(suffix) } && + file.lastModified() > since + } || BUILD_DIRS.any { String name -> changed(new File(dir, name), since) } } - private static boolean newerFile(File dir, long than) { + /** + * Whether a file in the directory, or the directory itself or one below it, changed: a directory + * changes when a file in it is added, deleted or renamed. + */ + private static boolean changed(File dir, long since) { if (!dir.isDirectory()) { return false } - boolean newer = false - dir.eachFileRecurse { File file -> - if (!newer && file.isFile() && file.lastModified() > than) { - newer = true - } + if (dir.lastModified() > since) { + return true + } + dir.listFiles()?.any { File file -> + file.isDirectory() ? !(file.name in OUTPUT_DIRS) && changed(file, since) : file.lastModified() > since } - newer } } diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectGradle.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectGradle.groovy index 62390184e18..143f9660e6a 100644 --- a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectGradle.groovy +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectGradle.groovy @@ -61,6 +61,7 @@ class ProjectGradle { * * @param arguments the tasks and arguments to run * @return the exit code of Gradle + * @throws IOException when the wrapper cannot be started */ int run(List arguments) { start(arguments) { ProcessBuilder builder -> builder.inheritIO() } @@ -71,6 +72,7 @@ class ProjectGradle { * * @param arguments the tasks and arguments to run * @return the exit code of Gradle + * @throws IOException when the wrapper cannot be started */ int runSilently(List arguments) { start(arguments) { ProcessBuilder builder -> @@ -89,6 +91,10 @@ class ProjectGradle { try { return builder.start().waitFor() } + catch (IllegalArgumentException e) { + // ProcessBuilder refuses some arguments of a batch file, such as one with a quote inside it + throw new IOException("Could not run ${wrapper}: ${e.message}", e) + } catch (InterruptedException e) { Thread.currentThread().interrupt() throw new IOException("Gradle was interrupted while running ${arguments}", e) diff --git a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy index 31ea0208b26..b8807812afd 100644 --- a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy +++ b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy @@ -22,6 +22,7 @@ import java.nio.file.Files import java.nio.file.attribute.PosixFilePermission import groovy.json.JsonOutput +import groovy.json.JsonSlurper import org.jline.reader.Candidate import org.jline.reader.LineReader @@ -68,6 +69,8 @@ class ProjectCommandsSpec extends Specification { name: 'url-mappings-report', description: 'url-mappings-report', arguments: [], options: [] ] + private static final List MANIFEST_TASK = ['generateCommandManifest', '-PgrailsCommandManifest=build/grails/commands.json'] + private static final Map PLUGIN_CREATE_APP = [ name: 'create-app', description: 'A plugin command named like a Forge command', arguments: [], options: [] ] @@ -119,7 +122,7 @@ class ProjectCommandsSpec extends Specification { CommandLine commandLine = ForgeCli.createCommandLine(context, false) then: "the build is run once, quietly, in the project directory" - gradleInvocations() == [['-p', dir.path, '-q', '--console=plain', 'generateCommandManifest']] + gradleInvocations() == [['-p', dir.path, '-q', '--console=plain'] + MANIFEST_TASK] output.toString().contains('Reading the commands of the project from its build...') and: "the project's commands are offered next to the Forge CLI's own" @@ -151,12 +154,49 @@ class ProjectCommandsSpec extends Specification { CommandLine commandLine = ForgeCli.createCommandLine(context, false) then: - gradleInvocations()*.last() == ['generateCommandManifest'] + gradleInvocations() == [['-p', dir.path, '-q', '--console=plain'] + MANIFEST_TASK] commandLine.subcommands.containsKey('s2-quickstart') where: changed << ['build.gradle', 'settings.gradle', 'gradle.properties', 'gradle/libs.versions.toml', - 'grails-app/commands/example/HelloCommand.groovy'] + 'dependencies.gradle', 'gradle/publishing.gradle', 'buildSrc/src/main/groovy/Conventions.groovy', + 'buildSrc/build.gradle', 'grails-app/commands/example/HelloCommand.groovy'] + } + + void 'the manifest is written again when a command is deleted from the application'() { + given: + File command = new File(dir, 'grails-app/commands/example/HelloCommand.groovy') + command.parentFile.mkdirs() + command.text = 'class HelloCommand {}' + writeManifest(URL_MAPPINGS_REPORT) + [command, command.parentFile, new File(dir, 'grails-app/commands')].each { it.setLastModified(manifest.lastModified() - 10_000) } + buildWrites(SECURITY_QUICKSTART) + + when: 'the command is deleted, which changes only its directory' + assert command.delete() + command.parentFile.setLastModified(manifest.lastModified() + 10_000) + CommandLine commandLine = ForgeCli.createCommandLine(context, false) + + then: + gradleInvocations()*.first() == ['-p'] + commandLine.subcommands.containsKey('s2-quickstart') + } + + void 'what the build writes for itself does not make the manifest outdated'() { + given: + writeManifest(URL_MAPPINGS_REPORT) + ['buildSrc/build/classes/Conventions.class', 'buildSrc/.gradle/file-system.probe'].each { String path -> + File written = new File(dir, path) + written.parentFile.mkdirs() + written.text = 'written by the build' + written.setLastModified(manifest.lastModified() + 10_000) + } + + when: + ForgeCli.createCommandLine(context, false) + + then: + gradleInvocations() == [] } void 'a directory without a Grails project has no project commands and runs no build'() { @@ -232,8 +272,12 @@ class ProjectCommandsSpec extends Specification { then: exitCode == 0 - gradleInvocations() == [['-p', dir.path, '-q', '--console=plain', 'runCommand', - '-Pargs=s2-quickstart com.example User Role --groupClassName=RoleGroup --uiOnly']] + gradleInvocations().size() == 1 + gradleInvocations().first().take(5) == ['-p', dir.path, '-q', '--console=plain', 'runCommand'] + commandArguments() == ['s2-quickstart', 'com.example', 'User', 'Role', '--groupClassName=RoleGroup', '--uiOnly'] + + and: 'the file of the arguments is gone once the command has run' + !new File(argumentsFile()).exists() } void 'an option value given as a separate argument is passed as one option'() { @@ -244,18 +288,63 @@ class ProjectCommandsSpec extends Specification { ForgeCli.execute(context, 's2-quickstart', 'com.example', 'User', 'Role', '--groupClassName', 'RoleGroup') then: - gradleInvocations()*.last() == ['-Pargs=s2-quickstart com.example User Role --groupClassName=RoleGroup'] + commandArguments() == ['s2-quickstart', 'com.example', 'User', 'Role', '--groupClassName=RoleGroup'] + } + + void 'a command that describes no options is given the words in the order they were typed'() { + given: + writeManifest(declared) + + when: 'an undeclared option whose value is a word of its own' + ForgeCli.execute(context, 'url-mappings-report', '--format', 'json', 'first') + + then: 'as runCommand gives them, so the command reads json as the value of --format' + commandArguments() == ['url-mappings-report', '--format', 'json', 'first'] + + where: + declared << [URL_MAPPINGS_REPORT, URL_MAPPINGS_REPORT + [arguments: [[name: 'first', required: false]]]] } - void 'a command that describes no arguments or options passes on whatever it is given, quoting what needs it'() { + void 'arguments are passed on unchanged, whatever quotes and spaces they hold'() { given: writeManifest(URL_MAPPINGS_REPORT) + List arguments = ['two words', 'say "hi"', 'it\'s "x" y', '', '%PATH% & | <>'] + + when: + ForgeCli.execute(context, (['url-mappings-report'] + arguments) as String[]) + + then: + commandArguments() == ['url-mappings-report'] + arguments + } + + void '-D sets a system property of the build, such as the environment, and is not passed to the command'() { + given: + writeManifest(SECURITY_QUICKSTART, URL_MAPPINGS_REPORT) + + when: + ForgeCli.execute(context, typed as String[]) + + then: + gradleInvocations().first().subList(4, 6) == ['-Dgrails.env=test', 'runCommand'] + commandArguments() == passed + + where: + typed || passed + ['s2-quickstart', '-Dgrails.env=test', 'com.example'] || ['s2-quickstart', 'com.example'] + ['url-mappings-report', '-Dgrails.env=test', 'first'] || ['url-mappings-report', 'first'] + ['url-mappings-report', 'first', '-D', 'grails.env=test'] || ['url-mappings-report', 'first'] + } + + void "a project command after the Forge CLI's own options runs the build that finds it"() { + given: + buildWrites(URL_MAPPINGS_REPORT) when: - ForgeCli.execute(context, 'url-mappings-report', 'first', 'two words', '--format=json', 'say "hi"', '') + int exitCode = ForgeCli.execute(context, '-x', 'url-mappings-report') - then: 'the arguments in the order given, then the options it does not know' - gradleInvocations()*.last() == ['-Pargs=url-mappings-report first "two words" \'say "hi"\' "" --format=json'] + then: + exitCode == 0 + gradleInvocations()*.get(4) == ['generateCommandManifest', 'runCommand'] } void 'the exit code of the command is the exit code of the build'() { @@ -296,7 +385,7 @@ class ProjectCommandsSpec extends Specification { gradleInvocations() == [] } - void 'a build that cannot write the manifest is reported, and the Forge CLI still runs'() { + void 'a build that cannot write the manifest, such as one of an earlier Grails, is reported once, and the Forge CLI still runs'() { given: new File(dir, 'gradle-exit-code').text = '1' @@ -304,9 +393,102 @@ class ProjectCommandsSpec extends Specification { CommandLine commandLine = ForgeCli.createCommandLine(context, false) then: - output.toString().contains('Could not read the commands of the project. Run its generateCommandManifest Gradle task to see why.') + output.toString().contains('Could not read the commands of the project: its build failed with exit code 1. ' + + 'Its commands are not offered, and the build is not run for them again until a build file changes. ' + + 'A project of Grails 9 or later lists them with its generateCommandManifest Gradle task; run it to see what went wrong.') commandLine.subcommands.containsKey('create-app') !commandLine.subcommands.containsKey('s2-quickstart') + + when: 'the Forge CLI starts again in the same project' + ForgeCli.createCommandLine(context, false) + + then: 'the build is not run again' + gradleInvocations().size() == 1 + + when: 'a build file changes, and the build works again' + new File(dir, 'gradle-exit-code').delete() + buildWrites(SECURITY_QUICKSTART) + new File(dir, 'build.gradle').setLastModified(System.currentTimeMillis() + 20_000) + commandLine = ForgeCli.createCommandLine(context, false) + + then: + gradleInvocations().size() == 2 + commandLine.subcommands.containsKey('s2-quickstart') + } + + void 'a failed build keeps the commands of the last manifest, and is not run again for them'() { + given: 'an outdated manifest, and a build that fails, as while the application does not compile' + writeManifest(URL_MAPPINGS_REPORT) + changedBefore(20_000, manifest) + changedBefore(10_000, new File(dir, 'build.gradle')) + new File(dir, 'gradle-exit-code').text = '1' + + when: + CommandLine commandLine = ForgeCli.createCommandLine(context, false) + + then: + commandLine.subcommands.containsKey('url-mappings-report') + output.toString().contains('The commands it listed before are offered') + + when: + new File(dir, 'gradle-exit-code').delete() + int exitCode = ForgeCli.execute(context, 'url-mappings-report') + + then: 'the command runs without the build being tried again' + exitCode == 0 + gradleInvocations()*.get(4) == ['generateCommandManifest', 'runCommand'] + } + + void 'a wrapper that cannot be started is reported, and the Forge CLI still runs'() { + given: + writeManifest(URL_MAPPINGS_REPORT) + new File(dir, 'build.gradle').setLastModified(manifest.lastModified() + 10_000) + Files.setPosixFilePermissions(new File(dir, 'gradlew').toPath(), [PosixFilePermission.OWNER_READ] as Set) + + when: + CommandLine commandLine = ForgeCli.createCommandLine(context, false) + + then: + output.toString().contains('Could not read the commands of the project: ') + commandLine.subcommands.containsKey('create-app') + commandLine.subcommands.containsKey('url-mappings-report') + } + + void 'a build whose manifest task is up to date is not run again'() { + given: 'a build file changed in a way that leaves the classpath, and so the manifest, as it was' + writeManifest(URL_MAPPINGS_REPORT) + changedBefore(20_000, manifest) + changedBefore(10_000, new File(dir, 'build.gradle')) + + when: + ForgeCli.createCommandLine(context, false) + ForgeCli.createCommandLine(context, false) + + then: + gradleInvocations().size() == 1 + } + + void 'a manifest #problem is reported, and the Forge CLI still runs'() { + given: + manifest.parentFile.mkdirs() + manifest.text = JsonOutput.toJson(content) + manifest.setLastModified(System.currentTimeMillis() + 10_000) + + when: + CommandLine commandLine = ForgeCli.createCommandLine(context, false) + + then: + output.toString().contains("Could not read the commands of the project from ${manifest}, so they are not offered: ${message}") + commandLine.subcommands.containsKey('create-app') + gradleInvocations() == [] + + where: + problem | content || message + 'of a newer format' | [version: 2, commands: [URL_MAPPINGS_REPORT]] || 'it was written in format 2, by a newer Grails' + 'without a format version' | [commands: [URL_MAPPINGS_REPORT]] || 'it has no format version' + 'whose commands are not a list' | [version: 1, commands: 'oops'] || 'it has no list of commands' + 'with a command without a name' | [version: 1, commands: [[description: 'nameless']]] || 'a command has no name' + 'that is not an object' | [URL_MAPPINGS_REPORT] || 'it is not a JSON object' } void 'a manifest that is not JSON is reported'() { @@ -318,7 +500,7 @@ class ProjectCommandsSpec extends Specification { CommandLine commandLine = ForgeCli.createCommandLine(context, false) then: - output.toString().contains("Could not read the commands of the project from ${manifest}") + output.toString().contains("Could not read the commands of the project from ${manifest}, so they are not offered") commandLine.subcommands.containsKey('create-app') } @@ -331,6 +513,8 @@ class ProjectCommandsSpec extends Specification { complete(commandLine, 's2-').containsAll(['s2-quickstart', 's2-create-persistent-token']) complete(commandLine, 's2-quickstart --').containsAll(['--groupClassName', '--uiOnly', '--help']) complete(commandLine, 's2-create-persistent-token --format ').containsAll(['json', 'yaml']) + complete(commandLine, 's2-create-persistent-token --format=').containsAll(['--format=json', '--format=yaml']) + complete(commandLine, 's2-create-persistent-token --format=j').contains('--format=json') } void 'a project of a multi-project build runs the wrapper at the root of the build'() { @@ -344,7 +528,8 @@ class ProjectCommandsSpec extends Specification { then: exitCode == 0 - gradleInvocations() == [['-p', project.path, '-q', '--console=plain', 'runCommand', '-Pargs=url-mappings-report']] + gradleInvocations()*.take(5) == [['-p', project.path, '-q', '--console=plain', 'runCommand']] + commandArguments() == ['url-mappings-report'] } void 'a wrapper above the root of the build is never run'() { @@ -389,6 +574,13 @@ class ProjectCommandsSpec extends Specification { project } + /** + * Dates the file in the past, so that what the Forge CLI writes now is newer than it. + */ + private static void changedBefore(long millis, File file) { + assert file.setLastModified(System.currentTimeMillis() - millis) + } + private void buildWrites(Map... commands) { new File(dir, 'manifest-source.json').text = JsonOutput.toJson([version: 1, commands: commands.toList()]) } @@ -421,12 +613,28 @@ class ProjectCommandsSpec extends Specification { invocations } + /** + * The arguments the last command ran with, from the file runCommand was given, as the stand-in + * wrapper logged it. + */ + private List commandArguments() { + String logged = gradleInvocations().last().find { String argument -> argument.startsWith('argsFile:') } + (List) new JsonSlurper().parseText(logged.substring('argsFile:'.length())) + } + + private String argumentsFile() { + gradleInvocations().last().find { String argument -> argument.startsWith('-PargsFile=') }.substring('-PargsFile='.length()) + } + private void writeGradleWrapper(File wrapperDir = dir) { File wrapper = new File(wrapperDir, 'gradlew') wrapper.text = '''\ #!/bin/sh dir=$(cd "$(dirname "$0")" && pwd) - { for argument in "$@"; do printf '%s\\n' "$argument"; done; echo '--'; } >> "$dir/gradle-invocations.txt" + { for argument in "$@"; do + printf '%s\\n' "$argument" + case "$argument" in -PargsFile=*) printf 'argsFile:%s\\n' "$(cat "${argument#-PargsFile=}")";; esac + done; echo '--'; } >> "$dir/gradle-invocations.txt" case " $* " in *" generateCommandManifest "*) if [ -f "$dir/manifest-source.json" ]; then From 94123f6965f8f73f1d4e304d4338b883f4707b43 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Tue, 17 Nov 2026 08:36:42 -0800 Subject: [PATCH 052/115] Document how project commands get their arguments, environment and refreshed manifest --- grails-doc/src/en/guide/commandLine.adoc | 49 ++++++++++++++----- .../commandLine/creatingCustomCommands.adoc | 29 +++++++++-- 2 files changed, 63 insertions(+), 15 deletions(-) diff --git a/grails-doc/src/en/guide/commandLine.adoc b/grails-doc/src/en/guide/commandLine.adoc index 134e5b266e1..0833b2bc1c0 100644 --- a/grails-doc/src/en/guide/commandLine.adoc +++ b/grails-doc/src/en/guide/commandLine.adoc @@ -213,26 +213,53 @@ grails> s2-quickstart com.example User Role -- ---- Each command shows its usage with `--help`, and runs through the project's `runCommand` Gradle task, -as `./gradlew runCommand -Pargs="s2-quickstart com.example User Role"` would run it. The Forge CLI's own -commands keep their name: a project command named like one of them is not offered. +as `./gradlew runCommand -Pargs="s2-quickstart com.example User Role"` would run it. The Forge CLI hands +the arguments to the task in a file, so an argument with spaces or quotes reaches the command as typed, +on every operating system. The Forge CLI's own commands keep their name: a project command named like +one of them is not offered. + +A command runs in the development environment unless `-D` says otherwise. `-D` sets a system property +of the project's build, which `runCommand` passes on to the command, as `./gradlew -Dgrails.env=test +runCommand` does: + +[source,shell] +---- +grails> dbm-update -Dgrails.env=test +---- The Forge CLI learns the commands from the project's build: the Grails Gradle plugin's `generateCommandManifest` task writes them to `build/grails/commands.json`. The Forge CLI runs the task -when the file is missing, or older than the project's build files (`build.gradle`, `settings.gradle`, -`gradle.properties` and `gradle/libs.versions.toml`) or its `grails-app/commands`, so a plugin added to -the build brings its commands the next time a project command is asked for. The build runs only when the -interactive prompt starts or a command the Forge CLI does not have is named: its own commands, such as -`create-controller`, and its options, such as `--help`, never run it. `grails-forge-cli --help` lists the -project's commands from the file the build last wrote. When the task fails, the Forge CLI says so and -offers only its own commands; run `./gradlew generateCommandManifest` to see why. +when the file is missing, or older than one of the files that decide the build or the application's +commands: + +* the `.gradle`, `.gradle.kts`, `.properties` and `.toml` files of the project directory and of the + root directory of its build, such as `build.gradle`, `settings.gradle` and `gradle.properties`; +* the files under their `gradle` and `buildSrc` directories, such as `gradle/libs.versions.toml` and + scripts applied from there; +* the files under `grails-app/commands`, including a command deleted from there. + +So a plugin added to the build brings its commands the next time a project command is asked for. A +change the Forge CLI cannot see, such as a new snapshot of a plugin, is picked up by running +`./gradlew generateCommandManifest`. + +The build runs only when the interactive prompt starts or a command the Forge CLI does not have is +named, also after the Forge CLI's own options, as in `grails-forge-cli -x dbm-update`. Its own commands, +such as `create-controller`, and its options alone, such as `--help`, never run it. +`grails-forge-cli --help` lists the project's commands from the file the build last wrote. + +When the build fails, as it does while the application does not compile or in a project of a Grails +release without `generateCommandManifest`, the Forge CLI says so once and keeps offering the commands of +the file the build last wrote, if any. It does not run the build for them again until one of the files +above changes; run `./gradlew generateCommandManifest` to see what went wrong. The build runs through the Gradle wrapper of the project, or of the build it belongs to: the wrapper in the first directory above it with a `settings.gradle` file. A project whose build has no wrapper has no commands offered, and no `gradle` found on the path is run instead. A command that describes its arguments and options, as xref:commandLine#creatingCustomCommands[Creating Custom Commands] -shows, has them checked and completed. A command that does not takes any arguments and options and -passes them on unchanged. +shows, has them checked and completed. A command that declares no options takes any arguments and +options, and is given the words of its command line in the order they were typed, as `runCommand` +gives them. The `grails` command of an SDKMAN! installation starts the profile CLI by default. Start the Forge CLI with `grails-forge-cli`, with `grails -t forge`, or by default with `GRAILS_PREFERRED_SHELL=forge`. diff --git a/grails-doc/src/en/guide/commandLine/creatingCustomCommands.adoc b/grails-doc/src/en/guide/commandLine/creatingCustomCommands.adoc index 00c92586c8a..05e446783eb 100644 --- a/grails-doc/src/en/guide/commandLine/creatingCustomCommands.adoc +++ b/grails-doc/src/en/guide/commandLine/creatingCustomCommands.adoc @@ -212,6 +212,16 @@ Now you can run the `greet` command with arguments and options: ./gradlew runCommand -Pargs="greet Alice --loud" ---- +`args` is split into words as a shell would split it. A tool that passes arguments it cannot quote for +every shell can put them in a JSON file as an array of strings and name it with `argsFile`, a path +relative to the project directory. Each is passed on unchanged, after those of `args`: + +[source,shell] +---- +echo '["greet", "Alice and Bob"]' > greet-args.json +./gradlew runCommand -PargsFile=greet-args.json +---- + This allows you to create more versatile and interactive custom commands for your Grails application. **Describing Arguments and Options** @@ -251,13 +261,24 @@ class GreetCommand implements GrailsApplicationCommand { } ---- -An argument is required unless it declares `required = false`. An option is given as `--name=value`, or -as `--name` when it is a `flag`. `values` lists the values offered when the argument or option is -completed. The annotations only describe the command: it still reads its arguments and options from the +An argument is required unless it declares `required = false`. An option is given as `--name value` or +`--name=value`, or as `--name` when it is a `flag`. `values` lists the values offered when the argument +or option is completed, in either form: + +[source,shell] +---- +grails> greet Alice --language +de en fr +grails> greet Alice --language= +--language=de --language=en --language=fr +---- + +The Forge CLI passes a declared option on as `--name=value`, after the arguments. The annotations only describe the command: it still reads its arguments and options from the command line as before. A command that declares no `@CommandArgument` takes any arguments, and one that declares no -`@CommandOption` takes any options, so a command without the annotations works as it always has. +`@CommandOption` takes any options and is given its command line in the order it was typed, so a command +without the annotations works as it always has. In summary, creating custom commands in Grails using the `GrailsApplicationCommand` trait is a powerful way to extend your application's functionality beyond the web interface. You can define the command's name, description, and logic, and then execute it from the command line, optionally passing arguments and options as needed. From 9e031ee6054251f322db4b1ef6eacb2ff531f253 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Tue, 17 Nov 2026 11:43:11 -0800 Subject: [PATCH 053/115] Point the content negotiation guide at upgrade section 25 The merge of 9.0.x renumbered the upgrade notes of grails.mime.negotiation.spring to section 25, which the guide still gave as section 22. The grails-9-upgrade skill now lists the setting with the other opt-in settings Grails 10 makes the default, and the answers it changes. --- .../en/guide/theWebLayer/contentNegotiation.adoc | 2 +- .../skills/grails-9-upgrade/SKILL.md | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc b/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc index 2cd642f3bf5..9de2792e674 100644 --- a/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc +++ b/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc @@ -347,7 +347,7 @@ type is configured for in the first configured format |Dropped: such a request, and one whose `Accept` header Spring cannot read, is not acceptable. |=== -Section 22 of xref:upgrading#upgrading90x[Upgrading from Grails 8 to Grails 9] lists how each answer changes. +Section 25 of xref:upgrading#upgrading90x[Upgrading from Grails 8 to Grails 9] lists how each answer changes. ==== Testing Content Negotiation diff --git a/grails-skills/upgrade-guide-9/skills/grails-9-upgrade/SKILL.md b/grails-skills/upgrade-guide-9/skills/grails-9-upgrade/SKILL.md index ce4817a689f..1172635fb72 100644 --- a/grails-skills/upgrade-guide-9/skills/grails-9-upgrade/SKILL.md +++ b/grails-skills/upgrade-guide-9/skills/grails-9-upgrade/SKILL.md @@ -26,7 +26,7 @@ Activate this skill when: - Reviewing a Grails 9 upgrade branch or pull request. - Fixing tests, build failures, runtime failures, or behavior changes after moving from Grails 8.x to Grails 9. - Deciding whether an application keeps or excludes `grails-xml`. -- Turning on `grails.web.rendering.json.spring`, `grails.converters.json.legacy: false`, `grails.databinding.json.jackson`, `grails.exceptionresolver.springHandlers` or `grails.controllers.responseBody`, and checking what changes. +- Turning on `grails.web.rendering.json.spring`, `grails.converters.json.legacy: false`, `grails.databinding.json.jackson`, `grails.exceptionresolver.springHandlers`, `grails.controllers.responseBody` or `grails.mime.negotiation.spring`, and checking what changes. - Replacing `JSON.createNamedConfig(...)`, `JSON.use(...)` or marshallers that customize `respond` with Jackson serializers and the `NamedJsonConfigurationRegistry`. - Fixing web unit tests (`ControllerUnitTest`, `TagLibUnitTest`, `UrlMappingsUnitTest`, `JsonViewUnitTest`) that fail after the upgrade. - Porting code that uses RxGORM, `grails-async-rxjava`, `grails-async-rxjava2`, `grails-events-rxjava`, or `grails-events-rxjava2` to RxJava 3. @@ -161,6 +161,7 @@ XML conversion, XML request-body binding, and the XML, Atom, HAL XML and Vnd.Err | `grails.web.rendering.xml.spring` | XML that `respond` writes for ordinary beans | `false` | unchanged until Grails 11 removes legacy XML conversion | | `grails.exceptionresolver.springHandlers` | Exceptions actions throw, resolved by Spring MVC `@ExceptionHandler` methods | `false` | `true` in Grails 10 | | `grails.controllers.responseBody` | The values of actions annotated `@ResponseBody`, written by Spring MVC as the body | `false` | `true` in Grails 10 | +| `grails.mime.negotiation.spring` | `request.format`, `response.format`, `withFormat` and the format `respond` writes in, resolved with Spring MVC's content negotiation manager | `false` | `true` in Grails 10 | - Grails 10 changes the three JSON settings together, so that `respond`, `render ... as JSON`, `request.JSON` and data binding write and read JSON with the same mapper. Test them together. At the least, set `grails.converters.json.legacy: false` and `grails.databinding.json.jackson: true` together, or `request.JSON` and data binding accept different JSON. - Grails 9 logs a warning while a legacy path is used: once when the legacy `respond` path renders, at startup for `JsonSlurper` request binding, and at startup while `grails.converters.json.legacy` is unset. Setting `grails.converters.json.legacy` to either value turns its warning off. @@ -336,6 +337,17 @@ The Grails HAL JSON renderers remain available through explicit renderer registr - A renderer or binding source creator registered for an interface of `java.lang.Class`, such as `Serializable`, no longer matches every class. - The MIME types and the `grails.mime.use.accept.header` and `grails.mime.disable.accept.header.*` settings live in the application context instead of static fields. Remove test code that reset those fields or called `HttpServletResponseExtension.loadMimeTypeConfig`, and set the settings in a spec's `doWithConfig`, since they are read once for each application context. A subclass of `DefaultAcceptHeaderParser` that overrode `createMimeTypeAndAddToList` must take the requested `MimeType` now. +### Opt Into Spring MVC Content Negotiation for Grails Formats + +With `grails.mime.negotiation.spring: true`, which Grails 10 makes the default, Grails formats are resolved with Spring MVC's content negotiation manager, and the `format` parameter, URL extensions and the `grails.mime` `Accept` settings also decide what Spring MVC writes for an action. Before turning it on, check these in functional tests: + +- `Accept: */*, application/json` is `json`, where it is `all` without the setting: a more specific type comes ahead of a wildcard of equal quality. A `+xml` type still comes ahead of the XML type of equal quality. +- A type with the `+json` or `+xml` suffix that no configured type names, such as `application/merge-patch+json`, is the `json` or `xml` format, in `Accept` and in `Content-Type`, so data binding reads such a body as JSON. +- A request that accepts no configured type, such as `Accept: image/png` alone, or whose `Accept` header Spring cannot read, has no format: `response.format` and `response.mimeType` are `null`, `respond` answers `406` and `withFormat` runs its `*` block, or none. Make code that reads them allow for `null`. +- A format parameter or URL extension no type is configured for is not acceptable, where it was answered in the first configured format. `?format=all` leaves the format to the `Accept` header. +- A `ResponseEntity`, `ProblemDetail` or other value Spring MVC writes for an action follows the URL extension and the format parameter: `/book/show/1.json` is JSON whatever the `Accept` header says, and a map at `/book/show/1.html` answers `406`. +- A wildcard such as `text/*` selects the first `withFormat` block, or the first format of the action, with a type compatible with it. + ## Web Unit Tests - `ControllerUnitTest`, `TagLibUnitTest` and the other web test traits always include the `restResponder` plugin, even when a spec overrides `getIncludePlugins()`. They also register JSON conversion beans with Boot's Jackson configuration, Spring MVC's message converters, the `mvcContentNegotiationManager` and `requestMappingHandlerAdapter` beans unless the spec declares beans of those names, and the `grails-xml` beans when that module is on the test classpath. Update specs that assert on the beans of their context. From c5c4ff6affa12a5ab87af5876277a81da234a11d Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Tue, 17 Nov 2026 11:47:42 -0800 Subject: [PATCH 054/115] Keep every equals sign of an option value after the first CommandLineParser split --name=value on every equals sign and kept only the second part, so --filter=a=b gave the command 'a', and --filter= threw ArrayIndexOutOfBoundsException. The value is now everything after the first equals sign. --- .../build/parsing/CommandLineParser.java | 9 +++--- .../parsing/CommandLineParserSpec.groovy | 29 +++++++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/grails-gradle/model/src/main/groovy/org/grails/build/parsing/CommandLineParser.java b/grails-gradle/model/src/main/groovy/org/grails/build/parsing/CommandLineParser.java index 0e4d522e0d0..35f1dc1a514 100644 --- a/grails-gradle/model/src/main/groovy/org/grails/build/parsing/CommandLineParser.java +++ b/grails-gradle/model/src/main/groovy/org/grails/build/parsing/CommandLineParser.java @@ -258,11 +258,12 @@ protected String processOption(DefaultCommandLine cl, String arg) { arg = (arg.charAt(1) == '-' ? arg.substring(2, arg.length()) : arg.substring(1, arg.length())).trim(); - if (arg.contains("=")) { - String[] split = arg.split("="); - String name = split[0].trim(); + int equals = arg.indexOf('='); + if (equals >= 0) { + // the value is everything after the first equals sign, which may contain more of them + String name = arg.substring(0, equals).trim(); validateOptionName(name); - String value = split[1].trim(); + String value = arg.substring(equals + 1).trim(); if (declaredOptions.containsKey(name)) { cl.addDeclaredOption(name, declaredOptions.get(name), value); } diff --git a/grails-gradle/model/src/test/groovy/org/grails/build/parsing/CommandLineParserSpec.groovy b/grails-gradle/model/src/test/groovy/org/grails/build/parsing/CommandLineParserSpec.groovy index e4a52ea0940..b366a5c25b3 100644 --- a/grails-gradle/model/src/test/groovy/org/grails/build/parsing/CommandLineParserSpec.groovy +++ b/grails-gradle/model/src/test/groovy/org/grails/build/parsing/CommandLineParserSpec.groovy @@ -406,4 +406,33 @@ class CommandLineParserSpec extends Specification { then: thrown ParseException } + + void "An option value keeps every equals sign after the first, for #arguments"() { + when: + def parser = new CommandLineParser() + parser.addOption('filter', 'A declared option') + def cl = parser.parse(arguments as String[]) + + then: + cl.commandName == 'report' + cl.optionValue(name) == value + cl.remainingArgs == ['first'] + + where: + arguments || name | value + ['report', '--filter=a=b', 'first'] || 'filter' | 'a=b' + ['report', '--other=x=y=z', 'first'] || 'other' | 'x=y=z' + ['report', '--filter==b', 'first'] || 'filter' | '=b' + ['report', '--filter=', 'first'] || 'filter' | '' + ['report', 'first', '--filter=url=/a?b=c'] || 'filter' | 'url=/a?b=c' + } + + void "parseString keeps every equals sign of a quoted option value"() { + when: + def cl = new CommandLineParser().parseString("report '--filter=a=b c' first") + + then: + cl.optionValue('filter') == 'a=b c' + cl.remainingArgs == ['first'] + } } From 2f3a016d0a0eb6c58b533f937367853671a0bd71 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Tue, 17 Nov 2026 11:47:42 -0800 Subject: [PATCH 055/115] Test that the Forge CLI passes an option value with equals signs whole --- .../grails/forge/cli/ProjectCommandsSpec.groovy | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy index b8807812afd..efe0422e5f4 100644 --- a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy +++ b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy @@ -291,6 +291,20 @@ class ProjectCommandsSpec extends Specification { commandArguments() == ['s2-quickstart', 'com.example', 'User', 'Role', '--groupClassName=RoleGroup'] } + void 'an option value with equals signs in it is passed whole, as #given'() { + given: + writeManifest(SECURITY_QUICKSTART) + + when: + ForgeCli.execute(context, (['s2-quickstart', 'com.example', 'User', 'Role'] + given) as String[]) + + then: + commandArguments() == ['s2-quickstart', 'com.example', 'User', 'Role', '--groupClassName=a=b=c'] + + where: + given << [['--groupClassName', 'a=b=c'], ['--groupClassName=a=b=c']] + } + void 'a command that describes no options is given the words in the order they were typed'() { given: writeManifest(declared) From 2b8156e0f9dd76e579f587fc9f68cb2cf3aa9ae9 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Tue, 17 Nov 2026 11:50:57 -0800 Subject: [PATCH 056/115] Refresh the manifest when the commands directory is removed Removing grails-app/commands with its last command left nothing whose time could change, so the manifest went on offering the removed commands. Without the directory, grails-app is compared instead, which changed when the directory was removed. The Forge CLI spec now also parses the arguments it hands runCommand with the parser runCommand uses. --- grails-doc/src/en/guide/commandLine.adoc | 2 +- .../forge/cli/command/ProjectCommands.groovy | 11 ++++- .../forge/cli/ProjectCommandsSpec.groovy | 43 +++++++++++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/grails-doc/src/en/guide/commandLine.adoc b/grails-doc/src/en/guide/commandLine.adoc index 0833b2bc1c0..1739538a857 100644 --- a/grails-doc/src/en/guide/commandLine.adoc +++ b/grails-doc/src/en/guide/commandLine.adoc @@ -236,7 +236,7 @@ commands: root directory of its build, such as `build.gradle`, `settings.gradle` and `gradle.properties`; * the files under their `gradle` and `buildSrc` directories, such as `gradle/libs.versions.toml` and scripts applied from there; -* the files under `grails-app/commands`, including a command deleted from there. +* the files under `grails-app/commands`, including a command deleted from there, or the whole directory removed. So a plugin added to the build brings its commands the next time a project command is asked for. A change the Forge CLI cannot see, such as a new snapshot of a plugin, is picked up by running diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy index 5fdf50d7fa9..b16aa6e497c 100644 --- a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy @@ -183,7 +183,16 @@ class ProjectCommands { return true } ([projectDir, gradle.rootDir] as LinkedHashSet).any { File dir -> changedBuild(dir, lastRead) } || - changed(new File(projectDir, COMMAND_SOURCES), lastRead) + changedSources(lastRead) + } + + /** + * Whether the commands of the application changed. When their directory is gone, {@code grails-app}, + * which every project has, changed when it was removed. + */ + private boolean changedSources(long since) { + File sources = new File(projectDir, COMMAND_SOURCES) + sources.isDirectory() ? changed(sources, since) : sources.parentFile.lastModified() > since } private static boolean changedBuild(File dir, long since) { diff --git a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy index efe0422e5f4..4b08b01eec7 100644 --- a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy +++ b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy @@ -34,6 +34,7 @@ import org.jline.terminal.TerminalBuilder import org.springframework.context.ConfigurableApplicationContext import picocli.CommandLine +import org.grails.build.parsing.CommandLineParser import org.grails.forge.cli.command.CreateAppCommand import spock.lang.AutoCleanup import spock.lang.IgnoreIf @@ -182,6 +183,41 @@ class ProjectCommandsSpec extends Specification { commandLine.subcommands.containsKey('s2-quickstart') } + void 'the manifest is written again when the commands directory is removed with the last command'() { + given: + File command = new File(dir, 'grails-app/commands/example/HelloCommand.groovy') + command.parentFile.mkdirs() + command.text = 'class HelloCommand {}' + writeManifest(URL_MAPPINGS_REPORT) + [command, command.parentFile, command.parentFile.parentFile, new File(dir, 'grails-app'), dir].each { + it.setLastModified(manifest.lastModified() - 10_000) + } + buildWrites(SECURITY_QUICKSTART) + + when: 'the directory is deleted, which changes only grails-app' + assert new File(dir, 'grails-app/commands').deleteDir() + new File(dir, 'grails-app').setLastModified(manifest.lastModified() + 10_000) + CommandLine commandLine = ForgeCli.createCommandLine(context, false) + + then: + gradleInvocations() == [['-p', dir.path, '-q', '--console=plain'] + MANIFEST_TASK] + commandLine.subcommands.containsKey('s2-quickstart') + } + + void 'a project without a commands directory does not run the build while nothing changed'() { + given: + assert new File(dir, 'grails-app/commands').deleteDir() + writeManifest(URL_MAPPINGS_REPORT) + [new File(dir, 'grails-app'), dir].each { it.setLastModified(manifest.lastModified() - 10_000) } + + when: + CommandLine commandLine = ForgeCli.createCommandLine(context, false) + + then: + gradleInvocations() == [] + commandLine.subcommands.containsKey('url-mappings-report') + } + void 'what the build writes for itself does not make the manifest outdated'() { given: writeManifest(URL_MAPPINGS_REPORT) @@ -301,6 +337,13 @@ class ProjectCommandsSpec extends Specification { then: commandArguments() == ['s2-quickstart', 'com.example', 'User', 'Role', '--groupClassName=a=b=c'] + and: 'the parser runCommand reads them with gives the command the whole value' + with(new CommandLineParser().parse(commandArguments() as String[])) { + commandName == 's2-quickstart' + remainingArgs == ['com.example', 'User', 'Role'] + optionValue('groupClassName') == 'a=b=c' + } + where: given << [['--groupClassName', 'a=b=c'], ['--groupClassName=a=b=c']] } From 5aaa6995f97ace319f5df592da062afa6d546290 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Tue, 17 Nov 2026 12:49:02 -0800 Subject: [PATCH 057/115] Carry upgrade notes 25 to 27 into the Grails 9 upgrade skill The skill follows the guide note for note and ended at note 24. It now has a section for each of the three auto-timestamp notes, with their required actions, and three checklist items. Note 25 says that unit tests still do not set the auditor properties, so note 27 is not read as applying to them. --- .../src/en/guide/upgrading/upgrading90x.adoc | 2 +- .../skills/grails-9-upgrade/SKILL.md | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/grails-doc/src/en/guide/upgrading/upgrading90x.adoc b/grails-doc/src/en/guide/upgrading/upgrading90x.adoc index fa2d13ba2c2..6831b42b890 100644 --- a/grails-doc/src/en/guide/upgrading/upgrading90x.adoc +++ b/grails-doc/src/en/guide/upgrading/upgrading90x.adoc @@ -1455,7 +1455,7 @@ With `cacheUsers` enabled, the Spring Security plugin's user cache now stores us ==== 25. Unit Tests Run Domain Class Events for Every Data Source and Tenant -In a unit test that implements `DataTest` or `DomainUnitTest`, a save through a data source other than the default one, as `book.secondary.save()` makes, and a save for a tenant with a database or a schema of its own, now run the domain class's events, such as `beforeInsert`, and set its `dateCreated` and `lastUpdated` properties, as they do in the application. In Grails 8 they ran neither. +In a unit test that implements `DataTest` or `DomainUnitTest`, a save through a data source other than the default one, as `book.secondary.save()` makes, and a save for a tenant with a database or a schema of its own, now run the domain class's events, such as `beforeInsert`, and set its `dateCreated` and `lastUpdated` properties, as they do in the application. In Grails 8 they ran neither. Unit tests still do not set the `@CreatedBy` and `@LastModifiedBy` properties, on any data source, even when the test defines an `AuditorAware` bean, as in Grails 8: the in-memory datastore is not given the application context, so note 27 does not apply to them. ===== Required Actions diff --git a/grails-skills/upgrade-guide-9/skills/grails-9-upgrade/SKILL.md b/grails-skills/upgrade-guide-9/skills/grails-9-upgrade/SKILL.md index ce4817a689f..ef200b64537 100644 --- a/grails-skills/upgrade-guide-9/skills/grails-9-upgrade/SKILL.md +++ b/grails-skills/upgrade-guide-9/skills/grails-9-upgrade/SKILL.md @@ -458,6 +458,31 @@ With `cacheUsers`, the Spring Security plugin's user cache stores users by value - `cacheConfig` on the `userCache` bean, a `MutableConfiguration` with `setStoreByValue(false)`, restores storing by reference. - In a native image, the plugin registers `User`, `GrailsUser` with a `Long` id and `SimpleGrantedAuthority` for serialization; a custom user class or id type needs metadata, which `traceNativeMetadata` records when the login form is in `forms`. +## Unit Tests Run Domain Class Events for Every Data Source and Tenant + +In a `DataTest` or `DomainUnitTest` spec, a save through a data source other than the default one, such as `book.secondary.save()`, and a save for a tenant with a database or a schema of its own, now run the domain class's events, such as `beforeInsert`, and set `dateCreated` and `lastUpdated`, as in the application. Grails 8 ran neither. Unit tests still do not set `@CreatedBy` and `@LastModifiedBy`, even with an `AuditorAware` bean in the test. + +- A test that expected such a save to keep the timestamps it was given saves inside `withoutTimestamps { }` of that data source's listener, such as `Book.secondary.autoTimestampEventListener`. +- A test that expected a domain class event not to run on such a save expects what the event does. +- The in-memory datastore follows `grails.gorm.events.autoTimestampInsertOverwrite` and `grails.gorm.autoTimestampCacheAnnotations` on every data source, the default one included. With `autoTimestampInsertOverwrite: false`, a test that expected a given `dateCreated` or `lastUpdated` to be overwritten expects it kept. + +## A Domain Class Has an `autoTimestampEventListener` Property + +Every domain class has a static `autoTimestampEventListener` property: the listener that sets its timestamp and auditor properties, for the data source its saves go to. + +- A domain class that declares a property named `autoTimestampEventListener` of another type no longer compiles, as its `getAutoTimestampEventListener()` return type is incompatible with the one in `GormEntity`. One typed `AutoTimestampEventListener`, such as an injected bean, compiles but hides the static property, so `Book.autoTimestampEventListener` throws `MissingPropertyException`. Rename the property. +- Code that used an injected `autoTimestampEventListener` bean in a domain class reads `Book.autoTimestampEventListener` instead. + +## Every Data Source Sets the Auditor Properties and Follows the Auto-Timestamp Settings + +With several data sources, or a database or a schema per tenant, a save through a data source other than the default one: + +- sets `@CreatedBy` and `@LastModifiedBy` from the application's `AuditorAware`; Grails 8 set them only on the default data source. +- follows `grails.gorm.events.autoTimestampInsertOverwrite` and `grails.gorm.autoTimestampCacheAnnotations`; Grails 8 overwrote a given `dateCreated` or `lastUpdated` whatever the setting said. +- with GORM for Neo4j, runs the domain class's events and sets `dateCreated` and `lastUpdated`; Grails 8 ran neither on another Neo4j connection. + +Most applications need no change. Check the domain classes saved through another data source whose auditor properties, given timestamps or, with Neo4j, events were expected to be left alone. + ## Verification Checklist - [ ] `gradle.properties` names the Grails 9 version and the wrapper is Gradle 9.8 or later @@ -487,6 +512,9 @@ With `cacheUsers`, the Spring Security plugin's user cache stores users by value - [ ] No code reads a child of a string or closure configuration value expecting the parent - [ ] No code reads a whole list through a key ending in a whole number, such as `server.ports.0` - [ ] No typed read with a default, such as `getProperty('x.y.enabled', Boolean, true)`, names a key below a configuration value that is not a map +- [ ] No domain class declares a property named `autoTimestampEventListener` +- [ ] Unit tests that save through another data source or a tenant expect the domain class events to run and the timestamps to be set, or save inside `withoutTimestamps { }` +- [ ] Domain classes saved through a data source other than the default one are checked for auditor properties, given timestamps and, with Neo4j, events that were expected to be left alone - [ ] The full test suite passes on Groovy 6 ## Pitfalls to Avoid From eae0fea5f1f8196ab815e72a9b0ac82507b1d524 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Tue, 17 Nov 2026 12:49:02 -0800 Subject: [PATCH 058/115] Check the auditor and the insert-overwrite setting on Hibernate 5 connections Hibernate 7 and MongoDB check that a save through another connection sets the auditor properties and keeps a given timestamp when autoTimestampInsertOverwrite is off; Hibernate 5 had only the tenant spec. The Hibernate 7 spec carries over unchanged. --- ...mestampEventListenerConnectionsSpec.groovy | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 grails-data-hibernate5/core/src/test/groovy/org/grails/orm/hibernate/connections/AutoTimestampEventListenerConnectionsSpec.groovy diff --git a/grails-data-hibernate5/core/src/test/groovy/org/grails/orm/hibernate/connections/AutoTimestampEventListenerConnectionsSpec.groovy b/grails-data-hibernate5/core/src/test/groovy/org/grails/orm/hibernate/connections/AutoTimestampEventListenerConnectionsSpec.groovy new file mode 100644 index 00000000000..df43d5b119f --- /dev/null +++ b/grails-data-hibernate5/core/src/test/groovy/org/grails/orm/hibernate/connections/AutoTimestampEventListenerConnectionsSpec.groovy @@ -0,0 +1,130 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.orm.hibernate.connections + +import java.util.function.Supplier + +import org.hibernate.dialect.H2Dialect +import spock.lang.AutoCleanup +import spock.lang.Specification + +import org.springframework.context.support.GenericApplicationContext + +import grails.gorm.annotation.CreatedBy +import grails.gorm.annotation.Entity +import grails.gorm.annotation.LastModifiedBy +import org.grails.datastore.gorm.timestamp.AuditorAware +import org.grails.datastore.mapping.config.Settings +import org.grails.datastore.mapping.core.DatastoreUtils +import org.grails.orm.hibernate.HibernateDatastore + +/** + * The listener of every connection, not only that of the default one, sets the auditor properties and follows the + * auto-timestamp settings of the configuration. + */ +class AutoTimestampEventListenerConnectionsSpec extends Specification { + + private static final Date PAST = new Date(110_000_000_000L) + + @AutoCleanup + GenericApplicationContext context + + @AutoCleanup + HibernateDatastore datastore + + void 'every connection sets the auditor properties from the application context of the datastore'() { + given: + context = new GenericApplicationContext() + context.registerBean(AuditorAware, { { -> Optional.of('auditor') } as AuditorAware } as Supplier) + context.refresh() + datastore = new HibernateDatastore(DatastoreUtils.createPropertyResolver(configuration()), AuditedConnectionNote) + datastore.setApplicationContext(context) + AuditedConnectionNote onDefault = new AuditedConnectionNote(text: 'default') + AuditedConnectionNote onSecondary = new AuditedConnectionNote(text: 'secondary') + + when: + AuditedConnectionNote.withTransaction { + onDefault.save(flush: true) + } + AuditedConnectionNote.secondary.withTransaction { + onSecondary.secondary.save(flush: true) + } + + then: + onDefault.createdBy == 'auditor' + onDefault.lastModifiedBy == 'auditor' + onSecondary.createdBy == 'auditor' + onSecondary.lastModifiedBy == 'auditor' + } + + void 'every connection keeps a given timestamp when the configuration turns overwriting off'() { + given: + datastore = new HibernateDatastore(DatastoreUtils.createPropertyResolver(configuration() + [ + (Settings.SETTING_AUTO_TIMESTAMP_INSERT_OVERWRITE): false + ]), AuditedConnectionNote) + AuditedConnectionNote onDefault = new AuditedConnectionNote(text: 'default', dateCreated: PAST) + AuditedConnectionNote onSecondary = new AuditedConnectionNote(text: 'secondary', dateCreated: PAST) + + when: + AuditedConnectionNote.withTransaction { + onDefault.save(flush: true) + } + AuditedConnectionNote.secondary.withTransaction { + onSecondary.secondary.save(flush: true) + } + + then: + AuditedConnectionNote.withNewTransaction { AuditedConnectionNote.get(onDefault.id).dateCreated } == PAST + AuditedConnectionNote.secondary.withNewTransaction { AuditedConnectionNote.secondary.get(onSecondary.id).dateCreated } == PAST + onSecondary.lastUpdated != null + } + + private static Map configuration() { + [ + 'dataSource.url' : 'jdbc:h2:mem:timestampConnectionsDefaultDB;LOCK_TIMEOUT=10000', + 'dataSource.dbCreate' : 'create-drop', + 'dataSource.dialect' : H2Dialect.name, + 'hibernate.hbm2ddl.auto': 'create', + 'dataSources.secondary' : [url: 'jdbc:h2:mem:timestampConnectionsSecondaryDB;LOCK_TIMEOUT=10000'] + ] as Map + } +} + +@Entity +class AuditedConnectionNote { + + String text + Date dateCreated + Date lastUpdated + + @CreatedBy + String createdBy + + @LastModifiedBy + String lastModifiedBy + + static constraints = { + createdBy nullable: true + lastModifiedBy nullable: true + } + + static mapping = { + datasource 'ALL' + } +} From 676919cb2dc0f6f8aa798eb1326f91d205913a77 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Tue, 17 Nov 2026 14:54:33 -0800 Subject: [PATCH 059/115] Refresh the manifest when a build file is deleted Deleting a build file in the project or build root directory, such as gradle.properties, left no file newer than the manifest, so the old commands stayed. The Forge CLI now records those build files next to the manifest, in build/grails/commands.inputs, and a different set makes the manifest outdated, which also catches a file moved in with an older time. A manifest the build wrote without the Forge CLI has its record taken from the files there when it is first read. --- grails-doc/src/en/guide/commandLine.adoc | 2 + .../forge/cli/command/ProjectCommands.groovy | 39 +++++++++++++++- .../forge/cli/ProjectCommandsSpec.groovy | 44 +++++++++++++++++++ 3 files changed, 83 insertions(+), 2 deletions(-) diff --git a/grails-doc/src/en/guide/commandLine.adoc b/grails-doc/src/en/guide/commandLine.adoc index 1739538a857..9ae10b0e0df 100644 --- a/grails-doc/src/en/guide/commandLine.adoc +++ b/grails-doc/src/en/guide/commandLine.adoc @@ -238,6 +238,8 @@ commands: scripts applied from there; * the files under `grails-app/commands`, including a command deleted from there, or the whole directory removed. +A file of these deleted, or moved in with an older time, counts as a change too. + So a plugin added to the build brings its commands the next time a project command is asked for. A change the Forge CLI cannot see, such as a new snapshot of a plugin, is picked up by running `./gradlew generateCommandManifest`. diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy index b16aa6e497c..abeef7010ee 100644 --- a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy @@ -49,6 +49,12 @@ class ProjectCommands { /** Kept next to the manifest when the build could not write it, so the build is not run again for nothing */ static final String FAILED_PATH = 'build/grails/commands.failed' + /** + * Kept next to the manifest: the build files in the project and build root directories when the build + * was last run, so that one deleted since, which leaves nothing newer behind, is noticed + */ + static final String BUILD_FILES_PATH = 'build/grails/commands.inputs' + /** The newest version of the manifest's format this CLI reads */ static final int FORMAT_VERSION = 1 @@ -128,6 +134,7 @@ class ProjectCommands { catch (IOException e) { problem = e.message } + record(buildFiles()) if (problem == null && manifest.isFile()) { // the task is up to date when the classpath is, without writing the manifest again manifest.setLastModified(System.currentTimeMillis()) @@ -182,6 +189,15 @@ class ProjectCommands { if (lastRead == 0L) { return true } + List buildFiles = buildFiles() + File recorded = new File(projectDir, BUILD_FILES_PATH) + if (!recorded.isFile()) { + // a manifest the project's own build wrote: its build files are taken to be those there now + record(buildFiles) + } + else if (recorded.readLines('UTF-8') != buildFiles) { + return true + } ([projectDir, gradle.rootDir] as LinkedHashSet).any { File dir -> changedBuild(dir, lastRead) } || changedSources(lastRead) } @@ -195,10 +211,29 @@ class ProjectCommands { sources.isDirectory() ? changed(sources, since) : sources.parentFile.lastModified() > since } + /** + * The build files directly in the project and build root directories, which a deleted one leaves no + * trace of; those under the build directories change the time of the directory that held them. + */ + private List buildFiles() { + ([projectDir, gradle.rootDir] as LinkedHashSet).collectMany { File dir -> + (dir.listFiles() ?: new File[0]).findAll { File file -> buildFile(file) }*.absolutePath + }.sort() + } + + private void record(List buildFiles) { + File recorded = new File(projectDir, BUILD_FILES_PATH) + recorded.parentFile.mkdirs() + recorded.setText(buildFiles.collect { String path -> path + '\n' }.join(''), 'UTF-8') + } + + private static boolean buildFile(File file) { + file.isFile() && BUILD_FILE_SUFFIXES.any { String suffix -> file.name.endsWith(suffix) } + } + private static boolean changedBuild(File dir, long since) { dir.listFiles()?.any { File file -> - file.isFile() && BUILD_FILE_SUFFIXES.any { String suffix -> file.name.endsWith(suffix) } && - file.lastModified() > since + buildFile(file) && file.lastModified() > since } || BUILD_DIRS.any { String name -> changed(new File(dir, name), since) } } diff --git a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy index 4b08b01eec7..78c1a718c31 100644 --- a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy +++ b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy @@ -218,6 +218,50 @@ class ProjectCommandsSpec extends Specification { commandLine.subcommands.containsKey('url-mappings-report') } + void 'the manifest is written again when #deleted is deleted'() { + given: 'a manifest the build wrote, read once, which records the build files there' + File buildFile = new File(dir, deleted) + buildFile.text = 'a=b' + buildFile.setLastModified(System.currentTimeMillis() - 20_000) + writeManifest(URL_MAPPINGS_REPORT) + ForgeCli.createCommandLine(context, false) + assert gradleInvocations() == [] + assert new File(dir, 'build/grails/commands.inputs').readLines().contains(buildFile.absolutePath) + buildWrites(SECURITY_QUICKSTART) + + when: 'the file is deleted, which leaves no file newer than the manifest' + assert buildFile.delete() + CommandLine commandLine = ForgeCli.createCommandLine(context, false) + + then: + gradleInvocations() == [['-p', dir.path, '-q', '--console=plain'] + MANIFEST_TASK] + commandLine.subcommands.containsKey('s2-quickstart') + + and: 'the record now matches the files there, so the build is not run again' + !new File(dir, 'build/grails/commands.inputs').readLines().contains(buildFile.absolutePath) + ForgeCli.createCommandLine(context, false) + gradleInvocations().size() == 1 + + where: + deleted << ['gradle.properties', 'settings.gradle', 'dependencies.gradle', 'libs.versions.toml'] + } + + void 'the manifest is written again when a build file older than it is moved into the project'() { + given: + writeManifest(URL_MAPPINGS_REPORT) + ForgeCli.createCommandLine(context, false) + buildWrites(SECURITY_QUICKSTART) + + when: 'a file keeps the time it had elsewhere' + File moved = new File(dir, 'gradle.properties') + moved.text = 'a=b' + moved.setLastModified(manifest.lastModified() - 10_000) + ForgeCli.createCommandLine(context, false) + + then: + gradleInvocations() == [['-p', dir.path, '-q', '--console=plain'] + MANIFEST_TASK] + } + void 'what the build writes for itself does not make the manifest outdated'() { given: writeManifest(URL_MAPPINGS_REPORT) From 1935c7d4f4473acc46cbae7fc8320ddf3d952c2a Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Tue, 17 Nov 2026 18:32:53 -0800 Subject: [PATCH 060/115] Read an argument of quotes or spaces alone without failing CommandLineParser stripped the quotes of an argument without checking its length, so the argument " threw StringIndexOutOfBoundsException, and so did "", which became empty after the check for emptiness. The quotes are now stripped only from an argument of two characters or more, and what is left is trimmed and checked again. --- .../grails/build/parsing/CommandLineParser.java | 8 ++++---- .../build/parsing/CommandLineParserSpec.groovy | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/grails-gradle/model/src/main/groovy/org/grails/build/parsing/CommandLineParser.java b/grails-gradle/model/src/main/groovy/org/grails/build/parsing/CommandLineParser.java index 35f1dc1a514..2c7b466f4f8 100644 --- a/grails-gradle/model/src/main/groovy/org/grails/build/parsing/CommandLineParser.java +++ b/grails-gradle/model/src/main/groovy/org/grails/build/parsing/CommandLineParser.java @@ -192,10 +192,10 @@ private void parseInternal(DefaultCommandLine cl, String[] args, boolean firstAr for (String arg : args) { if (arg == null) continue; String trimmed = arg.trim(); - if (trimmed != null && trimmed.length() > 0) { - if (trimmed.charAt(0) == '"' && trimmed.charAt(trimmed.length() - 1) == '"') { - trimmed = trimmed.substring(1, trimmed.length() - 1); - } + if (trimmed.length() > 1 && trimmed.charAt(0) == '"' && trimmed.charAt(trimmed.length() - 1) == '"') { + trimmed = trimmed.substring(1, trimmed.length() - 1).trim(); + } + if (trimmed.length() > 0) { if (trimmed.charAt(0) == '-') { lastWasOption = processOption(cl, trimmed); } diff --git a/grails-gradle/model/src/test/groovy/org/grails/build/parsing/CommandLineParserSpec.groovy b/grails-gradle/model/src/test/groovy/org/grails/build/parsing/CommandLineParserSpec.groovy index b366a5c25b3..5ae70c1f59e 100644 --- a/grails-gradle/model/src/test/groovy/org/grails/build/parsing/CommandLineParserSpec.groovy +++ b/grails-gradle/model/src/test/groovy/org/grails/build/parsing/CommandLineParserSpec.groovy @@ -435,4 +435,21 @@ class CommandLineParserSpec extends Specification { cl.optionValue('filter') == 'a=b c' cl.remainingArgs == ['first'] } + + void "An argument of quotes or spaces alone is read without failing, for #arguments"() { + when: + def cl = new CommandLineParser().parse(arguments as String[]) + + then: + cl.commandName == 'report' + cl.remainingArgs == remaining + + where: + arguments || remaining + ['report', '"', 'first'] || ['"', 'first'] + ['report', '""', 'first'] || ['first'] + ['report', '" "', 'first'] || ['first'] + ['report', ' ', 'first'] || ['first'] + ['report', '"two words"', 'first'] || ['two words', 'first'] + } } From 9178703375068ebb0ae712326df9758129f1a767 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Tue, 17 Nov 2026 18:32:54 -0800 Subject: [PATCH 061/115] Run command tasks in one named project, and set -D among their arguments A task named without its project path runs in every project below the one Gradle starts in that has it, so the Grails Forge CLI's runCommand also ran the command in a plugin subproject, and a failure there failed the manifest. The grailsCommandProject property now names the one project the command tasks and generateCommandManifest run in. The command tasks set the system properties among their arguments, as -Dname=value, on the command's JVM, so -Dgrails.env=test chooses the environment from runCommand's arguments as well. The Forge CLI passes -D there instead of on the wrapper's command line, where cmd.exe would read it. generateCommandManifest now names, under inputs, the directories whose files decide the commands: those of every project for their build files, and the gradle and buildSrc directories, grails-app/commands and src/main/resources/META-INF of every project, and every included build. --- .../commandLine/creatingCustomCommands.adoc | 19 ++++- .../ApplicationCommandManifestTask.groovy | 32 +++++++- .../commands/GrailsCliGradlePlugin.groovy | 82 ++++++++++++++++++- .../ApplicationCommandManifestTaskSpec.groovy | 48 +++++++++-- .../commands/CommandArgumentsSpec.groovy | 45 ++++++++++ .../cli/ApplicationCommandManifestWriter.java | 4 +- 6 files changed, 219 insertions(+), 11 deletions(-) diff --git a/grails-doc/src/en/guide/commandLine/creatingCustomCommands.adoc b/grails-doc/src/en/guide/commandLine/creatingCustomCommands.adoc index 05e446783eb..b0f218a5fe5 100644 --- a/grails-doc/src/en/guide/commandLine/creatingCustomCommands.adoc +++ b/grails-doc/src/en/guide/commandLine/creatingCustomCommands.adoc @@ -214,7 +214,8 @@ Now you can run the `greet` command with arguments and options: `args` is split into words as a shell would split it. A tool that passes arguments it cannot quote for every shell can put them in a JSON file as an array of strings and name it with `argsFile`, a path -relative to the project directory. Each is passed on unchanged, after those of `args`: +relative to the project directory or an absolute one. Each reaches the command's command line as it is in +the file, after those of `args`: [source,shell] ---- @@ -222,6 +223,22 @@ echo '["greet", "Alice and Bob"]' > greet-args.json ./gradlew runCommand -PargsFile=greet-args.json ---- +However they are given, the command reads the arguments through Grails' command line parser: each is +trimmed, a pair of double quotes around one is removed, an empty one is left out, and an option's value +is everything after the first `=`, as in `--filter=status=active`. A word that starts with `-` is an +option, and one whose name holds a space, such as `--dev mode`, is refused as invalid. An argument `-Dname=value` sets a +system property: `runCommand` starts the command with it set, so `-Dgrails.env=test` runs the command in +the test environment, and the command also finds it in the system properties of its command line. + +In a build of several projects, `./gradlew runCommand` from the root runs the task in every project that +has it. Name one project's directory with the `grailsCommandProject` property to run it there only; the +other command tasks and `generateCommandManifest` take it too: + +[source,shell] +---- +./gradlew runCommand -Pargs="greet Alice" -PgrailsCommandProject=app +---- + This allows you to create more versatile and interactive custom commands for your Grails application. **Describing Arguments and Options** diff --git a/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTask.groovy b/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTask.groovy index d7ec4840407..15de279e792 100644 --- a/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTask.groovy +++ b/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTask.groovy @@ -18,9 +18,13 @@ */ package org.grails.gradle.plugin.commands +import groovy.json.JsonOutput +import groovy.json.JsonSlurper import groovy.transform.CompileStatic import org.gradle.api.file.RegularFileProperty +import org.gradle.api.provider.ListProperty +import org.gradle.api.tasks.Input import org.gradle.api.tasks.JavaExec import org.gradle.api.tasks.OutputFile import org.gradle.api.tasks.TaskAction @@ -36,6 +40,11 @@ import javax.inject.Inject *

Runs on the classpath {@code runCommand} uses, so the file lists exactly the commands * {@code runCommand} can run. The task is up to date while that classpath is unchanged.

* + *

The file also names, under {@code inputs}, the directories whose files decide the commands without + * being on the classpath yet: those whose build files decide the build, and those that hold the sources + * of commands or of the build's logic. A tool reading the file runs the task again when one of their + * files changes.

+ * * @since 9.0 */ @CompileStatic @@ -48,6 +57,8 @@ abstract class ApplicationCommandManifestTask extends JavaExec { @Inject ApplicationCommandManifestTask() { mainClass.set(MANIFEST_WRITER_CLASS) + buildDirectories.convention([]) + sourceDirectories.convention([]) } /** @@ -56,12 +67,31 @@ abstract class ApplicationCommandManifestTask extends JavaExec { @OutputFile abstract RegularFileProperty getManifestFile() + /** + * @return the directories whose build files, such as {@code build.gradle} and + * {@code gradle.properties}, decide the build: those of every project of the build + */ + @Input + abstract ListProperty getBuildDirectories() + + /** + * @return the directories whose files, at any depth, decide the commands or the build: the sources of + * the commands of every project, the build's {@code gradle} and {@code buildSrc} directories, and + * every included build + */ + @Input + abstract ListProperty getSourceDirectories() + @Override @TaskAction void exec() { // the file is the task's output, so it is given to the writer here rather than as an // argument input, which would make the absolute path part of the task's inputs - args(manifestFile.get().asFile.absolutePath) + File file = manifestFile.get().asFile + args(file.absolutePath) super.exec() + Map manifest = (Map) new JsonSlurper().parse(file, 'UTF-8') + manifest.inputs = [buildDirectories: buildDirectories.get(), sourceDirectories: sourceDirectories.get()] + file.setText(JsonOutput.prettyPrint(JsonOutput.toJson(manifest)), 'UTF-8') } } diff --git a/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/GrailsCliGradlePlugin.groovy b/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/GrailsCliGradlePlugin.groovy index da51a244ba8..392c1305b9d 100644 --- a/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/GrailsCliGradlePlugin.groovy +++ b/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/GrailsCliGradlePlugin.groovy @@ -135,6 +135,14 @@ class GrailsCliGradlePlugin implements Plugin { */ public static final String ARGS_FILE_PROPERTY = 'argsFile' + /** + * Set this project property to the directory of a project to run {@code runCommand}, + * {@link #COMMAND_MANIFEST_TASK} and the other command tasks only in that project. Without it, a task + * named without its project path runs in the project Gradle starts in and in every project below it + * that has the task. + */ + public static final String COMMAND_PROJECT_PROPERTY = 'grailsCommandProject' + @Override void apply(Project project) { // self-sufficient when applied standalone: the auto-provisioning behavior is configured @@ -447,6 +455,56 @@ class GrailsCliGradlePlugin implements Plugin { arguments } + /** + * Returns the arguments of a command task, from {@link #commandArguments}, and prepares the task for + * them: the system properties they name as {@code -Dname=value} are set on the command's JVM, after + * {@code grails.env} from Gradle's own, so {@code -Dgrails.env=test} among them chooses the + * environment; and the task runs only in the project {@link #COMMAND_PROJECT_PROPERTY} names, when + * it is set. + * + * @param task the command task + * @param project the project the task belongs to + * @return the arguments, to which the task adds the application class + */ + @CompileDynamic + static List commandTaskArguments(JavaExec task, Project project) { + List arguments = commandArguments(project) + task.systemProperties(commandSystemProperties(arguments)) + runOnlyInNamedProject(task, project) + new ArrayList(arguments) + } + + /** + * Returns the system properties among a command's arguments: each argument {@code -Dname=value}, + * which the command also sees in its command line. + * + * @param arguments the arguments of the command + * @return the properties by name, in the order given + */ + static Map commandSystemProperties(List arguments) { + Map properties = new LinkedHashMap() + for (String argument : arguments) { + int equals = argument.indexOf('=') + if (argument.startsWith('-D') && equals > 2) { + properties[argument.substring(2, equals)] = argument.substring(equals + 1) + } + } + properties + } + + /** + * Skips the task unless {@link #COMMAND_PROJECT_PROPERTY} is unset or names the directory of the + * task's project. + */ + @CompileDynamic + static void runOnlyInNamedProject(org.gradle.api.Task task, Project project) { + def named = project.findProperty(COMMAND_PROJECT_PROPERTY) + if (named) { + boolean selected = new File(named as String).canonicalFile == project.projectDir.canonicalFile + task.onlyIf("the ${COMMAND_PROJECT_PROPERTY} property names this project".toString()) { selected } + } + } + @PackageScope static String requireMainClass(Provider mainClass, String taskName) { def mainClassName = mainClass.orNull @@ -488,7 +546,7 @@ class GrailsCliGradlePlugin implements Plugin { it.classpath = fileCollection it.command = commandName it.systemProperty(Environment.KEY, System.getProperty(Environment.KEY, Environment.DEVELOPMENT.getName())) - List args = new ArrayList(GrailsCliGradlePlugin.commandArguments(project)) + List args = GrailsCliGradlePlugin.commandTaskArguments(it, project) def appClassProvider = GrailsGradlePlugin.getMainClassProvider(project) @@ -620,7 +678,7 @@ class GrailsCliGradlePlugin implements Plugin { // with Grails' plugin management and bean wiring when running CLI scripts via Gradle it.systemProperty('spring.devtools.restart.enabled', 'false') - List args = new ArrayList(GrailsCliGradlePlugin.commandArguments(project)) + List args = GrailsCliGradlePlugin.commandTaskArguments(it, project) def appClassProvider = GrailsGradlePlugin.getMainClassProvider(project) @@ -649,7 +707,7 @@ class GrailsCliGradlePlugin implements Plugin { // with Grails' plugin management and bean wiring when running CLI commands via Gradle it.systemProperty('spring.devtools.restart.enabled', 'false') - List args = new ArrayList(GrailsCliGradlePlugin.commandArguments(project)) + List args = GrailsCliGradlePlugin.commandTaskArguments(it, project) def appClassProvider = GrailsGradlePlugin.getMainClassProvider(project) @@ -675,6 +733,7 @@ class GrailsCliGradlePlugin implements Plugin { Directory projectDirectory = project.layout.projectDirectory manifestTask.configure { it.description = 'Writes the Grails commands of the application, which the Grails Forge CLI offers' + GrailsCliGradlePlugin.runOnlyInNamedProject(it, project) it.manifestFile.convention(project.providers.gradleProperty(COMMAND_MANIFEST_PROPERTY) .map { String path -> projectDirectory.file(path) } .orElse(project.layout.buildDirectory.file(COMMAND_MANIFEST_PATH))) @@ -685,6 +744,23 @@ class GrailsCliGradlePlugin implements Plugin { it.classpath = mainSourceSet.runtimeClasspath + project.configurations.getByName(GRAILS_CLI_CLASSPATH_CONFIGURATION) } } + // every project of the build is known only once all are evaluated + project.gradle.projectsEvaluated { gradle -> + Project root = gradle.rootProject + List projectDirs = root.allprojects*.projectDir + List includedBuildDirs = gradle.includedBuilds*.projectDir + List buildDirectories = ([root.projectDir] + projectDirs) + .collect { File dir -> dir.absolutePath }.unique().sort() + List sourceDirectories = ([new File(root.projectDir, 'gradle'), new File(root.projectDir, 'buildSrc')] + + projectDirs.collectMany { File dir -> + [new File(dir, 'grails-app/commands'), new File(dir, 'src/main/resources/META-INF')] + } + includedBuildDirs) + .collect { File dir -> dir.absolutePath }.unique().sort() + manifestTask.configure { + it.buildDirectories.set(buildDirectories) + it.sourceDirectories.set(sourceDirectories) + } + } } } diff --git a/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTaskSpec.groovy b/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTaskSpec.groovy index ff3d1cc3a90..e1d4a4d141f 100644 --- a/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTaskSpec.groovy +++ b/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTaskSpec.groovy @@ -20,6 +20,7 @@ package org.grails.gradle.plugin.commands import java.nio.file.Path +import groovy.json.JsonSlurper import org.gradle.testkit.runner.TaskOutcome import org.grails.gradle.plugin.core.GradleSpecification @@ -80,10 +81,47 @@ class ApplicationCommandManifestTaskSpec extends GradleSpecification { !runner.projectDir.toPath().resolve('app/build/grails/commands.json').toFile().exists() } - private static Map read(Path file) { - file.toFile().readLines().collectEntries { String line -> - int separator = line.indexOf('=') - [(line.substring(0, separator)): line.substring(separator + 1)] - } + void 'the manifest names the directories whose files decide the commands and the build'() { + given: + def runner = setupTestResourceProject('command-manifest') + File root = runner.projectDir.canonicalFile + + when: + executeTask(':app:generateCommandManifest') + Map> inputs = (Map>) read(root.toPath().resolve('app/build/grails/commands.json')).inputs + + then: 'the directories of every project, for their build files' + inputs.buildDirectories.collect { new File(it).canonicalFile } as Set == + [root, new File(root, 'app'), new File(root, 'manifest-writer')] as Set + + and: 'the sources of commands of every project, and the logic of the build' + inputs.sourceDirectories.collect { new File(it).canonicalFile }.containsAll([ + new File(root, 'gradle'), new File(root, 'buildSrc'), + new File(root, 'app/grails-app/commands'), new File(root, 'app/src/main/resources/META-INF'), + new File(root, 'manifest-writer/grails-app/commands')]) + } + + void 'the task runs only in the project the grailsCommandProject property names'() { + given: + def runner = setupTestResourceProject('command-manifest') + File app = new File(runner.projectDir, 'app') + + when: 'another project is named' + def other = executeTask(':app:generateCommandManifest', ["-PgrailsCommandProject=${new File(runner.projectDir, 'manifest-writer').absolutePath}".toString()]) + + then: + other.task(':app:generateCommandManifest').outcome == TaskOutcome.SKIPPED + !new File(app, 'build/grails/commands.json').exists() + + when: 'the application is named' + def named = executeTask(':app:generateCommandManifest', ["-PgrailsCommandProject=${app.absolutePath}".toString()]) + + then: + named.task(':app:generateCommandManifest').outcome == TaskOutcome.SUCCESS + new File(app, 'build/grails/commands.json').exists() + } + + private static Map read(Path file) { + (Map) new JsonSlurper().parse(file.toFile(), 'UTF-8') } } diff --git a/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/CommandArgumentsSpec.groovy b/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/CommandArgumentsSpec.groovy index 544a1272422..04a21b0d821 100644 --- a/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/CommandArgumentsSpec.groovy +++ b/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/CommandArgumentsSpec.groovy @@ -22,6 +22,7 @@ import groovy.json.JsonOutput import org.gradle.api.GradleException import org.gradle.api.Project +import org.gradle.api.tasks.JavaExec import org.gradle.testfixtures.ProjectBuilder import spock.lang.Specification import spock.lang.TempDir @@ -73,6 +74,50 @@ class CommandArgumentsSpec extends Specification { 'is an object' | '{"a": "b"}' || 'must hold a JSON array of strings' } + void 'an args file given by its absolute path, as the Grails Forge CLI gives it, is read'() { + given: + File elsewhere = new File(projectDir, 'elsewhere/arguments.json') + elsewhere.parentFile.mkdirs() + elsewhere.text = JsonOutput.toJson(['greet', 'Alice']) + + expect: + GrailsCliGradlePlugin.commandArguments(project(argsFile: elsewhere.absolutePath)) == ['greet', 'Alice'] + } + + void 'the system properties among the arguments are set on the command JVM, and stay among the arguments'() { + given: + new File(projectDir, 'arguments.json').text = JsonOutput.toJson( + ['dbm-update', '-Dgrails.env=test', '-Dquery=a=b', '-D', '-Dnovalue', 'first']) + Project project = project(argsFile: 'arguments.json') + JavaExec task = project.tasks.register('commandTask', JavaExec).get() + task.systemProperty('grails.env', 'development') + + when: + List arguments = GrailsCliGradlePlugin.commandTaskArguments(task, project) + + then: + arguments == ['dbm-update', '-Dgrails.env=test', '-Dquery=a=b', '-D', '-Dnovalue', 'first'] + task.systemProperties == ['grails.env': 'test', query: 'a=b'] + task.onlyIf.isSatisfiedBy(task) + } + + void 'a command task runs only in the project the grailsCommandProject property names: #named'() { + given: + Project project = project(grailsCommandProject: new File(projectDir, named).absolutePath) + JavaExec task = project.tasks.register('commandTask', JavaExec).get() + + when: + GrailsCliGradlePlugin.commandTaskArguments(task, project) + + then: + task.onlyIf.isSatisfiedBy(task) == runs + + where: + named || runs + '' || true + 'other' || false + } + private Project project(Map properties) { Project project = ProjectBuilder.builder().withProjectDir(projectDir).build() properties.each { String name, String value -> project.extensions.extraProperties.set(name, value) } diff --git a/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/manifest-writer/src/main/java/org/apache/grails/core/cli/ApplicationCommandManifestWriter.java b/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/manifest-writer/src/main/java/org/apache/grails/core/cli/ApplicationCommandManifestWriter.java index 6808741cb7b..3f0a676e3b9 100644 --- a/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/manifest-writer/src/main/java/org/apache/grails/core/cli/ApplicationCommandManifestWriter.java +++ b/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/manifest-writer/src/main/java/org/apache/grails/core/cli/ApplicationCommandManifestWriter.java @@ -40,6 +40,8 @@ public static void main(String[] args) throws IOException { } Path file = Path.of(args[0]); Files.createDirectories(file.getParent()); - Files.writeString(file, "arguments=" + args.length + "\nfile=" + file + "\nruntimeMarker=" + runtimeMarker + "\n"); + // JSON, as the real writer writes, with what the spec checks in place of the commands + Files.writeString(file, "{\"version\": 1, \"commands\": [], \"arguments\": \"" + args.length + "\", \"file\": \"" + + file.toString().replace("\\", "\\\\") + "\", \"runtimeMarker\": \"" + runtimeMarker + "\"}\n"); } } From 87e28e84b9d3face1273ae20767ece7973fea7d6 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Tue, 17 Nov 2026 18:32:54 -0800 Subject: [PATCH 062/115] Record the files the project's commands depend on, instead of comparing times The Forge CLI decided whether the manifest was outdated by comparing file times with the manifest's, which missed a deleted file, a file replaced by an older copy, the other projects and included builds of the build, a file saved while the build ran, and a file dated in the future, which ran the build on every start. The Forge CLI now records the files of the directories the manifest names, with their times and sizes, before it runs the build, and runs it again when the record differs, or when there is none. Links are recorded, not followed. Outputs next to a build script are left out, but a source package named build is not. The record and the mark of a failed build live in the project's .gradle/grails, so cleaning the build does not rerun a build that failed, and a file that cannot be written there is reported rather than stopping the Forge CLI. Also: - A command the manifest describes that picocli cannot take, such as one with an option named help, costs only that command. - The Gradle runs name the project with grailsCommandProject. - -D reaches runCommand among the arguments. - The build's root is found from the settings file, as Gradle finds it. - A code generation command never runs the build, even where it does not apply, and no command follows --. - The prompt completes the commands a build found while it ran. --- grails-doc/src/en/guide/commandLine.adoc | 53 ++- .../forge/cli/CommandLineCompleter.groovy | 16 +- .../org/grails/forge/cli/ForgeCli.groovy | 31 +- .../grails/forge/cli/InteractiveShell.groovy | 23 +- .../forge/cli/command/ProjectCommand.groovy | 24 +- .../forge/cli/command/ProjectCommands.groovy | 262 +++++++---- .../forge/cli/command/ProjectGradle.groovy | 26 +- .../forge/cli/CommandLineCompleterSpec.groovy | 18 +- .../forge/cli/ProjectCommandsSpec.groovy | 414 +++++++++++++----- 9 files changed, 624 insertions(+), 243 deletions(-) diff --git a/grails-doc/src/en/guide/commandLine.adoc b/grails-doc/src/en/guide/commandLine.adoc index 9ae10b0e0df..60bdeedf1cc 100644 --- a/grails-doc/src/en/guide/commandLine.adoc +++ b/grails-doc/src/en/guide/commandLine.adoc @@ -214,13 +214,15 @@ grails> s2-quickstart com.example User Role -- Each command shows its usage with `--help`, and runs through the project's `runCommand` Gradle task, as `./gradlew runCommand -Pargs="s2-quickstart com.example User Role"` would run it. The Forge CLI hands -the arguments to the task in a file, so an argument with spaces or quotes reaches the command as typed, -on every operating system. The Forge CLI's own commands keep their name: a project command named like -one of them is not offered. +the arguments to the task in a file, so an argument with spaces or quotes reaches `runCommand` as typed, +on every operating system, and the command reads it as `runCommand` always has: trimmed, with a pair of +double quotes around it removed, and left out when it is empty. In a build of several projects, the task +runs in the project of the current directory only, not also in the projects below it. The Forge CLI's +own commands keep their name: a project command named like one of them is not offered. A command runs in the development environment unless `-D` says otherwise. `-D` sets a system property -of the project's build, which `runCommand` passes on to the command, as `./gradlew -Dgrails.env=test -runCommand` does: +of the command: it reaches `runCommand` among the arguments, as `-Dname=value`, and `runCommand` starts +the command with the property set, as `./gradlew runCommand -Pargs="dbm-update -Dgrails.env=test"` does: [source,shell] ---- @@ -228,31 +230,36 @@ grails> dbm-update -Dgrails.env=test ---- The Forge CLI learns the commands from the project's build: the Grails Gradle plugin's -`generateCommandManifest` task writes them to `build/grails/commands.json`. The Forge CLI runs the task -when the file is missing, or older than one of the files that decide the build or the application's -commands: - -* the `.gradle`, `.gradle.kts`, `.properties` and `.toml` files of the project directory and of the - root directory of its build, such as `build.gradle`, `settings.gradle` and `gradle.properties`; -* the files under their `gradle` and `buildSrc` directories, such as `gradle/libs.versions.toml` and - scripts applied from there; -* the files under `grails-app/commands`, including a command deleted from there, or the whole directory removed. - -A file of these deleted, or moved in with an older time, counts as a change too. - -So a plugin added to the build brings its commands the next time a project command is asked for. A -change the Forge CLI cannot see, such as a new snapshot of a plugin, is picked up by running -`./gradlew generateCommandManifest`. +`generateCommandManifest` task writes them to `build/grails/commands.json`, with the directories whose +files decide them: + +* the directory of every project of the build, for its `.gradle`, `.gradle.kts`, `.properties` and + `.toml` files, such as `build.gradle`, `settings.gradle`, `gradle.properties` and `libs.versions.toml`; +* everything under the `gradle` and `buildSrc` directories of the build's root, under + `grails-app/commands` and `src/main/resources/META-INF` of every project, and under every included + build, apart from what builds and tools write next to a build script, such as `build`, `.gradle` and + `.kotlin`. + +Each time it runs the task, the Forge CLI records those files, with their times and sizes, in the +project's `.gradle/grails` directory. It runs the task again when one of them is added, changed, replaced +or deleted, whatever time the file has, and when a manifest has no record, as one written by +`./gradlew generateCommandManifest` alone. So a plugin added to the build brings its commands the next +time a project command is asked for. A change the Forge CLI cannot see, such as a new snapshot of a +plugin, is picked up by running `./gradlew generateCommandManifest`. The build runs only when the interactive prompt starts or a command the Forge CLI does not have is named, also after the Forge CLI's own options, as in `grails-forge-cli -x dbm-update`. Its own commands, -such as `create-controller`, and its options alone, such as `--help`, never run it. -`grails-forge-cli --help` lists the project's commands from the file the build last wrote. +such as `create-controller`, never run it, even in a project they do not apply to, and neither do its +options alone, such as `--help`, or a command line after `--`. `grails-forge-cli --help` lists the +project's commands from the file the build last wrote. When a command at the prompt runs the build, the +prompt then completes the commands it found. When the build fails, as it does while the application does not compile or in a project of a Grails release without `generateCommandManifest`, the Forge CLI says so once and keeps offering the commands of the file the build last wrote, if any. It does not run the build for them again until one of the files -above changes; run `./gradlew generateCommandManifest` to see what went wrong. +above changes, also after `./gradlew clean`; run `./gradlew generateCommandManifest` to see what went +wrong. A command of the file that the Forge CLI cannot offer, such as one declaring an option named +`help`, is reported and left out, and the others are offered. The build runs through the Gradle wrapper of the project, or of the build it belongs to: the wrapper in the first directory above it with a `settings.gradle` file. A project whose build has no wrapper has no diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/CommandLineCompleter.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/CommandLineCompleter.groovy index 88559b5de89..e7564e8a9eb 100644 --- a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/CommandLineCompleter.groovy +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/CommandLineCompleter.groovy @@ -18,6 +18,8 @@ */ package org.grails.forge.cli +import java.util.function.Supplier + import groovy.transform.CompileStatic import org.jline.reader.Candidate @@ -30,14 +32,22 @@ import picocli.CommandLine.Model.CommandSpec /** * Completes the word under the cursor at the interactive prompt from the picocli commands, their * subcommands and their options. + * + *

The commands can be given as a supplier, so the prompt completes the commands offered now, such as + * those of a project whose build changed since the prompt started.

*/ @CompileStatic class CommandLineCompleter implements Completer { - private final CommandSpec spec + private final Supplier specs CommandLineCompleter(CommandSpec spec) { - this.spec = Objects.requireNonNull(spec, 'spec') + Objects.requireNonNull(spec, 'spec') + this.specs = { -> spec } as Supplier + } + + CommandLineCompleter(Supplier specs) { + this.specs = Objects.requireNonNull(specs, 'specs') } @Override @@ -56,7 +66,7 @@ class CommandLineCompleter implements Completer { wordIndex++ } List suggestions = [] - AutoComplete.complete(spec, words.toArray(new String[0]), wordIndex, 0, line.cursor(), suggestions) + AutoComplete.complete(specs.get(), words.toArray(new String[0]), wordIndex, 0, line.cursor(), suggestions) suggestions.each { CharSequence suggestion -> candidates.add(new Candidate(prefix + suggestion)) } diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/ForgeCli.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/ForgeCli.groovy index 6e215431a69..aa43f5fffeb 100644 --- a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/ForgeCli.groovy +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/ForgeCli.groovy @@ -19,6 +19,7 @@ package org.grails.forge.cli import java.util.function.BiFunction +import java.util.function.Supplier import groovy.transform.CompileStatic import org.springframework.context.ApplicationContext import org.springframework.context.ConfigurableApplicationContext @@ -60,6 +61,14 @@ final class ForgeCli { AddPropertyCommand ] as List> + /** + * The names of the code-gen commands, which are the Forge CLI's own even where they do not apply, so + * naming one never runs the project's build + */ + private static final Set CODE_GEN_COMMAND_NAMES = CODE_GEN_COMMANDS + .collect { Class type -> type.getAnnotation(CommandLine.Command).name() } + .toSet().asImmutable() + private static final BiFunction EXCEPTION_HANDLER = { Throwable e, CommandLine commandLine -> BaseCommand command = commandLine.getCommand() command.err(e.message) @@ -108,7 +117,7 @@ final class ForgeCli { addProjectCommands(commandLine, false) } } - else if (!commandLine.subcommands.containsKey(command)) { + else if (!commandLine.subcommands.containsKey(command) && !CODE_GEN_COMMAND_NAMES.contains(command)) { addProjectCommands(commandLine, true) } return commandLine.execute(args) @@ -116,13 +125,13 @@ final class ForgeCli { /** * The command a command line names: its first word that is neither one of the Forge CLI's options - * nor the value of one. + * nor the value of one. None follows {@code --}, after which picocli takes every word as a parameter. */ private static String commandName(CommandLine commandLine, String... args) { for (int i = 0; i < args.length; i++) { String word = args[i] if (word == '--') { - return i + 1 < args.length ? args[i + 1] : null + return null } if (!word.startsWith('-')) { return word @@ -140,7 +149,16 @@ final class ForgeCli { */ static void interactive(ApplicationContext context) { CommandLine commandLine = createCommandLine(context, true) - new InteractiveShell(commandLine, { String[] commandArgs -> execute(context, commandArgs) }, EXCEPTION_HANDLER).start() + // a line may run the build and change the project's commands, so the prompt then completes those + // of the manifest as it is, read quietly, since a problem with it was reported already + CommandLine[] offered = [commandLine] as CommandLine[] + Supplier current = { -> offered[0] } as Supplier + new InteractiveShell(commandLine, current, { String[] commandArgs -> + execute(context, commandArgs) + CommandLine refreshed = createForgeCommandLine(context, true) + addProjectCommands(refreshed, false, ConsoleOutput.NOOP) + offered[0] = refreshed + }, EXCEPTION_HANDLER, InteractiveShell.DEFAULT_PROMPT).start() } /** @@ -185,6 +203,10 @@ final class ForgeCli { * @param runBuild whether the project's build may run to update the commands */ private static void addProjectCommands(CommandLine commandLine, boolean runBuild) { + addProjectCommands(commandLine, runBuild, (ForgeCommand) commandLine.getCommand()) + } + + private static void addProjectCommands(CommandLine commandLine, boolean runBuild, ConsoleOutput console) { File projectDir try { projectDir = FileSystemOutputHandler.getDefaultBaseDirectory() @@ -192,7 +214,6 @@ final class ForgeCli { catch (IOException ignored) { return } - ConsoleOutput console = (ForgeCommand) commandLine.getCommand() for (ProjectCommand command : new ProjectCommands(projectDir).load(console, runBuild)) { if (!commandLine.subcommands.containsKey(command.name)) { commandLine.addSubcommand(command.name, command.commandSpec) diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/InteractiveShell.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/InteractiveShell.groovy index 8d86582208c..0b6a0972dc9 100644 --- a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/InteractiveShell.groovy +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/InteractiveShell.groovy @@ -29,17 +29,20 @@ import org.jline.reader.impl.DefaultParser import org.jline.terminal.Terminal import org.jline.terminal.TerminalBuilder import picocli.CommandLine +import picocli.CommandLine.Model.CommandSpec import java.util.function.BiFunction import java.util.function.Consumer +import java.util.function.Supplier import static picocli.CommandLine.Help.Ansi.AUTO @CompileStatic class InteractiveShell { - private static final String DEFAULT_PROMPT = '@|blue grails>|@ ' + static final String DEFAULT_PROMPT = '@|blue grails>|@ ' private final CommandLine commandLine + private final Supplier offered private final Consumer executor private final BiFunction onError private final String prompt @@ -54,7 +57,23 @@ class InteractiveShell { Consumer executor, BiFunction onError, String prompt) { + this(commandLine, { -> commandLine } as Supplier, executor, onError, prompt) + } + + /** + * @param commandLine the command line whose help and colors the prompt starts with + * @param offered the command line of the commands offered now, which the prompt completes + * @param executor runs each line entered + * @param onError reports what ends the prompt + * @param prompt the prompt, with picocli's markup + */ + InteractiveShell(CommandLine commandLine, + Supplier offered, + Consumer executor, + BiFunction onError, + String prompt) { this.commandLine = commandLine + this.offered = offered this.executor = executor this.onError = onError this.prompt = prompt @@ -65,7 +84,7 @@ class InteractiveShell { Terminal terminal = TerminalBuilder.terminal() LineReader reader = LineReaderBuilder.builder() .terminal(terminal) - .completer(new CommandLineCompleter(commandLine.commandSpec)) + .completer(new CommandLineCompleter({ -> offered.get().commandSpec } as Supplier)) .parser(new DefaultParser()) .variable(LineReader.LIST_MAX, 50) // max tab completion candidates .build() diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommand.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommand.groovy index 28d33b414df..a0a54c799e0 100644 --- a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommand.groovy +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommand.groovy @@ -39,8 +39,9 @@ import picocli.CommandLine.Model.PositionalParamSpec * does through {@code runCommand}.

* *

The arguments reach {@code runCommand} in a file, so they need no quoting for the shell or the - * wrapper script. {@code -D} sets a system property of the build, such as {@code -Dgrails.env=test}, - * which {@code runCommand} passes on to the command.

+ * wrapper script. {@code -D} sets a system property of the command, such as {@code -Dgrails.env=test}: + * it is passed among the arguments, as {@code -Dname=value}, and {@code runCommand} starts the + * command with it set.

*/ @CompileStatic class ProjectCommand extends BaseCommand implements Callable { @@ -74,7 +75,7 @@ class ProjectCommand extends BaseCommand implements Callable { .paramLabel('=') .type(Map) .auxiliaryTypes(String, String) - .description('Sets a system property of the build, such as -Dgrails.env=test to run the command in the test environment.') + .description('Sets a system property of the command, such as -Dgrails.env=test to run it in the test environment.') .build() this.commandSpec = createSpec(command) this.spec = commandSpec @@ -94,9 +95,7 @@ class ProjectCommand extends BaseCommand implements Callable { File argumentsFile = File.createTempFile('grails-command-arguments', '.json') try { argumentsFile.setText(JsonOutput.toJson(commandArguments), 'UTF-8') - List gradleArguments = givenSystemProperties() - gradleArguments << RUN_COMMAND_TASK << "-P${ARGS_FILE_PROPERTY}=${argumentsFile.absolutePath}".toString() - return gradle.run(gradleArguments) + return gradle.run([RUN_COMMAND_TASK, "-P${ARGS_FILE_PROPERTY}=${argumentsFile.absolutePath}".toString()]) } finally { argumentsFile.delete() @@ -128,23 +127,24 @@ class ProjectCommand extends BaseCommand implements Callable { : "${option.longestName()}=${option.stringValues().first()}".toString()) } given.addAll(parsed.unmatched()) + given.addAll(givenSystemProperties()) given } /** - * The words typed after the command's name, without those of {@code -D}: picocli cannot tell which - * word of an undeclared option is its value, so the command gets them in their order, as it does from - * {@code runCommand}. + * The words typed after the command's name: picocli cannot tell which word of an undeclared option + * is its value, so the command gets them in their order, as it does from {@code runCommand}. Only + * {@code -D name=value}, given as two words, is joined into the one word {@code runCommand} reads. */ private List typedArguments(List commandLine) { List typed = [] List afterName = commandLine.subList(commandLine.indexOf(name) + 1, commandLine.size()) for (int i = 0; i < afterName.size(); i++) { String word = afterName[i] - if (word == SYSTEM_PROPERTY_OPTION) { - i++ + if (word == SYSTEM_PROPERTY_OPTION && i + 1 < afterName.size()) { + typed << SYSTEM_PROPERTY_OPTION + afterName[++i] } - else if (!word.startsWith(SYSTEM_PROPERTY_OPTION)) { + else { typed << word } } diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy index abeef7010ee..a4ef0e6da60 100644 --- a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy @@ -18,9 +18,13 @@ */ package org.grails.forge.cli.command +import java.nio.file.Files +import java.nio.file.Path + import groovy.json.JsonException import groovy.json.JsonSlurper import groovy.transform.CompileStatic +import groovy.transform.EqualsAndHashCode import org.grails.forge.io.ConsoleOutput @@ -29,9 +33,10 @@ import org.grails.forge.io.ConsoleOutput * plugins, which {@code runCommand} runs. * *

The project's build describes them in a manifest, written by the Grails Gradle plugin's - * {@code generateCommandManifest} task. The manifest is read as long as it is newer than the files - * that decide the project's build and its own commands; otherwise the task is run first, so a plugin - * added to the build brings its commands with it. When the build fails, the commands of the last + * {@code generateCommandManifest} task, which also names the directories whose files decide the + * commands. Each time the build runs, the files of those directories are recorded, with their times and + * sizes; the manifest is read as long as they are unchanged, and otherwise the task is run first, so a + * plugin added to the build brings its commands with it. When the build fails, the commands of the last * manifest are kept, and the build is not run for them again until one of those files changes.

*/ @CompileStatic @@ -46,28 +51,24 @@ class ProjectCommands { /** Where the manifest is kept, relative to the project directory */ static final String MANIFEST_PATH = 'build/grails/commands.json' - /** Kept next to the manifest when the build could not write it, so the build is not run again for nothing */ - static final String FAILED_PATH = 'build/grails/commands.failed' - /** - * Kept next to the manifest: the build files in the project and build root directories when the build - * was last run, so that one deleted since, which leaves nothing newer behind, is noticed + * Where the files the commands depend on are recorded when the build runs, relative to the project + * directory: outside the build directory, so that cleaning the build does not forget a failed build */ - static final String BUILD_FILES_PATH = 'build/grails/commands.inputs' + static final String RECORD_PATH = '.gradle/grails/commands.record' + + /** Kept next to the record when the build could not write the manifest, so it is not run again for nothing */ + static final String FAILED_PATH = '.gradle/grails/commands.failed' /** The newest version of the manifest's format this CLI reads */ static final int FORMAT_VERSION = 1 private static final List BUILD_FILE_SUFFIXES = ['.gradle', '.gradle.kts', '.properties', '.toml'] - /** Directories of the build whose files change the build: scripts applied from them, and build logic */ - private static final List BUILD_DIRS = ['gradle', 'buildSrc'] - - /** Directories under the build directories that the build writes, rather than reads */ - private static final List OUTPUT_DIRS = ['build', '.gradle'] + private static final List BUILD_SCRIPTS = ['build.gradle', 'build.gradle.kts', 'settings.gradle', 'settings.gradle.kts'] - /** Where an application keeps its own commands */ - private static final String COMMAND_SOURCES = 'grails-app/commands' + /** What builds and tools write next to a build script, rather than read */ + private static final List OUTPUT_DIRS = ['build', '.gradle', '.kotlin', 'out', 'bin', '.git', '.idea', 'node_modules'] private final File projectDir private final ProjectGradle gradle @@ -90,8 +91,8 @@ class ProjectCommands { } /** - * Reads the commands of the project, after updating the manifest when it is missing or older than - * the project's build or its commands. + * Reads the commands of the project, after updating the manifest when it is missing or one of the + * files it depends on changed. * * @param console where to say that the build is being run, and why the commands could not be read * @return the commands, or none when the directory holds no Grails project or none could be read @@ -124,7 +125,9 @@ class ProjectCommands { } private void update(File manifest, ConsoleOutput console) { - File failed = new File(projectDir, FAILED_PATH) + // the files are recorded before the build runs, so one changed while it runs counts next time + Inputs before = inputsOf(manifest) + List recorded = snapshot(before) console.out('Reading the commands of the project from its build...') String problem try { @@ -134,22 +137,46 @@ class ProjectCommands { catch (IOException e) { problem = e.message } - record(buildFiles()) - if (problem == null && manifest.isFile()) { - // the task is up to date when the classpath is, without writing the manifest again - manifest.setLastModified(System.currentTimeMillis()) - failed.delete() + if (problem == null && !manifest.isFile()) { + problem = 'its build wrote no manifest' + } + if (problem == null) { + Inputs after = inputsOf(manifest) + save(after == before ? recorded : snapshot(after), null, console) return } - failed.parentFile.mkdirs() - failed.setText((problem ?: 'its build wrote no manifest') + '\n', 'UTF-8') + save(recorded, problem, console) String kept = manifest.isFile() ? 'The commands it listed before are offered' : 'Its commands are not offered' - console.warning("Could not read the commands of the project: ${problem ?: 'its build wrote no manifest'}. ${kept}, " + - 'and the build is not run for them again until a build file changes. A project of Grails 9 or ' + - "later lists them with its ${MANIFEST_TASK} Gradle task; run it to see what went wrong.") + console.warning("Could not read the commands of the project: ${problem}. ${kept}, and the build is not run " + + 'for them again until a build file changes. A project of Grails 9 or later lists them with its ' + + "${MANIFEST_TASK} Gradle task; run it to see what went wrong.") + } + + /** + * Records the files the commands depend on, and whether the build failed. When they cannot be + * written, the build is run again next time, which is said. + */ + private void save(List recorded, String problem, ConsoleOutput console) { + File record = new File(projectDir, RECORD_PATH) + File failed = new File(projectDir, FAILED_PATH) + try { + Files.createDirectories(record.parentFile.toPath()) + Files.write(record.toPath(), recorded.collect { String line -> line + '\n' }.join('').getBytes('UTF-8')) + if (problem == null) { + Files.deleteIfExists(failed.toPath()) + } + else { + Files.write(failed.toPath(), (problem + '\n').getBytes('UTF-8')) + } + } + catch (IOException e) { + console.warning("Could not record what the commands of the project depend on in ${record.parentFile}: " + + "${e.message ?: e.class.simpleName}. The build is run for them again next time.") + } } private List read(File manifest, ConsoleOutput console) { + List commands try { Object parsed = new JsonSlurper().parse(manifest, 'UTF-8') if (!(parsed instanceof Map)) { @@ -164,92 +191,167 @@ class ProjectCommands { throw new IllegalArgumentException("it was written in format ${version}, by a newer Grails, and this " + "Grails Forge CLI reads up to format ${FORMAT_VERSION}") } - Object commands = content.commands - if (!(commands instanceof List)) { + if (!(content.commands instanceof List)) { throw new IllegalArgumentException('it has no list of commands') } - return ((List) commands).collect { Object command -> create(command) } + commands = (List) content.commands } - catch (JsonException | RuntimeException e) { + catch (JsonException | IOException | RuntimeException e) { console.warning("Could not read the commands of the project from ${manifest}, so they are not offered: ${e.message}") return [] } + // a command picocli cannot take, such as one declaring an option twice, costs only that command + List created = [] + for (Object command : commands) { + try { + created << create(command) + } + catch (RuntimeException e) { + console.warning("The command ${nameOf(command)} of the project is not offered: ${e.message}") + } + } + created } private ProjectCommand create(Object command) { if (!(command instanceof Map) || !(((Map) command).name instanceof String) || !((Map) command).name) { - throw new IllegalArgumentException("a command has no name: ${command}") + throw new IllegalArgumentException("it has no name: ${command}") } new ProjectCommand((Map) command, gradle) } + private static String nameOf(Object command) { + Object name = command instanceof Map ? ((Map) command).name : null + name instanceof String && name ? (String) name : 'without a name' + } + + /** + * Whether the manifest needs the build to run: there is none and the build did not fail last time, + * nothing was recorded, or a recorded file changed. + */ private boolean outdated(File manifest) { - File failed = new File(projectDir, FAILED_PATH) - long lastRead = Math.max(manifest.isFile() ? manifest.lastModified() : 0L, failed.isFile() ? failed.lastModified() : 0L) - if (lastRead == 0L) { + if (!manifest.isFile() && !new File(projectDir, FAILED_PATH).isFile()) { return true } - List buildFiles = buildFiles() - File recorded = new File(projectDir, BUILD_FILES_PATH) - if (!recorded.isFile()) { - // a manifest the project's own build wrote: its build files are taken to be those there now - record(buildFiles) + File record = new File(projectDir, RECORD_PATH) + try { + // without a record, as when the project's own build wrote the manifest, a file deleted since + // cannot be told from one that was never there + return !record.isFile() || Files.readAllLines(record.toPath()) != snapshot(inputsOf(manifest)) } - else if (recorded.readLines('UTF-8') != buildFiles) { + catch (IOException ignored) { return true } - ([projectDir, gradle.rootDir] as LinkedHashSet).any { File dir -> changedBuild(dir, lastRead) } || - changedSources(lastRead) } /** - * Whether the commands of the application changed. When their directory is gone, {@code grails-app}, - * which every project has, changed when it was removed. + * The directories the commands depend on: those the manifest names, or, for a manifest that names + * none, those of the project and the root of its build. */ - private boolean changedSources(long since) { - File sources = new File(projectDir, COMMAND_SOURCES) - sources.isDirectory() ? changed(sources, since) : sources.parentFile.lastModified() > since + private Inputs inputsOf(File manifest) { + if (manifest.isFile()) { + try { + Object parsed = new JsonSlurper().parse(manifest, 'UTF-8') + Object inputs = parsed instanceof Map ? ((Map) parsed).inputs : null + if (inputs instanceof Map) { + List buildDirectories = files(((Map) inputs).buildDirectories) + List sourceDirectories = files(((Map) inputs).sourceDirectories) + if (buildDirectories != null && sourceDirectories != null) { + return new Inputs(buildDirectories, sourceDirectories) + } + } + } + catch (JsonException | IOException | RuntimeException ignored) { + // the manifest is reported when it is read + } + } + File rootDir = gradle.rootDir + new Inputs([projectDir, rootDir].unique(), + [new File(rootDir, 'gradle'), new File(rootDir, 'buildSrc'), + new File(projectDir, 'grails-app/commands'), new File(projectDir, 'src/main/resources/META-INF')]) + } + + private static List files(Object paths) { + if (!(paths instanceof List) || ((List) paths).any { !(it instanceof String) }) { + return null + } + ((List) paths).collect { String path -> new File(path) } } /** - * The build files directly in the project and build root directories, which a deleted one leaves no - * trace of; those under the build directories change the time of the directory that held them. + * What the files the commands depend on are now: the build files directly in each build directory, + * and every file and directory below each source directory, apart from what builds and tools write + * next to a build script. Each file is listed with its time and size, so a deleted, added, replaced + * or changed file changes the list, whatever the clock said when it was written. */ - private List buildFiles() { - ([projectDir, gradle.rootDir] as LinkedHashSet).collectMany { File dir -> - (dir.listFiles() ?: new File[0]).findAll { File file -> buildFile(file) }*.absolutePath - }.sort() + private static List snapshot(Inputs inputs) { + List lines = [] + for (File dir : inputs.buildDirectories) { + File[] files = dir.listFiles() + if (files == null) { + lines << "absent\t${dir.absolutePath}".toString() + continue + } + for (File file : files.sort(false) { File it -> it.name }) { + if (file.isFile() && BUILD_FILE_SUFFIXES.any { String suffix -> file.name.endsWith(suffix) }) { + lines << describe(file) + } + } + } + for (File dir : inputs.sourceDirectories) { + walk(dir, lines) + } + lines } - private void record(List buildFiles) { - File recorded = new File(projectDir, BUILD_FILES_PATH) - recorded.parentFile.mkdirs() - recorded.setText(buildFiles.collect { String path -> path + '\n' }.join(''), 'UTF-8') + private static void walk(File file, List lines) { + Path path = file.toPath() + if (Files.isSymbolicLink(path)) { + // not followed, so a link to a directory above it cannot loop + String target + try { + target = Files.readSymbolicLink(path).toString() + } + catch (IOException | UnsupportedOperationException ignored) { + target = '' + } + lines << "link\t${file.absolutePath}\t${target}".toString() + } + else if (file.isDirectory()) { + File[] children = file.listFiles() + if (children == null) { + lines << "unreadable\t${file.absolutePath}".toString() + return + } + lines << "directory\t${file.absolutePath}".toString() + boolean buildRoot = file.name == 'buildSrc' || BUILD_SCRIPTS.any { String script -> new File(file, script).isFile() } + for (File child : children.sort(false) { File it -> it.name }) { + if (!(buildRoot && child.name in OUTPUT_DIRS && child.isDirectory())) { + walk(child, lines) + } + } + } + else if (file.isFile()) { + lines << describe(file) + } + else { + lines << "absent\t${file.absolutePath}".toString() + } } - private static boolean buildFile(File file) { - file.isFile() && BUILD_FILE_SUFFIXES.any { String suffix -> file.name.endsWith(suffix) } + private static String describe(File file) { + "file\t${file.absolutePath}\t${file.lastModified()}\t${file.length()}".toString() } - private static boolean changedBuild(File dir, long since) { - dir.listFiles()?.any { File file -> - buildFile(file) && file.lastModified() > since - } || BUILD_DIRS.any { String name -> changed(new File(dir, name), since) } - } + @EqualsAndHashCode + private static final class Inputs { - /** - * Whether a file in the directory, or the directory itself or one below it, changed: a directory - * changes when a file in it is added, deleted or renamed. - */ - private static boolean changed(File dir, long since) { - if (!dir.isDirectory()) { - return false - } - if (dir.lastModified() > since) { - return true - } - dir.listFiles()?.any { File file -> - file.isDirectory() ? !(file.name in OUTPUT_DIRS) && changed(file, since) : file.lastModified() > since + final List buildDirectories + final List sourceDirectories + + Inputs(List buildDirectories, List sourceDirectories) { + this.buildDirectories = buildDirectories + this.sourceDirectories = sourceDirectories } } } diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectGradle.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectGradle.groovy index 143f9660e6a..51d06338374 100644 --- a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectGradle.groovy +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectGradle.groovy @@ -25,6 +25,9 @@ import groovy.transform.CompileStatic * directory, or the one in the root directory of the build the project belongs to, the first * directory above it with a settings file. No wrapper outside the build is ever run, and neither is * a {@code gradle} found on the path. + * + *

Every run names the project with the Grails Gradle plugin's {@code grailsCommandProject} property, + * so the tasks it runs run in that project only, and not also in the projects below it.

*/ @CompileStatic class ProjectGradle { @@ -33,12 +36,17 @@ class ProjectGradle { private static final List SETTINGS_FILES = ['settings.gradle', 'settings.gradle.kts'] + /** The project property of the Grails Gradle plugin that runs its command tasks in one project only */ + static final String COMMAND_PROJECT_PROPERTY = 'grailsCommandProject' + private final File projectDir private final File wrapper + private final File rootDir ProjectGradle(File projectDir) { this.projectDir = projectDir this.wrapper = findWrapper(projectDir) + this.rootDir = findSettingsDir(projectDir) ?: projectDir } /** @@ -49,11 +57,11 @@ class ProjectGradle { } /** - * @return the root directory of the build the project belongs to: the directory of its Gradle - * wrapper, or the project directory when it has none + * @return the root directory of the build the project belongs to: the first directory, from the + * project's upward, with a settings file, as Gradle finds it, or the project directory when there is none */ File getRootDir() { - wrapper?.parentFile ?: projectDir + rootDir } /** @@ -84,7 +92,8 @@ class ProjectGradle { if (wrapper == null) { throw new IOException("The build of ${projectDir} has no Gradle wrapper") } - List command = [wrapper.path, '-p', projectDir.path, '-q', '--console=plain'] + List command = [wrapper.path, '-p', projectDir.path, '-q', '--console=plain', + "-P${COMMAND_PROJECT_PROPERTY}=${projectDir.path}".toString()] command.addAll(arguments) ProcessBuilder builder = new ProcessBuilder(command).directory(projectDir) redirect.call(builder) @@ -109,10 +118,15 @@ class ProjectGradle { } // the build's root is the first directory with a settings file, as Gradle finds it; a wrapper // further up belongs to something else + File settingsDir = findSettingsDir(projectDir) + File rootWrapper = settingsDir == null ? null : new File(settingsDir, name) + rootWrapper?.isFile() ? rootWrapper : null + } + + private static File findSettingsDir(File projectDir) { for (File dir = projectDir; dir != null; dir = dir.parentFile) { if (SETTINGS_FILES.any { new File(dir, it).isFile() }) { - File rootWrapper = new File(dir, name) - return rootWrapper.isFile() ? rootWrapper : null + return dir } } null diff --git a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/CommandLineCompleterSpec.groovy b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/CommandLineCompleterSpec.groovy index 2139ee616aa..fd24596875c 100644 --- a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/CommandLineCompleterSpec.groovy +++ b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/CommandLineCompleterSpec.groovy @@ -26,6 +26,7 @@ import org.jline.terminal.Terminal import org.jline.terminal.TerminalBuilder import org.springframework.context.ConfigurableApplicationContext import picocli.CommandLine +import picocli.CommandLine.Model.CommandSpec import spock.lang.AutoCleanup import spock.lang.Shared import spock.lang.Specification @@ -72,13 +73,28 @@ class CommandLineCompleterSpec extends Specification { void 'the completer needs a command to complete from'() { when: - new CommandLineCompleter(null) + new CommandLineCompleter((CommandSpec) null) then: NullPointerException e = thrown() e.message == 'spec' } + void 'commands given by a supplier are completed as they are when the word is completed'() { + given: 'the commands of the prompt, which gain one after the completer is made' + CommandSpec offered = CommandSpec.create().addSubcommand('hello', CommandSpec.create()) + CommandLineCompleter completer = new CommandLineCompleter({ -> offered } as java.util.function.Supplier) + LineReader reader = LineReaderBuilder.builder().terminal(terminal).build() + + when: + offered.addSubcommand('help-me', CommandSpec.create()) + List candidates = [] + completer.complete(reader, new DefaultParser().parse('he', 2), candidates) + + then: + candidates*.value() as Set == ['hello', 'help-me'] as Set + } + private List complete(String typed) { LineReader reader = LineReaderBuilder.builder().terminal(terminal).build() List candidates = [] diff --git a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy index 78c1a718c31..195fcafa9cb 100644 --- a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy +++ b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy @@ -72,6 +72,16 @@ class ProjectCommandsSpec extends Specification { private static final List MANIFEST_TASK = ['generateCommandManifest', '-PgrailsCommandManifest=build/grails/commands.json'] + /** Files the commands depend on, in every place the Forge CLI watches without a manifest naming them */ + private static final List WATCHED_FILES = [ + 'build.gradle', 'settings.gradle', 'settings.gradle.kts', 'gradle.properties', 'dependencies.gradle', + 'libs.versions.toml', 'gradle/libs.versions.toml', 'gradle/publishing.gradle', + 'gradle/wrapper/gradle-wrapper.properties', 'buildSrc/build.gradle', + 'buildSrc/src/main/groovy/Conventions.groovy', 'buildSrc/src/main/groovy/com/acme/build/Conventions.groovy', + 'grails-app/commands/example/HelloCommand.groovy', 'grails-app/commands/com/acme/build/DeployCommand.groovy', + 'src/main/resources/META-INF/grails-cli.factories' + ] + private static final Map PLUGIN_CREATE_APP = [ name: 'create-app', description: 'A plugin command named like a Forge command', arguments: [], options: [] ] @@ -123,14 +133,14 @@ class ProjectCommandsSpec extends Specification { CommandLine commandLine = ForgeCli.createCommandLine(context, false) then: "the build is run once, quietly, in the project directory" - gradleInvocations() == [['-p', dir.path, '-q', '--console=plain'] + MANIFEST_TASK] + gradleInvocations() == [prefix() + MANIFEST_TASK] output.toString().contains('Reading the commands of the project from its build...') and: "the project's commands are offered next to the Forge CLI's own" commandLine.subcommands.keySet().containsAll(['create-app', 's2-quickstart', 'url-mappings-report']) } - void 'a manifest newer than the build is read without running the build'() { + void 'a manifest whose files are unchanged is read without running the build'() { given: writeManifest(SECURITY_QUICKSTART) @@ -138,145 +148,210 @@ class ProjectCommandsSpec extends Specification { CommandLine commandLine = ForgeCli.createCommandLine(context, false) then: - commandLine.subcommands.containsKey('s2-quickstart') gradleInvocations() == [] + commandLine.subcommands.containsKey('s2-quickstart') } - void 'the manifest is written again when #changed changed after it'() { - given: + void 'the manifest is written again when #path is #change'() { + given: 'the file there when the Forge CLI last ran the build, unless it is added later' + File file = new File(dir, path) + if (change != 'added') { + file.parentFile.mkdirs() + file.text = 'a=1' + file.setLastModified(System.currentTimeMillis() - 3_600_000) + } writeManifest(URL_MAPPINGS_REPORT) + long recorded = file.lastModified() buildWrites(SECURITY_QUICKSTART) - File changedFile = new File(dir, changed) - changedFile.parentFile.mkdirs() - changedFile.text = 'changed' - changedFile.setLastModified(manifest.lastModified() + 10_000) when: + switch (change) { + case 'added': + file.parentFile.mkdirs() + file.text = 'a=1' + break + case 'changed with its time kept': + file.text = 'a=12' + file.setLastModified(recorded) + break + case 'replaced by an older copy': + file.text = 'b=2' + file.setLastModified(recorded - 60_000) + break + case 'deleted': + assert file.delete() + break + } CommandLine commandLine = ForgeCli.createCommandLine(context, false) then: - gradleInvocations() == [['-p', dir.path, '-q', '--console=plain'] + MANIFEST_TASK] + gradleInvocations() == [prefix() + MANIFEST_TASK] commandLine.subcommands.containsKey('s2-quickstart') + and: 'the change is recorded, so the build is not run again for it' + ForgeCli.createCommandLine(context, false) + gradleInvocations().size() == 1 + where: - changed << ['build.gradle', 'settings.gradle', 'gradle.properties', 'gradle/libs.versions.toml', - 'dependencies.gradle', 'gradle/publishing.gradle', 'buildSrc/src/main/groovy/Conventions.groovy', - 'buildSrc/build.gradle', 'grails-app/commands/example/HelloCommand.groovy'] + [path, change] << [WATCHED_FILES, ['added', 'changed with its time kept', 'replaced by an older copy', 'deleted']] + .combinations() + .findAll { String path, String change -> !(path == 'build.gradle' && change in ['added', 'deleted']) } } - void 'the manifest is written again when a command is deleted from the application'() { + void 'the manifest is written again when the #removed directory is removed'() { given: - File command = new File(dir, 'grails-app/commands/example/HelloCommand.groovy') - command.parentFile.mkdirs() - command.text = 'class HelloCommand {}' + File file = new File(dir, "${removed}/example/Hello.groovy") + file.parentFile.mkdirs() + file.text = 'class Hello {}' writeManifest(URL_MAPPINGS_REPORT) - [command, command.parentFile, new File(dir, 'grails-app/commands')].each { it.setLastModified(manifest.lastModified() - 10_000) } buildWrites(SECURITY_QUICKSTART) - when: 'the command is deleted, which changes only its directory' - assert command.delete() - command.parentFile.setLastModified(manifest.lastModified() + 10_000) - CommandLine commandLine = ForgeCli.createCommandLine(context, false) + when: + assert new File(dir, removed).deleteDir() + ForgeCli.createCommandLine(context, false) then: - gradleInvocations()*.first() == ['-p'] - commandLine.subcommands.containsKey('s2-quickstart') + gradleInvocations() == [prefix() + MANIFEST_TASK] + + where: + removed << ['grails-app/commands', 'buildSrc', 'gradle', 'src/main/resources/META-INF'] } - void 'the manifest is written again when the commands directory is removed with the last command'() { + void 'a project without the directories the commands depend on does not run the build while nothing changed'() { given: - File command = new File(dir, 'grails-app/commands/example/HelloCommand.groovy') - command.parentFile.mkdirs() - command.text = 'class HelloCommand {}' + assert new File(dir, 'grails-app/commands').deleteDir() writeManifest(URL_MAPPINGS_REPORT) - [command, command.parentFile, command.parentFile.parentFile, new File(dir, 'grails-app'), dir].each { - it.setLastModified(manifest.lastModified() - 10_000) - } + + when: + CommandLine commandLine = ForgeCli.createCommandLine(context, false) + + then: + gradleInvocations() == [] + commandLine.subcommands.containsKey('url-mappings-report') + } + + void 'a manifest the build wrote without the Forge CLI is written again, as a file may have been deleted since'() { + given: + writeManifestOfTheBuild(URL_MAPPINGS_REPORT) buildWrites(SECURITY_QUICKSTART) - when: 'the directory is deleted, which changes only grails-app' - assert new File(dir, 'grails-app/commands').deleteDir() - new File(dir, 'grails-app').setLastModified(manifest.lastModified() + 10_000) + when: CommandLine commandLine = ForgeCli.createCommandLine(context, false) then: - gradleInvocations() == [['-p', dir.path, '-q', '--console=plain'] + MANIFEST_TASK] + gradleInvocations() == [prefix() + MANIFEST_TASK] commandLine.subcommands.containsKey('s2-quickstart') + + and: 'the files are recorded, so it is not run again' + ForgeCli.createCommandLine(context, false) + gradleInvocations().size() == 1 } - void 'a project without a commands directory does not run the build while nothing changed'() { + void 'what builds and tools write next to a build script, and the sources the build compiles, do not make the manifest outdated'() { given: - assert new File(dir, 'grails-app/commands').deleteDir() + new File(dir, 'buildSrc/build.gradle').with { parentFile.mkdirs(); text = '' } writeManifest(URL_MAPPINGS_REPORT) - [new File(dir, 'grails-app'), dir].each { it.setLastModified(manifest.lastModified() - 10_000) } when: - CommandLine commandLine = ForgeCli.createCommandLine(context, false) + ['buildSrc/build/classes/Conventions.class', 'buildSrc/.gradle/file-system.probe', 'buildSrc/.kotlin/sessions/a', + 'buildSrc/bin/main/Conventions.class', 'buildSrc/out/Conventions.class', 'build/libs/app.jar', + '.gradle/file-system.probe', 'src/main/groovy/example/Service.groovy', 'grails-app/controllers/Home.groovy', + 'README.md'].each { String path -> + File written = new File(dir, path) + written.parentFile.mkdirs() + written.text = 'written' + } + ForgeCli.createCommandLine(context, false) then: gradleInvocations() == [] - commandLine.subcommands.containsKey('url-mappings-report') } - void 'the manifest is written again when #deleted is deleted'() { - given: 'a manifest the build wrote, read once, which records the build files there' - File buildFile = new File(dir, deleted) - buildFile.text = 'a=b' - buildFile.setLastModified(System.currentTimeMillis() - 20_000) + void 'a source package named like an output directory is watched like any other'() { + given: + File command = new File(dir, "grails-app/commands/com/acme/${name}/DeployCommand.groovy") + command.parentFile.mkdirs() writeManifest(URL_MAPPINGS_REPORT) - ForgeCli.createCommandLine(context, false) - assert gradleInvocations() == [] - assert new File(dir, 'build/grails/commands.inputs').readLines().contains(buildFile.absolutePath) - buildWrites(SECURITY_QUICKSTART) - when: 'the file is deleted, which leaves no file newer than the manifest' - assert buildFile.delete() - CommandLine commandLine = ForgeCli.createCommandLine(context, false) + when: + command.text = 'class DeployCommand {}' + ForgeCli.createCommandLine(context, false) then: - gradleInvocations() == [['-p', dir.path, '-q', '--console=plain'] + MANIFEST_TASK] - commandLine.subcommands.containsKey('s2-quickstart') + gradleInvocations() == [prefix() + MANIFEST_TASK] - and: 'the record now matches the files there, so the build is not run again' - !new File(dir, 'build/grails/commands.inputs').readLines().contains(buildFile.absolutePath) + where: + name << ['build', 'bin', 'out'] + } + + void 'a file dated in the future makes the build run once, not on every start'() { + given: + File future = new File(dir, 'gradle.properties') + future.text = 'a=1' + future.setLastModified(System.currentTimeMillis() + 86_400_000) + + when: + ForgeCli.createCommandLine(context, false) ForgeCli.createCommandLine(context, false) + + then: gradleInvocations().size() == 1 + } - where: - deleted << ['gradle.properties', 'settings.gradle', 'dependencies.gradle', 'libs.versions.toml'] + void 'a file changed while the build runs makes the build run again next time'() { + given: 'a build during which a command is saved' + buildWrites(URL_MAPPINGS_REPORT) + new File(dir, 'during-build.sh').text = 'printf "class Hello {}" > "$1/grails-app/commands/Hello.groovy"\n' + + when: + ForgeCli.createCommandLine(context, false) + new File(dir, 'during-build.sh').delete() + ForgeCli.createCommandLine(context, false) + ForgeCli.createCommandLine(context, false) + + then: + gradleInvocations().size() == 2 } - void 'the manifest is written again when a build file older than it is moved into the project'() { - given: - writeManifest(URL_MAPPINGS_REPORT) + void 'the directories the manifest names are watched, such as those of the other projects of the build'() { + given: 'a manifest naming a plugin project next to the application' + File plugin = new File(dir, 'plugins/audit') + new File(plugin, 'grails-app/commands').mkdirs() + new File(plugin, 'build.gradle').text = '' + buildWritesWithInputs([buildDirectories: [dir.path, plugin.path], + sourceDirectories: [new File(dir, 'grails-app/commands').path, new File(plugin, 'grails-app/commands').path]], + URL_MAPPINGS_REPORT) ForgeCli.createCommandLine(context, false) - buildWrites(SECURITY_QUICKSTART) + forgetInvocations() + + when: 'the plugin gains a command' + new File(plugin, 'grails-app/commands/AuditCommand.groovy').text = 'class AuditCommand {}' + ForgeCli.createCommandLine(context, false) + + then: + gradleInvocations() == [prefix() + MANIFEST_TASK] - when: 'a file keeps the time it had elsewhere' - File moved = new File(dir, 'gradle.properties') - moved.text = 'a=b' - moved.setLastModified(manifest.lastModified() - 10_000) + when: "the plugin's build changes" + new File(plugin, 'build.gradle').text = 'plugins {}' ForgeCli.createCommandLine(context, false) then: - gradleInvocations() == [['-p', dir.path, '-q', '--console=plain'] + MANIFEST_TASK] + gradleInvocations().size() == 2 } - void 'what the build writes for itself does not make the manifest outdated'() { + void 'a link that loops is recorded, not followed'() { given: - writeManifest(URL_MAPPINGS_REPORT) - ['buildSrc/build/classes/Conventions.class', 'buildSrc/.gradle/file-system.probe'].each { String path -> - File written = new File(dir, path) - written.parentFile.mkdirs() - written.text = 'written by the build' - written.setLastModified(manifest.lastModified() + 10_000) - } + File source = new File(dir, 'buildSrc/src/main/groovy') + source.mkdirs() + Files.createSymbolicLink(new File(source, 'loop').toPath(), new File(dir, 'buildSrc').toPath()) when: ForgeCli.createCommandLine(context, false) + CommandLine commandLine = ForgeCli.createCommandLine(context, false) then: - gradleInvocations() == [] + gradleInvocations().size() == 1 + commandLine.subcommands.containsKey('create-app') } void 'a directory without a Grails project has no project commands and runs no build'() { @@ -301,6 +376,28 @@ class ProjectCommandsSpec extends Specification { gradleInvocations() == [] } + void "naming one of the Forge CLI's code generation commands never runs the build, even where it does not apply"() { + given: 'a project without the configuration the code generation commands need' + buildWrites(SECURITY_QUICKSTART) + + when: + ForgeCli.execute(context, 'create-controller', 'Book') + + then: + gradleInvocations() == [] + } + + void 'no command follows --, so it runs no build'() { + given: + buildWrites(URL_MAPPINGS_REPORT) + + when: + ForgeCli.execute(context, '--', 'url-mappings-report') + + then: + gradleInvocations() == [] + } + void "a project command named like one of the Forge CLI's commands does not replace it"() { given: writeManifest(PLUGIN_CREATE_APP, URL_MAPPINGS_REPORT) @@ -316,7 +413,7 @@ class ProjectCommandsSpec extends Specification { void '--help lists the commands the build already wrote, even when they are outdated, without running the build'() { given: writeManifest(SECURITY_QUICKSTART) - new File(dir, 'build.gradle').setLastModified(manifest.lastModified() + 10_000) + new File(dir, 'build.gradle') << '// changed\n' buildWrites(URL_MAPPINGS_REPORT) when: @@ -353,7 +450,7 @@ class ProjectCommandsSpec extends Specification { then: exitCode == 0 gradleInvocations().size() == 1 - gradleInvocations().first().take(5) == ['-p', dir.path, '-q', '--console=plain', 'runCommand'] + gradleInvocations().first().take(6) == prefix() + 'runCommand' commandArguments() == ['s2-quickstart', 'com.example', 'User', 'Role', '--groupClassName=RoleGroup', '--uiOnly'] and: 'the file of the arguments is gone once the command has run' @@ -418,7 +515,7 @@ class ProjectCommandsSpec extends Specification { commandArguments() == ['url-mappings-report'] + arguments } - void '-D sets a system property of the build, such as the environment, and is not passed to the command'() { + void '-D sets a system property of the command, passed among its arguments as runCommand reads it, not on the command line of Gradle'() { given: writeManifest(SECURITY_QUICKSTART, URL_MAPPINGS_REPORT) @@ -426,14 +523,18 @@ class ProjectCommandsSpec extends Specification { ForgeCli.execute(context, typed as String[]) then: - gradleInvocations().first().subList(4, 6) == ['-Dgrails.env=test', 'runCommand'] + gradleInvocations().first().take(7) == prefix() + ['runCommand', "-PargsFile=${argumentsFile()}".toString()] + gradleInvocations().first().count { it.startsWith('-D') } == 0 commandArguments() == passed + and: 'the parser runCommand uses reads it as a system property' + new CommandLineParser().parse(commandArguments() as String[]).systemProperties == ['grails.env': 'test', query: 'a=b'] + where: - typed || passed - ['s2-quickstart', '-Dgrails.env=test', 'com.example'] || ['s2-quickstart', 'com.example'] - ['url-mappings-report', '-Dgrails.env=test', 'first'] || ['url-mappings-report', 'first'] - ['url-mappings-report', 'first', '-D', 'grails.env=test'] || ['url-mappings-report', 'first'] + typed || passed + ['s2-quickstart', '-Dgrails.env=test', 'com.example', '-D', 'query=a=b'] || ['s2-quickstart', 'com.example', '-Dgrails.env=test', '-Dquery=a=b'] + ['url-mappings-report', '-Dgrails.env=test', 'first', '-Dquery=a=b'] || ['url-mappings-report', '-Dgrails.env=test', 'first', '-Dquery=a=b'] + ['url-mappings-report', 'first', '-D', 'grails.env=test', '-D', 'query=a=b'] || ['url-mappings-report', 'first', '-Dgrails.env=test', '-Dquery=a=b'] } void "a project command after the Forge CLI's own options runs the build that finds it"() { @@ -445,7 +546,7 @@ class ProjectCommandsSpec extends Specification { then: exitCode == 0 - gradleInvocations()*.get(4) == ['generateCommandManifest', 'runCommand'] + gradleInvocations()*.get(5) == ['generateCommandManifest', 'runCommand'] } void 'the exit code of the command is the exit code of the build'() { @@ -506,22 +607,29 @@ class ProjectCommandsSpec extends Specification { then: 'the build is not run again' gradleInvocations().size() == 1 + when: 'the build directory is cleaned' + new File(dir, 'build').deleteDir() + ForgeCli.createCommandLine(context, false) + + then: 'the failed build is still remembered' + gradleInvocations().size() == 1 + when: 'a build file changes, and the build works again' new File(dir, 'gradle-exit-code').delete() buildWrites(SECURITY_QUICKSTART) - new File(dir, 'build.gradle').setLastModified(System.currentTimeMillis() + 20_000) + new File(dir, 'build.gradle') << '// changed\n' commandLine = ForgeCli.createCommandLine(context, false) then: gradleInvocations().size() == 2 commandLine.subcommands.containsKey('s2-quickstart') + !new File(dir, '.gradle/grails/commands.failed').exists() } void 'a failed build keeps the commands of the last manifest, and is not run again for them'() { given: 'an outdated manifest, and a build that fails, as while the application does not compile' writeManifest(URL_MAPPINGS_REPORT) - changedBefore(20_000, manifest) - changedBefore(10_000, new File(dir, 'build.gradle')) + new File(dir, 'build.gradle') << '// changed\n' new File(dir, 'gradle-exit-code').text = '1' when: @@ -537,13 +645,13 @@ class ProjectCommandsSpec extends Specification { then: 'the command runs without the build being tried again' exitCode == 0 - gradleInvocations()*.get(4) == ['generateCommandManifest', 'runCommand'] + gradleInvocations()*.get(5) == ['generateCommandManifest', 'runCommand'] } void 'a wrapper that cannot be started is reported, and the Forge CLI still runs'() { given: writeManifest(URL_MAPPINGS_REPORT) - new File(dir, 'build.gradle').setLastModified(manifest.lastModified() + 10_000) + new File(dir, 'build.gradle') << '// changed\n' Files.setPosixFilePermissions(new File(dir, 'gradlew').toPath(), [PosixFilePermission.OWNER_READ] as Set) when: @@ -558,8 +666,7 @@ class ProjectCommandsSpec extends Specification { void 'a build whose manifest task is up to date is not run again'() { given: 'a build file changed in a way that leaves the classpath, and so the manifest, as it was' writeManifest(URL_MAPPINGS_REPORT) - changedBefore(20_000, manifest) - changedBefore(10_000, new File(dir, 'build.gradle')) + new File(dir, 'build.gradle') << '// changed\n' when: ForgeCli.createCommandLine(context, false) @@ -571,9 +678,7 @@ class ProjectCommandsSpec extends Specification { void 'a manifest #problem is reported, and the Forge CLI still runs'() { given: - manifest.parentFile.mkdirs() - manifest.text = JsonOutput.toJson(content) - manifest.setLastModified(System.currentTimeMillis() + 10_000) + buildWritesContent(content) when: CommandLine commandLine = ForgeCli.createCommandLine(context, false) @@ -581,17 +686,65 @@ class ProjectCommandsSpec extends Specification { then: output.toString().contains("Could not read the commands of the project from ${manifest}, so they are not offered: ${message}") commandLine.subcommands.containsKey('create-app') - gradleInvocations() == [] + + and: 'the build that wrote it is not run again for nothing' + ForgeCli.createCommandLine(context, false) + gradleInvocations().size() == 1 where: problem | content || message 'of a newer format' | [version: 2, commands: [URL_MAPPINGS_REPORT]] || 'it was written in format 2, by a newer Grails' 'without a format version' | [commands: [URL_MAPPINGS_REPORT]] || 'it has no format version' 'whose commands are not a list' | [version: 1, commands: 'oops'] || 'it has no list of commands' - 'with a command without a name' | [version: 1, commands: [[description: 'nameless']]] || 'a command has no name' 'that is not an object' | [URL_MAPPINGS_REPORT] || 'it is not a JSON object' } + void 'a command that cannot be offered, #problem, costs only that command'() { + given: + buildWritesContent([version: 1, commands: [command, URL_MAPPINGS_REPORT]]) + + when: + CommandLine commandLine = ForgeCli.createCommandLine(context, false) + + then: + output.toString().contains("The command ${name} of the project is not offered: ") + commandLine.subcommands.containsKey('url-mappings-report') + !commandLine.subcommands.containsKey('broken') + + where: + problem | command || name + 'as it has no name' | [description: 'nameless'] || 'without a name' + 'as an option takes --help' | [name: 'broken', options: [[name: 'help', flag: true]]] || 'broken' + 'as it declares an option twice' | [name: 'broken', options: [[name: 'format', flag: false], [name: 'format', flag: true]]] || 'broken' + } + + void 'a record that cannot be written is reported, and the Forge CLI still runs'() { + given: 'a file where the directory of the record would be, as a directory the user cannot write' + new File(dir, '.gradle').text = 'not a directory' + buildWrites(URL_MAPPINGS_REPORT) + + when: + CommandLine commandLine = ForgeCli.createCommandLine(context, false) + + then: + output.toString().contains('Could not record what the commands of the project depend on in ') + output.toString().contains('The build is run for them again next time.') + commandLine.subcommands.containsKey('url-mappings-report') + } + + void 'a build directory the Forge CLI cannot write is reported, and the Forge CLI still runs'() { + given: + buildWrites(URL_MAPPINGS_REPORT) + new File(dir, 'build').text = 'not a directory' + + when: + CommandLine commandLine = ForgeCli.createCommandLine(context, false) + + then: + output.toString().contains('Could not read the commands of the project: ') + commandLine.subcommands.containsKey('create-app') + } + void 'a manifest that is not JSON is reported'() { given: manifest.parentFile.mkdirs() @@ -622,22 +775,30 @@ class ProjectCommandsSpec extends Specification { given: 'the project in a subdirectory of a build whose wrapper and settings are at its root' File project = moveProjectInto('app') new File(dir, 'settings.gradle').text = "include 'app'\n" - writeManifest(new File(project, 'build/grails/commands.json'), URL_MAPPINGS_REPORT) + buildWrites(URL_MAPPINGS_REPORT) when: int exitCode = ForgeCli.execute(context, 'url-mappings-report') - then: + then: 'the tasks run in that project only' exitCode == 0 - gradleInvocations()*.take(5) == [['-p', project.path, '-q', '--console=plain', 'runCommand']] + gradleInvocations()*.take(6) == [prefix(project) + 'generateCommandManifest', prefix(project) + 'runCommand'] + new File(project, 'build/grails/commands.json').isFile() commandArguments() == ['url-mappings-report'] + + when: "a build file of the root changes" + new File(dir, 'gradle.properties').text = 'a=1' + ForgeCli.createCommandLine(context, false) + + then: + gradleInvocations().size() == 3 } void 'a wrapper above the root of the build is never run'() { given: 'a project with a settings file of its own but no wrapper, inside a directory that has one' File project = moveProjectInto('app') new File(project, 'settings.gradle').text = '' - writeManifest(new File(project, 'build/grails/commands.json'), URL_MAPPINGS_REPORT) + writeRawManifest(new File(project, 'build/grails/commands.json'), URL_MAPPINGS_REPORT) when: CommandLine commandLine = ForgeCli.createCommandLine(context, false) @@ -675,25 +836,53 @@ class ProjectCommandsSpec extends Specification { project } - /** - * Dates the file in the past, so that what the Forge CLI writes now is newer than it. - */ - private static void changedBefore(long millis, File file) { - assert file.setLastModified(System.currentTimeMillis() - millis) + /** The arguments the Forge CLI runs the wrapper with before the tasks, for the project in the directory */ + private static List prefix(File project) { + ['-p', project.path, '-q', '--console=plain', "-PgrailsCommandProject=${project.path}".toString()] } + private List prefix() { + prefix(dir) + } + + /** Has the stand-in build write a manifest of the commands, naming no directories it depends on */ private void buildWrites(Map... commands) { - new File(dir, 'manifest-source.json').text = JsonOutput.toJson([version: 1, commands: commands.toList()]) + buildWritesContent([version: 1, commands: commands.toList()]) + } + + /** Has the stand-in build write a manifest of the commands, naming the directories it depends on */ + private void buildWritesWithInputs(Map inputs, Map... commands) { + buildWritesContent([version: 1, commands: commands.toList(), inputs: inputs]) + } + + private void buildWritesContent(Object content) { + new File(dir, 'manifest-source.json').text = JsonOutput.toJson(content) } + /** + * Leaves the manifest as an earlier run of the Forge CLI left it: written by the build, with the files + * it depends on recorded. What that run did is forgotten. + */ private void writeManifest(Map... commands) { - writeManifest(manifest, commands) + buildWrites(commands) + ForgeCli.createCommandLine(context, false) + assert manifest.isFile() + forgetInvocations() + } + + /** Writes the manifest as the project's own build leaves it, without the Forge CLI's record */ + private void writeManifestOfTheBuild(Map... commands) { + writeRawManifest(manifest, commands) } - private static void writeManifest(File file, Map... commands) { + private static void writeRawManifest(File file, Map... commands) { file.parentFile.mkdirs() file.text = JsonOutput.toJson([version: 1, commands: commands.toList()]) - file.setLastModified(System.currentTimeMillis() + 10_000) + } + + private void forgetInvocations() { + new File(dir, 'gradle-invocations.txt').delete() + output.reset() } private List> gradleInvocations(File wrapperDir = dir) { @@ -732,14 +921,17 @@ class ProjectCommandsSpec extends Specification { wrapper.text = '''\ #!/bin/sh dir=$(cd "$(dirname "$0")" && pwd) + project=$dir; previous='' + for argument in "$@"; do [ "$previous" = '-p' ] && project=$argument; previous=$argument; done { for argument in "$@"; do printf '%s\\n' "$argument" case "$argument" in -PargsFile=*) printf 'argsFile:%s\\n' "$(cat "${argument#-PargsFile=}")";; esac done; echo '--'; } >> "$dir/gradle-invocations.txt" case " $* " in *" generateCommandManifest "*) + if [ -f "$dir/during-build.sh" ]; then sh "$dir/during-build.sh" "$project"; fi if [ -f "$dir/manifest-source.json" ]; then - mkdir -p "$dir/build/grails" && cp "$dir/manifest-source.json" "$dir/build/grails/commands.json" + mkdir -p "$project/build/grails" && cp "$dir/manifest-source.json" "$project/build/grails/commands.json" || exit 1 fi;; esac if [ -f "$dir/gradle-exit-code" ]; then exit "$(cat "$dir/gradle-exit-code")"; fi From bec6b4e43759adfb7e2baf7a36f82f81074d21c7 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Tue, 17 Nov 2026 19:46:17 -0800 Subject: [PATCH 063/115] Give an option an empty value instead of the word after it An empty argument after an option without a value was skipped, so the next word became the option's value: --other '' first gave other=first and no arguments. The empty argument is now the option's value. --- .../org/grails/build/parsing/CommandLineParser.java | 9 ++++++++- .../build/parsing/CommandLineParserSpec.groovy | 12 ++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/grails-gradle/model/src/main/groovy/org/grails/build/parsing/CommandLineParser.java b/grails-gradle/model/src/main/groovy/org/grails/build/parsing/CommandLineParser.java index 2c7b466f4f8..f654d78170e 100644 --- a/grails-gradle/model/src/main/groovy/org/grails/build/parsing/CommandLineParser.java +++ b/grails-gradle/model/src/main/groovy/org/grails/build/parsing/CommandLineParser.java @@ -195,7 +195,14 @@ private void parseInternal(DefaultCommandLine cl, String[] args, boolean firstAr if (trimmed.length() > 1 && trimmed.charAt(0) == '"' && trimmed.charAt(trimmed.length() - 1) == '"') { trimmed = trimmed.substring(1, trimmed.length() - 1).trim(); } - if (trimmed.length() > 0) { + if (trimmed.isEmpty()) { + // an empty word after an option without a value is that option's value, not nothing + if (lastWasOption != null) { + cl.addUndeclaredOption(lastWasOption, ""); + lastWasOption = null; + } + } + else { if (trimmed.charAt(0) == '-') { lastWasOption = processOption(cl, trimmed); } diff --git a/grails-gradle/model/src/test/groovy/org/grails/build/parsing/CommandLineParserSpec.groovy b/grails-gradle/model/src/test/groovy/org/grails/build/parsing/CommandLineParserSpec.groovy index 5ae70c1f59e..14d810202c8 100644 --- a/grails-gradle/model/src/test/groovy/org/grails/build/parsing/CommandLineParserSpec.groovy +++ b/grails-gradle/model/src/test/groovy/org/grails/build/parsing/CommandLineParserSpec.groovy @@ -452,4 +452,16 @@ class CommandLineParserSpec extends Specification { ['report', ' ', 'first'] || ['first'] ['report', '"two words"', 'first'] || ['two words', 'first'] } + + void "An empty word after an option is its empty value, and the next word stays an argument, for #arguments"() { + when: + def cl = new CommandLineParser().parse(arguments as String[]) + + then: + cl.optionValue('other') == '' + cl.remainingArgs == ['first'] + + where: + arguments << [['report', '--other', '', 'first'], ['report', '--other', '""', 'first'], ['report', '--other', ' ', 'first']] + } } From 468aac46199790db5abe2aedcb58d1f5b44d3645 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Tue, 17 Nov 2026 19:46:18 -0800 Subject: [PATCH 064/115] Name the sources of the listed commands, and read -D as the command does The manifest now gives each command's class, and generateCommandManifest lists, under inputs.sourceFiles, the source of each one a project of the build compiles, so a command of a plugin project in src/main/groovy that gains an option makes the Forge CLI read the commands again. The -D system properties of a command task are read from its arguments by Grails' own command line parser, so they are what the command sees, trimmed and unquoted, and are set when the task runs, after any setting made for every JavaExec task. A relative grailsCommandProject is the directory below the root of the build, not below the daemon's working directory. The fixture's builds include one through pluginManagement, which the spec shows among the inputs. --- .../ApplicationCommandManifestWriter.groovy | 4 +- ...pplicationCommandManifestWriterSpec.groovy | 1 + .../commandLine/creatingCustomCommands.adoc | 8 ++- .../ApplicationCommandManifestTask.groovy | 40 ++++++++++++- .../commands/GrailsCliGradlePlugin.groovy | 41 ++++++++++---- .../ApplicationCommandManifestTaskSpec.groovy | 6 ++ .../commands/CommandArgumentsSpec.groovy | 56 +++++++++++++++++-- .../main/java/org/example/FakeCommand.java | 23 ++++++++ .../build-logic/settings.gradle | 1 + .../cli/ApplicationCommandManifestWriter.java | 3 +- .../command-manifest/settings.gradle | 6 ++ .../command-manifest/shared/settings.gradle | 1 + 12 files changed, 164 insertions(+), 26 deletions(-) create mode 100644 grails-gradle/plugins/src/test/resources/test-projects/command-manifest/app/src/main/java/org/example/FakeCommand.java create mode 100644 grails-gradle/plugins/src/test/resources/test-projects/command-manifest/build-logic/settings.gradle create mode 100644 grails-gradle/plugins/src/test/resources/test-projects/command-manifest/shared/settings.gradle diff --git a/grails-core/src/cli/groovy/org/apache/grails/core/cli/ApplicationCommandManifestWriter.groovy b/grails-core/src/cli/groovy/org/apache/grails/core/cli/ApplicationCommandManifestWriter.groovy index e42de0d9208..65f2278d2d7 100644 --- a/grails-core/src/cli/groovy/org/apache/grails/core/cli/ApplicationCommandManifestWriter.groovy +++ b/grails-core/src/cli/groovy/org/apache/grails/core/cli/ApplicationCommandManifestWriter.groovy @@ -79,7 +79,9 @@ final class ApplicationCommandManifestWriter { [name: option.name(), description: option.description(), flag: option.flag(), values: option.values().toList()] as Map } - [name: command.name, description: command.description ?: '', arguments: arguments, options: options] as Map + // the class lets a build tool find the command's source, whose changes may change this description + [name: command.name, description: command.description ?: '', className: type.name, arguments: arguments, + options: options] as Map } // Class#getAnnotationsByType would read both forms at once, but the static compiler links it with an diff --git a/grails-core/src/test-cli/groovy/org/apache/grails/core/cli/ApplicationCommandManifestWriterSpec.groovy b/grails-core/src/test-cli/groovy/org/apache/grails/core/cli/ApplicationCommandManifestWriterSpec.groovy index 0f2da11f7fd..ffa69c8b515 100644 --- a/grails-core/src/test-cli/groovy/org/apache/grails/core/cli/ApplicationCommandManifestWriterSpec.groovy +++ b/grails-core/src/test-cli/groovy/org/apache/grails/core/cli/ApplicationCommandManifestWriterSpec.groovy @@ -60,6 +60,7 @@ class ApplicationCommandManifestWriterSpec extends Specification { and: with(command(manifest, 'described-manifest')) { description == 'Sets up the users of the application' + className == DescribedManifestCommand.name } and: "a command without a description of its own is described by the trait's default, its name" diff --git a/grails-doc/src/en/guide/commandLine/creatingCustomCommands.adoc b/grails-doc/src/en/guide/commandLine/creatingCustomCommands.adoc index b0f218a5fe5..e74ff3b60d5 100644 --- a/grails-doc/src/en/guide/commandLine/creatingCustomCommands.adoc +++ b/grails-doc/src/en/guide/commandLine/creatingCustomCommands.adoc @@ -226,13 +226,15 @@ echo '["greet", "Alice and Bob"]' > greet-args.json However they are given, the command reads the arguments through Grails' command line parser: each is trimmed, a pair of double quotes around one is removed, an empty one is left out, and an option's value is everything after the first `=`, as in `--filter=status=active`. A word that starts with `-` is an -option, and one whose name holds a space, such as `--dev mode`, is refused as invalid. An argument `-Dname=value` sets a +option, and one whose name holds a space, such as `--dev mode`, is refused as invalid. An empty argument +after an option without a value is that option's empty value. An argument `-Dname=value` sets a system property: `runCommand` starts the command with it set, so `-Dgrails.env=test` runs the command in the test environment, and the command also finds it in the system properties of its command line. In a build of several projects, `./gradlew runCommand` from the root runs the task in every project that -has it. Name one project's directory with the `grailsCommandProject` property to run it there only; the -other command tasks and `generateCommandManifest` take it too: +has it. Name one project's directory, absolute or relative to the root of the build, with the +`grailsCommandProject` property to run it there only; the other command tasks and +`generateCommandManifest` take it too: [source,shell] ---- diff --git a/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTask.groovy b/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTask.groovy index 15de279e792..d50b3608cb4 100644 --- a/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTask.groovy +++ b/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTask.groovy @@ -42,8 +42,8 @@ import javax.inject.Inject * *

The file also names, under {@code inputs}, the directories whose files decide the commands without * being on the classpath yet: those whose build files decide the build, and those that hold the sources - * of commands or of the build's logic. A tool reading the file runs the task again when one of their - * files changes.

+ * of commands or of the build's logic, and the source files of the listed commands found in the + * projects of the build. A tool reading the file runs the task again when one of their files changes.

* * @since 9.0 */ @@ -59,6 +59,7 @@ abstract class ApplicationCommandManifestTask extends JavaExec { mainClass.set(MANIFEST_WRITER_CLASS) buildDirectories.convention([]) sourceDirectories.convention([]) + commandSourceRoots.convention([]) } /** @@ -82,6 +83,38 @@ abstract class ApplicationCommandManifestTask extends JavaExec { @Input abstract ListProperty getSourceDirectories() + /** + * @return the source directories of every project of the build, where the source of a command the + * manifest lists is looked for by its class, so a change to it counts too + */ + @Input + abstract ListProperty getCommandSourceRoots() + + /** + * The source files of the listed commands found in the projects of the build; a command of a plugin + * from a repository has none. + */ + private List commandSources(Object commands) { + List found = [] + for (Object command : (commands instanceof List ? (List) commands : [])) { + Object className = command instanceof Map ? ((Map) command).className : null + if (!(className instanceof String)) { + continue + } + // a nested class is in the file of its outermost class + String path = ((String) className).replaceFirst(/\$.*/, '').replace('.', '/') + for (String root : commandSourceRoots.get()) { + for (String extension : ['.groovy', '.java', '.kt']) { + File source = new File(root, path + extension) + if (source.isFile()) { + found << source.absolutePath + } + } + } + } + found.unique().sort() + } + @Override @TaskAction void exec() { @@ -91,7 +124,8 @@ abstract class ApplicationCommandManifestTask extends JavaExec { args(file.absolutePath) super.exec() Map manifest = (Map) new JsonSlurper().parse(file, 'UTF-8') - manifest.inputs = [buildDirectories: buildDirectories.get(), sourceDirectories: sourceDirectories.get()] + manifest.inputs = [buildDirectories: buildDirectories.get(), sourceDirectories: sourceDirectories.get(), + sourceFiles: commandSources(manifest.commands)] file.setText(JsonOutput.prettyPrint(JsonOutput.toJson(manifest)), 'UTF-8') } } diff --git a/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/GrailsCliGradlePlugin.groovy b/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/GrailsCliGradlePlugin.groovy index 392c1305b9d..1cf46440ddf 100644 --- a/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/GrailsCliGradlePlugin.groovy +++ b/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/GrailsCliGradlePlugin.groovy @@ -55,6 +55,8 @@ import org.grails.gradle.plugin.util.ClasspathUtils import org.grails.gradle.plugin.util.SourceSets import org.grails.io.support.FactoriesLoaderSupport import org.grails.build.parsing.CommandLineParser +import org.grails.build.parsing.DefaultCommandLine +import org.grails.build.parsing.ParseException /** * Configures the CLI tier of a Grails application or plugin project: the {@code grailsCli} @@ -136,7 +138,8 @@ class GrailsCliGradlePlugin implements Plugin { public static final String ARGS_FILE_PROPERTY = 'argsFile' /** - * Set this project property to the directory of a project to run {@code runCommand}, + * Set this project property to the directory of a project, absolute or relative to the root of the + * build, to run {@code runCommand}, * {@link #COMMAND_MANIFEST_TASK} and the other command tasks only in that project. Without it, a task * named without its project path runs in the project Gradle starts in and in every project below it * that has the task. @@ -457,8 +460,8 @@ class GrailsCliGradlePlugin implements Plugin { /** * Returns the arguments of a command task, from {@link #commandArguments}, and prepares the task for - * them: the system properties they name as {@code -Dname=value} are set on the command's JVM, after - * {@code grails.env} from Gradle's own, so {@code -Dgrails.env=test} among them chooses the + * them: the system properties they name as {@code -Dname=value} are set on the command's JVM when + * the task runs, after every other setting, so {@code -Dgrails.env=test} among them chooses the * environment; and the task runs only in the project {@link #COMMAND_PROJECT_PROPERTY} names, when * it is set. * @@ -469,26 +472,34 @@ class GrailsCliGradlePlugin implements Plugin { @CompileDynamic static List commandTaskArguments(JavaExec task, Project project) { List arguments = commandArguments(project) - task.systemProperties(commandSystemProperties(arguments)) + Map systemProperties = commandSystemProperties(arguments) + if (systemProperties) { + // set when the task runs, so no setting of grails.env made for every JavaExec task wins over it + task.doFirst { JavaExec running -> running.systemProperties(systemProperties) } + } runOnlyInNamedProject(task, project) new ArrayList(arguments) } /** - * Returns the system properties among a command's arguments: each argument {@code -Dname=value}, - * which the command also sees in its command line. + * Returns the system properties among a command's arguments, as the command's own command line + * reads them: each argument {@code -Dname=value}, after the parser has trimmed it and removed the + * quotes around it. * * @param arguments the arguments of the command - * @return the properties by name, in the order given + * @return the properties by name; none when the parser refuses the arguments, as the command then will */ static Map commandSystemProperties(List arguments) { Map properties = new LinkedHashMap() - for (String argument : arguments) { - int equals = argument.indexOf('=') - if (argument.startsWith('-D') && equals > 2) { - properties[argument.substring(2, equals)] = argument.substring(equals + 1) + try { + // a command line of its own, so the parser's current one is left alone + new CommandLineParser().parse(new DefaultCommandLine(), arguments as String[]).systemProperties.each { Object name, Object value -> + properties[name as String] = value as String } } + catch (ParseException ignored) { + // the command reports it when it parses the same arguments + } properties } @@ -500,7 +511,8 @@ class GrailsCliGradlePlugin implements Plugin { static void runOnlyInNamedProject(org.gradle.api.Task task, Project project) { def named = project.findProperty(COMMAND_PROJECT_PROPERTY) if (named) { - boolean selected = new File(named as String).canonicalFile == project.projectDir.canonicalFile + // a relative path is the directory below the root of the build + boolean selected = project.rootProject.file(named as String).canonicalFile == project.projectDir.canonicalFile task.onlyIf("the ${COMMAND_PROJECT_PROPERTY} property names this project".toString()) { selected } } } @@ -756,9 +768,14 @@ class GrailsCliGradlePlugin implements Plugin { [new File(dir, 'grails-app/commands'), new File(dir, 'src/main/resources/META-INF')] } + includedBuildDirs) .collect { File dir -> dir.absolutePath }.unique().sort() + List commandSourceRoots = root.allprojects.collectMany { Project each -> + SourceSet main = each.extensions.findByType(org.gradle.api.tasks.SourceSetContainer)?.findByName('main') + main ? main.allSource.srcDirs.collect { File dir -> dir.absolutePath } : [] + }.unique().sort() manifestTask.configure { it.buildDirectories.set(buildDirectories) it.sourceDirectories.set(sourceDirectories) + it.commandSourceRoots.set(commandSourceRoots) } } } diff --git a/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTaskSpec.groovy b/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTaskSpec.groovy index e1d4a4d141f..99098a4e87c 100644 --- a/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTaskSpec.groovy +++ b/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTaskSpec.groovy @@ -99,6 +99,12 @@ class ApplicationCommandManifestTaskSpec extends GradleSpecification { new File(root, 'gradle'), new File(root, 'buildSrc'), new File(root, 'app/grails-app/commands'), new File(root, 'app/src/main/resources/META-INF'), new File(root, 'manifest-writer/grails-app/commands')]) + + and: 'every included build, also those that bring plugins of the build' + inputs.sourceDirectories.collect { new File(it).canonicalFile }.containsAll([new File(root, 'shared'), new File(root, 'build-logic')]) + + and: 'the sources of the listed commands found in the projects of the build' + inputs.sourceFiles.collect { new File(it).canonicalFile } == [new File(root, 'app/src/main/java/org/example/FakeCommand.java')] } void 'the task runs only in the project the grailsCommandProject property names'() { diff --git a/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/CommandArgumentsSpec.groovy b/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/CommandArgumentsSpec.groovy index 04a21b0d821..527b155c29e 100644 --- a/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/CommandArgumentsSpec.groovy +++ b/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/CommandArgumentsSpec.groovy @@ -84,10 +84,11 @@ class CommandArgumentsSpec extends Specification { GrailsCliGradlePlugin.commandArguments(project(argsFile: elsewhere.absolutePath)) == ['greet', 'Alice'] } - void 'the system properties among the arguments are set on the command JVM, and stay among the arguments'() { - given: - new File(projectDir, 'arguments.json').text = JsonOutput.toJson( - ['dbm-update', '-Dgrails.env=test', '-Dquery=a=b', '-D', '-Dnovalue', 'first']) + void 'the system properties among the arguments are set on the command JVM when it runs, and stay among the arguments'() { + given: 'arguments with properties in every form the command line parser reads' + List given = ['dbm-update', '-Dgrails.env=test', '-Dquery=a=b', ' -Dtrimmed=yes ', '"-Dquoted=yes"', + '-D', '-Dnovalue', 'first'] + new File(projectDir, 'arguments.json').text = JsonOutput.toJson(given) Project project = project(argsFile: 'arguments.json') JavaExec task = project.tasks.register('commandTask', JavaExec).get() task.systemProperty('grails.env', 'development') @@ -95,12 +96,55 @@ class CommandArgumentsSpec extends Specification { when: List arguments = GrailsCliGradlePlugin.commandTaskArguments(task, project) + then: 'nothing is set while the build is configured, where a setting for every JavaExec task could replace it' + arguments == given + task.systemProperties == ['grails.env': 'development'] + + when: 'the task starts' + task.actions.first().execute(task) + then: - arguments == ['dbm-update', '-Dgrails.env=test', '-Dquery=a=b', '-D', '-Dnovalue', 'first'] - task.systemProperties == ['grails.env': 'test', query: 'a=b'] + task.systemProperties == ['grails.env': 'test', query: 'a=b', trimmed: 'yes', quoted: 'yes'] task.onlyIf.isSatisfiedBy(task) } + void 'arguments the command line parser refuses set no system property'() { + given: + new File(projectDir, 'arguments.json').text = JsonOutput.toJson(['report', '-Dgrails.env=test', '--dev mode']) + Project project = project(argsFile: 'arguments.json') + JavaExec task = project.tasks.register('commandTask', JavaExec).get() + + int actions = task.actions.size() + + when: + GrailsCliGradlePlugin.commandTaskArguments(task, project) + + then: 'no action is added to set any' + task.actions.size() == actions + } + + void 'a relative grailsCommandProject is the directory below the root of the build'() { + given: + Project root = ProjectBuilder.builder().withProjectDir(projectDir).build() + File appDir = new File(projectDir, 'apps/app') + appDir.mkdirs() + Project app = ProjectBuilder.builder().withName('app').withProjectDir(appDir).withParent(root).build() + app.extensions.extraProperties.set('grailsCommandProject', named) + JavaExec task = app.tasks.register('commandTask', JavaExec).get() + + when: + GrailsCliGradlePlugin.commandTaskArguments(task, app) + + then: + task.onlyIf.isSatisfiedBy(task) == runs + + where: + named || runs + 'apps/app' || true + 'apps/other' || false + 'app' || false + } + void 'a command task runs only in the project the grailsCommandProject property names: #named'() { given: Project project = project(grailsCommandProject: new File(projectDir, named).absolutePath) diff --git a/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/app/src/main/java/org/example/FakeCommand.java b/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/app/src/main/java/org/example/FakeCommand.java new file mode 100644 index 00000000000..d5c9d6189b2 --- /dev/null +++ b/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/app/src/main/java/org/example/FakeCommand.java @@ -0,0 +1,23 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.example; + +/** Stands in for the source of a command the manifest lists. */ +public class FakeCommand { +} diff --git a/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/build-logic/settings.gradle b/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/build-logic/settings.gradle new file mode 100644 index 00000000000..6ef2ba8769a --- /dev/null +++ b/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/build-logic/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'build-logic' diff --git a/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/manifest-writer/src/main/java/org/apache/grails/core/cli/ApplicationCommandManifestWriter.java b/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/manifest-writer/src/main/java/org/apache/grails/core/cli/ApplicationCommandManifestWriter.java index 3f0a676e3b9..be41a293a92 100644 --- a/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/manifest-writer/src/main/java/org/apache/grails/core/cli/ApplicationCommandManifestWriter.java +++ b/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/manifest-writer/src/main/java/org/apache/grails/core/cli/ApplicationCommandManifestWriter.java @@ -41,7 +41,8 @@ public static void main(String[] args) throws IOException { Path file = Path.of(args[0]); Files.createDirectories(file.getParent()); // JSON, as the real writer writes, with what the spec checks in place of the commands - Files.writeString(file, "{\"version\": 1, \"commands\": [], \"arguments\": \"" + args.length + "\", \"file\": \"" + Files.writeString(file, "{\"version\": 1, \"commands\": [{\"name\": \"fake\", \"className\": \"org.example.FakeCommand\"}," + + " {\"name\": \"elsewhere\", \"className\": \"org.example.NotInTheBuild\"}], \"arguments\": \"" + args.length + "\", \"file\": \"" + file.toString().replace("\\", "\\\\") + "\", \"runtimeMarker\": \"" + runtimeMarker + "\"}\n"); } } diff --git a/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/settings.gradle b/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/settings.gradle index 5ca8892f9d9..5eabdad5c73 100644 --- a/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/settings.gradle +++ b/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/settings.gradle @@ -1,3 +1,9 @@ +pluginManagement { + includeBuild 'build-logic' +} + rootProject.name = 'command-manifest' include 'manifest-writer', 'app' + +includeBuild 'shared' diff --git a/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/shared/settings.gradle b/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/shared/settings.gradle new file mode 100644 index 00000000000..e1f3db7ca90 --- /dev/null +++ b/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/shared/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'shared' From e35633c987747b6fe82e4f15592ae283853d1667 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Tue, 17 Nov 2026 19:46:18 -0800 Subject: [PATCH 065/115] Watch the listed command sources; leave tool output out wherever a build is The Forge CLI records the source files the manifest lists. What builds and tools write is now left out of a watched tree outside a src or grails-app directory, whatever the tree's own path, rather than only next to a build script, so a subproject of an included build without a build file of its own, node_modules and .DS_Store no longer rerun the build. A manifest naming an input that is not an absolute path, such as /, a relative or an empty one, is read with the inputs of the project instead, and a path the system refuses no longer escapes. A line at the prompt now refreshes the commands it completes through ForgeCli.runLine, which keeps those it had when the refresh fails, and a spec runs it. --- grails-doc/src/en/guide/commandLine.adoc | 15 +-- .../org/grails/forge/cli/ForgeCli.groovy | 22 ++++- .../forge/cli/command/ProjectCommands.groovy | 84 +++++++++++++---- .../forge/cli/ProjectCommandsSpec.groovy | 94 ++++++++++++++++++- 4 files changed, 184 insertions(+), 31 deletions(-) diff --git a/grails-doc/src/en/guide/commandLine.adoc b/grails-doc/src/en/guide/commandLine.adoc index 60bdeedf1cc..11fbfe94690 100644 --- a/grails-doc/src/en/guide/commandLine.adoc +++ b/grails-doc/src/en/guide/commandLine.adoc @@ -237,13 +237,16 @@ files decide them: `.toml` files, such as `build.gradle`, `settings.gradle`, `gradle.properties` and `libs.versions.toml`; * everything under the `gradle` and `buildSrc` directories of the build's root, under `grails-app/commands` and `src/main/resources/META-INF` of every project, and under every included - build, apart from what builds and tools write next to a build script, such as `build`, `.gradle` and - `.kotlin`. + build, also one included with `pluginManagement { includeBuild }`, apart from what builds and tools + write outside a `src` or `grails-app` directory, such as `build`, `.gradle` and `.kotlin`, and from + `node_modules`; +* the source file of each command it lists that one of the projects of the build compiles, such as a + command of a plugin project in `src/main/groovy`. Each time it runs the task, the Forge CLI records those files, with their times and sizes, in the -project's `.gradle/grails` directory. It runs the task again when one of them is added, changed, replaced -or deleted, whatever time the file has, and when a manifest has no record, as one written by -`./gradlew generateCommandManifest` alone. So a plugin added to the build brings its commands the next +project's `.gradle/grails` directory. It runs the task again when one of them is added or deleted, or +its time or size changes, whether the time goes forward or back, and when a manifest has no record, as +one written by `./gradlew generateCommandManifest` alone. So a plugin added to the build brings its commands the next time a project command is asked for. A change the Forge CLI cannot see, such as a new snapshot of a plugin, is picked up by running `./gradlew generateCommandManifest`. @@ -262,7 +265,7 @@ wrong. A command of the file that the Forge CLI cannot offer, such as one declar `help`, is reported and left out, and the others are offered. The build runs through the Gradle wrapper of the project, or of the build it belongs to: the wrapper in -the first directory above it with a `settings.gradle` file. A project whose build has no wrapper has no +the first directory above it with a `settings.gradle` or `settings.gradle.kts` file. A project whose build has no wrapper has no commands offered, and no `gradle` found on the path is run instead. A command that describes its arguments and options, as xref:commandLine#creatingCustomCommands[Creating Custom Commands] diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/ForgeCli.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/ForgeCli.groovy index aa43f5fffeb..692f8eb1b22 100644 --- a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/ForgeCli.groovy +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/ForgeCli.groovy @@ -21,6 +21,7 @@ package org.grails.forge.cli import java.util.function.BiFunction import java.util.function.Supplier import groovy.transform.CompileStatic +import groovy.transform.PackageScope import org.springframework.context.ApplicationContext import org.springframework.context.ConfigurableApplicationContext import picocli.CommandLine @@ -153,12 +154,27 @@ final class ForgeCli { // of the manifest as it is, read quietly, since a problem with it was reported already CommandLine[] offered = [commandLine] as CommandLine[] Supplier current = { -> offered[0] } as Supplier - new InteractiveShell(commandLine, current, { String[] commandArgs -> - execute(context, commandArgs) + new InteractiveShell(commandLine, current, { String[] commandArgs -> runLine(context, offered, commandArgs) }, + EXCEPTION_HANDLER, InteractiveShell.DEFAULT_PROMPT).start() + } + + /** + * Runs a line entered at the prompt, then makes the commands it offers those of the project's + * manifest as it is now. A problem reading them keeps those offered before, so the prompt goes on. + * + * @param offered holds the command line the prompt completes from + */ + @PackageScope + static void runLine(ApplicationContext context, CommandLine[] offered, String[] commandArgs) { + execute(context, commandArgs) + try { CommandLine refreshed = createForgeCommandLine(context, true) addProjectCommands(refreshed, false, ConsoleOutput.NOOP) offered[0] = refreshed - }, EXCEPTION_HANDLER, InteractiveShell.DEFAULT_PROMPT).start() + } + catch (RuntimeException ignored) { + // the next line that runs reports it + } } /** diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy index a4ef0e6da60..54138597bf0 100644 --- a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy @@ -33,7 +33,7 @@ import org.grails.forge.io.ConsoleOutput * plugins, which {@code runCommand} runs. * *

The project's build describes them in a manifest, written by the Grails Gradle plugin's - * {@code generateCommandManifest} task, which also names the directories whose files decide the + * {@code generateCommandManifest} task, which also names the directories and files that decide the * commands. Each time the build runs, the files of those directories are recorded, with their times and * sizes; the manifest is read as long as they are unchanged, and otherwise the task is run first, so a * plugin added to the build brings its commands with it. When the build fails, the commands of the last @@ -65,10 +65,14 @@ class ProjectCommands { private static final List BUILD_FILE_SUFFIXES = ['.gradle', '.gradle.kts', '.properties', '.toml'] - private static final List BUILD_SCRIPTS = ['build.gradle', 'build.gradle.kts', 'settings.gradle', 'settings.gradle.kts'] + /** What builds and tools write, rather than read, outside a directory of sources */ + private static final List OUTPUT_DIRS = ['build', '.gradle', '.kotlin', 'out', 'bin', '.git', '.idea', '.settings'] - /** What builds and tools write next to a build script, rather than read */ - private static final List OUTPUT_DIRS = ['build', '.gradle', '.kotlin', 'out', 'bin', '.git', '.idea', 'node_modules'] + /** Directories whose files are sources, so a package below them named like an output is read */ + private static final List SOURCE_DIRS = ['src', 'grails-app'] + + /** What tools write anywhere, sources included */ + private static final List TOOL_FILES = ['node_modules', '.DS_Store'] private final File projectDir private final ProjectGradle gradle @@ -127,7 +131,7 @@ class ProjectCommands { private void update(File manifest, ConsoleOutput console) { // the files are recorded before the build runs, so one changed while it runs counts next time Inputs before = inputsOf(manifest) - List recorded = snapshot(before) + List recorded = snapshotOrNull(before) console.out('Reading the commands of the project from its build...') String problem try { @@ -142,7 +146,7 @@ class ProjectCommands { } if (problem == null) { Inputs after = inputsOf(manifest) - save(after == before ? recorded : snapshot(after), null, console) + save(after == before ? recorded : snapshotOrNull(after), null, console) return } save(recorded, problem, console) @@ -159,6 +163,11 @@ class ProjectCommands { private void save(List recorded, String problem, ConsoleOutput console) { File record = new File(projectDir, RECORD_PATH) File failed = new File(projectDir, FAILED_PATH) + if (recorded == null) { + console.warning('Could not read the files the commands of the project depend on. The build is run for ' + + 'them again next time.') + return + } try { Files.createDirectories(record.parentFile.toPath()) Files.write(record.toPath(), recorded.collect { String line -> line + '\n' }.join('').getBytes('UTF-8')) @@ -239,7 +248,7 @@ class ProjectCommands { // cannot be told from one that was never there return !record.isFile() || Files.readAllLines(record.toPath()) != snapshot(inputsOf(manifest)) } - catch (IOException ignored) { + catch (IOException | RuntimeException ignored) { return true } } @@ -254,10 +263,12 @@ class ProjectCommands { Object parsed = new JsonSlurper().parse(manifest, 'UTF-8') Object inputs = parsed instanceof Map ? ((Map) parsed).inputs : null if (inputs instanceof Map) { - List buildDirectories = files(((Map) inputs).buildDirectories) - List sourceDirectories = files(((Map) inputs).sourceDirectories) - if (buildDirectories != null && sourceDirectories != null) { - return new Inputs(buildDirectories, sourceDirectories) + Map given = (Map) inputs + List buildDirectories = files(given.buildDirectories) + List sourceDirectories = files(given.sourceDirectories) + List sourceFiles = given.containsKey('sourceFiles') ? files(given.sourceFiles) : [] + if (buildDirectories != null && sourceDirectories != null && sourceFiles != null) { + return new Inputs(buildDirectories, sourceDirectories, sourceFiles) } } } @@ -268,14 +279,31 @@ class ProjectCommands { File rootDir = gradle.rootDir new Inputs([projectDir, rootDir].unique(), [new File(rootDir, 'gradle'), new File(rootDir, 'buildSrc'), - new File(projectDir, 'grails-app/commands'), new File(projectDir, 'src/main/resources/META-INF')]) + new File(projectDir, 'grails-app/commands'), new File(projectDir, 'src/main/resources/META-INF')], []) } + /** The files of a list of absolute paths, or none when one is not such a path, such as a mistyped one */ private static List files(Object paths) { - if (!(paths instanceof List) || ((List) paths).any { !(it instanceof String) }) { + if (!(paths instanceof List)) { return null } - ((List) paths).collect { String path -> new File(path) } + List files = [] + for (Object path : (List) paths) { + if (!(path instanceof String) || !absolute((String) path)) { + return null + } + files << new File((String) path) + } + files + } + + private static boolean absolute(String path) { + try { + return Path.of(path).absolute && Path.of(path).nameCount > 0 + } + catch (RuntimeException ignored) { + return false + } } /** @@ -299,12 +327,26 @@ class ProjectCommands { } } for (File dir : inputs.sourceDirectories) { - walk(dir, lines) + // the sources of commands are sources from the start; a tree of a build becomes so below src + walk(dir, lines, dir.name == 'META-INF' || (dir.name == 'commands' && dir.parentFile?.name == 'grails-app')) + } + for (File file : inputs.sourceFiles) { + walk(file, lines, true) } lines } - private static void walk(File file, List lines) { + /** The record of the files, or none when they cannot be read, such as one of a path the system refuses */ + private static List snapshotOrNull(Inputs inputs) { + try { + return snapshot(inputs) + } + catch (RuntimeException ignored) { + return null + } + } + + private static void walk(File file, List lines, boolean inSources) { Path path = file.toPath() if (Files.isSymbolicLink(path)) { // not followed, so a link to a directory above it cannot loop @@ -324,10 +366,10 @@ class ProjectCommands { return } lines << "directory\t${file.absolutePath}".toString() - boolean buildRoot = file.name == 'buildSrc' || BUILD_SCRIPTS.any { String script -> new File(file, script).isFile() } for (File child : children.sort(false) { File it -> it.name }) { - if (!(buildRoot && child.name in OUTPUT_DIRS && child.isDirectory())) { - walk(child, lines) + boolean output = !inSources && child.name in OUTPUT_DIRS && child.isDirectory() + if (!output && !(child.name in TOOL_FILES)) { + walk(child, lines, inSources || child.name in SOURCE_DIRS) } } } @@ -348,10 +390,12 @@ class ProjectCommands { final List buildDirectories final List sourceDirectories + final List sourceFiles - Inputs(List buildDirectories, List sourceDirectories) { + Inputs(List buildDirectories, List sourceDirectories, List sourceFiles) { this.buildDirectories = buildDirectories this.sourceDirectories = sourceDirectories + this.sourceFiles = sourceFiles } } } diff --git a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy index 195fcafa9cb..3f712691b70 100644 --- a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy +++ b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy @@ -170,7 +170,7 @@ class ProjectCommandsSpec extends Specification { file.parentFile.mkdirs() file.text = 'a=1' break - case 'changed with its time kept': + case 'changed in size with its time kept': file.text = 'a=12' file.setLastModified(recorded) break @@ -193,7 +193,7 @@ class ProjectCommandsSpec extends Specification { gradleInvocations().size() == 1 where: - [path, change] << [WATCHED_FILES, ['added', 'changed with its time kept', 'replaced by an older copy', 'deleted']] + [path, change] << [WATCHED_FILES, ['added', 'changed in size with its time kept', 'replaced by an older copy', 'deleted']] .combinations() .findAll { String path, String change -> !(path == 'build.gradle' && change in ['added', 'deleted']) } } @@ -339,6 +339,73 @@ class ProjectCommandsSpec extends Specification { gradleInvocations().size() == 2 } + void 'the source of a command the manifest names is watched, wherever it is'() { + given: + File source = new File(dir, 'plugins/audit/src/main/groovy/com/acme/AuditCommand.groovy') + source.parentFile.mkdirs() + source.text = 'class AuditCommand {}' + buildWritesWithInputs([buildDirectories: [dir.path], sourceDirectories: [], sourceFiles: [source.path]], URL_MAPPINGS_REPORT) + ForgeCli.createCommandLine(context, false) + forgetInvocations() + + when: 'the command gains an option' + source.text = 'class AuditCommand { /* @CommandOption */ }' + ForgeCli.createCommandLine(context, false) + + then: + gradleInvocations() == [prefix() + MANIFEST_TASK] + } + + void 'what tools write in an included build does not make the manifest outdated, wherever the build is'() { + given: 'an included build, below a directory named src, with a subproject that has no build file of its own' + File included = new File(dir, 'src/shared') + new File(included, 'lib/src/main/groovy/com/acme/build').mkdirs() + new File(included, 'lib/src/main/webapp').mkdirs() + new File(included, 'settings.gradle.kts').text = 'include("lib")' + buildWritesWithInputs([buildDirectories: [dir.path], sourceDirectories: [included.path]], URL_MAPPINGS_REPORT) + ForgeCli.createCommandLine(context, false) + forgetInvocations() + + when: + ['lib/build/classes/A.class', 'lib/.gradle/probe', '.gradle/probe', '.kotlin/sessions/a', 'lib/bin/A.class', + 'node_modules/.cache/x', 'lib/src/main/webapp/node_modules/.cache/x', '.DS_Store', 'lib/.settings/prefs'].each { String path -> + File written = new File(included, path) + written.parentFile.mkdirs() + written.text = 'written' + } + ForgeCli.createCommandLine(context, false) + + then: + gradleInvocations() == [] + + when: 'a source of the included build changes, in a package named build' + new File(included, 'lib/src/main/groovy/com/acme/build/Plugin.groovy').text = 'class Plugin {}' + ForgeCli.createCommandLine(context, false) + + then: + gradleInvocations().size() == 1 + } + + void 'a manifest naming #what as an input is read with the inputs of the project and its build instead'() { + given: + buildWritesWithInputs([buildDirectories: paths, sourceDirectories: []], URL_MAPPINGS_REPORT) + + when: + CommandLine first = ForgeCli.createCommandLine(context, false) + ForgeCli.createCommandLine(context, false) + + then: + first.subcommands.containsKey('url-mappings-report') + gradleInvocations().size() == 1 + + where: + what | paths + 'a relative path' | ['relative/dir'] + 'the root' | ['/'] + 'an empty path' | [''] + 'a path with NUL' | ['/tmp/a\u0000b'] + } + void 'a link that loops is recorded, not followed'() { given: File source = new File(dir, 'buildSrc/src/main/groovy') @@ -758,6 +825,29 @@ class ProjectCommandsSpec extends Specification { commandLine.subcommands.containsKey('create-app') } + void 'after a line that runs the build, the prompt completes the commands the build found'() { + given: 'the prompt started with the commands of the last manifest' + writeManifest(URL_MAPPINGS_REPORT) + CommandLine[] offered = [ForgeCli.createCommandLine(context, true)] as CommandLine[] + assert !complete(offered[0], 's2-q').contains('s2-quickstart') + buildWrites(URL_MAPPINGS_REPORT, SECURITY_QUICKSTART) + new File(dir, 'build.gradle') << '// a plugin added\n' + + when: 'a line names a command the Forge CLI does not have, which runs the build' + ForgeCli.runLine(context, offered, ['s2-quickstart', '--help'] as String[]) + + then: + gradleInvocations()*.get(5) == ['generateCommandManifest'] + complete(offered[0], 's2-q').contains('s2-quickstart') + + when: 'a line runs no build' + ForgeCli.runLine(context, offered, ['--help'] as String[]) + + then: 'the commands stay those of the manifest' + complete(offered[0], 's2-q').contains('s2-quickstart') + gradleInvocations().size() == 1 + } + void 'the prompt completes project commands, their options and the values they declare'() { given: writeManifest(SECURITY_QUICKSTART, CREATE_TOKEN) From 13e4f858fbd4fa4cbf20619ea3f682bd7f4ad441 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Tue, 17 Nov 2026 19:49:25 -0800 Subject: [PATCH 066/115] Look for the sources of the listed commands in every source set A plugin keeps its commands in its cli source set, src/cli/groovy, which the search of the main source set missed, so the Spring Security commands of this repository had no source listed. --- .../gradle/plugin/commands/GrailsCliGradlePlugin.groovy | 5 +++-- .../commands/ApplicationCommandManifestTaskSpec.groovy | 4 ++-- .../test-projects/command-manifest/app/build.gradle | 5 +++++ .../app/src/{main => cli}/java/org/example/FakeCommand.java | 0 4 files changed, 10 insertions(+), 4 deletions(-) rename grails-gradle/plugins/src/test/resources/test-projects/command-manifest/app/src/{main => cli}/java/org/example/FakeCommand.java (100%) diff --git a/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/GrailsCliGradlePlugin.groovy b/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/GrailsCliGradlePlugin.groovy index 1cf46440ddf..e949a180540 100644 --- a/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/GrailsCliGradlePlugin.groovy +++ b/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/GrailsCliGradlePlugin.groovy @@ -768,9 +768,10 @@ class GrailsCliGradlePlugin implements Plugin { [new File(dir, 'grails-app/commands'), new File(dir, 'src/main/resources/META-INF')] } + includedBuildDirs) .collect { File dir -> dir.absolutePath }.unique().sort() + // every source set, as a plugin keeps its commands in its cli source set, src/cli/groovy List commandSourceRoots = root.allprojects.collectMany { Project each -> - SourceSet main = each.extensions.findByType(org.gradle.api.tasks.SourceSetContainer)?.findByName('main') - main ? main.allSource.srcDirs.collect { File dir -> dir.absolutePath } : [] + def sourceSets = each.extensions.findByType(org.gradle.api.tasks.SourceSetContainer) + sourceSets ? sourceSets.collectMany { SourceSet set -> set.allSource.srcDirs*.absolutePath } : [] }.unique().sort() manifestTask.configure { it.buildDirectories.set(buildDirectories) diff --git a/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTaskSpec.groovy b/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTaskSpec.groovy index 99098a4e87c..c1439d60c39 100644 --- a/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTaskSpec.groovy +++ b/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTaskSpec.groovy @@ -103,8 +103,8 @@ class ApplicationCommandManifestTaskSpec extends GradleSpecification { and: 'every included build, also those that bring plugins of the build' inputs.sourceDirectories.collect { new File(it).canonicalFile }.containsAll([new File(root, 'shared'), new File(root, 'build-logic')]) - and: 'the sources of the listed commands found in the projects of the build' - inputs.sourceFiles.collect { new File(it).canonicalFile } == [new File(root, 'app/src/main/java/org/example/FakeCommand.java')] + and: 'the sources of the listed commands found in the projects of the build, in any source set' + inputs.sourceFiles.collect { new File(it).canonicalFile } == [new File(root, 'app/src/cli/java/org/example/FakeCommand.java')] } void 'the task runs only in the project the grailsCommandProject property names'() { diff --git a/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/app/build.gradle b/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/app/build.gradle index 178e89eb151..516571119a4 100644 --- a/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/app/build.gradle +++ b/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/app/build.gradle @@ -3,6 +3,11 @@ plugins { id 'org.apache.grails.gradle.grails-cli' } +// commands of a plugin live in its cli source set +sourceSets { + cli +} + dependencies { grailsCli project(':manifest-writer') } diff --git a/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/app/src/main/java/org/example/FakeCommand.java b/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/app/src/cli/java/org/example/FakeCommand.java similarity index 100% rename from grails-gradle/plugins/src/test/resources/test-projects/command-manifest/app/src/main/java/org/example/FakeCommand.java rename to grails-gradle/plugins/src/test/resources/test-projects/command-manifest/app/src/cli/java/org/example/FakeCommand.java From 63b699adf429ae2a8998afe0ab1e26dfcb62ca3e Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Tue, 17 Nov 2026 20:35:09 -0800 Subject: [PATCH 067/115] Add JLine 4 to the Grails 9 upgrade skill The upgrade guide's JLine 4 section had no counterpart in the skill: the removed APIs to replace, the JNA and Jansi terminal modules to drop, and the picocli bridge to move to, with a checklist item. --- .../upgrade-guide-9/skills/grails-9-upgrade/SKILL.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/grails-skills/upgrade-guide-9/skills/grails-9-upgrade/SKILL.md b/grails-skills/upgrade-guide-9/skills/grails-9-upgrade/SKILL.md index 2e34d4fb8f9..6e6da66f5a8 100644 --- a/grails-skills/upgrade-guide-9/skills/grails-9-upgrade/SKILL.md +++ b/grails-skills/upgrade-guide-9/skills/grails-9-upgrade/SKILL.md @@ -466,6 +466,14 @@ With a database or a schema per tenant, a domain class that does not implement ` - With GORM for MongoDB, documents of such a class saved inside a tenant block in Grails 8 are in the tenant's database, which Grails 9 no longer reads for it. A class whose instances belong to one tenant implements `MultiTenant` before the upgrade. A class that a multi-tenant class references is loaded from the tenant's database, so it implements `MultiTenant` too. Only shared instances that no multi-tenant class references move to the default database. - A data service compiled against an earlier release keeps following the tenant until it is compiled again. +## JLine 4 + +Grails 9 manages JLine 4 (`org.jline:jline`), the line Groovy 6's `groovysh` is built on, in place of JLine 3. It is the terminal library behind the prompts of the `grails` command line tools and `grails shell`, and behind `GrailsConsole`. The `org.jline` packages are unchanged. + +- Recompile code that uses JLine, such as an `org.jline.reader.Completer` of a command, against JLine 4, which removed APIs JLine 3 had deprecated: use `org.jline.builtins.Completers.FileNameCompleter` for `org.jline.reader.impl.completer.FileNameCompleter`, and `Terminal.inputEncoding()`/`outputEncoding()` for `stdinEncoding()`, `stdoutEncoding()` and `stderrEncoding()`; `TerminalBuilder.codepage(int)`, `PROP_JNA` and `PROP_JANSI` are gone. +- Remove dependencies on `org.jline:jline-terminal-jna` and `org.jline:jline-terminal-jansi`, which have no JLine 4 version. +- `info.picocli:picocli-shell-jline3` still runs, but its `PicocliCommands` implements the deprecated `org.jline.console.CommandRegistry`; `org.jline:jline-picocli`, at the JLine version the Grails BOM manages, is the JLine 4 bridge to move to. + ## Verification Checklist - [ ] `gradle.properties` names the Grails 9 version and the wrapper is Gradle 9.8 or later @@ -497,6 +505,7 @@ With a database or a schema per tenant, a domain class that does not implement ` - [ ] No typed read with a default, such as `getProperty('x.y.enabled', Boolean, true)`, names a key below a configuration value that is not a map - [ ] With a database or a schema per tenant, every domain class saved inside a tenant block whose instances belong to a tenant, or that a multi-tenant class references, implements `MultiTenant` - [ ] Every write of a class that is not multi-tenant inside a tenant's transaction runs in the class's data service or its own `withTransaction`, or uses `flush: true` +- [ ] Code that uses JLine compiles against JLine 4, and no build file declares `org.jline:jline-terminal-jna` or `org.jline:jline-terminal-jansi` - [ ] The full test suite passes on Groovy 6 ## Pitfalls to Avoid From 912f66dcefc540354df8608e92d96d0320c8ee80 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Wed, 18 Nov 2026 07:19:48 -0800 Subject: [PATCH 068/115] Keep the build's environment and skip what unnamed command tasks depend on Reading the system properties among a command's arguments set grails.env in the JVM that configures the build, since the command line the parser fills sets it for the environment it reads. Every task configured after it took that environment, and in the plugin's tests the test JVM kept it, which failed GroovyPageCompileEnvironmentFunctionalSpec in the same fork. The arguments are now read into a command line that keeps the environment to itself. A command task of a project the grailsCommandProject property does not name was skipped, but the tasks it depends on still ran, so a project below the named one that does not compile failed the named one's command and its command list. Such a task now depends on nothing and has no classpath. Also put the Javadoc of requireMainClass back on it, and link the guide's new cross references so they stay on the single-page guide. --- grails-doc/src/en/guide/commandLine.adoc | 2 +- .../commandLine/creatingCustomCommands.adoc | 7 +- .../src/en/guide/introduction/whatsNew.adoc | 4 +- .../commands/GrailsCliGradlePlugin.groovy | 65 +++++++++++++------ .../ApplicationCommandManifestTaskSpec.groovy | 35 ++++++++++ .../commands/CommandArgumentsSpec.groovy | 41 ++++++++++++ 6 files changed, 129 insertions(+), 25 deletions(-) diff --git a/grails-doc/src/en/guide/commandLine.adoc b/grails-doc/src/en/guide/commandLine.adoc index 11fbfe94690..220d267ad0c 100644 --- a/grails-doc/src/en/guide/commandLine.adoc +++ b/grails-doc/src/en/guide/commandLine.adoc @@ -268,7 +268,7 @@ The build runs through the Gradle wrapper of the project, or of the build it bel the first directory above it with a `settings.gradle` or `settings.gradle.kts` file. A project whose build has no wrapper has no commands offered, and no `gradle` found on the path is run instead. -A command that describes its arguments and options, as xref:commandLine#creatingCustomCommands[Creating Custom Commands] +A command that describes its arguments and options, as <> shows, has them checked and completed. A command that declares no options takes any arguments and options, and is given the words of its command line in the order they were typed, as `runCommand` gives them. diff --git a/grails-doc/src/en/guide/commandLine/creatingCustomCommands.adoc b/grails-doc/src/en/guide/commandLine/creatingCustomCommands.adoc index e74ff3b60d5..e459186c0ce 100644 --- a/grails-doc/src/en/guide/commandLine/creatingCustomCommands.adoc +++ b/grails-doc/src/en/guide/commandLine/creatingCustomCommands.adoc @@ -234,7 +234,8 @@ the test environment, and the command also finds it in the system properties of In a build of several projects, `./gradlew runCommand` from the root runs the task in every project that has it. Name one project's directory, absolute or relative to the root of the build, with the `grailsCommandProject` property to run it there only; the other command tasks and -`generateCommandManifest` take it too: +`generateCommandManifest` take it too. In the other projects the task is skipped together with the +tasks it would depend on, so a project that does not compile leaves the command of another alone: [source,shell] ---- @@ -248,7 +249,7 @@ This allows you to create more versatile and interactive custom commands for you A command can describe the arguments and options it takes with the link:{api}org/apache/grails/core/cli/CommandArgument.html[CommandArgument] and link:{api}org/apache/grails/core/cli/CommandOption.html[CommandOption] annotations. Tools that offer the -commands of a project, such as the xref:commandLine#projectCommands[Grails Forge CLI], show them in the +commands of a project, such as the <>, show them in the command's help, check them and complete them. Declare one `@CommandArgument` per argument, in the order the arguments are given, and one `@CommandOption` per option: @@ -462,7 +463,7 @@ dependencies { ./gradlew auditReport ---- + -Inside the application, the xref:commandLine#projectCommands[Grails Forge CLI] offers and completes the +Inside the application, the <> offers and completes the command as well, and runs it through `runCommand`: + [source,shell] diff --git a/grails-doc/src/en/guide/introduction/whatsNew.adoc b/grails-doc/src/en/guide/introduction/whatsNew.adoc index fa59390f2a4..3d4dedcff44 100644 --- a/grails-doc/src/en/guide/introduction/whatsNew.adoc +++ b/grails-doc/src/en/guide/introduction/whatsNew.adoc @@ -388,8 +388,8 @@ the new `@CommandArgument` and `@CommandOption` annotations, which the Forge CLI help, checks and completes. Commands without them, such as those of plugins built for an earlier release, are offered too and take any arguments. -See xref:commandLine#projectCommands[Commands of the Project] and -xref:commandLine#creatingCustomCommands[Creating Custom Commands]. +See <> and +<>. ==== GSP Tag Library Improvements diff --git a/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/GrailsCliGradlePlugin.groovy b/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/GrailsCliGradlePlugin.groovy index e949a180540..76c7fef69ca 100644 --- a/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/GrailsCliGradlePlugin.groovy +++ b/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/GrailsCliGradlePlugin.groovy @@ -413,17 +413,6 @@ class GrailsCliGradlePlugin implements Plugin { dependency } - /** - * Returns the application main class for a task that runs against the application, or fails the task with - * an explanation when the project has none, as is the case for a plugin without an {@code Application} class. - * - *

Task actions call it qualified with the class name: with the configuration cache, an action's closure - * does not keep its owner, so an unqualified call would be looked up on the task instead.

- * - * @param mainClass the main class found by the {@code findMainClass} task - * @param taskName the name of the task that requires the main class - * @return the name of the main class - */ /** * Returns the arguments a command task passes to its command: those of {@link #ARGS_PROPERTY}, split * as a shell would, then those of the file {@link #ARGS_FILE_PROPERTY} names, unchanged. @@ -492,8 +481,7 @@ class GrailsCliGradlePlugin implements Plugin { static Map commandSystemProperties(List arguments) { Map properties = new LinkedHashMap() try { - // a command line of its own, so the parser's current one is left alone - new CommandLineParser().parse(new DefaultCommandLine(), arguments as String[]).systemProperties.each { Object name, Object value -> + new CommandLineParser().parse(new BuildCommandLine(), arguments as String[]).systemProperties.each { Object name, Object value -> properties[name as String] = value as String } } @@ -505,18 +493,57 @@ class GrailsCliGradlePlugin implements Plugin { /** * Skips the task unless {@link #COMMAND_PROJECT_PROPERTY} is unset or names the directory of the - * task's project. + * task's project. A skipped task depends on nothing and has no classpath, so the build neither + * compiles nor resolves anything for it: a project below the named one that does not compile + * leaves the named one's command alone. + * + *

Call it after the task's classpath is set.

*/ @CompileDynamic static void runOnlyInNamedProject(org.gradle.api.Task task, Project project) { + if (isNamedCommandProject(project)) { + return + } + task.onlyIf("the ${COMMAND_PROJECT_PROPERTY} property names this project".toString()) { false } + task.setDependsOn([]) + if (task instanceof JavaExec) { + task.classpath = project.files() + } + } + + /** + * Returns whether the commands of the project run: when {@link #COMMAND_PROJECT_PROPERTY} is unset, + * or names the directory of the project, either absolute or relative to the root of the build. + */ + private static boolean isNamedCommandProject(Project project) { def named = project.findProperty(COMMAND_PROJECT_PROPERTY) - if (named) { - // a relative path is the directory below the root of the build - boolean selected = project.rootProject.file(named as String).canonicalFile == project.projectDir.canonicalFile - task.onlyIf("the ${COMMAND_PROJECT_PROPERTY} property names this project".toString()) { selected } + !named || project.rootProject.file(named as String).canonicalFile == project.projectDir.canonicalFile + } + + /** + * A command line that keeps the environment it reads to itself, where the command line the + * command reads sets {@code grails.env} on its JVM. Here that JVM is the one that configures the + * build, whose other tasks read {@code grails.env} for their own environment. + */ + @CompileStatic + private static class BuildCommandLine extends DefaultCommandLine { + + @Override + void setEnvironment(String environment) { } } + /** + * Returns the application main class for a task that runs against the application, or fails the task with + * an explanation when the project has none, as is the case for a plugin without an {@code Application} class. + * + *

Task actions call it qualified with the class name: with the configuration cache, an action's closure + * does not keep its owner, so an unqualified call would be looked up on the task instead.

+ * + * @param mainClass the main class found by the {@code findMainClass} task + * @param taskName the name of the task that requires the main class + * @return the name of the main class + */ @PackageScope static String requireMainClass(Provider mainClass, String taskName) { def mainClassName = mainClass.orNull @@ -745,7 +772,6 @@ class GrailsCliGradlePlugin implements Plugin { Directory projectDirectory = project.layout.projectDirectory manifestTask.configure { it.description = 'Writes the Grails commands of the application, which the Grails Forge CLI offers' - GrailsCliGradlePlugin.runOnlyInNamedProject(it, project) it.manifestFile.convention(project.providers.gradleProperty(COMMAND_MANIFEST_PROPERTY) .map { String path -> projectDirectory.file(path) } .orElse(project.layout.buildDirectory.file(COMMAND_MANIFEST_PATH))) @@ -754,6 +780,7 @@ class GrailsCliGradlePlugin implements Plugin { manifestTask.configure { SourceSet mainSourceSet = SourceSets.findMainSourceSet(project) it.classpath = mainSourceSet.runtimeClasspath + project.configurations.getByName(GRAILS_CLI_CLASSPATH_CONFIGURATION) + GrailsCliGradlePlugin.runOnlyInNamedProject(it, project) } } // every project of the build is known only once all are evaluated diff --git a/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTaskSpec.groovy b/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTaskSpec.groovy index c1439d60c39..6dbb613350a 100644 --- a/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTaskSpec.groovy +++ b/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTaskSpec.groovy @@ -127,6 +127,41 @@ class ApplicationCommandManifestTaskSpec extends GradleSpecification { new File(app, 'build/grails/commands.json').exists() } + void 'a project below the named one that does not compile leaves the named one alone'() { + given: 'a project below the application whose source does not compile' + def runner = setupTestResourceProject('command-manifest') + File root = runner.projectDir + new File(root, 'settings.gradle') << "\ninclude 'app:broken'\n" + File broken = new File(root, 'app/broken') + new File(broken, 'src/main/java/org/example').mkdirs() + new File(broken, 'build.gradle').text = """ + plugins { + id 'java' + id 'org.apache.grails.gradle.grails-cli' + } + """.stripIndent() + new File(broken, 'src/main/java/org/example/Broken.java').text = 'class Broken { not java }' + + when: 'the command list of the application is written by name, from its directory' + def manifest = executeTask('generateCommandManifest', ['-p', 'app', '-PgrailsCommandProject=app']) + + then: 'the project below is skipped, and nothing of it is compiled' + manifest.task(':app:generateCommandManifest').outcome == TaskOutcome.SUCCESS + manifest.task(':app:broken:generateCommandManifest').outcome == TaskOutcome.SKIPPED + !manifest.tasks*.path.contains(':app:broken:compileJava') + + when: 'a command of the application is planned the same way' + ['app', 'app/broken'].each { + new File(root, "${it}/build.gradle") << "\ntasks.register('findMainClass', org.grails.gradle.plugin.run.FindMainClassTask)\n" + } + def command = executeTask('runCommand', ['-m', '-p', 'app', '-PgrailsCommandProject=app']) + List planned = command.output.readLines().findAll { it.startsWith(':') }*.tokenize(' ')*.first() + + then: 'the project below brings only its own command task, which is skipped, and nothing it would depend on' + planned.containsAll([':app:compileJava', ':app:runCommand']) + planned.findAll { it.startsWith(':app:broken:') } == [':app:broken:runCommand'] + } + private static Map read(Path file) { (Map) new JsonSlurper().parse(file.toFile(), 'UTF-8') } diff --git a/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/CommandArgumentsSpec.groovy b/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/CommandArgumentsSpec.groovy index 527b155c29e..1f9aa176917 100644 --- a/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/CommandArgumentsSpec.groovy +++ b/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/CommandArgumentsSpec.groovy @@ -25,6 +25,7 @@ import org.gradle.api.Project import org.gradle.api.tasks.JavaExec import org.gradle.testfixtures.ProjectBuilder import spock.lang.Specification +import spock.util.environment.RestoreSystemProperties import spock.lang.TempDir class CommandArgumentsSpec extends Specification { @@ -123,6 +124,46 @@ class CommandArgumentsSpec extends Specification { task.actions.size() == actions } + @RestoreSystemProperties + void 'configuring a command task with #given leaves the environment of the build alone'() { + given: + System.setProperty('grails.env', 'development') + new File(projectDir, 'arguments.json').text = JsonOutput.toJson(given) + Project project = project(argsFile: 'arguments.json') + JavaExec task = project.tasks.register('commandTask', JavaExec).get() + + when: + GrailsCliGradlePlugin.commandTaskArguments(task, project) + task.actions.first().execute(task) + + then: 'the JVM that configures the build keeps its environment' + System.getProperty('grails.env') == 'development' + + and: 'the command JVM gets the one it names' + task.systemProperties == properties + + where: + given || properties + ['dbm-update', '-Dgrails.env=test'] || ['grails.env': 'test'] + ['test', 'dbm-update', '-Dother=value'] || [other: 'value'] + } + + void 'a command task of a project the grailsCommandProject property does not name depends on nothing'() { + given: + Project project = project(grailsCommandProject: new File(projectDir, 'other').absolutePath) + JavaExec task = project.tasks.register('commandTask', JavaExec).get() + task.dependsOn('classes') + task.classpath = project.files('classes') + + when: + GrailsCliGradlePlugin.commandTaskArguments(task, project) + + then: 'nothing is compiled or resolved for it' + task.dependsOn.empty + task.classpath.empty + !task.onlyIf.isSatisfiedBy(task) + } + void 'a relative grailsCommandProject is the directory below the root of the build'() { given: Project root = ProjectBuilder.builder().withProjectDir(projectDir).build() From 288b14b53c5699280415b9702c454d56cd9faab8 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Wed, 18 Nov 2026 07:23:28 -0800 Subject: [PATCH 069/115] Start the shell of the shell spec on a terminal with an empty input The spec started groovysh on the terminal of the test JVM, which waits for input when an IDE attaches its console. GroovyshApplicationContext now builds the shell's options in shellOptions(), which a subclass can add a terminal to, and the spec gives it a dumb terminal whose input is empty. --- .../support/GroovyshApplicationContext.groovy | 14 ++++++++++- .../GroovyshApplicationContextSpec.groovy | 23 +++++++++++++++---- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/grails-console/src/main/groovy/grails/ui/shell/support/GroovyshApplicationContext.groovy b/grails-console/src/main/groovy/grails/ui/shell/support/GroovyshApplicationContext.groovy index 01d7f3155d1..be3864f3e97 100644 --- a/grails-console/src/main/groovy/grails/ui/shell/support/GroovyshApplicationContext.groovy +++ b/grails-console/src/main/groovy/grails/ui/shell/support/GroovyshApplicationContext.groovy @@ -20,6 +20,7 @@ package grails.ui.shell.support import groovy.transform.CompileStatic +import org.apache.groovy.groovysh.GroovyshOptions import org.apache.groovy.groovysh.Main import org.springframework.context.support.GenericApplicationContext @@ -39,7 +40,18 @@ class GroovyshApplicationContext extends GenericApplicationContext { } protected void startConsole() { - Main.start([ + Main.start(shellOptions().build()) + } + + /** + * Returns the options the shell starts with: this context as {@code ctx} and the Grails application in + * its binding, on the terminal of the JVM. A subclass can add to them, for example a terminal of its own. + * + * @return the options of the shell + * @since 9.0 + */ + protected GroovyshOptions.Builder shellOptions() { + GroovyshOptions.builder().bindings([ ctx: this, (GrailsApplication.APPLICATION_ID): getBean(GrailsApplication) ]) diff --git a/grails-console/src/test/groovy/grails/ui/shell/support/GroovyshApplicationContextSpec.groovy b/grails-console/src/test/groovy/grails/ui/shell/support/GroovyshApplicationContextSpec.groovy index 9ac2f09dc0e..a854a78cb20 100644 --- a/grails-console/src/test/groovy/grails/ui/shell/support/GroovyshApplicationContextSpec.groovy +++ b/grails-console/src/test/groovy/grails/ui/shell/support/GroovyshApplicationContextSpec.groovy @@ -19,6 +19,9 @@ package grails.ui.shell.support import java.nio.file.Path +import org.apache.groovy.groovysh.GroovyshOptions +import org.jline.terminal.Terminal +import org.jline.terminal.TerminalBuilder import spock.lang.Specification import spock.lang.TempDir import spock.util.environment.RestoreSystemProperties @@ -41,18 +44,30 @@ class GroovyshApplicationContextSpec extends Specification { } void 'the shell starts with the application in its binding and ends when its input ends'() { - given: 'an application context with a Grails application, as grails shell starts it' - def context = new GroovyshApplicationContext() + given: 'a terminal whose input is empty, so the shell never waits for the console of an IDE' + ByteArrayOutputStream output = new ByteArrayOutputStream() + Terminal terminal = TerminalBuilder.builder().system(false).dumb(true) + .streams(new ByteArrayInputStream(new byte[0]), output).build() + + and: 'an application context with a Grails application, as grails shell starts it' + def context = new GroovyshApplicationContext() { + @Override + protected GroovyshOptions.Builder shellOptions() { + super.shellOptions().terminal(terminal) + } + } context.registerBean(GrailsApplication.APPLICATION_ID, GrailsApplication, { new DefaultGrailsApplication() }) - when: 'the context refreshes, which starts the shell on the test JVM terminal' + when: 'the context refreshes, which starts the shell' context.refresh() - then: 'the shell runs and returns without a JLine class missing from the class path' + then: 'the shell runs on that terminal and returns without a JLine class missing from the class path' noExceptionThrown() context.active + output.toString('UTF-8').contains('groovy> ') cleanup: context.close() + terminal.close() } } From 1e73d79acec05857cfa430dde21bbb5644c702ed Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Wed, 18 Nov 2026 07:23:28 -0800 Subject: [PATCH 070/115] Name options in camelCase in the CommandOption example The Spring Security commands and the guide name options in camelCase, so the Javadoc example does too, and the s2-quickstart Javadoc names its uiOnly option as the command declares it. --- .../groovy/org/apache/grails/core/cli/CommandOption.groovy | 4 ++-- .../org/apache/grails/security/cli/S2QuickstartCommand.groovy | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandOption.groovy b/grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandOption.groovy index e680a0cb0cf..68fdf718310 100644 --- a/grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandOption.groovy +++ b/grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandOption.groovy @@ -32,8 +32,8 @@ import java.lang.annotation.Target *

An option is given as {@code --name=value}, or as {@code --name} when it is a {@link #flag()}:

* *
- * @CommandOption(name = 'group-class-name', description = 'Also generate role group classes with this name')
- * @CommandOption(name = 'ui-only', flag = true, description = 'Configure the plugin without generating classes')
+ * @CommandOption(name = 'groupClassName', description = 'Also generate role group classes with this name')
+ * @CommandOption(name = 'uiOnly', flag = true, description = 'Configure the plugin without generating classes')
  * class SetupUsersCommand implements GrailsApplicationCommand { ... }
  * 
* diff --git a/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2QuickstartCommand.groovy b/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2QuickstartCommand.groovy index a2d5b0ed5a4..2da1c1971e9 100644 --- a/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2QuickstartCommand.groovy +++ b/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2QuickstartCommand.groovy @@ -26,7 +26,7 @@ import groovy.transform.CompileStatic /** * Creates domain classes and updates config settings for the Spring Security plugin. * Usage: ./grailsw s2-quickstart [DOMAIN_CLASS_PACKAGE] [USER_CLASS_NAME] [ROLE_CLASS_NAME] [REQUEST_MAP_CLASS_NAME] --groupClassName=[GROUP_CLASS_NAME] or - * s2-quickstart --ui-only + * ./grailsw s2-quickstart --uiOnly * * For Example: * 1. ./grailsw s2-quickstart com.yourapp User Role --groupClassName=RoleGroup From 58f87ff3af9f60e27bc0513955224a6085163fcd Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Wed, 18 Nov 2026 07:33:21 -0800 Subject: [PATCH 071/115] Pass a command's arguments in a file only its owner can read The Forge CLI wrote the arguments of a project command to a temporary file created with the permissions of the umask, usually readable by everyone, and the arguments of a dbm-* command can hold a password. The file is now created with Files.createTempFile, readable and writable by its owner only on a POSIX file system. --- .../forge/cli/command/ProjectCommand.groovy | 12 ++++++++---- .../grails/forge/cli/ProjectCommandsSpec.groovy | 16 +++++++++++++++- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommand.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommand.groovy index a0a54c799e0..c5236152905 100644 --- a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommand.groovy +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommand.groovy @@ -18,6 +18,9 @@ */ package org.grails.forge.cli.command +import java.nio.charset.StandardCharsets +import java.nio.file.Files +import java.nio.file.Path import java.util.concurrent.Callable import groovy.json.JsonOutput @@ -92,13 +95,14 @@ class ProjectCommand extends BaseCommand implements Callable { Integer call() throws Exception { List commandArguments = [name] commandArguments.addAll(givenArguments()) - File argumentsFile = File.createTempFile('grails-command-arguments', '.json') + // only its owner can read it, where the file system allows that, since the arguments can hold a password + Path argumentsFile = Files.createTempFile('grails-command-arguments', '.json') try { - argumentsFile.setText(JsonOutput.toJson(commandArguments), 'UTF-8') - return gradle.run([RUN_COMMAND_TASK, "-P${ARGS_FILE_PROPERTY}=${argumentsFile.absolutePath}".toString()]) + Files.writeString(argumentsFile, JsonOutput.toJson(commandArguments), StandardCharsets.UTF_8) + return gradle.run([RUN_COMMAND_TASK, "-P${ARGS_FILE_PROPERTY}=${argumentsFile.toAbsolutePath()}".toString()]) } finally { - argumentsFile.delete() + Files.deleteIfExists(argumentsFile) } } diff --git a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy index 3f712691b70..9490cf2e9b2 100644 --- a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy +++ b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy @@ -582,6 +582,18 @@ class ProjectCommandsSpec extends Specification { commandArguments() == ['url-mappings-report'] + arguments } + void 'the file that passes the arguments, which can hold a password, is readable by its owner only, and removed after the command'() { + given: + writeManifest(URL_MAPPINGS_REPORT) + + when: + ForgeCli.execute(context, ['url-mappings-report', 'secret'] as String[]) + + then: + gradleInvocations().last().contains('argsFileMode:-rw-------') + !new File(argumentsFile()).exists() + } + void '-D sets a system property of the command, passed among its arguments as runCommand reads it, not on the command line of Gradle'() { given: writeManifest(SECURITY_QUICKSTART, URL_MAPPINGS_REPORT) @@ -1015,7 +1027,9 @@ class ProjectCommandsSpec extends Specification { for argument in "$@"; do [ "$previous" = '-p' ] && project=$argument; previous=$argument; done { for argument in "$@"; do printf '%s\\n' "$argument" - case "$argument" in -PargsFile=*) printf 'argsFile:%s\\n' "$(cat "${argument#-PargsFile=}")";; esac + case "$argument" in -PargsFile=*) + printf 'argsFile:%s\\n' "$(cat "${argument#-PargsFile=}")" + printf 'argsFileMode:%s\\n' "$(ls -l "${argument#-PargsFile=}" | cut -c1-10)";; esac done; echo '--'; } >> "$dir/gradle-invocations.txt" case " $* " in *" generateCommandManifest "*) From e37d18e3954738c1bc76e2bd0ae83ef4d8fc7615 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Wed, 18 Nov 2026 07:33:21 -0800 Subject: [PATCH 072/115] Let GRAILS_FORGE_RUN_BUILD=false keep the Forge CLI from running the build Inside a Grails project the Forge CLI runs the project's build on its own to learn its commands, when the prompt starts and when a command it does not have is named, a typo included. That runs code of the checkout, which a developer may not expect of a code generator. With the environment variable GRAILS_FORGE_RUN_BUILD, or the system property grails.forge.runBuild, set to false, it offers the commands of the manifest the build last wrote and runs the build only for a project command the user names. The guide and the threat model say so. --- THREAT_MODEL.md | 2 ++ grails-doc/src/en/guide/commandLine.adoc | 11 +++++++++ .../org/grails/forge/cli/ForgeCli.groovy | 20 ++++++++++++++++ .../forge/cli/ProjectCommandsSpec.groovy | 24 +++++++++++++++++++ 4 files changed, 57 insertions(+) diff --git a/THREAT_MODEL.md b/THREAT_MODEL.md index f6be2914680..b2e17c4169c 100644 --- a/THREAT_MODEL.md +++ b/THREAT_MODEL.md @@ -65,6 +65,7 @@ The framework is large; not every module has the same threat profile. The model | Compile-time AST transforms | [`GlobalGrailsClassInjectorTransformation`](./grails-core/src/main/groovy/org/grails/compiler/injection/GlobalGrailsClassInjectorTransformation.groovy), [`ResourceTransform`](./grails-rest-transforms/src/main/groovy/org/grails/plugins/web/rest/transform/ResourceTransform.groovy), [`ViewsTransform`](./grails-views-core/src/main/groovy/grails/views/compiler/ViewsTransform.groovy) | No (build only) | **In** (developer-trusted inputs only - see §6) | | CLI / shell (`grails-shell-cli`) | [`GrailsCli`](./grails-shell-cli/src/main/groovy/org/grails/cli/GrailsCli.groovy), [`MavenProfileRepository`](./grails-shell-cli/src/main/groovy/org/grails/cli/profile/repository/MavenProfileRepository.groovy) | Yes - network (Maven), filesystem | **In** (with caveats - see §3 and §7) | | Interactive consoles (`grails-console`) | [`GrailsShell`](./grails-console/src/main/groovy/grails/ui/shell/GrailsShell.groovy), `GroovyConsoleApplicationContext` | Local I/O | **Out** - developer-only tool, see §3 | +| Forge CLI (`grails-forge-cli`) project commands | [`ProjectCommands`](./grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy), [`ProjectGradle`](./grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectGradle.groovy) | Yes - runs the Gradle wrapper of the project in the current directory | **Out** - developer-only tool; the project's build is code the developer trusts, as when running `./gradlew`. See §11. | | Hosted `grails-forge` HTTP APIs | [`ZipCreateController`](./grails-forge/grails-forge-web/grails-app/controllers/org/grails/forge/web/ZipCreateController.groovy) and sibling controllers | Yes - network | **Out** - separate Grails 8 web application deployed independently from user applications built with Grails; modeled separately by the Forge service operators. *(inferred)* | | Generated application scaffolding | `grails-profiles/`, files emitted by `create-app` | n/a | **Out** - separately authored; the framework's contract does not extend to scaffolded code once it is in the user's repository. See §3. | | `grails-test-examples/`, demo apps | n/a | n/a | **Out** - test/demo code, not shipped. See §3. | @@ -386,6 +387,7 @@ In-the-wild patterns the API permits but that violate the assumptions in §5-§7 - **Exposing `GrailsShell`, `GroovyConsole`, or any Spring Boot Actuator endpoint that evaluates Groovy** on a network-reachable interface. These modules provide unrestricted code execution by design (§3). Reaching them from an HTTP listener is the operator's mistake, but the application's the one that loses. *Fix*: bind those endpoints to localhost or disable them in production; do not configure the embedded container to expose `grails-console` artefacts. - **Treating constraint validation as authorization.** A constraint passing does not mean the user is permitted to perform the operation. A `nullable: false` field will still accept an attacker-supplied value if the attacker is authenticated and the action allows them to submit it. *Fix*: keep authz separate from validation; run authz checks at the action entry, validation at the binding boundary, and never confuse "validates" with "authorized". *(inferred)* - **Liquibase Groovy change-log files loaded from a path the developer does not fully control.** `GroovyChangeLogParser` in `grails-data-hibernate5/dbmigration` evaluates change-log Groovy via `GroovyShell.parse(...).run()`. If the change-log location is influenced by a CI/CD parameter, an artifact registry, or any externally-writable path, this is full Groovy code execution at application startup. *Fix*: keep migration files in the application's own source tree under version control; never source change-log paths from runtime input. *(inferred)* +- **Starting the Grails Forge CLI inside a checkout that is not trusted.** Inside a Grails project, the Forge CLI runs the project's Gradle wrapper and build on its own to learn the project's commands: when its interactive prompt starts, and when a command line names a command it does not have, a typo included. The build is code of the checkout, so this runs it as `./gradlew` would, which is what a developer may not expect of a code generator. *Fix*: set the environment variable `GRAILS_FORGE_RUN_BUILD` (or the system property `grails.forge.runBuild`) to `false`; the Forge CLI then runs the build only for a project command the user names. *(documented: [commandLine.adoc](./grails-doc/src/en/guide/commandLine.adoc))* - **Putting secrets in HTML comments inside GSP templates.** They reach the client. *Fix*: use GSP comments (`<%-- … --%>`) or omit. *(inferred)* - **Using `params.id` directly in a GORM `get(id)` call without an authorization check.** The "guessable IDs" pattern. Returns objects the requesting user has no business viewing or mutating. *Fix*: authorize the loaded object against the authenticated principal before returning, rendering, or mutating it - via the Spring Security plugin's ACL support, a per-action interceptor, or an explicit `if (book.owner != currentUser) { notFound(); return }`. *(documented: [securingAgainstAttacks.adoc](./grails-doc/src/en/guide/security/securingAgainstAttacks.adoc) "Guessable IDs")* - **Trusting `MultipartFile.originalFilename`** as a destination filename. The value is attacker-controlled and may include `../`, NUL bytes, or shell metacharacters. *Fix*: generate server-side filenames; never let user data join a filesystem path. *(inferred)* diff --git a/grails-doc/src/en/guide/commandLine.adoc b/grails-doc/src/en/guide/commandLine.adoc index 220d267ad0c..dc99c85c84b 100644 --- a/grails-doc/src/en/guide/commandLine.adoc +++ b/grails-doc/src/en/guide/commandLine.adoc @@ -257,6 +257,17 @@ options alone, such as `--help`, or a command line after `--`. `grails-forge-cli project's commands from the file the build last wrote. When a command at the prompt runs the build, the prompt then completes the commands it found. +Since the build is code of the project, the Forge CLI runs it as `./gradlew` would. In a checkout you +do not trust, set the environment variable `GRAILS_FORGE_RUN_BUILD`, or the system property +`grails.forge.runBuild`, to `false`. The Forge CLI then never runs the build on its own: it offers the +commands of the file the build last wrote, which `./gradlew generateCommandManifest` writes, and runs the +build only for a project command you name. + +[source,shell] +---- +$ GRAILS_FORGE_RUN_BUILD=false grails-forge-cli +---- + When the build fails, as it does while the application does not compile or in a project of a Grails release without `generateCommandManifest`, the Forge CLI says so once and keeps offering the commands of the file the build last wrote, if any. It does not run the build for them again until one of the files diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/ForgeCli.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/ForgeCli.groovy index 692f8eb1b22..5cfca4e796a 100644 --- a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/ForgeCli.groovy +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/ForgeCli.groovy @@ -70,6 +70,16 @@ final class ForgeCli { .collect { Class type -> type.getAnnotation(CommandLine.Command).name() } .toSet().asImmutable() + /** + * The environment variable that, set to {@code false}, keeps the Forge CLI from running the build of the + * project on its own to update the project's commands: it offers those of a manifest the build already + * wrote, and runs the build only for a project command the user names. + */ + static final String RUN_BUILD_VARIABLE = 'GRAILS_FORGE_RUN_BUILD' + + /** The system property that does what {@link #RUN_BUILD_VARIABLE} does, and wins over it */ + static final String RUN_BUILD_PROPERTY = 'grails.forge.runBuild' + private static final BiFunction EXCEPTION_HANDLER = { Throwable e, CommandLine commandLine -> BaseCommand command = commandLine.getCommand() command.err(e.message) @@ -218,11 +228,21 @@ final class ForgeCli { * * @param runBuild whether the project's build may run to update the commands */ + /** + * Whether the build of the project may run to update the project's commands: unless + * {@link #RUN_BUILD_PROPERTY}, or else {@link #RUN_BUILD_VARIABLE}, is {@code false}. + */ + private static boolean buildMayRun() { + String setting = System.getProperty(RUN_BUILD_PROPERTY) ?: System.getenv(RUN_BUILD_VARIABLE) + !'false'.equalsIgnoreCase(setting?.trim()) + } + private static void addProjectCommands(CommandLine commandLine, boolean runBuild) { addProjectCommands(commandLine, runBuild, (ForgeCommand) commandLine.getCommand()) } private static void addProjectCommands(CommandLine commandLine, boolean runBuild, ConsoleOutput console) { + runBuild = runBuild && buildMayRun() File projectDir try { projectDir = FileSystemOutputHandler.getDefaultBaseDirectory() diff --git a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy index 9490cf2e9b2..9374d2720ac 100644 --- a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy +++ b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy @@ -40,6 +40,7 @@ import spock.lang.AutoCleanup import spock.lang.IgnoreIf import spock.lang.Shared import spock.lang.Specification +import spock.util.environment.RestoreSystemProperties // the project's build is a shell script standing in for the Gradle wrapper @IgnoreIf({ os.windows }) @@ -582,6 +583,29 @@ class ProjectCommandsSpec extends Specification { commandArguments() == ['url-mappings-report'] + arguments } + @RestoreSystemProperties + void 'with grails.forge.runBuild=false the build runs only for a command the user names'() { + given: 'a project whose build has not described its commands' + System.setProperty('grails.forge.runBuild', 'false') + buildWrites(URL_MAPPINGS_REPORT) + + when: 'the prompt starts, and a word that is no command of the Forge CLI is named' + ForgeCli.createCommandLine(context, true) + int unknown = ForgeCli.execute(context, ['url-mappings-report'] as String[]) + + then: 'the build does not run, so the Forge CLI knows no such command' + gradleInvocations().empty + unknown != 0 + + when: 'the user runs the build that describes the commands, which then are out of date' + writeManifestOfTheBuild(URL_MAPPINGS_REPORT) + ForgeCli.execute(context, ['url-mappings-report'] as String[]) + + then: 'the command of that manifest runs, and the build is not run to describe them again' + gradleInvocations().size() == 1 + gradleInvocations().first().contains('runCommand') + } + void 'the file that passes the arguments, which can hold a password, is readable by its owner only, and removed after the command'() { given: writeManifest(URL_MAPPINGS_REPORT) From 01ee46294ceb4b149679e7747cd2e04ddb9efea3 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Wed, 18 Nov 2026 07:33:53 -0800 Subject: [PATCH 073/115] Note what the command line parser reads differently in the upgrade guide Section 27 of the 9.0 upgrade guide, and the grails-9-upgrade skill, say that an empty argument after an option is now its value and that text inside quotes is trimmed, and list the three inputs the parser used to fail on or cut short, so a command's workaround for them can go. --- .../src/en/guide/upgrading/upgrading90x.adoc | 18 ++++++++++++++++++ .../skills/grails-9-upgrade/SKILL.md | 9 +++++++++ 2 files changed, 27 insertions(+) diff --git a/grails-doc/src/en/guide/upgrading/upgrading90x.adoc b/grails-doc/src/en/guide/upgrading/upgrading90x.adoc index 8d7c1a3b363..234815cd794 100644 --- a/grails-doc/src/en/guide/upgrading/upgrading90x.adoc +++ b/grails-doc/src/en/guide/upgrading/upgrading90x.adoc @@ -1489,3 +1489,21 @@ Grails 9 manages JLine 4 (`org.jline:jline`), the line Groovy 6's `groovysh` is * JLine 4 has no JNA or Jansi terminal provider. Remove a dependency on `org.jline:jline-terminal-jna` or `org.jline:jline-terminal-jansi`: neither has a JLine 4 version, and JLine finds the terminal through its remaining providers. picocli's `info.picocli:picocli-shell-jline3` runs on JLine 4, but picocli has no JLine 4 version of it, and its `PicocliCommands` implements `org.jline.console.CommandRegistry`, which JLine 4 deprecates. JLine's own picocli bridge, `org.jline:jline-picocli`, is built for JLine 4. To move to it, use it at the JLine version the Grails BOM manages. + +==== 27. Command Lines of Application Commands + +The command line parser of the `grails` command line tool and of `runCommand`, which reads the command line of an application or plugin command, reads two kinds of input differently: + +* An empty argument after an option without a value is that option's value, an empty one. `--name "" value` used to give the option `name` the value `value`; now `name` is empty, and `value` is an argument. +* The text inside a pair of double quotes is trimmed, as an argument without quotes always was. `" a "` used to reach the command as ` a `, and now reaches it as `a`. + +It also reads three kinds of input it used to fail on or cut short: + +* `--name=`, an option with an empty value, threw an `ArrayIndexOutOfBoundsException`, and is now the option `name` with an empty value. +* An argument that is a single `"` threw a `StringIndexOutOfBoundsException`, and is now passed on as it is. +* The value of an option is everything after the first `=`: `--filter=status=active` used to give `status`, and now gives `status=active`. + +===== Required Actions + +* Check commands that read an option followed by an empty argument, or arguments with spaces inside quotes, and expect the values above. +* Remove workarounds for the three failures, such as passing `--filter=status%3Dactive` and decoding it in the command. diff --git a/grails-skills/upgrade-guide-9/skills/grails-9-upgrade/SKILL.md b/grails-skills/upgrade-guide-9/skills/grails-9-upgrade/SKILL.md index 6e6da66f5a8..e3b2d2b7bd4 100644 --- a/grails-skills/upgrade-guide-9/skills/grails-9-upgrade/SKILL.md +++ b/grails-skills/upgrade-guide-9/skills/grails-9-upgrade/SKILL.md @@ -474,6 +474,14 @@ Grails 9 manages JLine 4 (`org.jline:jline`), the line Groovy 6's `groovysh` is - Remove dependencies on `org.jline:jline-terminal-jna` and `org.jline:jline-terminal-jansi`, which have no JLine 4 version. - `info.picocli:picocli-shell-jline3` still runs, but its `PicocliCommands` implements the deprecated `org.jline.console.CommandRegistry`; `org.jline:jline-picocli`, at the JLine version the Grails BOM manages, is the JLine 4 bridge to move to. +## Command Lines of Application Commands + +The command line parser of the `grails` tool and of `runCommand`, which application and plugin commands read their command line through, changed in Grails 9: + +- An empty argument after an option without a value is that option's empty value: `--name "" value` gives an empty `name` and the argument `value`, where it used to give `name=value`. +- The text inside a pair of double quotes is trimmed: `" a "` reaches the command as `a`, where it used to be ` a `. +- `--name=` gives an empty `name` (it threw `ArrayIndexOutOfBoundsException`), a lone `"` argument is passed on (it threw `StringIndexOutOfBoundsException`), and an option's value is everything after the first `=`: `--filter=status=active` gives `status=active`, where it gave `status`. Remove workarounds for these. + ## Verification Checklist - [ ] `gradle.properties` names the Grails 9 version and the wrapper is Gradle 9.8 or later @@ -506,6 +514,7 @@ Grails 9 manages JLine 4 (`org.jline:jline`), the line Groovy 6's `groovysh` is - [ ] With a database or a schema per tenant, every domain class saved inside a tenant block whose instances belong to a tenant, or that a multi-tenant class references, implements `MultiTenant` - [ ] Every write of a class that is not multi-tenant inside a tenant's transaction runs in the class's data service or its own `withTransaction`, or uses `flush: true` - [ ] Code that uses JLine compiles against JLine 4, and no build file declares `org.jline:jline-terminal-jna` or `org.jline:jline-terminal-jansi` +- [ ] Commands that read an option followed by an empty argument, or quoted arguments with spaces at their ends, expect the Grails 9 values, and no command works around a value cut at its second `=` - [ ] The full test suite passes on Groovy 6 ## Pitfalls to Avoid From f2b7160c32cdd2c66ddf41c1c22fa787b7ffafb5 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Wed, 18 Nov 2026 07:53:06 -0800 Subject: [PATCH 074/115] Test the prompt as it is used, on a terminal the shell is given ProjectCommandsSpec ran the lines of the prompt through the package-private ForgeCli.runLine, since InteractiveShell built the system terminal itself. The shell now takes a terminal, ForgeCli.interactive passes one on, and runLine is private. The spec types on an xterm terminal as a user would, a line once the prompt shows, and completes with tab: after a line that runs the build, the next line completes a command the build found. --- .../org/grails/forge/cli/ForgeCli.groovy | 16 +++- .../grails/forge/cli/InteractiveShell.groovy | 21 ++++- .../forge/cli/ProjectCommandsSpec.groovy | 91 +++++++++++++++---- 3 files changed, 106 insertions(+), 22 deletions(-) diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/ForgeCli.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/ForgeCli.groovy index 5cfca4e796a..1f31ad5ebec 100644 --- a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/ForgeCli.groovy +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/ForgeCli.groovy @@ -21,7 +21,7 @@ package org.grails.forge.cli import java.util.function.BiFunction import java.util.function.Supplier import groovy.transform.CompileStatic -import groovy.transform.PackageScope +import org.jline.terminal.Terminal import org.springframework.context.ApplicationContext import org.springframework.context.ConfigurableApplicationContext import picocli.CommandLine @@ -159,13 +159,22 @@ final class ForgeCli { * Runs the interactive shell on a running context until the user leaves it. */ static void interactive(ApplicationContext context) { + interactive(context, null) + } + + /** + * Runs the interactive shell on a running context until the user leaves it or its input ends. + * + * @param terminal the terminal the shell reads and writes, or {@code null} for the terminal of the JVM + */ + static void interactive(ApplicationContext context, Terminal terminal) { CommandLine commandLine = createCommandLine(context, true) // a line may run the build and change the project's commands, so the prompt then completes those // of the manifest as it is, read quietly, since a problem with it was reported already CommandLine[] offered = [commandLine] as CommandLine[] Supplier current = { -> offered[0] } as Supplier new InteractiveShell(commandLine, current, { String[] commandArgs -> runLine(context, offered, commandArgs) }, - EXCEPTION_HANDLER, InteractiveShell.DEFAULT_PROMPT).start() + EXCEPTION_HANDLER, InteractiveShell.DEFAULT_PROMPT, terminal).start() } /** @@ -174,8 +183,7 @@ final class ForgeCli { * * @param offered holds the command line the prompt completes from */ - @PackageScope - static void runLine(ApplicationContext context, CommandLine[] offered, String[] commandArgs) { + private static void runLine(ApplicationContext context, CommandLine[] offered, String[] commandArgs) { execute(context, commandArgs) try { CommandLine refreshed = createForgeCommandLine(context, true) diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/InteractiveShell.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/InteractiveShell.groovy index 0b6a0972dc9..759333e619f 100644 --- a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/InteractiveShell.groovy +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/InteractiveShell.groovy @@ -46,6 +46,7 @@ class InteractiveShell { private final Consumer executor private final BiFunction onError private final String prompt + private final Terminal terminal InteractiveShell(CommandLine commandLine, Consumer executor, @@ -72,6 +73,24 @@ class InteractiveShell { Consumer executor, BiFunction onError, String prompt) { + this(commandLine, offered, executor, onError, prompt, null) + } + + /** + * @param commandLine the command line whose help and colors the prompt starts with + * @param offered the command line of the commands offered now, which the prompt completes + * @param executor runs each line entered + * @param onError reports what ends the prompt + * @param prompt the prompt, with picocli's markup + * @param terminal the terminal the prompt reads and writes, or {@code null} for the terminal of the JVM + */ + InteractiveShell(CommandLine commandLine, + Supplier offered, + Consumer executor, + BiFunction onError, + String prompt, + Terminal terminal) { + this.terminal = terminal this.commandLine = commandLine this.offered = offered this.executor = executor @@ -81,7 +100,7 @@ class InteractiveShell { void start() { try { - Terminal terminal = TerminalBuilder.terminal() + Terminal terminal = this.terminal ?: TerminalBuilder.terminal() LineReader reader = LineReaderBuilder.builder() .terminal(terminal) .completer(new CommandLineCompleter({ -> offered.get().commandSpec } as Supplier)) diff --git a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy index 9374d2720ac..c5809ba92e5 100644 --- a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy +++ b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy @@ -19,6 +19,8 @@ package org.grails.forge.cli import java.nio.file.Files +import java.util.concurrent.LinkedBlockingQueue +import java.util.concurrent.TimeUnit import java.nio.file.attribute.PosixFilePermission import groovy.json.JsonOutput @@ -40,6 +42,7 @@ import spock.lang.AutoCleanup import spock.lang.IgnoreIf import spock.lang.Shared import spock.lang.Specification +import spock.lang.Timeout import spock.util.environment.RestoreSystemProperties // the project's build is a shell script standing in for the Gradle wrapper @@ -83,6 +86,12 @@ class ProjectCommandsSpec extends Specification { 'src/main/resources/META-INF/grails-cli.factories' ] + /** What a terminal writes to ask the emulator for the position of the cursor */ + private static final String CURSOR_POSITION_QUESTION = '\u001b[6n' + + /** What the prompt writes when it is ready for a line: it turns on bracketed paste */ + private static final String PROMPT_SHOWN = '\u001b[?2004h' + private static final Map PLUGIN_CREATE_APP = [ name: 'create-app', description: 'A plugin command named like a Forge command', arguments: [], options: [] ] @@ -861,27 +870,20 @@ class ProjectCommandsSpec extends Specification { commandLine.subcommands.containsKey('create-app') } + @Timeout(120) void 'after a line that runs the build, the prompt completes the commands the build found'() { - given: 'the prompt started with the commands of the last manifest' + given: 'the prompt starts with the commands of the last manifest' writeManifest(URL_MAPPINGS_REPORT) - CommandLine[] offered = [ForgeCli.createCommandLine(context, true)] as CommandLine[] - assert !complete(offered[0], 's2-q').contains('s2-quickstart') - buildWrites(URL_MAPPINGS_REPORT, SECURITY_QUICKSTART) - new File(dir, 'build.gradle') << '// a plugin added\n' - - when: 'a line names a command the Forge CLI does not have, which runs the build' - ForgeCli.runLine(context, offered, ['s2-quickstart', '--help'] as String[]) - then: - gradleInvocations()*.get(5) == ['generateCommandManifest'] - complete(offered[0], 's2-q').contains('s2-quickstart') - - when: 'a line runs no build' - ForgeCli.runLine(context, offered, ['--help'] as String[]) + when: 'a plugin is added while it waits, a line names a command the Forge CLI does not have, which runs the build, the next completes s2-q, and the last runs no build' + prompt({ + buildWrites(URL_MAPPINGS_REPORT, SECURITY_QUICKSTART) + new File(dir, 'build.gradle') << '// a plugin added\n' + }, 's2-quickstart --help\n', 's2-q\t\n', '--help\n') - then: 'the commands stay those of the manifest' - complete(offered[0], 's2-q').contains('s2-quickstart') - gradleInvocations().size() == 1 + then: 'the build ran once, and the completed line ran the command it found' + gradleInvocations()*.get(5) == ['generateCommandManifest', 'runCommand'] + commandArguments() == ['s2-quickstart'] } void 'the prompt completes project commands, their options and the values they declare'() { @@ -1069,6 +1071,61 @@ class ProjectCommandsSpec extends Specification { PosixFilePermission.OWNER_EXECUTE] as Set) } + /** + * Runs the interactive prompt on a terminal that a user types the given lines on, each once the prompt + * shows, and then {@code exit}. + * + * @param whileWaiting runs once the prompt first shows, before a line is typed + */ + private void prompt(Closure whileWaiting, String... lines) { + Deque toType = new ArrayDeque(lines.toList() + ['exit\n']) + LinkedBlockingQueue typed = new LinkedBlockingQueue() + boolean[] waited = [false] as boolean[] + // what the terminal writes, read as a user and their terminal emulator would + OutputStream screen = new OutputStream() { + private final StringBuilder recent = new StringBuilder() + + @Override + synchronized void write(int b) { + recent.append((char) (b & 0xff)) + if (endsWith(CURSOR_POSITION_QUESTION)) { + // the emulator answers where the cursor is + type('\u001b[1;1R') + } + else if (endsWith(PROMPT_SHOWN) && !toType.empty) { + if (!waited[0]) { + waited[0] = true + whileWaiting.call() + } + type(toType.poll()) + } + } + + private boolean endsWith(String text) { + recent.length() >= text.length() && recent.substring(recent.length() - text.length()) == text + } + + private void type(String text) { + text.getBytes('UTF-8').each { byte b -> typed.put(b & 0xff) } + } + } + InputStream keyboard = new InputStream() { + @Override + int read() { + Integer next = typed.poll(30, TimeUnit.SECONDS) + next == null ? -1 : next + } + } + // a terminal that completes on tab, as a dumb one does not + Terminal terminal = TerminalBuilder.builder().system(false).type('xterm').streams(keyboard, screen).build() + try { + ForgeCli.interactive(context, terminal) + } + finally { + terminal.close() + } + } + private List complete(CommandLine commandLine, String typed) { LineReader reader = LineReaderBuilder.builder().terminal(terminal).build() List candidates = [] From 1de3403bb1b3a1889cb24fa724e1820dd9c8b356 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Wed, 18 Nov 2026 08:07:19 -0800 Subject: [PATCH 075/115] Rename @CommandArgument to @CommandParameter The new annotation had the simple name of org.grails.cli.profile.CommandArgument, which profile commands use, so code with both had to name one in full. @CommandParameter, with its container @CommandParameters, next to @CommandOption follows picocli's @Parameters and @Option. The annotation is new in 9.0, so nothing released used the old name. --- .../core/cli/ApplicationCommandManifestWriter.groovy | 8 ++++---- ...{CommandArgument.groovy => CommandParameter.groovy} | 8 ++++---- ...ommandArguments.groovy => CommandParameters.groovy} | 6 +++--- .../cli/ApplicationCommandManifestWriterSpec.groovy | 6 +++--- .../en/guide/commandLine/creatingCustomCommands.adoc | 10 +++++----- grails-doc/src/en/guide/introduction/whatsNew.adoc | 2 +- .../security/cli/S2CreatePersistentTokenCommand.groovy | 4 ++-- .../cli/S2CreateRoleHierarchyEntryCommand.groovy | 4 ++-- .../grails/security/cli/S2QuickstartCommand.groovy | 10 +++++----- 9 files changed, 29 insertions(+), 29 deletions(-) rename grails-core/src/cli/groovy/org/apache/grails/core/cli/{CommandArgument.groovy => CommandParameter.groovy} (90%) rename grails-core/src/cli/groovy/org/apache/grails/core/cli/{CommandArguments.groovy => CommandParameters.groovy} (89%) diff --git a/grails-core/src/cli/groovy/org/apache/grails/core/cli/ApplicationCommandManifestWriter.groovy b/grails-core/src/cli/groovy/org/apache/grails/core/cli/ApplicationCommandManifestWriter.groovy index 65f2278d2d7..71aa2abdc71 100644 --- a/grails-core/src/cli/groovy/org/apache/grails/core/cli/ApplicationCommandManifestWriter.groovy +++ b/grails-core/src/cli/groovy/org/apache/grails/core/cli/ApplicationCommandManifestWriter.groovy @@ -71,7 +71,7 @@ final class ApplicationCommandManifestWriter { private static Map describe(ApplicationCommand command) { Class type = command.class - List> arguments = declaredArguments(type).collect { CommandArgument argument -> + List> arguments = declaredArguments(type).collect { CommandParameter argument -> [name: argument.name(), description: argument.description(), required: argument.required(), values: argument.values().toList()] as Map } @@ -87,9 +87,9 @@ final class ApplicationCommandManifestWriter { // Class#getAnnotationsByType would read both forms at once, but the static compiler links it with an // Object[] return type, which the JDK method does not have, so the call fails with NoSuchMethodError. // getAnnotation returns a single annotation and links correctly. - private static List declaredArguments(Class type) { - CommandArgument single = type.getAnnotation(CommandArgument) - single ? [single] : (type.getAnnotation(CommandArguments)?.value()?.toList() ?: []) + private static List declaredArguments(Class type) { + CommandParameter single = type.getAnnotation(CommandParameter) + single ? [single] : (type.getAnnotation(CommandParameters)?.value()?.toList() ?: []) } private static List declaredOptions(Class type) { diff --git a/grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandArgument.groovy b/grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandParameter.groovy similarity index 90% rename from grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandArgument.groovy rename to grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandParameter.groovy index 71964dd81bf..9d515d2af46 100644 --- a/grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandArgument.groovy +++ b/grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandParameter.groovy @@ -32,8 +32,8 @@ import java.lang.annotation.Target *

Declare one annotation per argument, in the order the arguments are given on the command line:

* *
- * @CommandArgument(name = 'package', description = 'The package of the domain classes')
- * @CommandArgument(name = 'user-class-name', description = 'The name of the user class')
+ * @CommandParameter(name = 'package', description = 'The package of the domain classes')
+ * @CommandParameter(name = 'user-class-name', description = 'The name of the user class')
  * class SetupUsersCommand implements GrailsApplicationCommand { ... }
  * 
* @@ -46,8 +46,8 @@ import java.lang.annotation.Target @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) -@Repeatable(CommandArguments) -@interface CommandArgument { +@Repeatable(CommandParameters) +@interface CommandParameter { /** * @return the name of the argument, shown in the command's help diff --git a/grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandArguments.groovy b/grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandParameters.groovy similarity index 89% rename from grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandArguments.groovy rename to grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandParameters.groovy index 00dc375cbf0..c770c53f87d 100644 --- a/grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandArguments.groovy +++ b/grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandParameters.groovy @@ -25,17 +25,17 @@ import java.lang.annotation.RetentionPolicy import java.lang.annotation.Target /** - * Holds the {@link CommandArgument} annotations of a command that declares more than one. + * Holds the {@link CommandParameter} annotations of a command that declares more than one. * * @since 9.0 */ @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) -@interface CommandArguments { +@interface CommandParameters { /** * @return the arguments of the command, in the order they are given on the command line */ - CommandArgument[] value() + CommandParameter[] value() } diff --git a/grails-core/src/test-cli/groovy/org/apache/grails/core/cli/ApplicationCommandManifestWriterSpec.groovy b/grails-core/src/test-cli/groovy/org/apache/grails/core/cli/ApplicationCommandManifestWriterSpec.groovy index ffa69c8b515..f9cba7e1b4b 100644 --- a/grails-core/src/test-cli/groovy/org/apache/grails/core/cli/ApplicationCommandManifestWriterSpec.groovy +++ b/grails-core/src/test-cli/groovy/org/apache/grails/core/cli/ApplicationCommandManifestWriterSpec.groovy @@ -155,8 +155,8 @@ class UndescribedManifestCommand implements ApplicationCommand { } } -@CommandArgument(name = 'package', description = 'The package of the domain classes') -@CommandArgument(name = 'user-class-name', description = 'The name of the user class', required = false) +@CommandParameter(name = 'package', description = 'The package of the domain classes') +@CommandParameter(name = 'user-class-name', description = 'The name of the user class', required = false) @CommandOption(name = 'groupClassName', description = 'Also generate role group classes') @CommandOption(name = 'uiOnly', description = 'Configure without generating classes', flag = true) @CommandOption(name = 'format', values = ['json', 'yaml']) @@ -170,7 +170,7 @@ class DescribedManifestCommand implements ApplicationCommand { } } -@CommandArgument(name = 'domain-class', values = ['Book', 'Author']) +@CommandParameter(name = 'domain-class', values = ['Book', 'Author']) @CommandOption(name = 'force', flag = true) class SingleDeclarationManifestCommand implements ApplicationCommand { diff --git a/grails-doc/src/en/guide/commandLine/creatingCustomCommands.adoc b/grails-doc/src/en/guide/commandLine/creatingCustomCommands.adoc index e459186c0ce..b8dafa00da8 100644 --- a/grails-doc/src/en/guide/commandLine/creatingCustomCommands.adoc +++ b/grails-doc/src/en/guide/commandLine/creatingCustomCommands.adoc @@ -247,10 +247,10 @@ This allows you to create more versatile and interactive custom commands for you **Describing Arguments and Options** A command can describe the arguments and options it takes with the -link:{api}org/apache/grails/core/cli/CommandArgument.html[CommandArgument] and +link:{api}org/apache/grails/core/cli/CommandParameter.html[CommandParameter] and link:{api}org/apache/grails/core/cli/CommandOption.html[CommandOption] annotations. Tools that offer the commands of a project, such as the <>, show them in the -command's help, check them and complete them. Declare one `@CommandArgument` per argument, in the order +command's help, check them and complete them. Declare one `@CommandParameter` per argument, in the order the arguments are given, and one `@CommandOption` per option: [source,groovy] @@ -258,11 +258,11 @@ the arguments are given, and one `@CommandOption` per option: // grails-app/commands/com/example/GreetCommand.groovy package com.example -import org.apache.grails.core.cli.CommandArgument import org.apache.grails.core.cli.CommandOption +import org.apache.grails.core.cli.CommandParameter import org.apache.grails.core.cli.GrailsApplicationCommand -@CommandArgument(name = 'name', required = false, description = 'Who to greet') +@CommandParameter(name = 'name', required = false, description = 'Who to greet') @CommandOption(name = 'loud', flag = true, description = 'Greet in capitals') @CommandOption(name = 'language', values = ['en', 'de', 'fr'], description = 'The language of the greeting') class GreetCommand implements GrailsApplicationCommand { @@ -296,7 +296,7 @@ grails> greet Alice --language= The Forge CLI passes a declared option on as `--name=value`, after the arguments. The annotations only describe the command: it still reads its arguments and options from the command line as before. -A command that declares no `@CommandArgument` takes any arguments, and one that declares no +A command that declares no `@CommandParameter` takes any arguments, and one that declares no `@CommandOption` takes any options and is given its command line in the order it was typed, so a command without the annotations works as it always has. diff --git a/grails-doc/src/en/guide/introduction/whatsNew.adoc b/grails-doc/src/en/guide/introduction/whatsNew.adoc index 3d4dedcff44..737a3292f1b 100644 --- a/grails-doc/src/en/guide/introduction/whatsNew.adoc +++ b/grails-doc/src/en/guide/introduction/whatsNew.adoc @@ -384,7 +384,7 @@ grails> s2-quickstart com.example User Role --groupClassName=RoleGroup The Forge CLI reads the commands from a manifest that the project's build writes with the new `generateCommandManifest` task, so it never loads the project's classpath itself, and runs a command through the project's `runCommand` task. A command describes the arguments and options it takes with -the new `@CommandArgument` and `@CommandOption` annotations, which the Forge CLI shows in the command's +the new `@CommandParameter` and `@CommandOption` annotations, which the Forge CLI shows in the command's help, checks and completes. Commands without them, such as those of plugins built for an earlier release, are offered too and take any arguments. diff --git a/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2CreatePersistentTokenCommand.groovy b/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2CreatePersistentTokenCommand.groovy index 02f4280ea65..3bc7e86753d 100644 --- a/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2CreatePersistentTokenCommand.groovy +++ b/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2CreatePersistentTokenCommand.groovy @@ -18,7 +18,7 @@ package org.apache.grails.security.cli import grails.build.logging.ConsoleLogger import grails.build.logging.GrailsConsole import grails.codegen.model.Model -import org.apache.grails.core.cli.CommandArgument +import org.apache.grails.core.cli.CommandParameter import org.apache.grails.core.cli.GrailsApplicationCommand import groovy.transform.CompileStatic @@ -30,7 +30,7 @@ import groovy.transform.CompileStatic * * @since 6.0.0 */ -@CommandArgument(name = 'domain-class-name', +@CommandParameter(name = 'domain-class-name', description = 'The fully qualified name of the persistent token domain class, e.g. com.yourapp.PersistentLogin') @CompileStatic class S2CreatePersistentTokenCommand implements GrailsApplicationCommand, CommandLineHelper, SkipBootstrap { diff --git a/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2CreateRoleHierarchyEntryCommand.groovy b/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2CreateRoleHierarchyEntryCommand.groovy index 620b92333a5..8cca9a5a336 100644 --- a/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2CreateRoleHierarchyEntryCommand.groovy +++ b/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2CreateRoleHierarchyEntryCommand.groovy @@ -18,7 +18,7 @@ package org.apache.grails.security.cli import grails.build.logging.ConsoleLogger import grails.build.logging.GrailsConsole import grails.codegen.model.Model -import org.apache.grails.core.cli.CommandArgument +import org.apache.grails.core.cli.CommandParameter import org.apache.grails.core.cli.GrailsApplicationCommand import groovy.transform.CompileStatic @@ -29,7 +29,7 @@ import groovy.transform.CompileStatic * * @since 6.0.0 */ -@CommandArgument(name = 'domain-class-name', +@CommandParameter(name = 'domain-class-name', description = 'The fully qualified name of the role hierarchy domain class, e.g. com.yourapp.RoleHierarchyEntry') @CompileStatic class S2CreateRoleHierarchyEntryCommand implements GrailsApplicationCommand, CommandLineHelper, SkipBootstrap { diff --git a/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2QuickstartCommand.groovy b/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2QuickstartCommand.groovy index 2da1c1971e9..3e8fc44c1f6 100644 --- a/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2QuickstartCommand.groovy +++ b/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2QuickstartCommand.groovy @@ -18,8 +18,8 @@ package org.apache.grails.security.cli import grails.build.logging.ConsoleLogger import grails.build.logging.GrailsConsole import grails.codegen.model.Model -import org.apache.grails.core.cli.CommandArgument import org.apache.grails.core.cli.CommandOption +import org.apache.grails.core.cli.CommandParameter import org.apache.grails.core.cli.GrailsApplicationCommand import groovy.transform.CompileStatic @@ -36,11 +36,11 @@ import groovy.transform.CompileStatic * * @since 6.0.0 */ -@CommandArgument(name = 'domain-class-package', required = false, +@CommandParameter(name = 'domain-class-package', required = false, description = 'The package of the domain classes') -@CommandArgument(name = 'user-class-name', required = false, description = 'The name of the user class') -@CommandArgument(name = 'role-class-name', required = false, description = 'The name of the role class') -@CommandArgument(name = 'requestmap-class-name', required = false, +@CommandParameter(name = 'user-class-name', required = false, description = 'The name of the user class') +@CommandParameter(name = 'role-class-name', required = false, description = 'The name of the role class') +@CommandParameter(name = 'requestmap-class-name', required = false, description = 'The name of the request map class, when request maps secure the URLs') @CommandOption(name = 'groupClassName', description = 'Also creates role group classes with this name') @CommandOption(name = 'uiOnly', flag = true, From b254917695a78b1b702f646c642ee9a9e54fa0c8 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Wed, 18 Nov 2026 14:02:22 -0800 Subject: [PATCH 076/115] Watch the cli source sets for commands a plugin project adds A command compiled from a cli source set registers itself, and a hand-written src/cli/resources/META-INF/grails-cli.factories is merged into the generated registrations, but the manifest named neither. So a plugin project of the build that added a command there left the old command list, and the Forge CLI rejected the new command. The manifest now names every directory of each project's cli source set under inputs.commandDirectories, and src/cli/resources/META-INF of every project among the source directories. The Forge CLI walks the command directories as sources from the top, so a package named build or out is watched too. A reader that does not know the new key ignores it, so the format version stays 1. --- grails-doc/src/en/guide/commandLine.adoc | 10 +++-- .../forge/cli/command/ProjectCommands.groovy | 18 ++++++--- .../forge/cli/ProjectCommandsSpec.groovy | 38 +++++++++++++++++++ .../ApplicationCommandManifestTask.groovy | 16 ++++++-- .../commands/GrailsCliGradlePlugin.groovy | 11 +++++- .../ApplicationCommandManifestTaskSpec.groovy | 8 +++- 6 files changed, 87 insertions(+), 14 deletions(-) diff --git a/grails-doc/src/en/guide/commandLine.adoc b/grails-doc/src/en/guide/commandLine.adoc index dc99c85c84b..726d4882852 100644 --- a/grails-doc/src/en/guide/commandLine.adoc +++ b/grails-doc/src/en/guide/commandLine.adoc @@ -236,10 +236,13 @@ files decide them: * the directory of every project of the build, for its `.gradle`, `.gradle.kts`, `.properties` and `.toml` files, such as `build.gradle`, `settings.gradle`, `gradle.properties` and `libs.versions.toml`; * everything under the `gradle` and `buildSrc` directories of the build's root, under - `grails-app/commands` and `src/main/resources/META-INF` of every project, and under every included - build, also one included with `pluginManagement { includeBuild }`, apart from what builds and tools + `grails-app/commands`, `src/main/resources/META-INF` and `src/cli/resources/META-INF` of every + project, and under every included build, also one included with `pluginManagement { includeBuild }`, apart from what builds and tools write outside a `src` or `grails-app` directory, such as `build`, `.gradle` and `.kotlin`, and from `node_modules`; +* everything under the directories of the `cli` source set of every project that has one, such as + `src/cli/groovy` and `src/cli/resources`, where a command a plugin project of the build adds registers + itself; * the source file of each command it lists that one of the projects of the build compiles, such as a command of a plugin project in `src/main/groovy`. @@ -248,7 +251,8 @@ project's `.gradle/grails` directory. It runs the task again when one of them is its time or size changes, whether the time goes forward or back, and when a manifest has no record, as one written by `./gradlew generateCommandManifest` alone. So a plugin added to the build brings its commands the next time a project command is asked for. A change the Forge CLI cannot see, such as a new snapshot of a -plugin, is picked up by running `./gradlew generateCommandManifest`. +plugin, or a command added to a project's `src/main/groovy` rather than to `grails-app/commands` or a +`cli` source set, is picked up by running `./gradlew generateCommandManifest`. The build runs only when the interactive prompt starts or a command the Forge CLI does not have is named, also after the Forge CLI's own options, as in `grails-forge-cli -x dbm-update`. Its own commands, diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy index 54138597bf0..dc2e2b569ec 100644 --- a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy @@ -266,9 +266,10 @@ class ProjectCommands { Map given = (Map) inputs List buildDirectories = files(given.buildDirectories) List sourceDirectories = files(given.sourceDirectories) + List commandDirectories = given.containsKey('commandDirectories') ? files(given.commandDirectories) : [] List sourceFiles = given.containsKey('sourceFiles') ? files(given.sourceFiles) : [] - if (buildDirectories != null && sourceDirectories != null && sourceFiles != null) { - return new Inputs(buildDirectories, sourceDirectories, sourceFiles) + if (buildDirectories != null && sourceDirectories != null && commandDirectories != null && sourceFiles != null) { + return new Inputs(buildDirectories, sourceDirectories, commandDirectories, sourceFiles) } } } @@ -279,7 +280,8 @@ class ProjectCommands { File rootDir = gradle.rootDir new Inputs([projectDir, rootDir].unique(), [new File(rootDir, 'gradle'), new File(rootDir, 'buildSrc'), - new File(projectDir, 'grails-app/commands'), new File(projectDir, 'src/main/resources/META-INF')], []) + new File(projectDir, 'grails-app/commands'), new File(projectDir, 'src/main/resources/META-INF')], + [new File(projectDir, 'src/cli')], []) } /** The files of a list of absolute paths, or none when one is not such a path, such as a mistyped one */ @@ -309,7 +311,7 @@ class ProjectCommands { /** * What the files the commands depend on are now: the build files directly in each build directory, * and every file and directory below each source directory, apart from what builds and tools write - * next to a build script. Each file is listed with its time and size, so a deleted, added, replaced + * next to a build script, and below each directory of commands, all of which are sources. Each file is listed with its time and size, so a deleted, added, replaced * or changed file changes the list, whatever the clock said when it was written. */ private static List snapshot(Inputs inputs) { @@ -330,6 +332,9 @@ class ProjectCommands { // the sources of commands are sources from the start; a tree of a build becomes so below src walk(dir, lines, dir.name == 'META-INF' || (dir.name == 'commands' && dir.parentFile?.name == 'grails-app')) } + for (File dir : inputs.commandDirectories) { + walk(dir, lines, true) + } for (File file : inputs.sourceFiles) { walk(file, lines, true) } @@ -390,11 +395,14 @@ class ProjectCommands { final List buildDirectories final List sourceDirectories + final List commandDirectories final List sourceFiles - Inputs(List buildDirectories, List sourceDirectories, List sourceFiles) { + Inputs(List buildDirectories, List sourceDirectories, List commandDirectories, + List sourceFiles) { this.buildDirectories = buildDirectories this.sourceDirectories = sourceDirectories + this.commandDirectories = commandDirectories this.sourceFiles = sourceFiles } } diff --git a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy index c5809ba92e5..f7bfc58b62e 100644 --- a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy +++ b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy @@ -74,6 +74,8 @@ class ProjectCommandsSpec extends Specification { name: 'url-mappings-report', description: 'url-mappings-report', arguments: [], options: [] ] + private static final Map AUDIT = [name: 'audit', description: 'Audits the application', arguments: [], options: []] + private static final List MANIFEST_TASK = ['generateCommandManifest', '-PgrailsCommandManifest=build/grails/commands.json'] /** Files the commands depend on, in every place the Forge CLI watches without a manifest naming them */ @@ -366,6 +368,42 @@ class ProjectCommandsSpec extends Specification { gradleInvocations() == [prefix() + MANIFEST_TASK] } + void "a command a plugin project of the build adds to its cli source set is offered and runs"() { + given: "a manifest naming the cli source set of a plugin project next to the application" + File plugin = new File(dir, 'plugins/audit') + File groovy = new File(plugin, 'src/cli/groovy') + File resources = new File(plugin, 'src/cli/resources') + new File(resources, 'META-INF').mkdirs() + groovy.mkdirs() + Map inputs = [buildDirectories: [dir.path], sourceDirectories: [], commandDirectories: [groovy.path, resources.path]] + buildWritesWithInputs(inputs, URL_MAPPINGS_REPORT) + ForgeCli.createCommandLine(context, false) + forgetInvocations() + + when: 'a command class is added, in a package named like an output directory' + File command = new File(groovy, 'com/acme/build/AuditCommand.groovy') + command.parentFile.mkdirs() + command.text = 'class AuditCommand {}' + buildWritesWithInputs(inputs, URL_MAPPINGS_REPORT, AUDIT) + int exitCode = ForgeCli.execute(context, 'audit') + ForgeCli.createCommandLine(context, false) + + then: 'the build runs once, and the new command runs instead of being rejected' + exitCode == 0 + gradleInvocations()*.get(5) == ['generateCommandManifest', 'runCommand'] + commandArguments() == ['audit'] + + when: 'a command is registered by hand in the factories file' + forgetInvocations() + new File(resources, 'META-INF/grails-cli.factories').text = + 'org.apache.grails.core.cli.ApplicationCommand=com.acme.ExportCommand\n' + ForgeCli.createCommandLine(context, false) + ForgeCli.createCommandLine(context, false) + + then: + gradleInvocations() == [prefix() + MANIFEST_TASK] + } + void 'what tools write in an included build does not make the manifest outdated, wherever the build is'() { given: 'an included build, below a directory named src, with a subproject that has no build file of its own' File included = new File(dir, 'src/shared') diff --git a/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTask.groovy b/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTask.groovy index d50b3608cb4..267bd5b673e 100644 --- a/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTask.groovy +++ b/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTask.groovy @@ -41,9 +41,9 @@ import javax.inject.Inject * {@code runCommand} can run. The task is up to date while that classpath is unchanged.

* *

The file also names, under {@code inputs}, the directories whose files decide the commands without - * being on the classpath yet: those whose build files decide the build, and those that hold the sources - * of commands or of the build's logic, and the source files of the listed commands found in the - * projects of the build. A tool reading the file runs the task again when one of their files changes.

+ * being on the classpath yet: those whose build files decide the build, those that hold the sources + * of commands or of the build's logic, those of the {@code cli} source sets, where every source can be a + * command, and the source files of the listed commands found in the projects of the build. A tool reading the file runs the task again when one of their files changes.

* * @since 9.0 */ @@ -59,6 +59,7 @@ abstract class ApplicationCommandManifestTask extends JavaExec { mainClass.set(MANIFEST_WRITER_CLASS) buildDirectories.convention([]) sourceDirectories.convention([]) + commandDirectories.convention([]) commandSourceRoots.convention([]) } @@ -83,6 +84,13 @@ abstract class ApplicationCommandManifestTask extends JavaExec { @Input abstract ListProperty getSourceDirectories() + /** + * @return the directories of the {@code cli} source set of every project of the build, which hold + * commands and what they use, so a command added there, which registers itself, counts too + */ + @Input + abstract ListProperty getCommandDirectories() + /** * @return the source directories of every project of the build, where the source of a command the * manifest lists is looked for by its class, so a change to it counts too @@ -125,7 +133,7 @@ abstract class ApplicationCommandManifestTask extends JavaExec { super.exec() Map manifest = (Map) new JsonSlurper().parse(file, 'UTF-8') manifest.inputs = [buildDirectories: buildDirectories.get(), sourceDirectories: sourceDirectories.get(), - sourceFiles: commandSources(manifest.commands)] + commandDirectories: commandDirectories.get(), sourceFiles: commandSources(manifest.commands)] file.setText(JsonOutput.prettyPrint(JsonOutput.toJson(manifest)), 'UTF-8') } } diff --git a/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/GrailsCliGradlePlugin.groovy b/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/GrailsCliGradlePlugin.groovy index 76c7fef69ca..743873b43e8 100644 --- a/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/GrailsCliGradlePlugin.groovy +++ b/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/GrailsCliGradlePlugin.groovy @@ -790,11 +790,19 @@ class GrailsCliGradlePlugin implements Plugin { List includedBuildDirs = gradle.includedBuilds*.projectDir List buildDirectories = ([root.projectDir] + projectDirs) .collect { File dir -> dir.absolutePath }.unique().sort() + // the hand-written registrations in either META-INF are merged into the generated ones List sourceDirectories = ([new File(root.projectDir, 'gradle'), new File(root.projectDir, 'buildSrc')] + projectDirs.collectMany { File dir -> - [new File(dir, 'grails-app/commands'), new File(dir, 'src/main/resources/META-INF')] + [new File(dir, 'grails-app/commands'), new File(dir, 'src/main/resources/META-INF'), + new File(dir, 'src/cli/resources/META-INF')] } + includedBuildDirs) .collect { File dir -> dir.absolutePath }.unique().sort() + // a command compiled from the cli source set registers itself, so a new one adds a command + List commandDirectories = root.allprojects.collectMany { Project each -> + def sourceSets = each.extensions.findByType(org.gradle.api.tasks.SourceSetContainer) + SourceSet cli = sourceSets?.findByName(GrailsCliArtifactGradlePlugin.CLI_SOURCE_SET_NAME) + cli ? cli.allSource.srcDirs*.absolutePath : [] + }.unique().sort() // every source set, as a plugin keeps its commands in its cli source set, src/cli/groovy List commandSourceRoots = root.allprojects.collectMany { Project each -> def sourceSets = each.extensions.findByType(org.gradle.api.tasks.SourceSetContainer) @@ -803,6 +811,7 @@ class GrailsCliGradlePlugin implements Plugin { manifestTask.configure { it.buildDirectories.set(buildDirectories) it.sourceDirectories.set(sourceDirectories) + it.commandDirectories.set(commandDirectories) it.commandSourceRoots.set(commandSourceRoots) } } diff --git a/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTaskSpec.groovy b/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTaskSpec.groovy index 6dbb613350a..a15639259a4 100644 --- a/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTaskSpec.groovy +++ b/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTaskSpec.groovy @@ -98,7 +98,13 @@ class ApplicationCommandManifestTaskSpec extends GradleSpecification { inputs.sourceDirectories.collect { new File(it).canonicalFile }.containsAll([ new File(root, 'gradle'), new File(root, 'buildSrc'), new File(root, 'app/grails-app/commands'), new File(root, 'app/src/main/resources/META-INF'), - new File(root, 'manifest-writer/grails-app/commands')]) + new File(root, 'app/src/cli/resources/META-INF'), + new File(root, 'manifest-writer/grails-app/commands'), + new File(root, 'manifest-writer/src/cli/resources/META-INF')]) + + and: 'the directories of the cli source sets, where a new source can be a new command' + inputs.commandDirectories.collect { new File(it).canonicalFile } as Set == + [new File(root, 'app/src/cli/java'), new File(root, 'app/src/cli/resources')] as Set and: 'every included build, also those that bring plugins of the build' inputs.sourceDirectories.collect { new File(it).canonicalFile }.containsAll([new File(root, 'shared'), new File(root, 'build-logic')]) From 1ba5e639dabaf13255637484772a389cc93d6f7a Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Wed, 18 Nov 2026 14:02:22 -0800 Subject: [PATCH 077/115] Put the addProjectCommands Javadoc back on its method It sat above buildMayRun's own Javadoc, leaving addProjectCommands without one. --- .../main/groovy/org/grails/forge/cli/ForgeCli.groovy | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/ForgeCli.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/ForgeCli.groovy index 1f31ad5ebec..8f5c7d1b9c3 100644 --- a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/ForgeCli.groovy +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/ForgeCli.groovy @@ -230,12 +230,6 @@ final class ForgeCli { return commandLine } - /** - * Adds the commands of the Grails project in the current directory, which run through the project's - * build. A command named like one of the Forge CLI's own is left out: the Forge CLI's command runs. - * - * @param runBuild whether the project's build may run to update the commands - */ /** * Whether the build of the project may run to update the project's commands: unless * {@link #RUN_BUILD_PROPERTY}, or else {@link #RUN_BUILD_VARIABLE}, is {@code false}. @@ -245,6 +239,12 @@ final class ForgeCli { !'false'.equalsIgnoreCase(setting?.trim()) } + /** + * Adds the commands of the Grails project in the current directory, which run through the project's + * build. A command named like one of the Forge CLI's own is left out: the Forge CLI's command runs. + * + * @param runBuild whether the project's build may run to update the commands + */ private static void addProjectCommands(CommandLine commandLine, boolean runBuild) { addProjectCommands(commandLine, runBuild, (ForgeCommand) commandLine.getCommand()) } From 2a9111223731381dcb8bfb77749139961860c697 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Wed, 18 Nov 2026 14:07:49 -0800 Subject: [PATCH 078/115] Keep an auditor set while the listener looks one up getAuditorAware() checked whether the auditor was resolved, looked it up in the datastore's application context, and then stored the result. An auditor set with setAuditorAware while the lookup ran was overwritten by the lookup's result, null when the context has none. The lookup still runs outside a lock, but its result is stored only if no auditor was set meanwhile, and setAuditorAware and setApplicationContext write under the same lock. --- .../events/AutoTimestampEventListener.java | 28 ++++++++--- .../AutoTimestampEventListenerSpec.groovy | 46 +++++++++++++++++++ 2 files changed, 67 insertions(+), 7 deletions(-) diff --git a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/events/AutoTimestampEventListener.java b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/events/AutoTimestampEventListener.java index a3a4483b6e5..caf7bbbcdbf 100644 --- a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/events/AutoTimestampEventListener.java +++ b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/events/AutoTimestampEventListener.java @@ -101,6 +101,10 @@ public class AutoTimestampEventListener extends AbstractPersistenceEventListener // the application context of the datastore when an entity is saved. private volatile boolean auditorAwareResolved; + // Guards the writes of auditorAware and auditorAwareResolved, so that an auditor set while a lookup runs is not + // replaced by what the lookup finds. + private final Object auditorAwareLock = new Object(); + /** * Creates the listener of the datastore, which reads {@link Settings#SETTING_AUTO_TIMESTAMP_INSERT_OVERWRITE} and * {@link Settings#SETTING_AUTO_TIMESTAMP_CACHE_ANNOTATIONS} from the datastore's configuration. Unless an @@ -131,10 +135,12 @@ public void setApplicationContext(ApplicationContext applicationContext) throws // Spring calls this after it has set the bean's properties, so an auditor the listener was configured with // is kept when the context has none of its own. AuditorAware contextAuditorAware = lookUpAuditorAware(applicationContext); - if (contextAuditorAware != null) { - this.auditorAware = contextAuditorAware; + synchronized (auditorAwareLock) { + if (contextAuditorAware != null) { + this.auditorAware = contextAuditorAware; + } + this.auditorAwareResolved = true; } - this.auditorAwareResolved = true; } private void applySettings(Datastore datastore) { @@ -386,16 +392,24 @@ public AuditorAware getAuditorAware() { if (!auditorAwareResolved) { ApplicationContext applicationContext = activeApplicationContextOfDatastore(); if (applicationContext != null) { - auditorAware = lookUpAuditorAware(applicationContext); - auditorAwareResolved = true; + // The lookup runs outside the lock, and what it finds is kept only if no auditor was set meanwhile. + AuditorAware contextAuditorAware = lookUpAuditorAware(applicationContext); + synchronized (auditorAwareLock) { + if (!auditorAwareResolved) { + auditorAware = contextAuditorAware; + auditorAwareResolved = true; + } + } } } return auditorAware; } public void setAuditorAware(AuditorAware auditorAware) { - this.auditorAware = auditorAware; - this.auditorAwareResolved = true; + synchronized (auditorAwareLock) { + this.auditorAware = auditorAware; + this.auditorAwareResolved = true; + } } private static boolean isDisabled(final ThreadLocal disabledTimestamps, final String entityName) { diff --git a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/timestamp/AutoTimestampEventListenerSpec.groovy b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/timestamp/AutoTimestampEventListenerSpec.groovy index ee0505927d6..704011bb080 100644 --- a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/timestamp/AutoTimestampEventListenerSpec.groovy +++ b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/timestamp/AutoTimestampEventListenerSpec.groovy @@ -31,6 +31,7 @@ import java.util.function.Supplier import groovy.transform.InheritConstructors import spock.lang.Specification +import org.springframework.beans.factory.NoSuchBeanDefinitionException import org.springframework.beans.factory.config.BeanDefinition import org.springframework.beans.factory.config.BeanDefinitionCustomizer import org.springframework.context.support.GenericApplicationContext @@ -737,6 +738,34 @@ class AutoTimestampEventListenerSpec extends Specification { context.close() } + void "an auditor set while the listener looks one up in the application context of its datastore is kept"() { + given: 'a context whose lookup of the auditor blocks until it is released, and then finds none' + BlockingAuditorLookupContext context = new BlockingAuditorLookupContext() + context.refresh() + context.blockAuditorLookup = true + TestEventListener contextListener = new TestEventListener(Stub(Datastore) { + getMappingContext() >> null + getApplicationEventPublisher() >> new ConfigurableApplicationContextEventPublisher(context) + }) + AuditorAware setAuditor = { -> Optional.of('setUser') } as AuditorAware + Thread lookup = new Thread({ contextListener.auditorAware } as Runnable) + + when: 'an auditor is set while the lookup runs' + lookup.start() + assert context.lookupEntered.await(10, TimeUnit.SECONDS) + contextListener.auditorAware = setAuditor + context.release.countDown() + lookup.join(10000) + + then: + !lookup.alive + contextListener.auditorAware.is(setAuditor) + + cleanup: + context.release.countDown() + context.close() + } + private PersistentEntity auditedEntity() { PersistentEntity entity = null PersistentProperty createdBy = auditedProperty('createdBy') { -> entity } @@ -824,3 +853,20 @@ class TestEventListener extends AutoTimestampEventListener { } } } + +class BlockingAuditorLookupContext extends GenericApplicationContext { + + final CountDownLatch lookupEntered = new CountDownLatch(1) + final CountDownLatch release = new CountDownLatch(1) + volatile boolean blockAuditorLookup + + @Override + T getBean(Class requiredType) { + if (blockAuditorLookup && requiredType == AuditorAware) { + lookupEntered.countDown() + release.await(10, TimeUnit.SECONDS) + throw new NoSuchBeanDefinitionException(AuditorAware) + } + super.getBean(requiredType) + } +} From a12cab5a5442417b0b12ca13a8ed0bdf1e57b7d6 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Wed, 18 Nov 2026 14:07:49 -0800 Subject: [PATCH 079/115] Say that execute() routes only a multi-tenant class to a bound tenant Since a class that is not multi-tenant keeps its datastore inside a tenant block, the comment of getAutoTimestampEventListener() no longer matched what execute() does. --- .../main/groovy/org/grails/datastore/gorm/GormStaticApi.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/GormStaticApi.groovy b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/GormStaticApi.groovy index f35bc2e7d72..33e9b0dcdef 100644 --- a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/GormStaticApi.groovy +++ b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/GormStaticApi.groovy @@ -182,7 +182,7 @@ class GormStaticApi extends AbstractGormApi implements GormAllOperations session.datastore } as SessionCallback)) } From e17f6527525dc7106f25e8bc74d34d26aac0bdea Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Wed, 18 Nov 2026 14:09:59 -0800 Subject: [PATCH 080/115] Link the content negotiation upgrade notes by anchor What's New still named section 25 for the upgrade notes of grails.mime.negotiation.spring, which 9.0.x gave to classes that are not multi-tenant; it now names section 26. The notes get the anchor upgradingSpringFormatNegotiation, which the content negotiation guide links to in place of a section number, so the next renumbering cannot leave it stale, and What's New links to the guide section with <<...>>, so readers of the single-page guide stay on the page. The grails-upgrade skill names no section numbers, so it needs no change. --- grails-doc/src/en/guide/introduction/whatsNew.adoc | 5 ++--- grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc | 3 ++- grails-doc/src/en/guide/upgrading/upgrading90x.adoc | 1 + 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/grails-doc/src/en/guide/introduction/whatsNew.adoc b/grails-doc/src/en/guide/introduction/whatsNew.adoc index 8562f59a7ab..61964a48f47 100644 --- a/grails-doc/src/en/guide/introduction/whatsNew.adoc +++ b/grails-doc/src/en/guide/introduction/whatsNew.adoc @@ -171,10 +171,9 @@ With `grails.mime.negotiation.spring: true`, which Grails 10 makes the default, `withFormat` and the format `respond` writes in are resolved with Spring MVC's content negotiation manager, so that the `spring.mvc.contentnegotiation.*` settings apply to them, and the `format` parameter, URL extensions and the `grails.mime` settings of the `Accept` header decide what Spring MVC writes for an action too, such as a -`ResponseEntity` it returns. See xref:theWebLayer#springFormatNegotiation[Resolving Formats with Spring MVC Content -Negotiation]. +`ResponseEntity` it returns. See <>. -See xref:upgrading#upgrading90x[Upgrading from Grails 8 to Grails 9], sections 4 to 11, 13 to 16 and 25. +See xref:upgrading#upgrading90x[Upgrading from Grails 8 to Grails 9], sections 4 to 11, 13 to 16 and 26. ==== Hibernate 7 Criteria Queries Can Be Extended diff --git a/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc b/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc index b2e84961b73..2d2d5187103 100644 --- a/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc +++ b/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc @@ -347,7 +347,8 @@ type is configured for in the first configured format |Dropped: such a request, and one whose `Accept` header Spring cannot read, is not acceptable. |=== -Section 26 of xref:upgrading#upgrading90x[Upgrading from Grails 8 to Grails 9] lists how each answer changes. +<>, in the guide to +upgrading from Grails 8 to Grails 9, lists how each answer changes. ==== Testing Content Negotiation diff --git a/grails-doc/src/en/guide/upgrading/upgrading90x.adoc b/grails-doc/src/en/guide/upgrading/upgrading90x.adoc index 26476cea876..e5da07f9927 100644 --- a/grails-doc/src/en/guide/upgrading/upgrading90x.adoc +++ b/grails-doc/src/en/guide/upgrading/upgrading90x.adoc @@ -1484,6 +1484,7 @@ With GORM for MongoDB, a class that is not multi-tenant and was saved inside a t Data services choose their transaction manager in code generated at compile time, so a data service compiled against an earlier release keeps following the tenant until it is compiled again. +[[upgradingSpringFormatNegotiation]] ==== 26. Opt Into Spring MVC Content Negotiation for Grails Formats Set `grails.mime.negotiation.spring: true` to resolve `request.format`, `response.format`, `withFormat` and the format From c903885e6e517988215359dd24f819caafd0dcdd Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Wed, 18 Nov 2026 14:11:09 -0800 Subject: [PATCH 081/115] Test a text Spring MVC writes in the format of its URL extension as text /springWritten/text.json answered the plain string Grails labelled application/json, which reads as if JSON-labelled text were the intent. The spec now asks for /springWritten/text.text with Accept: application/json and expects text/plain: the URL extension still wins over the Accept header, and the body is what its type says. --- .../SpringFormatNegotiationSpec.groovy | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/grails-test-examples/spring-format-negotiation/src/integration-test/groovy/springformatnegotiation/SpringFormatNegotiationSpec.groovy b/grails-test-examples/spring-format-negotiation/src/integration-test/groovy/springformatnegotiation/SpringFormatNegotiationSpec.groovy index b261db0a940..e74d5168d17 100644 --- a/grails-test-examples/spring-format-negotiation/src/integration-test/groovy/springformatnegotiation/SpringFormatNegotiationSpec.groovy +++ b/grails-test-examples/spring-format-negotiation/src/integration-test/groovy/springformatnegotiation/SpringFormatNegotiationSpec.groovy @@ -39,9 +39,9 @@ class SpringFormatNegotiationSpec extends Specification implements HttpClientSup http('/springWritten/entity.json', Accept: 'text/html').assertStatus(200).contentType.startsWith('application/json') } - void 'a text Spring MVC writes for an action is labelled with the format of its URL extension'() { + void 'a text Spring MVC writes for an action is in the format of its URL extension, whatever the Accept header asks for'() { expect: - http('/springWritten/text.json', Accept: 'text/plain').assertStatus(200).contentType.startsWith('application/json') + http('/springWritten/text.text', Accept: 'application/json').assertStatus(200).contentType.startsWith('text/plain') } void 'a request that names a format no type is configured for is not acceptable'() { From a7ae6fcd8604a9b19cf9d44fdb93ec8219be3c3c Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Wed, 18 Nov 2026 21:04:23 -0800 Subject: [PATCH 082/115] Write a format only Spring MVC maps in the type it maps it to For a request a URL mapping handles, Grails reads the format parameter and the URL extension in place of Spring MVC's parameter strategy, and rejected a format grails.mime.types does not list before Spring MVC could map it: with spring.mvc.contentnegotiation.media-types.plain: text/plain, ?format=plain or .plain with Accept: application/json answered 406 for a ResponseEntity the action returns. A format grails.mime.types does not list is now the type Spring MVC's content negotiation manager maps it to, with or without favor-parameter, and only a format neither maps is not acceptable. A format Grails configures keeps its own types, and respond still answers 406 for a format only Spring MVC maps, since Grails has no type of it to render. The guide, the upgrade notes and the grails-upgrade skill say so. --- .../guide/theWebLayer/contentNegotiation.adoc | 9 ++-- .../src/en/guide/upgrading/upgrading90x.adoc | 6 ++- .../GrailsContentNegotiationStrategy.groovy | 26 ++++++++++-- ...ilsFormatContentNegotiationStrategy.groovy | 7 +++- .../mime/SpringFormatNegotiationSpec.groovy | 41 +++++++++++++++++++ .../upgrade/skills/grails-upgrade/SKILL.md | 2 +- .../grails-app/conf/application.yml | 4 ++ .../SpringFormatNegotiationSpec.groovy | 12 +++++- .../SpringFormatNegotiationRespondSpec.groovy | 5 ++- 9 files changed, 99 insertions(+), 13 deletions(-) diff --git a/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc b/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc index 2d2d5187103..77317acd7f5 100644 --- a/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc +++ b/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc @@ -304,7 +304,9 @@ Spring cannot read, has no format: `response.format` and `response.mimeType` are that accepts any type, with the `all` format. * The `format` parameter and a URL extension name the format, ahead of the `Accept` header, as before. The first value of the parameter counts, as for Spring MVC; a format no type is configured for is not acceptable, where the Grails rules -answer in the first configured format; and the `all` format, which is any type whether or not `grails.mime.types` lists +answer in the first configured format, but what Spring MVC writes for a format `grails.mime.types` does not list and +`spring.mvc.contentnegotiation.media-types` maps is in the type that maps it, while `respond` answers it with +`406 Not Acceptable`; and the `all` format, which is any type whether or not `grails.mime.types` lists it, leaves the format to the `Accept` header. Where `spring.mvc.contentnegotiation.favor-parameter` has Spring MVC read the `format` parameter too, Grails reads it in its place for a request a URL mapping handles, so `?format=all` is left to the `Accept` header there as well, while a Spring MVC endpoint reads it as Spring MVC does. @@ -327,8 +329,9 @@ no URL mapping handles. Each rule of Grails' negotiation becomes: |Grails rule |With `grails.mime.negotiation.spring` |The `format` parameter and URL extensions select the format -|Kept, ahead of Spring MVC's strategies, and also for what Spring MVC writes for the request. An unknown format is not -acceptable, and the `all` format leaves the choice to the `Accept` header. +|Kept, ahead of Spring MVC's strategies, and also for what Spring MVC writes for the request, which a format only +`spring.mvc.contentnegotiation.media-types` maps decides too. A format neither maps is not acceptable, and the `all` +format leaves the choice to the `Accept` header. |`grails.mime.use.accept.header` and `grails.mime.disable.accept.header.*` ignore the `Accept` header |Kept, also for what Spring MVC writes for the request. diff --git a/grails-doc/src/en/guide/upgrading/upgrading90x.adoc b/grails-doc/src/en/guide/upgrading/upgrading90x.adoc index e5da07f9927..2632ce87bd9 100644 --- a/grails-doc/src/en/guide/upgrading/upgrading90x.adoc +++ b/grails-doc/src/en/guide/upgrading/upgrading90x.adoc @@ -1520,8 +1520,10 @@ where the Grails rules answer as for a client that accepts any type, with the `a leniently. Code that reads `response.format` or `response.mimeType` must allow for `null`. * *The `format` parameter and URL extensions.* A format no type is configured for is not acceptable, to `respond`, which answers `406`, and to what Spring MVC writes, where the Grails rules answer in the first configured format; -`withFormat` answers it from the `Accept` header, as for a format it has no block for. The `all` format leaves the -format to the `Accept` header: `?format=all` with `Accept: application/json` is `json`, where it is otherwise `all`. The +`withFormat` answers it from the `Accept` header, as for a format it has no block for. A format that +`spring.mvc.contentnegotiation.media-types` maps, such as `plain: text/plain`, decides what Spring MVC writes, as +without the setting where `spring.mvc.contentnegotiation.favor-parameter` is on, while `respond` still answers `406` +for it. The `all` format leaves the format to the `Accept` header: `?format=all` with `Accept: application/json` is `json`, where it is otherwise `all`. The first value of the parameter counts. * *What Spring MVC writes for an action.* The `format` parameter and URL extensions now decide the type of a `ResponseEntity`, a `ProblemDetail` or another value Spring MVC writes for a request a URL mapping handles, where Spring diff --git a/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationStrategy.groovy b/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationStrategy.groovy index dc3d08d3aff..dd6543aca30 100644 --- a/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationStrategy.groovy +++ b/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationStrategy.groovy @@ -259,12 +259,32 @@ class GrailsContentNegotiationStrategy { } /** - * @return the media types of every configured MIME type Spring can read, which a request that names a format no type - * is configured for is not acceptable in + * The media type Spring MVC's content negotiation manager maps a format to, as {@code spring.mvc.contentnegotiation.media-types} + * maps one, for a format no type of {@code grails.mime.types} stands for, so that what Spring MVC writes for a request + * that names it is in that type, as without {@code grails.mime.negotiation.spring}. + * + * @param format the format + * @return the media type, as a list, or {@code null} where the manager maps no type to the format + * @since 9.0 + */ + List springMediaTypesOf(String format) { + MediaType mediaType = format != null ? + contentNegotiationManager?.get()?.mediaTypeMappings?.get(format.toLowerCase(Locale.ENGLISH)) : null + return mediaType != null ? [mediaType] : null + } + + /** + * @return the media types of every configured MIME type Spring can read, and of every format Spring MVC's content + * negotiation manager maps, which a request that names a format neither maps is not acceptable in * @since 9.0 */ List getConfiguredMediaTypes() { - return mediaTypes.findAll { it }.unique() as List + List configured = mediaTypes.findAll { it } as List + Map springMappings = contentNegotiationManager?.get()?.mediaTypeMappings + if (springMappings) { + configured.addAll(springMappings.values()) + } + return configured.unique() } /** diff --git a/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsFormatContentNegotiationStrategy.groovy b/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsFormatContentNegotiationStrategy.groovy index 9db039048b1..0ca63b474fc 100644 --- a/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsFormatContentNegotiationStrategy.groovy +++ b/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsFormatContentNegotiationStrategy.groovy @@ -36,7 +36,9 @@ import org.grails.web.servlet.mvc.GrailsWebRequest * names, with its {@code format} parameter or a URL extension, to the media types {@code grails.mime.types} gives it, * ahead of Spring MVC's own strategies, so that what Spring MVC writes for it is in the format Grails resolves for it. It * leaves any other request, such as one a Spring MVC endpoint handles, to them, and so a request whose format names no - * type, as the {@code all} format does. A request that names a format no type is configured for is not acceptable. + * type, as the {@code all} format does. A format no type of {@code grails.mime.types} stands for is the type Spring MVC's + * manager maps it to, as {@code spring.mvc.contentnegotiation.media-types} maps one, and a request that names a format + * neither maps is not acceptable. * * @since 9.0 */ @@ -65,7 +67,8 @@ class GrailsFormatContentNegotiationStrategy implements ContentNegotiationStrate if (format == null) { return MEDIA_TYPE_ALL_LIST } - List mediaTypes = strategy.mediaTypesOf(format.toString()) + // a format grails.mime.types configures keeps its types; any other is the type Spring MVC's manager maps it to + List mediaTypes = strategy.mediaTypesOf(format.toString()) ?: strategy.springMediaTypesOf(format.toString()) if (mediaTypes == null) { throw new HttpMediaTypeNotAcceptableException(strategy.configuredMediaTypes) } diff --git a/grails-mimetypes/src/test/groovy/org/grails/web/mime/SpringFormatNegotiationSpec.groovy b/grails-mimetypes/src/test/groovy/org/grails/web/mime/SpringFormatNegotiationSpec.groovy index 2cb13b1d2f8..65d4d459575 100644 --- a/grails-mimetypes/src/test/groovy/org/grails/web/mime/SpringFormatNegotiationSpec.groovy +++ b/grails-mimetypes/src/test/groovy/org/grails/web/mime/SpringFormatNegotiationSpec.groovy @@ -420,6 +420,38 @@ class SpringFormatNegotiationSpec extends Specification { 'all' || 'not acceptable' } + void "a format only Spring MVC's media types map is written in the type they map it to, #how"() { + given: 'plain mapped to text/plain in Spring MVC only, as spring.mvc.contentnegotiation.media-types.plain maps it' + start((Settings.MIME_NEGOTIATION_SPRING): true, (ParameterConfiguration.PLAIN): true, + (ParameterConfiguration.ENABLED): favorParameter, (ParameterConfiguration.NAME): 'format') + def request = request('Accept': 'application/json') + request.addParameter('format', format) + handledBy(request, urlMapping()) + + expect: + resolve(request) == expected + + where: + how | favorParameter | format || expected + 'without parameter negotiation' | false | 'plain' || [MediaType.TEXT_PLAIN] + 'with parameter negotiation of the format parameter' | true | 'plain' || [MediaType.TEXT_PLAIN] + 'whatever the case of the format' | false | 'PLAIN' || [MediaType.TEXT_PLAIN] + 'and a format neither maps is not acceptable' | false | 'unknown' || 'not acceptable' + 'and with parameter negotiation, still not acceptable' | true | 'unknown' || 'not acceptable' + } + + void "a format Grails configures keeps the types grails.mime.types gives it, whatever type Spring MVC's media types map it to"() { + given: + start((Settings.MIME_NEGOTIATION_SPRING): true, 'grails.mime.types': [all: '*/*', json: 'application/json', plain: 'text/csv'], + (ParameterConfiguration.PLAIN): true) + def request = request('Accept': 'application/json') + request.addParameter('format', 'plain') + handledBy(request, urlMapping()) + + expect: + resolve(request) == [MediaType.parseMediaType('text/csv')] + } + void 'the Content-Type #contentType is the request format #format'() { given: start((Settings.MIME_NEGOTIATION_SPRING): true) @@ -585,13 +617,19 @@ class SpringFormatNegotiationSpec extends Specification { static final String NAME = 'test.parameter.negotiation.name' + /** Maps the {@code plain} format to {@code text/plain}, as {@code spring.mvc.contentnegotiation.media-types.plain} does. */ + static final String PLAIN = 'test.parameter.negotiation.plain' + private final boolean enabled private final String name + private final boolean plain + ParameterConfiguration(ConfigurableEnvironment environment) { enabled = environment.getProperty(ENABLED, Boolean, false) name = environment.getProperty(NAME, 'mediaType') + plain = environment.getProperty(PLAIN, Boolean, false) } @Override @@ -599,6 +637,9 @@ class SpringFormatNegotiationSpec extends Specification { if (enabled) { configurer.favorParameter(true).parameterName(name) } + if (plain) { + configurer.mediaType('plain', MediaType.TEXT_PLAIN) + } } } } diff --git a/grails-skills/upgrade/skills/grails-upgrade/SKILL.md b/grails-skills/upgrade/skills/grails-upgrade/SKILL.md index 44a0c6b3ba2..69c42ecd510 100644 --- a/grails-skills/upgrade/skills/grails-upgrade/SKILL.md +++ b/grails-skills/upgrade/skills/grails-upgrade/SKILL.md @@ -448,7 +448,7 @@ With `grails.mime.negotiation.spring: true`, which Grails 10 makes the default, - `Accept: */*, application/json` is `json`, where it is `all` without the setting: a more specific type comes ahead of a wildcard of equal quality. A `+xml` type still comes ahead of the XML type of equal quality. - A type with the `+json` or `+xml` suffix that no configured type names, such as `application/merge-patch+json`, is the `json` or `xml` format, in `Accept` and in `Content-Type`, so data binding reads such a body as JSON. - A request that accepts no configured type, such as `Accept: image/png` alone, or whose `Accept` header Spring cannot read, has no format: `response.format` and `response.mimeType` are `null`, `respond` answers `406` and `withFormat` runs its `*` block, or none. Make code that reads them allow for `null`. -- A format parameter or URL extension no type is configured for is not acceptable, where it was answered in the first configured format. `?format=all` leaves the format to the `Accept` header. +- A format parameter or URL extension no type is configured for is not acceptable, where it was answered in the first configured format. A format only `spring.mvc.contentnegotiation.media-types` maps still decides what Spring MVC writes for an action, such as a `ResponseEntity`, but `respond` answers it with `406`. `?format=all` leaves the format to the `Accept` header. - A `ResponseEntity`, `ProblemDetail` or other value Spring MVC writes for an action follows the URL extension and the format parameter: `/book/show/1.json` is JSON whatever the `Accept` header says, and a map at `/book/show/1.html` answers `406`. - A wildcard such as `text/*` selects the first `withFormat` block, or the first format of the action, with a type compatible with it. diff --git a/grails-test-examples/spring-format-negotiation/grails-app/conf/application.yml b/grails-test-examples/spring-format-negotiation/grails-app/conf/application.yml index 4b8bc514017..f451db25241 100644 --- a/grails-test-examples/spring-format-negotiation/grails-app/conf/application.yml +++ b/grails-test-examples/spring-format-negotiation/grails-app/conf/application.yml @@ -30,3 +30,7 @@ spring: groovy: template: check-template-location: false + mvc: + contentnegotiation: + media-types: + plain: text/plain diff --git a/grails-test-examples/spring-format-negotiation/src/integration-test/groovy/springformatnegotiation/SpringFormatNegotiationSpec.groovy b/grails-test-examples/spring-format-negotiation/src/integration-test/groovy/springformatnegotiation/SpringFormatNegotiationSpec.groovy index e74d5168d17..ea570a0c924 100644 --- a/grails-test-examples/spring-format-negotiation/src/integration-test/groovy/springformatnegotiation/SpringFormatNegotiationSpec.groovy +++ b/grails-test-examples/spring-format-negotiation/src/integration-test/groovy/springformatnegotiation/SpringFormatNegotiationSpec.groovy @@ -44,7 +44,17 @@ class SpringFormatNegotiationSpec extends Specification implements HttpClientSup http('/springWritten/text.text', Accept: 'application/json').assertStatus(200).contentType.startsWith('text/plain') } - void 'a request that names a format no type is configured for is not acceptable'() { + void "a format only Spring MVC's media types map decides what Spring MVC writes for an action, by #how"() { + expect: + http(uri, Accept: 'application/json').assertStatus(200).contentType.startsWith('text/plain') + + where: + how | uri + 'the format parameter' | '/springWritten/text?format=plain' + 'the URL extension' | '/springWritten/text.plain' + } + + void 'a request that names a format neither grails.mime.types nor Spring MVC maps is not acceptable'() { expect: http('/springWritten/entity?format=unknown', Accept: 'application/json').assertStatus(406) } diff --git a/grails-test-suite-web/src/test/groovy/grails/rest/web/SpringFormatNegotiationRespondSpec.groovy b/grails-test-suite-web/src/test/groovy/grails/rest/web/SpringFormatNegotiationRespondSpec.groovy index 8d44f1fd709..7e7d0a8c1e7 100644 --- a/grails-test-suite-web/src/test/groovy/grails/rest/web/SpringFormatNegotiationRespondSpec.groovy +++ b/grails-test-suite-web/src/test/groovy/grails/rest/web/SpringFormatNegotiationRespondSpec.groovy @@ -187,7 +187,8 @@ class SpringFormatNegotiationWithoutAllRespondSpec extends Specification impleme /** * With {@code grails.mime.negotiation.spring} and Spring MVC's parameter negotiation of the {@code format} parameter, * which {@code spring.mvc.contentnegotiation.favor-parameter} enables, {@code respond} answers the format the parameter - * names, and the {@code all} format with the {@code Accept} header. + * names, and the {@code all} format with the {@code Accept} header. A format only Spring MVC's media types map is not one + * {@code respond} renders. */ class SpringFormatNegotiationParameterRespondSpec extends Specification implements ControllerUnitTest { @@ -196,6 +197,7 @@ class SpringFormatNegotiationParameterRespondSpec extends Specification implemen config[Settings.WEB_RENDERING_JSON_SPRING] = true config[Settings.MIME_NEGOTIATION_SPRING] = true config['spring.mvc.contentnegotiation.favor-parameter'] = true + config['spring.mvc.contentnegotiation.media-types.plain'] = 'text/plain' } } @@ -216,6 +218,7 @@ class SpringFormatNegotiationParameterRespondSpec extends Specification implemen 'all' || 200 | 'application/json' 'xml' || 200 | 'text/xml' 'unknown' || 406 | '' + 'plain' || 406 | '' } } From 159b5a8ce0b08adac09c25797c9ab6904104bce5 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Wed, 18 Nov 2026 22:40:47 -0800 Subject: [PATCH 083/115] Keep the record from before a first build when a known file changed during it The manifest always names more inputs than the Forge CLI watches without one, so on a first build the files were recorded as they were after the build. A command saved while the build ran then counted as compiled, and the manifest that did not list it counted as current. The files known before the build are read again once it is done. When one of them changed, the record from before the build is kept, so the next lookup runs the build again. A file only the new manifest names has no state from before the build, so a change to it during that build is still not seen. --- .../forge/cli/command/ProjectCommands.groovy | 5 +++- .../forge/cli/ProjectCommandsSpec.groovy | 26 +++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy index dc2e2b569ec..26ccc458030 100644 --- a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy @@ -145,8 +145,11 @@ class ProjectCommands { problem = 'its build wrote no manifest' } if (problem == null) { + // a manifest naming other files is recorded as they are now, unless one of the files known + // before the build changed while it ran: then the record from before makes the build run again Inputs after = inputsOf(manifest) - save(after == before ? recorded : snapshotOrNull(after), null, console) + boolean changedDuringBuild = after != before && snapshotOrNull(before) != recorded + save(after == before || changedDuringBuild ? recorded : snapshotOrNull(after), null, console) return } save(recorded, problem, console) diff --git a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy index f7bfc58b62e..0c6fc8fdb77 100644 --- a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy +++ b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy @@ -310,9 +310,15 @@ class ProjectCommandsSpec extends Specification { gradleInvocations().size() == 1 } - void 'a file changed while the build runs makes the build run again next time'() { + void 'a file changed while the build runs makes the build run again next time, with a manifest #naming'() { given: 'a build during which a command is saved' - buildWrites(URL_MAPPINGS_REPORT) + if (namesInputs) { + buildWritesWithInputs([buildDirectories: [dir.path], sourceDirectories: [new File(dir, 'grails-app/commands').path]], + URL_MAPPINGS_REPORT) + } + else { + buildWrites(URL_MAPPINGS_REPORT) + } new File(dir, 'during-build.sh').text = 'printf "class Hello {}" > "$1/grails-app/commands/Hello.groovy"\n' when: @@ -323,6 +329,22 @@ class ProjectCommandsSpec extends Specification { then: gradleInvocations().size() == 2 + + where: + namesInputs << [false, true] + naming = namesInputs ? 'naming its inputs' : 'naming no inputs' + } + + void 'a first build whose manifest names its inputs runs once while nothing changes'() { + given: + buildWritesWithInputs([buildDirectories: [dir.path], sourceDirectories: [new File(dir, 'grails-app/commands').path]], + URL_MAPPINGS_REPORT) + + when: + 3.times { ForgeCli.createCommandLine(context, false) } + + then: + gradleInvocations().size() == 1 } void 'the directories the manifest names are watched, such as those of the other projects of the build'() { From bdc635cfb36ec8f2afb7e5f86b2f20f890a4187f Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Wed, 18 Nov 2026 22:42:13 -0800 Subject: [PATCH 084/115] Keep the inputs of the last manifest for when cleaning the build removes it The inputs the manifest names were only in build/grails/commands.json. After a failed refresh and ./gradlew clean, the Forge CLI fell back to the project's own inputs, and the next failed build recorded only those. Fixing a command of another project of the build then never ran the build again. After a successful build the Forge CLI copies the manifest's inputs to .gradle/grails/commands.inputs, next to the record, and watches them while there is no manifest. A manifest that names no inputs removes the copy. --- .../forge/cli/command/ProjectCommands.groovy | 97 +++++++++++++++---- .../forge/cli/ProjectCommandsSpec.groovy | 32 ++++++ 2 files changed, 109 insertions(+), 20 deletions(-) diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy index 26ccc458030..24c39c38b33 100644 --- a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy @@ -22,6 +22,7 @@ import java.nio.file.Files import java.nio.file.Path import groovy.json.JsonException +import groovy.json.JsonOutput import groovy.json.JsonSlurper import groovy.transform.CompileStatic import groovy.transform.EqualsAndHashCode @@ -60,6 +61,12 @@ class ProjectCommands { /** Kept next to the record when the build could not write the manifest, so it is not run again for nothing */ static final String FAILED_PATH = '.gradle/grails/commands.failed' + /** + * A copy of the inputs the last manifest named, kept next to the record, so they are still watched when + * cleaning the build removed the manifest + */ + static final String INPUTS_PATH = '.gradle/grails/commands.inputs' + /** The newest version of the manifest's format this CLI reads */ static final int FORMAT_VERSION = 1 @@ -150,6 +157,7 @@ class ProjectCommands { Inputs after = inputsOf(manifest) boolean changedDuringBuild = after != before && snapshotOrNull(before) != recorded save(after == before || changedDuringBuild ? recorded : snapshotOrNull(after), null, console) + keepInputs(manifest) return } save(recorded, problem, console) @@ -257,27 +265,44 @@ class ProjectCommands { } /** - * The directories the commands depend on: those the manifest names, or, for a manifest that names - * none, those of the project and the root of its build. + * Keeps a copy of the inputs the manifest names, or forgets the last one when it names none. + */ + private void keepInputs(File manifest) { + Path kept = new File(projectDir, INPUTS_PATH).toPath() + try { + Object parsed = new JsonSlurper().parse(manifest, 'UTF-8') + Object inputs = parsed instanceof Map ? ((Map) parsed).inputs : null + if (inputs instanceof Map) { + Files.createDirectories(kept.parent) + Files.write(kept, JsonOutput.toJson(inputs).getBytes('UTF-8')) + } + else { + Files.deleteIfExists(kept) + } + } + catch (JsonException | IOException | RuntimeException ignored) { + // without the copy, a manifest removed by cleaning the build is watched through the project's inputs + } + } + + /** + * The directories the commands depend on: those the manifest names; without a manifest, as after + * cleaning the build, those the last manifest named; or else those of the project and the root of + * its build. */ private Inputs inputsOf(File manifest) { if (manifest.isFile()) { - try { - Object parsed = new JsonSlurper().parse(manifest, 'UTF-8') - Object inputs = parsed instanceof Map ? ((Map) parsed).inputs : null - if (inputs instanceof Map) { - Map given = (Map) inputs - List buildDirectories = files(given.buildDirectories) - List sourceDirectories = files(given.sourceDirectories) - List commandDirectories = given.containsKey('commandDirectories') ? files(given.commandDirectories) : [] - List sourceFiles = given.containsKey('sourceFiles') ? files(given.sourceFiles) : [] - if (buildDirectories != null && sourceDirectories != null && commandDirectories != null && sourceFiles != null) { - return new Inputs(buildDirectories, sourceDirectories, commandDirectories, sourceFiles) - } - } + Object parsed = parseOrNull(manifest) + Inputs named = inputsFrom(parsed instanceof Map ? ((Map) parsed).inputs : null) + if (named != null) { + return named } - catch (JsonException | IOException | RuntimeException ignored) { - // the manifest is reported when it is read + } + else { + File kept = new File(projectDir, INPUTS_PATH) + Inputs named = kept.isFile() ? inputsFrom(parseOrNull(kept)) : null + if (named != null) { + return named } } File rootDir = gradle.rootDir @@ -287,6 +312,37 @@ class ProjectCommands { [new File(projectDir, 'src/cli')], []) } + private static Object parseOrNull(File file) { + try { + return new JsonSlurper().parse(file, 'UTF-8') + } + catch (JsonException | IOException | RuntimeException ignored) { + // the manifest is reported when it is read + return null + } + } + + /** The inputs a manifest names, or none when it names none or they are not lists of absolute paths */ + private static Inputs inputsFrom(Object inputs) { + if (!(inputs instanceof Map)) { + return null + } + Map given = (Map) inputs + List buildDirectories = files(given.buildDirectories) + List sourceDirectories = files(given.sourceDirectories) + List commandDirectories = optionalFiles(given, 'commandDirectories') + List sourceFiles = optionalFiles(given, 'sourceFiles') + if (buildDirectories == null || sourceDirectories == null || commandDirectories == null || sourceFiles == null) { + return null + } + new Inputs(buildDirectories, sourceDirectories, commandDirectories, sourceFiles) + } + + /** A list a manifest written by an earlier Grails may not have, which then names no files */ + private static List optionalFiles(Map inputs, String key) { + inputs.containsKey(key) ? files(inputs[key]) : [] + } + /** The files of a list of absolute paths, or none when one is not such a path, such as a mistyped one */ private static List files(Object paths) { if (!(paths instanceof List)) { @@ -313,9 +369,10 @@ class ProjectCommands { /** * What the files the commands depend on are now: the build files directly in each build directory, - * and every file and directory below each source directory, apart from what builds and tools write - * next to a build script, and below each directory of commands, all of which are sources. Each file is listed with its time and size, so a deleted, added, replaced - * or changed file changes the list, whatever the clock said when it was written. + * every file and directory below each source directory, apart from what builds and tools write next + * to a build script, and below each directory of commands, all of which are sources. Each such file + * is listed with its time and size, so a deleted, added, replaced or changed file changes the list, + * whatever the clock said when it was written. */ private static List snapshot(Inputs inputs) { List lines = [] diff --git a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy index 0c6fc8fdb77..d52592f70cb 100644 --- a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy +++ b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy @@ -347,6 +347,38 @@ class ProjectCommandsSpec extends Specification { gradleInvocations().size() == 1 } + void "the inputs of the last manifest are still watched when cleaning the build removed it after a failed build"() { + given: "a manifest naming a plugin project's commands" + File command = new File(dir, 'plugins/audit/grails-app/commands/AuditCommand.groovy') + command.parentFile.mkdirs() + command.text = 'class AuditCommand {}' + buildWritesWithInputs([buildDirectories: [dir.path], sourceDirectories: [command.parentFile.path]], + URL_MAPPINGS_REPORT, AUDIT) + ForgeCli.createCommandLine(context, false) + + and: 'a change to the command that does not compile' + command.text = 'class AuditCommand { broken' + new File(dir, 'gradle-exit-code').text = '1' + ForgeCli.createCommandLine(context, false) + forgetInvocations() + + when: 'the build is cleaned, and the Forge CLI starts while the command is still broken' + new File(dir, 'build').deleteDir() + ForgeCli.createCommandLine(context, false) + + then: 'the failed build is not run again for nothing' + gradleInvocations() == [] + + when: 'the command is fixed' + command.text = 'class AuditCommand { /* fixed */ }' + new File(dir, 'gradle-exit-code').delete() + CommandLine commandLine = ForgeCli.createCommandLine(context, false) + + then: + gradleInvocations() == [prefix() + MANIFEST_TASK] + commandLine.subcommands.containsKey('audit') + } + void 'the directories the manifest names are watched, such as those of the other projects of the build'() { given: 'a manifest naming a plugin project next to the application' File plugin = new File(dir, 'plugins/audit') From 07d5333ff95de6dd23c5d0f801909c2f8562cced Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Wed, 18 Nov 2026 22:48:07 -0800 Subject: [PATCH 085/115] Watch the main sources for files named like a command A command compiled from a project's main sources registers itself, but the manifest named only the sources of the commands it already listed, so a command a plugin project of the build added to src/main/groovy left the old command list, and the Forge CLI rejected it. The manifest names the main Java and Groovy source directories of every project under inputs.commandFileDirectories. The Forge CLI records the files there named like a command, *Command.groovy or *Command.java, by name only: adding, removing or renaming a command runs the build, while editing or adding another source does not. Without a manifest naming inputs, the project's own src/main/groovy and src/main/java are watched the same way. The guide lists these, and that the inputs of the last manifest are kept for after ./gradlew clean. --- grails-doc/src/en/guide/commandLine.adoc | 16 ++++--- .../forge/cli/command/ProjectCommands.groovy | 42 ++++++++++++++++--- .../forge/cli/ProjectCommandsSpec.groovy | 38 +++++++++++++++++ .../ApplicationCommandManifestTask.groovy | 15 ++++++- .../commands/GrailsCliGradlePlugin.groovy | 7 ++++ .../ApplicationCommandManifestTaskSpec.groovy | 4 ++ 6 files changed, 109 insertions(+), 13 deletions(-) diff --git a/grails-doc/src/en/guide/commandLine.adoc b/grails-doc/src/en/guide/commandLine.adoc index 726d4882852..f24107f693c 100644 --- a/grails-doc/src/en/guide/commandLine.adoc +++ b/grails-doc/src/en/guide/commandLine.adoc @@ -244,15 +244,19 @@ files decide them: `src/cli/groovy` and `src/cli/resources`, where a command a plugin project of the build adds registers itself; * the source file of each command it lists that one of the projects of the build compiles, such as a - command of a plugin project in `src/main/groovy`. - -Each time it runs the task, the Forge CLI records those files, with their times and sizes, in the -project's `.gradle/grails` directory. It runs the task again when one of them is added or deleted, or + command of a plugin project in `src/main/groovy`; +* the names of the files in the main Java and Groovy sources of every project, such as `src/main/groovy`, + that are named like a command, `AuditCommand.groovy` or `AuditCommand.java`, so a command added there + counts, while editing or adding another source does not. + +Each time it runs the task, the Forge CLI records those files, with their times and sizes, and the +directories the manifest names, in the project's `.gradle/grails` directory, so they are still watched +after `./gradlew clean` removes the manifest. It runs the task again when one of them is added or deleted, or its time or size changes, whether the time goes forward or back, and when a manifest has no record, as one written by `./gradlew generateCommandManifest` alone. So a plugin added to the build brings its commands the next time a project command is asked for. A change the Forge CLI cannot see, such as a new snapshot of a -plugin, or a command added to a project's `src/main/groovy` rather than to `grails-app/commands` or a -`cli` source set, is picked up by running `./gradlew generateCommandManifest`. +plugin, or a command in the main sources of a file not named after it, such as a class that becomes a +command, is picked up by running `./gradlew generateCommandManifest`. The build runs only when the interactive prompt starts or a command the Forge CLI does not have is named, also after the Forge CLI's own options, as in `grails-forge-cli -x dbm-update`. Its own commands, diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy index 24c39c38b33..53c6fbd0e72 100644 --- a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy @@ -78,6 +78,9 @@ class ProjectCommands { /** Directories whose files are sources, so a package below them named like an output is read */ private static final List SOURCE_DIRS = ['src', 'grails-app'] + /** How the file of a command is named, after the class whose name gives the command its own */ + private static final List COMMAND_FILE_SUFFIXES = ['Command.groovy', 'Command.java'] + /** What tools write anywhere, sources included */ private static final List TOOL_FILES = ['node_modules', '.DS_Store'] @@ -309,7 +312,8 @@ class ProjectCommands { new Inputs([projectDir, rootDir].unique(), [new File(rootDir, 'gradle'), new File(rootDir, 'buildSrc'), new File(projectDir, 'grails-app/commands'), new File(projectDir, 'src/main/resources/META-INF')], - [new File(projectDir, 'src/cli')], []) + [new File(projectDir, 'src/cli')], + [new File(projectDir, 'src/main/groovy'), new File(projectDir, 'src/main/java')], []) } private static Object parseOrNull(File file) { @@ -331,11 +335,13 @@ class ProjectCommands { List buildDirectories = files(given.buildDirectories) List sourceDirectories = files(given.sourceDirectories) List commandDirectories = optionalFiles(given, 'commandDirectories') + List commandFileDirectories = optionalFiles(given, 'commandFileDirectories') List sourceFiles = optionalFiles(given, 'sourceFiles') - if (buildDirectories == null || sourceDirectories == null || commandDirectories == null || sourceFiles == null) { + if (buildDirectories == null || sourceDirectories == null || commandDirectories == null || + commandFileDirectories == null || sourceFiles == null) { return null } - new Inputs(buildDirectories, sourceDirectories, commandDirectories, sourceFiles) + new Inputs(buildDirectories, sourceDirectories, commandDirectories, commandFileDirectories, sourceFiles) } /** A list a manifest written by an earlier Grails may not have, which then names no files */ @@ -372,7 +378,8 @@ class ProjectCommands { * every file and directory below each source directory, apart from what builds and tools write next * to a build script, and below each directory of commands, all of which are sources. Each such file * is listed with its time and size, so a deleted, added, replaced or changed file changes the list, - * whatever the clock said when it was written. + * whatever the clock said when it was written. Below each directory of command files, only the + * names of the files named like a command are listed. */ private static List snapshot(Inputs inputs) { List lines = [] @@ -395,6 +402,10 @@ class ProjectCommands { for (File dir : inputs.commandDirectories) { walk(dir, lines, true) } + // a new command there registers itself, and names its file after itself; other sources are left out + for (File dir : inputs.commandFileDirectories) { + walkNames(dir, lines) + } for (File file : inputs.sourceFiles) { walk(file, lines, true) } @@ -446,6 +457,25 @@ class ProjectCommands { } } + /** The files below the directory named like a command, by name only, so editing one changes nothing */ + private static void walkNames(File dir, List lines) { + if (Files.isSymbolicLink(dir.toPath())) { + return + } + File[] children = dir.listFiles() + if (children == null) { + return + } + for (File child : children.sort(false) { File it -> it.name }) { + if (child.isDirectory()) { + walkNames(child, lines) + } + else if (COMMAND_FILE_SUFFIXES.any { String suffix -> child.name.endsWith(suffix) }) { + lines << "named\t${child.absolutePath}".toString() + } + } + } + private static String describe(File file) { "file\t${file.absolutePath}\t${file.lastModified()}\t${file.length()}".toString() } @@ -456,13 +486,15 @@ class ProjectCommands { final List buildDirectories final List sourceDirectories final List commandDirectories + final List commandFileDirectories final List sourceFiles Inputs(List buildDirectories, List sourceDirectories, List commandDirectories, - List sourceFiles) { + List commandFileDirectories, List sourceFiles) { this.buildDirectories = buildDirectories this.sourceDirectories = sourceDirectories this.commandDirectories = commandDirectories + this.commandFileDirectories = commandFileDirectories this.sourceFiles = sourceFiles } } diff --git a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy index d52592f70cb..7944a584959 100644 --- a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy +++ b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy @@ -379,6 +379,44 @@ class ProjectCommandsSpec extends Specification { commandLine.subcommands.containsKey('audit') } + void "a command added to a plugin project's main sources is offered and runs, while other sources change nothing"() { + given: "a manifest naming the main sources of a plugin project next to the application" + File sources = new File(dir, 'plugins/audit/src/main/groovy') + File service = new File(sources, 'com/acme/AuditService.groovy') + service.parentFile.mkdirs() + service.text = 'class AuditService {}' + Map inputs = [buildDirectories: [dir.path], sourceDirectories: [], commandFileDirectories: [sources.path]] + buildWritesWithInputs(inputs, URL_MAPPINGS_REPORT) + ForgeCli.createCommandLine(context, false) + forgetInvocations() + + when: 'a command class is added' + new File(sources, 'com/acme/AuditCommand.groovy').text = 'class AuditCommand {}' + buildWritesWithInputs(inputs, URL_MAPPINGS_REPORT, AUDIT) + int exitCode = ForgeCli.execute(context, 'audit') + + then: 'the build runs once, and the new command runs instead of being rejected' + exitCode == 0 + gradleInvocations()*.get(5) == ['generateCommandManifest', 'runCommand'] + commandArguments() == ['audit'] + + when: 'another source is changed and one is added' + forgetInvocations() + service.text = 'class AuditService { void audit() {} }' + new File(sources, 'com/acme/Helper.groovy').text = 'class Helper {}' + ForgeCli.createCommandLine(context, false) + + then: + gradleInvocations() == [] + + when: 'the command is removed' + new File(sources, 'com/acme/AuditCommand.groovy').delete() + ForgeCli.createCommandLine(context, false) + + then: + gradleInvocations() == [prefix() + MANIFEST_TASK] + } + void 'the directories the manifest names are watched, such as those of the other projects of the build'() { given: 'a manifest naming a plugin project next to the application' File plugin = new File(dir, 'plugins/audit') diff --git a/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTask.groovy b/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTask.groovy index 267bd5b673e..c7ec95fc42d 100644 --- a/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTask.groovy +++ b/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTask.groovy @@ -43,7 +43,8 @@ import javax.inject.Inject *

The file also names, under {@code inputs}, the directories whose files decide the commands without * being on the classpath yet: those whose build files decide the build, those that hold the sources * of commands or of the build's logic, those of the {@code cli} source sets, where every source can be a - * command, and the source files of the listed commands found in the projects of the build. A tool reading the file runs the task again when one of their files changes.

+ * command, those of the main sources, where a file named like a command can add one, and the source files + * of the listed commands found in the projects of the build. A tool reading the file runs the task again when one of their files changes.

* * @since 9.0 */ @@ -60,6 +61,7 @@ abstract class ApplicationCommandManifestTask extends JavaExec { buildDirectories.convention([]) sourceDirectories.convention([]) commandDirectories.convention([]) + commandFileDirectories.convention([]) commandSourceRoots.convention([]) } @@ -91,6 +93,14 @@ abstract class ApplicationCommandManifestTask extends JavaExec { @Input abstract ListProperty getCommandDirectories() + /** + * @return the directories of the main Java and Groovy sources of every project of the build, where a + * command compiled from a new file, named after its class as a command is, registers itself; a tool + * watches the names of those files, not what they hold + */ + @Input + abstract ListProperty getCommandFileDirectories() + /** * @return the source directories of every project of the build, where the source of a command the * manifest lists is looked for by its class, so a change to it counts too @@ -133,7 +143,8 @@ abstract class ApplicationCommandManifestTask extends JavaExec { super.exec() Map manifest = (Map) new JsonSlurper().parse(file, 'UTF-8') manifest.inputs = [buildDirectories: buildDirectories.get(), sourceDirectories: sourceDirectories.get(), - commandDirectories: commandDirectories.get(), sourceFiles: commandSources(manifest.commands)] + commandDirectories: commandDirectories.get(), + commandFileDirectories: commandFileDirectories.get(), sourceFiles: commandSources(manifest.commands)] file.setText(JsonOutput.prettyPrint(JsonOutput.toJson(manifest)), 'UTF-8') } } diff --git a/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/GrailsCliGradlePlugin.groovy b/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/GrailsCliGradlePlugin.groovy index 743873b43e8..a1ed8b6f63f 100644 --- a/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/GrailsCliGradlePlugin.groovy +++ b/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/GrailsCliGradlePlugin.groovy @@ -803,6 +803,12 @@ class GrailsCliGradlePlugin implements Plugin { SourceSet cli = sourceSets?.findByName(GrailsCliArtifactGradlePlugin.CLI_SOURCE_SET_NAME) cli ? cli.allSource.srcDirs*.absolutePath : [] }.unique().sort() + // so is one compiled from the main sources, which is named after its class like any command + List commandFileDirectories = root.allprojects.collectMany { Project each -> + def sourceSets = each.extensions.findByType(org.gradle.api.tasks.SourceSetContainer) + SourceSet main = sourceSets?.findByName(SourceSet.MAIN_SOURCE_SET_NAME) + main ? main.allJava.srcDirs*.absolutePath : [] + }.unique().sort() // every source set, as a plugin keeps its commands in its cli source set, src/cli/groovy List commandSourceRoots = root.allprojects.collectMany { Project each -> def sourceSets = each.extensions.findByType(org.gradle.api.tasks.SourceSetContainer) @@ -812,6 +818,7 @@ class GrailsCliGradlePlugin implements Plugin { it.buildDirectories.set(buildDirectories) it.sourceDirectories.set(sourceDirectories) it.commandDirectories.set(commandDirectories) + it.commandFileDirectories.set(commandFileDirectories) it.commandSourceRoots.set(commandSourceRoots) } } diff --git a/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTaskSpec.groovy b/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTaskSpec.groovy index a15639259a4..6b43fb5f4d2 100644 --- a/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTaskSpec.groovy +++ b/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTaskSpec.groovy @@ -106,6 +106,10 @@ class ApplicationCommandManifestTaskSpec extends GradleSpecification { inputs.commandDirectories.collect { new File(it).canonicalFile } as Set == [new File(root, 'app/src/cli/java'), new File(root, 'app/src/cli/resources')] as Set + and: 'the main sources of every project, where a file named like a command can add one' + inputs.commandFileDirectories.collect { new File(it).canonicalFile } as Set == + [new File(root, 'app/src/main/java'), new File(root, 'manifest-writer/src/main/java')] as Set + and: 'every included build, also those that bring plugins of the build' inputs.sourceDirectories.collect { new File(it).canonicalFile }.containsAll([new File(root, 'shared'), new File(root, 'build-logic')]) From dda082a04230ff388e7087c8f7ddc7d132e95599 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Thu, 19 Nov 2026 06:52:25 -0800 Subject: [PATCH 086/115] Run the build again when a command that did not compile is fixed Files named like a command in the main sources are recorded by name only, and a failed build is recorded so it is not run again for nothing. So a new command that did not compile was recorded by name, fixing it changed nothing recorded, and the Forge CLI kept rejecting it. While the last build failed, those files are recorded with their times and sizes, so fixing one runs the build again; nothing else changing still runs no build. After a successful build they go back to being recorded by name, so editing a command or another source does not run it. --- grails-doc/src/en/guide/commandLine.adoc | 5 ++- .../forge/cli/command/ProjectCommands.groovy | 37 ++++++++++------- .../forge/cli/ProjectCommandsSpec.groovy | 40 +++++++++++++++++++ 3 files changed, 65 insertions(+), 17 deletions(-) diff --git a/grails-doc/src/en/guide/commandLine.adoc b/grails-doc/src/en/guide/commandLine.adoc index f24107f693c..181bba0827c 100644 --- a/grails-doc/src/en/guide/commandLine.adoc +++ b/grails-doc/src/en/guide/commandLine.adoc @@ -279,8 +279,9 @@ $ GRAILS_FORGE_RUN_BUILD=false grails-forge-cli When the build fails, as it does while the application does not compile or in a project of a Grails release without `generateCommandManifest`, the Forge CLI says so once and keeps offering the commands of the file the build last wrote, if any. It does not run the build for them again until one of the files -above changes, also after `./gradlew clean`; run `./gradlew generateCommandManifest` to see what went -wrong. A command of the file that the Forge CLI cannot offer, such as one declaring an option named +above changes, also after `./gradlew clean`. While the build fails, editing a file named like a command +counts as a change too, so fixing a new command that did not compile runs the build again. Run +`./gradlew generateCommandManifest` to see what went wrong. A command of the file that the Forge CLI cannot offer, such as one declaring an option named `help`, is reported and left out, and the others are offered. The build runs through the Gradle wrapper of the project, or of the build it belongs to: the wrapper in diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy index 53c6fbd0e72..35a15e8687d 100644 --- a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy @@ -141,7 +141,10 @@ class ProjectCommands { private void update(File manifest, ConsoleOutput console) { // the files are recorded before the build runs, so one changed while it runs counts next time Inputs before = inputsOf(manifest) - List recorded = snapshotOrNull(before) + List recorded = snapshotOrNull(before, false) + // a failed build is recorded with the contents of the files named like a command too, so fixing + // a new command that did not compile runs the build again, while nothing else changed does not + List recordedWhole = snapshotOrNull(before, true) console.out('Reading the commands of the project from its build...') String problem try { @@ -158,12 +161,12 @@ class ProjectCommands { // a manifest naming other files is recorded as they are now, unless one of the files known // before the build changed while it ran: then the record from before makes the build run again Inputs after = inputsOf(manifest) - boolean changedDuringBuild = after != before && snapshotOrNull(before) != recorded - save(after == before || changedDuringBuild ? recorded : snapshotOrNull(after), null, console) + boolean changedDuringBuild = after != before && snapshotOrNull(before, false) != recorded + save(after == before || changedDuringBuild ? recorded : snapshotOrNull(after, false), null, console) keepInputs(manifest) return } - save(recorded, problem, console) + save(recordedWhole, problem, console) String kept = manifest.isFile() ? 'The commands it listed before are offered' : 'Its commands are not offered' console.warning("Could not read the commands of the project: ${problem}. ${kept}, and the build is not run " + 'for them again until a build file changes. A project of Grails 9 or later lists them with its ' + @@ -260,7 +263,8 @@ class ProjectCommands { try { // without a record, as when the project's own build wrote the manifest, a file deleted since // cannot be told from one that was never there - return !record.isFile() || Files.readAllLines(record.toPath()) != snapshot(inputsOf(manifest)) + boolean failedLast = new File(projectDir, FAILED_PATH).isFile() + return !record.isFile() || Files.readAllLines(record.toPath()) != snapshot(inputsOf(manifest), failedLast) } catch (IOException | RuntimeException ignored) { return true @@ -378,10 +382,10 @@ class ProjectCommands { * every file and directory below each source directory, apart from what builds and tools write next * to a build script, and below each directory of commands, all of which are sources. Each such file * is listed with its time and size, so a deleted, added, replaced or changed file changes the list, - * whatever the clock said when it was written. Below each directory of command files, only the - * names of the files named like a command are listed. + * whatever the clock said when it was written. Below each directory of command files, the files + * named like a command are listed by name only, or, after a failed build, with their times and sizes. */ - private static List snapshot(Inputs inputs) { + private static List snapshot(Inputs inputs, boolean wholeCommandFiles) { List lines = [] for (File dir : inputs.buildDirectories) { File[] files = dir.listFiles() @@ -404,7 +408,7 @@ class ProjectCommands { } // a new command there registers itself, and names its file after itself; other sources are left out for (File dir : inputs.commandFileDirectories) { - walkNames(dir, lines) + walkNames(dir, lines, wholeCommandFiles) } for (File file : inputs.sourceFiles) { walk(file, lines, true) @@ -413,9 +417,9 @@ class ProjectCommands { } /** The record of the files, or none when they cannot be read, such as one of a path the system refuses */ - private static List snapshotOrNull(Inputs inputs) { + private static List snapshotOrNull(Inputs inputs, boolean wholeCommandFiles) { try { - return snapshot(inputs) + return snapshot(inputs, wholeCommandFiles) } catch (RuntimeException ignored) { return null @@ -457,8 +461,11 @@ class ProjectCommands { } } - /** The files below the directory named like a command, by name only, so editing one changes nothing */ - private static void walkNames(File dir, List lines) { + /** + * The files below the directory named like a command: by name only, so editing one changes nothing, + * or else with their times and sizes + */ + private static void walkNames(File dir, List lines, boolean whole) { if (Files.isSymbolicLink(dir.toPath())) { return } @@ -468,10 +475,10 @@ class ProjectCommands { } for (File child : children.sort(false) { File it -> it.name }) { if (child.isDirectory()) { - walkNames(child, lines) + walkNames(child, lines, whole) } else if (COMMAND_FILE_SUFFIXES.any { String suffix -> child.name.endsWith(suffix) }) { - lines << "named\t${child.absolutePath}".toString() + lines << (whole ? describe(child) : "named\t${child.absolutePath}".toString()) } } } diff --git a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy index 7944a584959..9a13cd3d980 100644 --- a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy +++ b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy @@ -417,6 +417,46 @@ class ProjectCommandsSpec extends Specification { gradleInvocations() == [prefix() + MANIFEST_TASK] } + void "a new command in a plugin project's main sources that did not compile runs the build again once it is fixed"() { + given: "a manifest naming the main sources of a plugin project next to the application" + File sources = new File(dir, 'plugins/audit/src/main/groovy') + File command = new File(sources, 'com/acme/AuditCommand.groovy') + command.parentFile.mkdirs() + Map inputs = [buildDirectories: [dir.path], sourceDirectories: [], commandFileDirectories: [sources.path]] + buildWritesWithInputs(inputs, URL_MAPPINGS_REPORT) + ForgeCli.createCommandLine(context, false) + forgetInvocations() + + when: 'a command is added that does not compile' + command.text = 'class AuditCommand { broken' + new File(dir, 'gradle-exit-code').text = '1' + int rejected = ForgeCli.execute(context, 'audit') + ForgeCli.createCommandLine(context, false) + + then: 'the failed build runs once, not again while nothing changes' + rejected != 0 + gradleInvocations() == [prefix() + MANIFEST_TASK] + + when: 'the command is fixed' + forgetInvocations() + command.text = 'class AuditCommand { /* fixed */ }' + new File(dir, 'gradle-exit-code').delete() + buildWritesWithInputs(inputs, URL_MAPPINGS_REPORT, AUDIT) + int exitCode = ForgeCli.execute(context, 'audit') + + then: 'the build runs again, and the command runs' + exitCode == 0 + gradleInvocations()*.get(5) == ['generateCommandManifest', 'runCommand'] + + when: 'the command is edited once the build has succeeded' + forgetInvocations() + command.text = 'class AuditCommand { /* edited */ }' + ForgeCli.createCommandLine(context, false) + + then: 'it is watched by name again' + gradleInvocations() == [] + } + void 'the directories the manifest names are watched, such as those of the other projects of the build'() { given: 'a manifest naming a plugin project next to the application' File plugin = new File(dir, 'plugins/audit') From d083edaa5fe93bc0c1f080155840f04e451dfa01 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Thu, 19 Nov 2026 08:49:49 -0800 Subject: [PATCH 087/115] Look up the negotiation strategy once for each request GrailsContentNegotiationStrategy.forRequest looked the strategy up in the application context on every call, and, in a context without MimeTypesConfiguration, such as one a test builds by hand, built a new one each time. request.format, response.format, response.mimeType, withFormat and respond each call it, also without grails.mime.negotiation.spring, since the setting is only known through the strategy. The strategy is now kept as an attribute of the request it was resolved for, so each request looks it up once, for every caller, and the RestResponder trait gets no field. --- .../GrailsContentNegotiationStrategy.groovy | 15 +++++++++- .../mime/SpringFormatNegotiationSpec.groovy | 28 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationStrategy.groovy b/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationStrategy.groovy index dd6543aca30..9348ecf1a3b 100644 --- a/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationStrategy.groovy +++ b/grails-mimetypes/src/main/groovy/org/grails/web/mime/GrailsContentNegotiationStrategy.groovy @@ -71,6 +71,8 @@ class GrailsContentNegotiationStrategy { private static final String ALL_FORMAT = 'all' + private static final String REQUEST_STRATEGY_ATTRIBUTE = GrailsContentNegotiationStrategy.name + '.REQUEST_STRATEGY' + /** The formats a type with the structured syntax suffix of RFC 6839 of their name is a document of. */ private static final Set SUFFIX_FORMATS = Set.of('json', 'xml') @@ -335,13 +337,24 @@ class GrailsContentNegotiationStrategy { /** * The strategy that resolves the formats of a request: that of the application's {@code MimeTypesConfiguration}, or, * in a context without it, such as one a test builds by hand, one built for the request from the application's - * settings. + * settings. It is looked up once for each request, which keeps it as an attribute, so that {@code request.format}, + * {@code response.format}, {@code withFormat} and {@code respond} do not look it up again. * * @param request the request * @return the strategy * @since 9.0 */ static GrailsContentNegotiationStrategy forRequest(HttpServletRequest request) { + Object kept = request.getAttribute(REQUEST_STRATEGY_ATTRIBUTE) + if (kept instanceof GrailsContentNegotiationStrategy) { + return (GrailsContentNegotiationStrategy) kept + } + GrailsContentNegotiationStrategy strategy = lookUp(request) + request.setAttribute(REQUEST_STRATEGY_ATTRIBUTE, strategy) + return strategy + } + + private static GrailsContentNegotiationStrategy lookUp(HttpServletRequest request) { ApplicationContext applicationContext = GrailsWebRequest.lookup()?.applicationContext if (applicationContext == null && request.servletContext != null) { applicationContext = WebApplicationContextUtils.getWebApplicationContext(request.servletContext) diff --git a/grails-mimetypes/src/test/groovy/org/grails/web/mime/SpringFormatNegotiationSpec.groovy b/grails-mimetypes/src/test/groovy/org/grails/web/mime/SpringFormatNegotiationSpec.groovy index 65d4d459575..bd5ed8518f5 100644 --- a/grails-mimetypes/src/test/groovy/org/grails/web/mime/SpringFormatNegotiationSpec.groovy +++ b/grails-mimetypes/src/test/groovy/org/grails/web/mime/SpringFormatNegotiationSpec.groovy @@ -69,6 +69,34 @@ class SpringFormatNegotiationSpec extends Specification { RequestContextHolder.resetRequestAttributes() } + void 'the strategy that resolves the formats of a request is looked up once for it, #how'() { + given: + if (configured) { + start((Settings.MIME_NEGOTIATION_SPRING): true) + } + def first = configured ? request('Accept': 'application/json') : GrailsWebMockUtil.bindMockWebRequest().currentRequest + def strategy = GrailsContentNegotiationStrategy.forRequest(first) + + when: 'the format of the request is read, and the strategy asked for again' + String format = GrailsWebRequest.lookup().currentResponse.format + + then: + format == (configured ? 'json' : 'all') + GrailsContentNegotiationStrategy.forRequest(first).is(strategy) + + when: 'another request is negotiated' + RequestContextHolder.resetRequestAttributes() + def second = configured ? request('Accept': 'application/json') : GrailsWebMockUtil.bindMockWebRequest().currentRequest + + then: "it gets the application's strategy, or, without one, one of its own" + GrailsContentNegotiationStrategy.forRequest(second).is(strategy) == configured + + where: + how | configured + "of the application's MimeTypesConfiguration" | true + 'built for the request in a context without one' | false + } + void "without the setting, Spring MVC's manager is left as it is"() { when: start([:]) From c9608089d2aa7d98ec068105fd061053850212c4 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Thu, 19 Nov 2026 08:56:32 -0800 Subject: [PATCH 088/115] Mark the requests a URL mapping handles only with grails.mime.negotiation.spring UrlMappingsHandlerMapping set BEST_MATCHING_HANDLER_ATTRIBUTE to the mapping it matched, and put an interceptor that takes it off first in every handler chain, and a web unit test set it for the action it calls, whatever the setting said. Only the content negotiation strategies the setting installs read it. The handler mapping now does both only where markHandledRequests is set, which the URL mappings plugin sets from grails.mime.negotiation.spring in the Grails configuration, as the negotiation reads it, and a web unit test sets the attribute only with the setting. Without it, the handler chain and the request attributes are those of 9.0.x. --- .../SpringFormatNegotiationRespondSpec.groovy | 18 +++++ .../testing/web/GrailsWebUnitTest.groovy | 12 ++-- ...appingsBeanDefinitionsPostProcessor.groovy | 15 ++++ .../mapping/UrlMappingsGrailsPlugin.groovy | 7 +- .../UrlMappingsGrailsPluginSpec.groovy | 27 +++++++ .../mvc/UrlMappingsHandlerMapping.groovy | 23 +++++- .../mvc/UrlMappingsHandlerMappingSpec.groovy | 71 +++++++++++++++++-- 7 files changed, 158 insertions(+), 15 deletions(-) diff --git a/grails-test-suite-web/src/test/groovy/grails/rest/web/SpringFormatNegotiationRespondSpec.groovy b/grails-test-suite-web/src/test/groovy/grails/rest/web/SpringFormatNegotiationRespondSpec.groovy index 7e7d0a8c1e7..2dae949093f 100644 --- a/grails-test-suite-web/src/test/groovy/grails/rest/web/SpringFormatNegotiationRespondSpec.groovy +++ b/grails-test-suite-web/src/test/groovy/grails/rest/web/SpringFormatNegotiationRespondSpec.groovy @@ -23,11 +23,13 @@ import org.springframework.http.MediaType import org.springframework.http.ProblemDetail import org.springframework.web.accept.ContentNegotiationManager import org.springframework.web.context.request.ServletWebRequest +import org.springframework.web.servlet.HandlerMapping import grails.artefact.Artefact import grails.config.Settings import grails.testing.web.controllers.ControllerUnitTest import grails.validation.Validateable +import grails.web.mapping.UrlMappingInfo import org.grails.web.mime.GrailsContentNegotiationManager import spock.lang.Specification @@ -49,6 +51,14 @@ class SpringFormatNegotiationRespondSpec extends Specification implements Contro applicationContext.getBean('mvcContentNegotiationManager', ContentNegotiationManager) instanceof GrailsContentNegotiationManager } + void 'the request of the action a web unit test calls is handled by a URL mapping, as the URL mappings mark it in an application'() { + when: + controller + + then: + request.getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE) instanceof UrlMappingInfo + } + void 'what Spring MVC writes for an action of a web unit test is negotiated with the format the request names, as in an application'() { given: request.addHeader('Accept', 'application/json') @@ -231,6 +241,14 @@ class GrailsFormatNegotiationRespondSpec extends Specification implements Contro { config -> config[Settings.WEB_RENDERING_JSON_SPRING] = true } } + void 'the request of the action a web unit test calls is not marked as handled by a URL mapping, as in Grails 9'() { + when: + controller + + then: + request.getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE) == null + } + void "Spring MVC's manager is left as it is"() { expect: applicationContext.getBean('mvcContentNegotiationManager', ContentNegotiationManager).getClass() == ContentNegotiationManager diff --git a/grails-testing-support-web/src/main/groovy/grails/testing/web/GrailsWebUnitTest.groovy b/grails-testing-support-web/src/main/groovy/grails/testing/web/GrailsWebUnitTest.groovy index a66b100fd89..b6b1d8fa797 100644 --- a/grails-testing-support-web/src/main/groovy/grails/testing/web/GrailsWebUnitTest.groovy +++ b/grails-testing-support-web/src/main/groovy/grails/testing/web/GrailsWebUnitTest.groovy @@ -28,6 +28,7 @@ import org.springframework.web.servlet.HandlerMapping import grails.artefact.Controller import grails.artefact.TagLibrary +import grails.config.Settings import grails.core.GrailsClass import grails.core.GrailsControllerClass import grails.core.gsp.GrailsTagLibClass @@ -150,10 +151,13 @@ trait GrailsWebUnitTest implements GrailsUnitTest { webRequest.request.setAttribute(GrailsApplicationAttributes.CONTROLLER, controller) webRequest.controllerName = GrailsNameUtils.getLogicalPropertyName(controller.class.name, ControllerArtefactHandler.TYPE) - // A URL mapping handles the request, as in an application, so that what Spring MVC writes for the action is - // negotiated as there - webRequest.request.setAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE, - new ForwardUrlMappingInfo(controllerName: webRequest.controllerName)) + if (grailsApplication.config.getProperty(Settings.MIME_NEGOTIATION_SPRING, Boolean, + Boolean.valueOf(Settings.MIME_NEGOTIATION_SPRING_DEFAULT))) { + // A URL mapping handles the request, as the URL mappings mark it in an application with the setting, so that + // what Spring MVC writes for the action is negotiated as there + webRequest.request.setAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE, + new ForwardUrlMappingInfo(controllerName: webRequest.controllerName)) + } controller as T } diff --git a/grails-url-mappings/src/main/groovy/org/grails/plugins/web/mapping/UrlMappingsBeanDefinitionsPostProcessor.groovy b/grails-url-mappings/src/main/groovy/org/grails/plugins/web/mapping/UrlMappingsBeanDefinitionsPostProcessor.groovy index ea7f4621df2..b5df11d4ae9 100644 --- a/grails-url-mappings/src/main/groovy/org/grails/plugins/web/mapping/UrlMappingsBeanDefinitionsPostProcessor.groovy +++ b/grails-url-mappings/src/main/groovy/org/grails/plugins/web/mapping/UrlMappingsBeanDefinitionsPostProcessor.groovy @@ -72,15 +72,27 @@ class UrlMappingsBeanDefinitionsPostProcessor implements BeanDefinitionRegistryP private final boolean reloadEnabled private final boolean corsFilterEnabled private final boolean resolveHiddenHttpMethod + private final boolean markHandledRequests UrlMappingsBeanDefinitionsPostProcessor(boolean reloadEnabled, boolean corsFilterEnabled) { this(reloadEnabled, corsFilterEnabled, false) } UrlMappingsBeanDefinitionsPostProcessor(boolean reloadEnabled, boolean corsFilterEnabled, boolean resolveHiddenHttpMethod) { + this(reloadEnabled, corsFilterEnabled, resolveHiddenHttpMethod, false) + } + + /** + * @param markHandledRequests whether the handler mapping marks the requests its mappings handle for Spring MVC's + * content negotiation, as {@code grails.mime.negotiation.spring} has it + * @since 9.0 + */ + UrlMappingsBeanDefinitionsPostProcessor(boolean reloadEnabled, boolean corsFilterEnabled, boolean resolveHiddenHttpMethod, + boolean markHandledRequests) { this.reloadEnabled = reloadEnabled this.corsFilterEnabled = corsFilterEnabled this.resolveHiddenHttpMethod = resolveHiddenHttpMethod + this.markHandledRequests = markHandledRequests } @Override @@ -95,6 +107,9 @@ class UrlMappingsBeanDefinitionsPostProcessor implements BeanDefinitionRegistryP if (resolveHiddenHttpMethod) { handlerMapping.propertyValues.addPropertyValue('resolveHiddenHttpMethod', true) } + if (markHandledRequests) { + handlerMapping.propertyValues.addPropertyValue('markHandledRequests', true) + } registry.registerBeanDefinition('urlMappingsHandlerMapping', handlerMapping) } diff --git a/grails-url-mappings/src/main/groovy/org/grails/plugins/web/mapping/UrlMappingsGrailsPlugin.groovy b/grails-url-mappings/src/main/groovy/org/grails/plugins/web/mapping/UrlMappingsGrailsPlugin.groovy index 7c374bbbb21..550a34cf3c4 100644 --- a/grails-url-mappings/src/main/groovy/org/grails/plugins/web/mapping/UrlMappingsGrailsPlugin.groovy +++ b/grails-url-mappings/src/main/groovy/org/grails/plugins/web/mapping/UrlMappingsGrailsPlugin.groovy @@ -132,6 +132,10 @@ class UrlMappingsGrailsPlugin extends Plugin { // With no servlet filter rewriting the request method, the handler mapping resolves "_method" // itself so browser forms still reach the PUT, PATCH and DELETE routes of a 'resources' mapping. boolean resolveHiddenHttpMethod = !HiddenHttpMethod.isServletFilterMode(environment) + // Only Spring MVC's content negotiation of grails.mime.negotiation.spring reads which requests a URL + // mapping handles; it is read from the Grails configuration, as the negotiation reads it + boolean markHandledRequests = grailsApplication.config?.getProperty(Settings.MIME_NEGOTIATION_SPRING, Boolean, + Boolean.valueOf(Settings.MIME_NEGOTIATION_SPRING_DEFAULT)) ?: false // The url-mapping holder is a ProxyFactoryBean (reload mode) whose produced UrlMappings // type must stay answerable from the bean definition for by-type autowiring of @@ -140,7 +144,8 @@ class UrlMappingsGrailsPlugin extends Plugin { // FactoryBean.OBJECT_TYPE_ATTRIBUTE. See that class for why the attribute is load-bearing. registry.registerBean('urlMappingsBeanDefinitionsPostProcessor', UrlMappingsBeanDefinitionsPostProcessor) { it.infrastructure().supplier { - new UrlMappingsBeanDefinitionsPostProcessor(reloadEnabled, corsFilterEnabled, resolveHiddenHttpMethod) + new UrlMappingsBeanDefinitionsPostProcessor(reloadEnabled, corsFilterEnabled, resolveHiddenHttpMethod, + markHandledRequests) } } } diff --git a/grails-url-mappings/src/test/groovy/org/grails/plugins/web/mapping/UrlMappingsGrailsPluginSpec.groovy b/grails-url-mappings/src/test/groovy/org/grails/plugins/web/mapping/UrlMappingsGrailsPluginSpec.groovy index 25fd4e32e4e..8ae5d239f59 100644 --- a/grails-url-mappings/src/test/groovy/org/grails/plugins/web/mapping/UrlMappingsGrailsPluginSpec.groovy +++ b/grails-url-mappings/src/test/groovy/org/grails/plugins/web/mapping/UrlMappingsGrailsPluginSpec.groovy @@ -25,9 +25,11 @@ import org.springframework.beans.factory.support.BeanRegistryAdapter import org.springframework.beans.factory.support.DefaultListableBeanFactory import org.springframework.core.env.StandardEnvironment +import grails.config.Settings import grails.core.GrailsApplication import grails.core.GrailsClass import grails.web.mapping.UrlMappings +import org.grails.config.PropertySourcesConfig import org.grails.core.artefact.UrlMappingsArtefactHandler import org.grails.spring.beans.factory.HotSwappableTargetSourceFactoryBean import org.grails.web.mapping.UrlMappingsHolderFactoryBean @@ -103,6 +105,31 @@ class UrlMappingsGrailsPluginSpec extends Specification { !registry.containsBeanDefinition('urlMappingsTargetSource') } + void "the handler mapping marks the requests its mappings handle only with grails.mime.negotiation.spring: #setting"() { + given: + def beanFactory = new DefaultListableBeanFactory() + def config = new PropertySourcesConfig(setting == null ? [:] : [(Settings.MIME_NEGOTIATION_SPRING): setting]) + def application = Mock(GrailsApplication) { + getArtefacts(UrlMappingsArtefactHandler.TYPE) >> { [Mock(GrailsClass)] as GrailsClass[] } + getConfig() >> config + } + applyRegistrar(beanFactory, application) + def registry = new DefaultListableBeanFactory() + + when: + beanFactory.getBean('urlMappingsBeanDefinitionsPostProcessor', UrlMappingsBeanDefinitionsPostProcessor) + .postProcessBeanDefinitionRegistry(registry) + + then: + registry.getBeanDefinition('urlMappingsHandlerMapping').propertyValues.getPropertyValue('markHandledRequests')?.value == marks + + where: + setting || marks + null || null + false || null + true || true + } + private static void applyRegistrar(DefaultListableBeanFactory beanFactory, GrailsApplication application) { def plugin = new UrlMappingsGrailsPlugin(grailsApplication: application) def registrar = plugin.beanRegistrar() diff --git a/grails-web-url-mappings/src/main/groovy/org/grails/web/mapping/mvc/UrlMappingsHandlerMapping.groovy b/grails-web-url-mappings/src/main/groovy/org/grails/web/mapping/mvc/UrlMappingsHandlerMapping.groovy index 617b7983e96..b6f7b61aae6 100644 --- a/grails-web-url-mappings/src/main/groovy/org/grails/web/mapping/mvc/UrlMappingsHandlerMapping.groovy +++ b/grails-web-url-mappings/src/main/groovy/org/grails/web/mapping/mvc/UrlMappingsHandlerMapping.groovy @@ -83,6 +83,17 @@ class UrlMappingsHandlerMapping extends AbstractHandlerMapping { */ boolean resolveHiddenHttpMethod = false + /** + * Whether the mapping that handles a request is set as its + * {@link org.springframework.web.servlet.HandlerMapping#BEST_MATCHING_HANDLER_ATTRIBUTE} until the dispatch + * completes, so that what Spring MVC negotiates for the request can tell that a URL mapping handles it. Set with + * {@code grails.mime.negotiation.spring}, whose strategies are all that read it; without it, the request and the + * handler chain are left as Spring MVC's handler mapping leaves them. + * + * @since 9.0 + */ + boolean markHandledRequests = false + protected UrlMappingsHolder urlMappingsHolder // Deliberately not UrlPathHelper.defaultInstance: that instance is read-only, and this field is // protected, so a subclass configuring it (alwaysUseFullPath and friends) must keep working. @@ -165,8 +176,10 @@ class UrlMappingsHandlerMapping extends AbstractHandlerMapping { // before(), so that a failure in one is reported under the route that failed chain.addInterceptor(webRequestInterceptorCount, OBSERVATION_ROUTE_HANDLER) chain.addInterceptor(ERROR_HANDLING_HANDLER) - // First, so that it completes last, whichever interceptor stops the request - chain.addInterceptor(0, HANDLED_REQUEST_HANDLER) + if (markHandledRequests) { + // First, so that it completes last, whichever interceptor stops the request + chain.addInterceptor(0, HANDLED_REQUEST_HANDLER) + } return chain } @@ -185,11 +198,15 @@ class UrlMappingsHandlerMapping extends AbstractHandlerMapping { * The mapping that handles the request, which is also the request's * {@link org.springframework.web.servlet.HandlerMapping#BEST_MATCHING_HANDLER_ATTRIBUTE}, as each of Spring MVC's * handler mappings sets it for the handler it finds, until the dispatch completes, so that what Spring MVC - * negotiates for the request while a URL mapping handles it can tell that one does. + * negotiates for the request while a URL mapping handles it can tell that one does, where + * {@link #markHandledRequests} says so. */ @Override protected Object getHandlerInternal(HttpServletRequest request) throws Exception { Object handler = matchHandler(request) + if (!markHandledRequests) { + return handler + } if (handler != null) { request.setAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE, handler) } diff --git a/grails-web-url-mappings/src/test/groovy/org/grails/web/mapping/mvc/UrlMappingsHandlerMappingSpec.groovy b/grails-web-url-mappings/src/test/groovy/org/grails/web/mapping/mvc/UrlMappingsHandlerMappingSpec.groovy index 85cbaeb2826..7f7590cefc9 100644 --- a/grails-web-url-mappings/src/test/groovy/org/grails/web/mapping/mvc/UrlMappingsHandlerMappingSpec.groovy +++ b/grails-web-url-mappings/src/test/groovy/org/grails/web/mapping/mvc/UrlMappingsHandlerMappingSpec.groovy @@ -31,6 +31,7 @@ import grails.util.GrailsWebMockUtil import grails.web.Action import grails.web.HyphenatedUrlConverter import grails.web.mapping.AbstractUrlMappingsSpec +import grails.web.mapping.UrlMappingInfo import grails.web.mime.MimeTypeResolver import org.grails.web.errors.GrailsWrappedRuntimeException import org.grails.web.mapping.DefaultUrlMappingData @@ -356,7 +357,7 @@ class UrlMappingsHandlerMappingSpec extends AbstractUrlMappingsSpec { result.view.getUrl() == "/index.html" } - void "the handler chain completes the handled request last, keeps WebRequestInterceptors first, then the route, and appends the error handling last"() { + void "the handler chain keeps WebRequestInterceptors first, then the route, and appends the error handling last"() { given: "a handler mapping with both a WebRequestInterceptor and an ordinary HandlerInterceptor" def handlerMapping = handlerMapping() def plainInterceptor = new HandlerInterceptor() {} @@ -374,22 +375,45 @@ class UrlMappingsHandlerMappingSpec extends AbstractUrlMappingsSpec { webRequest.request.setRequestURI("/foo/bar") def interceptors = handlerMapping.getHandler(webRequest.request).interceptorList - then: "the handled request is completed by the first interceptor, which completes last whichever stops the request" - interceptors[0].class.simpleName == 'HandledRequestHandler' - - and: "the WebRequestInterceptor is adapted and ordered next, ahead of ordinary interceptors" - interceptors[1] instanceof WebRequestHandlerInterceptorAdapter + then: "the WebRequestInterceptor is adapted and ordered first, ahead of ordinary interceptors" + interceptors[0] instanceof WebRequestHandlerInterceptorAdapter interceptors.contains(plainInterceptor) and: "the route is set ahead of the ordinary interceptors, which can fail, and the error handling is last" + interceptors[1].class.simpleName == 'ObservationRouteHandler' + interceptors.indexOf(plainInterceptor) > 1 + interceptors[-1].class.simpleName == 'ErrorHandlingHandler' + !interceptors.any { it.class.simpleName == 'HandledRequestHandler' } + } + + void "where the handler mapping marks the requests it handles, the handled request is completed first in the chain, ahead of the WebRequestInterceptors"() { + given: + def handlerMapping = handlerMapping() + handlerMapping.markHandledRequests = true + def webRequestInterceptor = new WebRequestInterceptor() { + void preHandle(WebRequest request) {} + void postHandle(WebRequest request, ModelMap model) {} + void afterCompletion(WebRequest request, Exception ex) {} + } + handlerMapping.setWebRequestInterceptors([webRequestInterceptor] as WebRequestInterceptor[]) + handlerMapping.setApplicationContext(new StaticWebApplicationContext()) + + when: + def webRequest = GrailsWebMockUtil.bindMockWebRequest() + webRequest.request.setRequestURI("/foo/bar") + def interceptors = handlerMapping.getHandler(webRequest.request).interceptorList + + then: "the first interceptor completes last, whichever interceptor stops the request" + interceptors[0].class.simpleName == 'HandledRequestHandler' + interceptors[1] instanceof WebRequestHandlerInterceptorAdapter interceptors[2].class.simpleName == 'ObservationRouteHandler' - interceptors.indexOf(plainInterceptor) > 2 interceptors[-1].class.simpleName == 'ErrorHandlingHandler' } void "the Grails interceptors are shared rather than allocated per request"() { given: def handlerMapping = handlerMapping() + handlerMapping.markHandledRequests = true handlerMapping.setApplicationContext(new StaticWebApplicationContext()) when: "two separate requests are handled" @@ -655,6 +679,34 @@ class UrlMappingsHandlerMappingSpec extends AbstractUrlMappingsSpec { context.pathPattern == '/foo/bar' } + void 'by default, the mapping that handles a request is not set as its best matching handler, as before Grails 9'() { + given: + def grailsApplication = new DefaultGrailsApplication(FooController) + grailsApplication.initialise() + def handler = new UrlMappingsHandlerMapping(new GrailsControllerUrlMappings(grailsApplication, getUrlMappingsHolder { + '/foo/bar'(controller: 'foo', action: 'bar') + })) + def webRequest = GrailsWebMockUtil.bindMockWebRequest() + def request = webRequest.request + request.requestURI = '/foo/bar' + + when: + def chain = handler.getHandler(request) + + then: + chain.handler instanceof UrlMappingInfo + request.getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE) == null + + when: 'another handler mapping set it, and the URL mappings match nothing for the next dispatch' + def earlier = new ForwardUrlMappingInfo(controllerName: 'foo') + request.setAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE, earlier) + request.requestURI = '/spring/endpoint' + + then: 'it is left as it is' + handler.getHandler(request) == null + request.getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE).is(earlier) + } + void 'the mapping that handles a request is its best matching handler, as a Spring MVC handler mapping sets it'() { given: def grailsApplication = new DefaultGrailsApplication(FooController) @@ -663,6 +715,7 @@ class UrlMappingsHandlerMappingSpec extends AbstractUrlMappingsSpec { '/foo/bar'(controller: 'foo', action: 'bar') '/'(view: '/index') })) + handler.markHandledRequests = true def request = GrailsWebMockUtil.bindMockWebRequest().request when: 'a request a controller mapping matches' @@ -690,6 +743,7 @@ class UrlMappingsHandlerMappingSpec extends AbstractUrlMappingsSpec { def handler = new UrlMappingsHandlerMapping(new GrailsControllerUrlMappings(grailsApplication, getUrlMappingsHolder { '/foo/bar'(controller: 'foo', action: 'bar') })) + handler.markHandledRequests = true boolean proceeds = proceed handler.setHandlerInterceptors([new HandlerInterceptor() { @Override @@ -724,6 +778,7 @@ class UrlMappingsHandlerMappingSpec extends AbstractUrlMappingsSpec { def handler = new UrlMappingsHandlerMapping(new GrailsControllerUrlMappings(grailsApplication, getUrlMappingsHolder { '/foo/bar'(controller: 'foo', action: 'bar') })) + handler.markHandledRequests = true def webRequest = GrailsWebMockUtil.bindMockWebRequest() def request = webRequest.request request.requestURI = '/foo/bar' @@ -745,6 +800,7 @@ class UrlMappingsHandlerMappingSpec extends AbstractUrlMappingsSpec { def handler = new UrlMappingsHandlerMapping(new GrailsControllerUrlMappings(grailsApplication, getUrlMappingsHolder { '/foo/bar'(controller: 'foo', action: 'bar') })) + handler.markHandledRequests = true def request = GrailsWebMockUtil.bindMockWebRequest().request request.requestURI = '/foo/bar' handler.getHandler(request) @@ -763,6 +819,7 @@ class UrlMappingsHandlerMappingSpec extends AbstractUrlMappingsSpec { def handler = new UrlMappingsHandlerMapping(getUrlMappingsHolder { '/foo/bar'(controller: 'foo', action: 'bar') }) + handler.markHandledRequests = true def request = GrailsWebMockUtil.bindMockWebRequest().request def springHandler = new Object() request.setAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE, springHandler) From d8c20685c5ddf7eed7efd632a05b63da1d814b06 Mon Sep 17 00:00:00 2001 From: Mattias Reichel Date: Thu, 19 Nov 2026 18:12:21 +0100 Subject: [PATCH 089/115] fix(datastore-web): keep a request's session open over an include, and close it when the request goes asynchronous The open session in view interceptor that the MongoDB plugin registers unbound and closed the session of a request when an include within it completed, so the rest of the page ran without a session. When a request went asynchronous, Spring MVC called neither postHandle nor afterCompletion on the thread that had started it, so the session stayed bound to that thread, and the next request the thread served used it and flushed it. The interceptor now records in the request when it uses a session that is already bound, and leaves that session to the call that opened it, as Spring's Hibernate interceptor does. It implements AsyncWebRequestInterceptor: when a request goes asynchronous, it flushes the session it opened, as postHandle does, and unbinds and closes it. --- .../support/OpenSessionInViewInterceptor.java | 79 ++++++++-- .../OpenSessionInViewInterceptorSpec.groovy | 142 ++++++++++++++++++ .../mongodb/base/build.gradle | 1 + .../AsyncRequestSessionController.groovy | 39 +++++ .../tests/RequestSessionController.groovy | 50 ++++++ .../grails-app/views/requestSession/page.gsp | 22 +++ .../tests/OpenSessionInViewSpec.groovy | 53 ++++++- .../functional/tests/RequestSessions.groovy | 58 +++++++ 8 files changed, 432 insertions(+), 12 deletions(-) create mode 100644 grails-datastore-web/src/test/groovy/org/grails/datastore/mapping/web/support/OpenSessionInViewInterceptorSpec.groovy create mode 100644 grails-test-examples/mongodb/base/grails-app/controllers/functional/tests/AsyncRequestSessionController.groovy create mode 100644 grails-test-examples/mongodb/base/grails-app/controllers/functional/tests/RequestSessionController.groovy create mode 100644 grails-test-examples/mongodb/base/grails-app/views/requestSession/page.gsp create mode 100644 grails-test-examples/mongodb/base/src/main/groovy/functional/tests/RequestSessions.groovy diff --git a/grails-datastore-web/src/main/groovy/org/grails/datastore/mapping/web/support/OpenSessionInViewInterceptor.java b/grails-datastore-web/src/main/groovy/org/grails/datastore/mapping/web/support/OpenSessionInViewInterceptor.java index dfea96d6f76..88710966939 100644 --- a/grails-datastore-web/src/main/groovy/org/grails/datastore/mapping/web/support/OpenSessionInViewInterceptor.java +++ b/grails-datastore-web/src/main/groovy/org/grails/datastore/mapping/web/support/OpenSessionInViewInterceptor.java @@ -26,8 +26,8 @@ import org.springframework.transaction.support.TransactionSynchronizationManager; import org.springframework.ui.ModelMap; +import org.springframework.web.context.request.AsyncWebRequestInterceptor; import org.springframework.web.context.request.WebRequest; -import org.springframework.web.context.request.WebRequestInterceptor; import org.grails.datastore.mapping.core.Datastore; import org.grails.datastore.mapping.core.DatastoreUtils; @@ -36,12 +36,19 @@ /** * A {@link org.springframework.web.context.request.WebRequestInterceptor} instance that - * handles opening a Datastore session within the scope of a request + * handles opening a Datastore session within the scope of a request. + * + *

A request that finds a session already bound, such as an include within a request that opened one, uses that + * session and leaves it to the one that opened it. When a request goes asynchronous, the session it opened is + * flushed and closed as when the request completes, so that it does not stay bound to the thread for the next + * request that thread serves.

*/ -public class OpenSessionInViewInterceptor implements WebRequestInterceptor { +public class OpenSessionInViewInterceptor implements AsyncWebRequestInterceptor { private static final Log LOG = LogFactory.getLog(OpenSessionInViewInterceptor.class); + private static final String PARTICIPATE_SUFFIX = ".PARTICIPATE"; + Datastore datastore; FlushModeType flushMode = FlushModeType.AUTO; @@ -55,6 +62,10 @@ public Datastore getDatastore() { public void preHandle(WebRequest webRequest) throws Exception { if (hasSessionBound()) { + // a session is already open for this request: use it, and leave it to the one that opened it + String participateAttributeName = getParticipateAttributeName(); + Integer count = (Integer) webRequest.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST); + webRequest.setAttribute(participateAttributeName, count != null ? count + 1 : 1, WebRequest.SCOPE_REQUEST); return; } @@ -77,13 +88,7 @@ public void postHandle(WebRequest webRequest, ModelMap modelMap) throws Exceptio SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(getDatastore()); LOG.debug("Flushing single Datastore Session in OpenSessionInViewInterceptor"); - final Session session = sessionHolder.getSession(); - - if (session.getFlushMode() == FlushModeType.AUTO) { - if (session.hasTransaction()) { - session.flush(); - } - } + flushIfNecessary(sessionHolder.getSession()); } protected boolean hasSessionBound() { @@ -91,7 +96,7 @@ protected boolean hasSessionBound() { } public void afterCompletion(WebRequest webRequest, Exception e) throws Exception { - if (!hasSessionBound()) { + if (decrementParticipateCount(webRequest) || !hasSessionBound()) { return; } @@ -101,4 +106,56 @@ public void afterCompletion(WebRequest webRequest, Exception e) throws Exception LOG.debug("Closing single Datastore Session in OpenSessionInViewInterceptor"); DatastoreUtils.closeSession(sessionHolder.getSession()); } + + /** + * Called instead of {@link #postHandle} and {@link #afterCompletion} on the thread that started asynchronous + * handling. Flushes the session this interceptor opened, as {@link #postHandle} would, and closes it, so that the + * thread goes back to its pool without a session bound. + */ + public void afterConcurrentHandlingStarted(WebRequest webRequest) { + if (decrementParticipateCount(webRequest) || !hasSessionBound()) { + return; + } + + SessionHolder sessionHolder = + (SessionHolder) TransactionSynchronizationManager.getResource(getDatastore()); + Session session = sessionHolder.getSession(); + try { + LOG.debug("Flushing single Datastore Session in OpenSessionInViewInterceptor as asynchronous handling starts"); + flushIfNecessary(session); + } + finally { + TransactionSynchronizationManager.unbindResource(getDatastore()); + LOG.debug("Closing single Datastore Session in OpenSessionInViewInterceptor as asynchronous handling starts"); + DatastoreUtils.closeSession(session); + } + } + + private void flushIfNecessary(Session session) { + if (session.getFlushMode() == FlushModeType.AUTO && session.hasTransaction()) { + session.flush(); + } + } + + /** + * @return {@code true} when this request used a session that was already open, which is then left as it is + */ + private boolean decrementParticipateCount(WebRequest webRequest) { + String participateAttributeName = getParticipateAttributeName(); + Integer count = (Integer) webRequest.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST); + if (count == null) { + return false; + } + if (count > 1) { + webRequest.setAttribute(participateAttributeName, count - 1, WebRequest.SCOPE_REQUEST); + } + else { + webRequest.removeAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST); + } + return true; + } + + private String getParticipateAttributeName() { + return OpenSessionInViewInterceptor.class.getName() + '.' + System.identityHashCode(getDatastore()) + PARTICIPATE_SUFFIX; + } } diff --git a/grails-datastore-web/src/test/groovy/org/grails/datastore/mapping/web/support/OpenSessionInViewInterceptorSpec.groovy b/grails-datastore-web/src/test/groovy/org/grails/datastore/mapping/web/support/OpenSessionInViewInterceptorSpec.groovy new file mode 100644 index 00000000000..f08533283dd --- /dev/null +++ b/grails-datastore-web/src/test/groovy/org/grails/datastore/mapping/web/support/OpenSessionInViewInterceptorSpec.groovy @@ -0,0 +1,142 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.datastore.mapping.web.support + +import jakarta.persistence.FlushModeType + +import org.grails.datastore.mapping.core.Datastore +import org.grails.datastore.mapping.core.DatastoreUtils +import org.grails.datastore.mapping.core.Session +import org.grails.datastore.mapping.transactions.SessionHolder +import org.springframework.mock.web.MockHttpServletRequest +import org.springframework.transaction.support.TransactionSynchronizationManager +import org.springframework.web.context.request.ServletWebRequest +import spock.lang.Specification + +/** + * The session of a request is opened by the first interceptor call that finds none bound, used by an include within + * that request, and closed when the request completes or when it goes asynchronous. + */ +class OpenSessionInViewInterceptorSpec extends Specification { + + Datastore datastore = Mock() + Session session = Mock() + OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor(datastore: datastore) + ServletWebRequest request = new ServletWebRequest(new MockHttpServletRequest('GET', '/books')) + + void setup() { + datastore.connect() >> session + session.datastore >> datastore + session.flushMode >> FlushModeType.AUTO + session.hasTransaction() >> true + } + + void cleanup() { + TransactionSynchronizationManager.resourceMap.keySet().toList().each { key -> + TransactionSynchronizationManager.unbindResource(key) + } + } + + private Session boundSession() { + ((SessionHolder) TransactionSynchronizationManager.getResource(datastore))?.session + } + + void 'a request without a session opens one, flushes it after the action and closes it when it completes'() { + when: + interceptor.preHandle(request) + + then: + boundSession().is(session) + + when: + interceptor.postHandle(request, null) + + then: + 1 * session.flush() + + when: + interceptor.afterCompletion(request, null) + + then: + 1 * session.disconnect() + boundSession() == null + } + + void 'an include uses the session of the request and leaves it bound and open'() { + given: 'the request has opened its session' + interceptor.preHandle(request) + + when: 'an include within it starts and completes' + interceptor.preHandle(request) + interceptor.postHandle(request, null) + interceptor.afterCompletion(request, null) + + then: + 0 * session.disconnect() + boundSession().is(session) + + when: 'the request completes' + interceptor.afterCompletion(request, null) + + then: + 1 * session.disconnect() + boundSession() == null + } + + void 'a request that goes asynchronous flushes and closes its session and leaves the thread without one'() { + given: + interceptor.preHandle(request) + + when: + interceptor.afterConcurrentHandlingStarted(request) + + then: + 1 * session.flush() + 1 * session.disconnect() + boundSession() == null + } + + void 'the session is closed and unbound when its flush fails as the request goes asynchronous'() { + given: + interceptor.preHandle(request) + + when: + interceptor.afterConcurrentHandlingStarted(request) + + then: + 1 * session.flush() >> { throw new IllegalStateException('flush failed') } + thrown(IllegalStateException) + 1 * session.disconnect() + boundSession() == null + } + + void 'a session that was bound before the request stays bound and open when the request goes asynchronous'() { + given: 'a session bound by the caller' + DatastoreUtils.bindSession(session) + + when: + interceptor.preHandle(request) + interceptor.afterConcurrentHandlingStarted(request) + + then: + 0 * session.flush() + 0 * session.disconnect() + boundSession().is(session) + } +} diff --git a/grails-test-examples/mongodb/base/build.gradle b/grails-test-examples/mongodb/base/build.gradle index ca62c6af906..2a360631bf6 100644 --- a/grails-test-examples/mongodb/base/build.gradle +++ b/grails-test-examples/mongodb/base/build.gradle @@ -45,6 +45,7 @@ dependencies { implementation 'org.apache.grails:grails-layout' } implementation 'org.apache.grails:grails-gsp' + implementation 'org.apache.grails:grails-async' testAndDevelopmentOnly platform(project(':grails-bom')) testAndDevelopmentOnly 'org.webjars.npm:jquery' diff --git a/grails-test-examples/mongodb/base/grails-app/controllers/functional/tests/AsyncRequestSessionController.groovy b/grails-test-examples/mongodb/base/grails-app/controllers/functional/tests/AsyncRequestSessionController.groovy new file mode 100644 index 00000000000..cf05ce6c749 --- /dev/null +++ b/grails-test-examples/mongodb/base/grails-app/controllers/functional/tests/AsyncRequestSessionController.groovy @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package functional.tests + +import java.util.concurrent.TimeUnit + +import grails.async.web.AsyncController + +/** + * An action that starts asynchronous handling itself, with the Servlet API. + */ +class AsyncRequestSessionController implements AsyncController { + + def start() { + RequestSessions.saveBeforeGoingAsynchronous() + final context = startAsync() + context.start { + RequestSessions.release.await(30, TimeUnit.SECONDS) + context.response.writer.write('done') + context.complete() + } + } +} diff --git a/grails-test-examples/mongodb/base/grails-app/controllers/functional/tests/RequestSessionController.groovy b/grails-test-examples/mongodb/base/grails-app/controllers/functional/tests/RequestSessionController.groovy new file mode 100644 index 00000000000..d1705c8f2ab --- /dev/null +++ b/grails-test-examples/mongodb/base/grails-app/controllers/functional/tests/RequestSessionController.groovy @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package functional.tests + +import java.util.concurrent.TimeUnit + +import grails.async.Promises + +/** + * Actions that show which session a request reads through: a page that includes another action, and an action + * that returns a promise, so that the request goes asynchronous. + */ +class RequestSessionController { + + def page() { + [include: params.boolean('include')] + } + + def included() { + render(text: "included:${RequestSessions.describe(RequestSessions.current())}") + } + + def current() { + render(text: RequestSessions.describe(RequestSessions.current())) + } + + def promise() { + RequestSessions.saveBeforeGoingAsynchronous() + Promises.task { + RequestSessions.release.await(30, TimeUnit.SECONDS) + 'done' + } + } +} diff --git a/grails-test-examples/mongodb/base/grails-app/views/requestSession/page.gsp b/grails-test-examples/mongodb/base/grails-app/views/requestSession/page.gsp new file mode 100644 index 00000000000..b1f7d557b09 --- /dev/null +++ b/grails-test-examples/mongodb/base/grails-app/views/requestSession/page.gsp @@ -0,0 +1,22 @@ +<%-- + ~ Licensed to the Apache Software Foundation (ASF) under one + ~ or more contributor license agreements. See the NOTICE file + ~ distributed with this work for additional information + ~ regarding copyright ownership. The ASF licenses this file + ~ to you under the Apache License, Version 2.0 (the + ~ "License"); you may not use this file except in compliance + ~ with the License. You may obtain a copy of the License at + ~ + ~ https://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, + ~ software distributed under the License is distributed on an + ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + ~ KIND, either express or implied. See the License for the + ~ specific language governing permissions and limitations + ~ under the License. + --%> +before:${functional.tests.RequestSessions.describe(functional.tests.RequestSessions.current())} + +after:${functional.tests.RequestSessions.describe(functional.tests.RequestSessions.current())} +read:${functional.tests.RequestSessions.readPlayers()} diff --git a/grails-test-examples/mongodb/base/src/integration-test/groovy/functional/tests/OpenSessionInViewSpec.groovy b/grails-test-examples/mongodb/base/src/integration-test/groovy/functional/tests/OpenSessionInViewSpec.groovy index 024df424972..8aa589285fb 100644 --- a/grails-test-examples/mongodb/base/src/integration-test/groovy/functional/tests/OpenSessionInViewSpec.groovy +++ b/grails-test-examples/mongodb/base/src/integration-test/groovy/functional/tests/OpenSessionInViewSpec.groovy @@ -19,9 +19,12 @@ package functional.tests +import java.util.concurrent.CountDownLatch + import org.springframework.beans.factory.annotation.Autowired import org.springframework.context.ApplicationContext import spock.lang.Specification +import spock.util.concurrent.PollingConditions import grails.testing.mixin.integration.Integration import org.apache.grails.testing.http.client.HttpClientSupport @@ -32,7 +35,8 @@ import org.apache.grails.testing.http.client.TestHttpResponse * action without a transaction can read what its queries return. Without that session, the one a * query opens for itself closes before the action reads the results: MongoDB returns the first 101 * documents with the query and they are decoded at once, but decoding a later batch fails with - * "No datastore session found". + * "No datastore session found". An include uses the session of its request, and a request that + * goes asynchronous closes its session, so that it is not left bound to the thread. */ @Integration(applicationClass = Application) class OpenSessionInViewSpec extends Specification implements HttpClientSupport { @@ -85,6 +89,53 @@ class OpenSessionInViewSpec extends Specification implements HttpClientSupport { Player.withNewSession { Player.get(playerId).name } == 'Player 0' } + void "an include uses the session of the request and leaves it open for the rest of the page"() { + when: + final page = pageLines('/requestSession/page?include=true') + + then: 'the include reads through the session of the page' + page.included == page.before + + and: 'the page keeps its session after the include, and reads beyond the first batch' + page.after == page.before + page.read == '102' + } + + void "an action that goes asynchronous with #how closes its session as the request goes asynchronous"() { + given: + RequestSessions.release = new CountDownLatch(1) + RequestSessions.asyncSession = null + final asyncRequest = Thread.start { http(path).assertStatus(200) } + + expect: 'the session the action ran with is closed once the request has gone asynchronous' + new PollingConditions(timeout: 10).eventually { + assert RequestSessions.asyncSession != null + assert !RequestSessions.asyncSession.connected + } + + and: 'what the action saved was flushed then, as when an action returns' + Player.withNewSession { Player.findByName('Saved before going asynchronous') } != null + + and: 'a later request gets a session of its own' + !http('/requestSession/current').body().startsWith("${System.identityHashCode(RequestSessions.asyncSession)}/") + + cleanup: + RequestSessions.release.countDown() + asyncRequest?.join(30000) + + where: + how | path + 'a promise' | '/requestSession/promise' + 'AsyncController.startAsync()' | '/asyncRequestSession/start' + } + + private Map pageLines(String path) { + http(path).assertStatus(200).body().readLines()*.trim().findAll().collectEntries { String line -> + final parts = line.split(':', 2) + [(parts[0]): parts[1]] + } + } + private TestHttpResponse postForm(String path, String form) { httpPost(path, form, 'application/x-www-form-urlencoded') } diff --git a/grails-test-examples/mongodb/base/src/main/groovy/functional/tests/RequestSessions.groovy b/grails-test-examples/mongodb/base/src/main/groovy/functional/tests/RequestSessions.groovy new file mode 100644 index 00000000000..cc639ac27ca --- /dev/null +++ b/grails-test-examples/mongodb/base/src/main/groovy/functional/tests/RequestSessions.groovy @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package functional.tests + +import java.util.concurrent.CountDownLatch + +import grails.util.Holders +import org.grails.datastore.mapping.core.Session +import org.grails.datastore.mapping.transactions.SessionHolder +import org.springframework.transaction.support.TransactionSynchronizationManager + +/** + * The session that the open session in view interceptor has bound to the current thread, and the session an + * asynchronous action of the scenario controllers ran with. + */ +class RequestSessions { + + static volatile CountDownLatch release = new CountDownLatch(1) + static volatile Session asyncSession + + static Session current() { + final datastore = Holders.applicationContext.getBean('mongoOpenSessionInViewInterceptor').datastore + ((SessionHolder) TransactionSynchronizationManager.getResource(datastore))?.session + } + + static String describe(Session session) { + session == null ? 'none' : "${System.identityHashCode(session)}/${session.connected ? 'open' : 'closed'}" + } + + static String readPlayers() { + try { + Player.list().collect { it.team?.name }.size().toString() + } catch (Exception e) { + e.getClass().simpleName + } + } + + static void saveBeforeGoingAsynchronous() { + asyncSession = current() + new Player(name: 'Saved before going asynchronous', team: Team.first()).save() + } +} From b2fad4e1b742e80474a023c2e8dd588010cb378c Mon Sep 17 00:00:00 2001 From: Mattias Reichel Date: Thu, 19 Nov 2026 18:38:56 +0100 Subject: [PATCH 090/115] fix(hibernate5): keep the sessions of the other datasources open over an include, and close them when a request goes asynchronous GrailsOpenSessionInViewInterceptor opens a session for each datasource other than the default one. When an include within a request completed, it unbound and closed those sessions, so the rest of the page ran without them. When a request went asynchronous, only the parent class's handling of the default session ran, so the sessions of the other datasources stayed bound to the thread that had started the request, and the next request that thread served used them. The interceptor now records in the request when it uses a session of another datasource that is already bound, and leaves that session to the call that opened it, as the parent class does for the default session. When a request goes asynchronous, it unbinds and closes the sessions it opened for the other datasources. --- .../GrailsOpenSessionInViewInterceptor.java | 95 ++++++++++++--- .../support/MultiDataSourceSessionSpec.groovy | 112 +++++++++++++++++- grails-test-examples/datasources/build.gradle | 2 + .../RequestSessionController.groovy | 50 ++++++++ .../grails-app/views/requestSession/page.gsp | 21 ++++ .../OsivRequestSessionSpec.groovy | 72 +++++++++++ .../groovy/datasources/RequestSessions.groovy | 47 ++++++++ 7 files changed, 379 insertions(+), 20 deletions(-) create mode 100644 grails-test-examples/datasources/grails-app/controllers/datasources/RequestSessionController.groovy create mode 100644 grails-test-examples/datasources/grails-app/views/requestSession/page.gsp create mode 100644 grails-test-examples/datasources/src/integration-test/groovy/functionaltests/OsivRequestSessionSpec.groovy create mode 100644 grails-test-examples/datasources/src/main/groovy/datasources/RequestSessions.groovy diff --git a/grails-data-hibernate5/grails-plugin/src/main/groovy/org/grails/plugin/hibernate/support/GrailsOpenSessionInViewInterceptor.java b/grails-data-hibernate5/grails-plugin/src/main/groovy/org/grails/plugin/hibernate/support/GrailsOpenSessionInViewInterceptor.java index 782e1710a65..6b1a4a1bf90 100644 --- a/grails-data-hibernate5/grails-plugin/src/main/groovy/org/grails/plugin/hibernate/support/GrailsOpenSessionInViewInterceptor.java +++ b/grails-data-hibernate5/grails-plugin/src/main/groovy/org/grails/plugin/hibernate/support/GrailsOpenSessionInViewInterceptor.java @@ -45,7 +45,11 @@ * The default datasource's SessionFactory is managed by the parent class. * Additional (non-default) datasource SessionFactories are managed by this * subclass, which opens and closes sessions for each one alongside the - * default session. + * default session. As the parent class does for the default session, a request + * that finds a session already bound, such as an include within a request that + * opened one, uses that session and leaves it to the one that opened it, and a + * request that goes asynchronous closes the sessions it opened, so that they do + * not stay bound to the thread for the next request that thread serves. * * @author Graeme Rocher * @since 0.5 @@ -90,6 +94,8 @@ public void preHandle(WebRequest request) throws DataAccessException { for (AdditionalSessionFactoryConfig config : additionalSessionFactories) { SessionFactory sf = config.sessionFactory; if (TransactionSynchronizationManager.hasResource(sf)) { + // a session is already open for this request: use it, and leave it to the one that opened it + incrementParticipateCount(request, sf); continue; } if (logger.isDebugEnabled()) { @@ -165,30 +171,81 @@ public void postHandle(WebRequest request, ModelMap model) throws DataAccessExce @Override public void afterCompletion(WebRequest request, Exception ex) throws DataAccessException { try { - for (int i = additionalSessionFactories.size() - 1; i >= 0; i--) { - AdditionalSessionFactoryConfig config = additionalSessionFactories.get(i); - SessionFactory sf = config.sessionFactory; - SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sf); - if (sessionHolder != null) { - Session session = sessionHolder.getSession(); - TransactionSynchronizationManager.unbindResource(sf); - if (logger.isDebugEnabled()) { - logger.debug("Closing additional Hibernate Session for datasource '" + config.connectionName + "' in OpenSessionInViewInterceptor"); - } - try { - SessionFactoryUtils.closeSession(session); - } - catch (RuntimeException closeEx) { - logger.error("Unexpected exception on closing additional Hibernate Session for datasource '" + config.connectionName + "'", closeEx); - } - } - } + closeAdditionalSessions(request); } finally { super.afterCompletion(request, ex); } } + /** + * Called instead of {@link #postHandle} and {@link #afterCompletion} on the thread that started asynchronous + * handling. The parent class unbinds the default session; the sessions this interceptor opened for the other + * datasources are closed here, so that the thread goes back to its pool without them. + */ + @Override + public void afterConcurrentHandlingStarted(WebRequest request) { + try { + closeAdditionalSessions(request); + } + finally { + super.afterConcurrentHandlingStarted(request); + } + } + + private void closeAdditionalSessions(WebRequest request) { + for (int i = additionalSessionFactories.size() - 1; i >= 0; i--) { + AdditionalSessionFactoryConfig config = additionalSessionFactories.get(i); + SessionFactory sf = config.sessionFactory; + if (decrementParticipateCount(request, sf)) { + continue; + } + SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sf); + if (sessionHolder != null) { + Session session = sessionHolder.getSession(); + TransactionSynchronizationManager.unbindResource(sf); + if (logger.isDebugEnabled()) { + logger.debug("Closing additional Hibernate Session for datasource '" + config.connectionName + "' in OpenSessionInViewInterceptor"); + } + try { + SessionFactoryUtils.closeSession(session); + } + catch (RuntimeException closeEx) { + logger.error("Unexpected exception on closing additional Hibernate Session for datasource '" + config.connectionName + "'", closeEx); + } + } + } + } + + private static void incrementParticipateCount(WebRequest request, SessionFactory sf) { + String participateAttributeName = participateAttributeName(sf); + Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST); + request.setAttribute(participateAttributeName, count != null ? count + 1 : 1, WebRequest.SCOPE_REQUEST); + } + + /** + * @return {@code true} when this request used a session of the given factory that was already open, which is + * then left as it is + */ + private static boolean decrementParticipateCount(WebRequest request, SessionFactory sf) { + String participateAttributeName = participateAttributeName(sf); + Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST); + if (count == null) { + return false; + } + if (count > 1) { + request.setAttribute(participateAttributeName, count - 1, WebRequest.SCOPE_REQUEST); + } + else { + request.removeAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST); + } + return true; + } + + private static String participateAttributeName(SessionFactory sf) { + return sf.toString() + PARTICIPATE_SUFFIX; + } + public void setHibernateDatastore(AbstractHibernateDatastore hibernateDatastore) { String defaultFlushModeName = hibernateDatastore.getDefaultFlushModeName(); if (hibernateDatastore.isOsivReadOnly()) { diff --git a/grails-data-hibernate5/grails-plugin/src/test/groovy/org/grails/plugin/hibernate/support/MultiDataSourceSessionSpec.groovy b/grails-data-hibernate5/grails-plugin/src/test/groovy/org/grails/plugin/hibernate/support/MultiDataSourceSessionSpec.groovy index 8b9e07513d7..5abd227b701 100644 --- a/grails-data-hibernate5/grails-plugin/src/test/groovy/org/grails/plugin/hibernate/support/MultiDataSourceSessionSpec.groovy +++ b/grails-data-hibernate5/grails-plugin/src/test/groovy/org/grails/plugin/hibernate/support/MultiDataSourceSessionSpec.groovy @@ -46,6 +46,17 @@ class MultiDataSourceSessionSpec extends Specification { @Shared @AutoCleanup HibernateDatastore datastore = new HibernateDatastore(DatastoreUtils.createPropertyResolver(config), OsivBook, OsivAuthor) + void cleanup() { + [datastore.sessionFactory, datastore.getDatastoreForConnection('secondary').sessionFactory].each { SessionFactory sessionFactory -> + if (TransactionSynchronizationManager.hasResource(sessionFactory)) { + Session session = ((SessionHolder) TransactionSynchronizationManager.unbindResource(sessionFactory)).session + if (session.open) { + session.close() + } + } + } + } + def "withSession on default datasource works with OSIV"() { given: "OSIV interceptor configured with the datastore" def interceptor = new GrailsOpenSessionInViewInterceptor() @@ -119,7 +130,7 @@ class MultiDataSourceSessionSpec extends Specification { given: "a pre-bound session for the secondary datasource" def interceptor = new GrailsOpenSessionInViewInterceptor() interceptor.setHibernateDatastore(datastore) - WebRequest webRequest = Mock(WebRequest) + WebRequest webRequest = webRequest() def secondaryDatastore = datastore.getDatastoreForConnection('secondary') SessionFactory secondarySf = secondaryDatastore.sessionFactory @@ -134,12 +145,98 @@ class MultiDataSourceSessionSpec extends Specification { def currentHolder = TransactionSynchronizationManager.getResource(secondarySf) currentHolder.is(preBoundHolder) + when: "the request completes" + interceptor.afterCompletion(webRequest, null) + + then: "the pre-bound session is left bound and open" + TransactionSynchronizationManager.getResource(secondarySf).is(preBoundHolder) + preBoundSession.open + cleanup: + if (TransactionSynchronizationManager.hasResource(secondarySf)) { + TransactionSynchronizationManager.unbindResource(secondarySf) + } + preBoundSession.close() + } + + def "an include uses the sessions of the request for every datasource and leaves them open"() { + given: "the request has opened its sessions" + def interceptor = new GrailsOpenSessionInViewInterceptor() + interceptor.setHibernateDatastore(datastore) + WebRequest webRequest = webRequest() + SessionFactory secondarySf = datastore.getDatastoreForConnection('secondary').sessionFactory + interceptor.preHandle(webRequest) + Session defaultSession = boundSession(datastore.sessionFactory) + Session secondarySession = boundSession(secondarySf) + + when: "an include within it starts and completes" + interceptor.preHandle(webRequest) + interceptor.postHandle(webRequest, null) interceptor.afterCompletion(webRequest, null) + + then: "the sessions of the request are still bound and open" + boundSession(datastore.sessionFactory).is(defaultSession) + boundSession(secondarySf).is(secondarySession) + secondarySession.open + + when: "the request completes" + interceptor.afterCompletion(webRequest, null) + + then: "they are unbound and closed" + !TransactionSynchronizationManager.hasResource(datastore.sessionFactory) + !TransactionSynchronizationManager.hasResource(secondarySf) + !secondarySession.open + } + + def "a request that goes asynchronous closes the sessions it opened and leaves the thread without them"() { + given: "the request has opened its sessions" + def interceptor = new GrailsOpenSessionInViewInterceptor() + interceptor.setHibernateDatastore(datastore) + WebRequest webRequest = webRequest() + SessionFactory secondarySf = datastore.getDatastoreForConnection('secondary').sessionFactory + interceptor.preHandle(webRequest) + Session defaultSession = boundSession(datastore.sessionFactory) + Session secondarySession = boundSession(secondarySf) + + when: "the request goes asynchronous" + interceptor.afterConcurrentHandlingStarted(webRequest) + + then: "no session is bound to the thread any more" + !TransactionSynchronizationManager.hasResource(datastore.sessionFactory) + !TransactionSynchronizationManager.hasResource(secondarySf) + + and: "the session of the other datasource is closed" + !secondarySession.open + + cleanup: + defaultSession?.close() + } + + def "a session of another datasource bound before the request stays bound and open when the request goes asynchronous"() { + given: "a pre-bound session for the secondary datasource" + def interceptor = new GrailsOpenSessionInViewInterceptor() + interceptor.setHibernateDatastore(datastore) + WebRequest webRequest = webRequest() + SessionFactory secondarySf = datastore.getDatastoreForConnection('secondary').sessionFactory + Session preBoundSession = secondarySf.openSession() + SessionHolder preBoundHolder = new SessionHolder(preBoundSession) + TransactionSynchronizationManager.bindResource(secondarySf, preBoundHolder) + interceptor.preHandle(webRequest) + Session defaultSession = boundSession(datastore.sessionFactory) + + when: "the request goes asynchronous" + interceptor.afterConcurrentHandlingStarted(webRequest) + + then: "the pre-bound session is left bound and open" + TransactionSynchronizationManager.getResource(secondarySf).is(preBoundHolder) + preBoundSession.open + + cleanup: if (TransactionSynchronizationManager.hasResource(secondarySf)) { TransactionSynchronizationManager.unbindResource(secondarySf) } preBoundSession.close() + defaultSession?.close() } def "CRUD operations work on secondary datasource with OSIV"() { @@ -174,6 +271,19 @@ class MultiDataSourceSessionSpec extends Specification { } interceptor.afterCompletion(webRequest, null) } + + private WebRequest webRequest() { + Map attributes = [:] + Stub(WebRequest) { + getAttribute(_ as String, WebRequest.SCOPE_REQUEST) >> { String name, int scope -> attributes[name] } + setAttribute(_ as String, _, WebRequest.SCOPE_REQUEST) >> { String name, Object value, int scope -> attributes[name] = value } + removeAttribute(_ as String, WebRequest.SCOPE_REQUEST) >> { String name, int scope -> attributes.remove(name) } + } + } + + private static Session boundSession(SessionFactory sessionFactory) { + ((SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory))?.session + } } @Entity diff --git a/grails-test-examples/datasources/build.gradle b/grails-test-examples/datasources/build.gradle index 35d4c25f8d4..3cf702682c5 100644 --- a/grails-test-examples/datasources/build.gradle +++ b/grails-test-examples/datasources/build.gradle @@ -42,6 +42,7 @@ dependencies { } implementation 'org.apache.grails:grails-data-hibernate5' + implementation 'org.apache.grails:grails-async' implementation 'org.apache.grails:grails-cache' runtimeOnly 'org.apache.grails:grails-scaffolding' @@ -54,6 +55,7 @@ dependencies { console 'org.apache.grails:grails-console' integrationTestImplementation testFixtures('org.apache.grails:grails-geb') + integrationTestImplementation project(':grails-testing-support-http-client') } apply { diff --git a/grails-test-examples/datasources/grails-app/controllers/datasources/RequestSessionController.groovy b/grails-test-examples/datasources/grails-app/controllers/datasources/RequestSessionController.groovy new file mode 100644 index 00000000000..83cb4e6abd0 --- /dev/null +++ b/grails-test-examples/datasources/grails-app/controllers/datasources/RequestSessionController.groovy @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package datasources + +import java.util.concurrent.TimeUnit + +import grails.async.Promises + +/** + * Actions that show which session of the secondary datasource a request reads through: a page that includes + * another action, and an action that returns a promise, so that the request goes asynchronous. + */ +class RequestSessionController { + + def page() { + [include: params.boolean('include')] + } + + def included() { + render(text: "included:${RequestSessions.describe(RequestSessions.secondary())}") + } + + def current() { + render(text: RequestSessions.describe(RequestSessions.secondary())) + } + + def promise() { + RequestSessions.asyncSecondarySession = RequestSessions.secondary() + Promises.task { + RequestSessions.release.await(30, TimeUnit.SECONDS) + 'done' + } + } +} diff --git a/grails-test-examples/datasources/grails-app/views/requestSession/page.gsp b/grails-test-examples/datasources/grails-app/views/requestSession/page.gsp new file mode 100644 index 00000000000..5dc981059f8 --- /dev/null +++ b/grails-test-examples/datasources/grails-app/views/requestSession/page.gsp @@ -0,0 +1,21 @@ +<%-- + ~ Licensed to the Apache Software Foundation (ASF) under one + ~ or more contributor license agreements. See the NOTICE file + ~ distributed with this work for additional information + ~ regarding copyright ownership. The ASF licenses this file + ~ to you under the Apache License, Version 2.0 (the + ~ "License"); you may not use this file except in compliance + ~ with the License. You may obtain a copy of the License at + ~ + ~ https://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, + ~ software distributed under the License is distributed on an + ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + ~ KIND, either express or implied. See the License for the + ~ specific language governing permissions and limitations + ~ under the License. + --%> +before:${datasources.RequestSessions.describe(datasources.RequestSessions.secondary())} + +after:${datasources.RequestSessions.describe(datasources.RequestSessions.secondary())} diff --git a/grails-test-examples/datasources/src/integration-test/groovy/functionaltests/OsivRequestSessionSpec.groovy b/grails-test-examples/datasources/src/integration-test/groovy/functionaltests/OsivRequestSessionSpec.groovy new file mode 100644 index 00000000000..843f6544a0c --- /dev/null +++ b/grails-test-examples/datasources/src/integration-test/groovy/functionaltests/OsivRequestSessionSpec.groovy @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package functionaltests + +import java.util.concurrent.CountDownLatch + +import datasources.RequestSessions +import grails.testing.mixin.integration.Integration +import org.apache.grails.testing.http.client.HttpClientSupport +import spock.lang.Specification +import spock.util.concurrent.PollingConditions + +/** + * The session the open session in view interceptor opens for the secondary datasource is used by an include within + * the request, and closed when the request goes asynchronous, so that it is not left bound to the thread. + */ +@Integration +class OsivRequestSessionSpec extends Specification implements HttpClientSupport { + + void 'an include uses the session of the secondary datasource of the request and leaves it open'() { + when: + final page = pageLines('/requestSession/page?include=true') + + then: + page.before.endsWith('/open') + page.included == page.before + page.after == page.before + } + + void 'a request that goes asynchronous closes its session of the secondary datasource'() { + given: + RequestSessions.release = new CountDownLatch(1) + RequestSessions.asyncSecondarySession = null + final asyncRequest = Thread.start { http('/requestSession/promise').assertStatus(200) } + + expect: 'the session the action ran with is closed once the request has gone asynchronous' + new PollingConditions(timeout: 10).eventually { + assert RequestSessions.asyncSecondarySession != null + assert !RequestSessions.asyncSecondarySession.open + } + + and: 'a later request gets a session of its own' + !http('/requestSession/current').body().startsWith("${System.identityHashCode(RequestSessions.asyncSecondarySession)}/") + + cleanup: + RequestSessions.release.countDown() + asyncRequest?.join(30000) + } + + private Map pageLines(String path) { + http(path).assertStatus(200).body().readLines()*.trim().findAll().collectEntries { String line -> + final parts = line.split(':', 2) + [(parts[0]): parts[1]] + } + } +} diff --git a/grails-test-examples/datasources/src/main/groovy/datasources/RequestSessions.groovy b/grails-test-examples/datasources/src/main/groovy/datasources/RequestSessions.groovy new file mode 100644 index 00000000000..f68786aa611 --- /dev/null +++ b/grails-test-examples/datasources/src/main/groovy/datasources/RequestSessions.groovy @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package datasources + +import java.util.concurrent.CountDownLatch + +import grails.util.Holders +import org.grails.orm.hibernate.HibernateDatastore +import org.hibernate.Session +import org.springframework.orm.hibernate5.SessionHolder +import org.springframework.transaction.support.TransactionSynchronizationManager + +/** + * The session of the secondary datasource that the open session in view interceptor has bound to the current + * thread, and the one an asynchronous action of the scenario controller ran with. + */ +class RequestSessions { + + static volatile CountDownLatch release = new CountDownLatch(1) + static volatile Session asyncSecondarySession + + static Session secondary() { + final sessionFactory = Holders.applicationContext.getBean(HibernateDatastore) + .getDatastoreForConnection('secondary').sessionFactory + ((SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory))?.session + } + + static String describe(Session session) { + session == null ? 'none' : "${System.identityHashCode(session)}/${session.open ? 'open' : 'closed'}" + } +} From 7d5e11e1b12e4380a3efdd8b15dac313808c38e7 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Thu, 19 Nov 2026 11:32:01 -0800 Subject: [PATCH 091/115] Take the Forge CLI plugin commands out of the JLine 4 change The plugin commands go to a pull request of their own, to be weighed against the unified command line of #16348. This change keeps the move to JLine 4, the Forge prompt's completer, the grails shell spec, and the fixes to the command line parser runCommand already uses, with upgrade guide sections 26 and 27 and their part of the grails-upgrade skill. Removed: generateCommandManifest and its writer, the @CommandParameter and @CommandOption annotations, the Forge CLI's project commands, the argsFile and grailsCommandProject properties, the Spring Security command descriptions, and the guide and threat model text for them. The prompt and its completer are back to what the JLine change needs. --- THREAT_MODEL.md | 2 - .../ApplicationCommandManifestWriter.groovy | 99 -- .../grails/core/cli/CommandOption.groovy | 71 - .../grails/core/cli/CommandOptions.groovy | 41 - .../grails/core/cli/CommandParameter.groovy | 71 - .../grails/core/cli/CommandParameters.groovy | 41 - ...pplicationCommandManifestWriterSpec.groovy | 181 --- grails-doc/src/en/guide/commandLine.adoc | 101 -- .../commandLine/creatingCustomCommands.adoc | 94 -- .../src/en/guide/introduction/whatsNew.adoc | 22 - grails-forge/grails-forge-cli/build.gradle | 1 - .../forge/cli/CommandLineCompleter.groovy | 31 +- .../org/grails/forge/cli/ForgeCli.groovy | 139 +- .../grails/forge/cli/InteractiveShell.groovy | 44 +- .../forge/cli/command/ProjectCommand.groovy | 215 --- .../forge/cli/command/ProjectCommands.groovy | 508 ------- .../forge/cli/command/ProjectGradle.groovy | 134 -- .../forge/cli/CommandLineCompleterSpec.groovy | 18 +- .../forge/cli/ProjectCommandsSpec.groovy | 1306 ----------------- .../ApplicationCommandManifestTask.groovy | 150 -- .../commands/GrailsCliGradlePlugin.groovy | 246 +--- .../ApplicationCommandManifestTaskSpec.groovy | 178 --- .../commands/CommandArgumentsSpec.groovy | 211 --- .../command-manifest/app/build.gradle | 13 - .../src/cli/java/org/example/FakeCommand.java | 23 - .../app/src/main/resources/runtime-marker.txt | 1 - .../build-logic/settings.gradle | 1 - .../command-manifest/gradle.properties | 3 - .../manifest-writer/build.gradle | 3 - .../cli/ApplicationCommandManifestWriter.java | 48 - .../command-manifest/settings.gradle | 9 - .../command-manifest/shared/settings.gradle | 1 - .../cli/S2CreatePersistentTokenCommand.groovy | 5 - .../S2CreateRoleHierarchyEntryCommand.groovy | 5 - .../security/cli/S2QuickstartCommand.groovy | 14 +- .../cli/SecurityCommandManifestSpec.groovy | 70 - 36 files changed, 26 insertions(+), 4074 deletions(-) delete mode 100644 grails-core/src/cli/groovy/org/apache/grails/core/cli/ApplicationCommandManifestWriter.groovy delete mode 100644 grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandOption.groovy delete mode 100644 grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandOptions.groovy delete mode 100644 grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandParameter.groovy delete mode 100644 grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandParameters.groovy delete mode 100644 grails-core/src/test-cli/groovy/org/apache/grails/core/cli/ApplicationCommandManifestWriterSpec.groovy delete mode 100644 grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommand.groovy delete mode 100644 grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy delete mode 100644 grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectGradle.groovy delete mode 100644 grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy delete mode 100644 grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTask.groovy delete mode 100644 grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTaskSpec.groovy delete mode 100644 grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/CommandArgumentsSpec.groovy delete mode 100644 grails-gradle/plugins/src/test/resources/test-projects/command-manifest/app/build.gradle delete mode 100644 grails-gradle/plugins/src/test/resources/test-projects/command-manifest/app/src/cli/java/org/example/FakeCommand.java delete mode 100644 grails-gradle/plugins/src/test/resources/test-projects/command-manifest/app/src/main/resources/runtime-marker.txt delete mode 100644 grails-gradle/plugins/src/test/resources/test-projects/command-manifest/build-logic/settings.gradle delete mode 100644 grails-gradle/plugins/src/test/resources/test-projects/command-manifest/gradle.properties delete mode 100644 grails-gradle/plugins/src/test/resources/test-projects/command-manifest/manifest-writer/build.gradle delete mode 100644 grails-gradle/plugins/src/test/resources/test-projects/command-manifest/manifest-writer/src/main/java/org/apache/grails/core/cli/ApplicationCommandManifestWriter.java delete mode 100644 grails-gradle/plugins/src/test/resources/test-projects/command-manifest/settings.gradle delete mode 100644 grails-gradle/plugins/src/test/resources/test-projects/command-manifest/shared/settings.gradle delete mode 100644 grails-spring-security/plugin/src/test-cli/groovy/org/apache/grails/security/cli/SecurityCommandManifestSpec.groovy diff --git a/THREAT_MODEL.md b/THREAT_MODEL.md index b2e17c4169c..f6be2914680 100644 --- a/THREAT_MODEL.md +++ b/THREAT_MODEL.md @@ -65,7 +65,6 @@ The framework is large; not every module has the same threat profile. The model | Compile-time AST transforms | [`GlobalGrailsClassInjectorTransformation`](./grails-core/src/main/groovy/org/grails/compiler/injection/GlobalGrailsClassInjectorTransformation.groovy), [`ResourceTransform`](./grails-rest-transforms/src/main/groovy/org/grails/plugins/web/rest/transform/ResourceTransform.groovy), [`ViewsTransform`](./grails-views-core/src/main/groovy/grails/views/compiler/ViewsTransform.groovy) | No (build only) | **In** (developer-trusted inputs only - see §6) | | CLI / shell (`grails-shell-cli`) | [`GrailsCli`](./grails-shell-cli/src/main/groovy/org/grails/cli/GrailsCli.groovy), [`MavenProfileRepository`](./grails-shell-cli/src/main/groovy/org/grails/cli/profile/repository/MavenProfileRepository.groovy) | Yes - network (Maven), filesystem | **In** (with caveats - see §3 and §7) | | Interactive consoles (`grails-console`) | [`GrailsShell`](./grails-console/src/main/groovy/grails/ui/shell/GrailsShell.groovy), `GroovyConsoleApplicationContext` | Local I/O | **Out** - developer-only tool, see §3 | -| Forge CLI (`grails-forge-cli`) project commands | [`ProjectCommands`](./grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy), [`ProjectGradle`](./grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectGradle.groovy) | Yes - runs the Gradle wrapper of the project in the current directory | **Out** - developer-only tool; the project's build is code the developer trusts, as when running `./gradlew`. See §11. | | Hosted `grails-forge` HTTP APIs | [`ZipCreateController`](./grails-forge/grails-forge-web/grails-app/controllers/org/grails/forge/web/ZipCreateController.groovy) and sibling controllers | Yes - network | **Out** - separate Grails 8 web application deployed independently from user applications built with Grails; modeled separately by the Forge service operators. *(inferred)* | | Generated application scaffolding | `grails-profiles/`, files emitted by `create-app` | n/a | **Out** - separately authored; the framework's contract does not extend to scaffolded code once it is in the user's repository. See §3. | | `grails-test-examples/`, demo apps | n/a | n/a | **Out** - test/demo code, not shipped. See §3. | @@ -387,7 +386,6 @@ In-the-wild patterns the API permits but that violate the assumptions in §5-§7 - **Exposing `GrailsShell`, `GroovyConsole`, or any Spring Boot Actuator endpoint that evaluates Groovy** on a network-reachable interface. These modules provide unrestricted code execution by design (§3). Reaching them from an HTTP listener is the operator's mistake, but the application's the one that loses. *Fix*: bind those endpoints to localhost or disable them in production; do not configure the embedded container to expose `grails-console` artefacts. - **Treating constraint validation as authorization.** A constraint passing does not mean the user is permitted to perform the operation. A `nullable: false` field will still accept an attacker-supplied value if the attacker is authenticated and the action allows them to submit it. *Fix*: keep authz separate from validation; run authz checks at the action entry, validation at the binding boundary, and never confuse "validates" with "authorized". *(inferred)* - **Liquibase Groovy change-log files loaded from a path the developer does not fully control.** `GroovyChangeLogParser` in `grails-data-hibernate5/dbmigration` evaluates change-log Groovy via `GroovyShell.parse(...).run()`. If the change-log location is influenced by a CI/CD parameter, an artifact registry, or any externally-writable path, this is full Groovy code execution at application startup. *Fix*: keep migration files in the application's own source tree under version control; never source change-log paths from runtime input. *(inferred)* -- **Starting the Grails Forge CLI inside a checkout that is not trusted.** Inside a Grails project, the Forge CLI runs the project's Gradle wrapper and build on its own to learn the project's commands: when its interactive prompt starts, and when a command line names a command it does not have, a typo included. The build is code of the checkout, so this runs it as `./gradlew` would, which is what a developer may not expect of a code generator. *Fix*: set the environment variable `GRAILS_FORGE_RUN_BUILD` (or the system property `grails.forge.runBuild`) to `false`; the Forge CLI then runs the build only for a project command the user names. *(documented: [commandLine.adoc](./grails-doc/src/en/guide/commandLine.adoc))* - **Putting secrets in HTML comments inside GSP templates.** They reach the client. *Fix*: use GSP comments (`<%-- … --%>`) or omit. *(inferred)* - **Using `params.id` directly in a GORM `get(id)` call without an authorization check.** The "guessable IDs" pattern. Returns objects the requesting user has no business viewing or mutating. *Fix*: authorize the loaded object against the authenticated principal before returning, rendering, or mutating it - via the Spring Security plugin's ACL support, a per-action interceptor, or an explicit `if (book.owner != currentUser) { notFound(); return }`. *(documented: [securingAgainstAttacks.adoc](./grails-doc/src/en/guide/security/securingAgainstAttacks.adoc) "Guessable IDs")* - **Trusting `MultipartFile.originalFilename`** as a destination filename. The value is attacker-controlled and may include `../`, NUL bytes, or shell metacharacters. *Fix*: generate server-side filenames; never let user data join a filesystem path. *(inferred)* diff --git a/grails-core/src/cli/groovy/org/apache/grails/core/cli/ApplicationCommandManifestWriter.groovy b/grails-core/src/cli/groovy/org/apache/grails/core/cli/ApplicationCommandManifestWriter.groovy deleted file mode 100644 index 71aa2abdc71..00000000000 --- a/grails-core/src/cli/groovy/org/apache/grails/core/cli/ApplicationCommandManifestWriter.groovy +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.grails.core.cli - -import groovy.json.JsonOutput -import groovy.transform.CompileStatic - -/** - * Writes the commands an application can run, with their descriptions, arguments and options, to a - * JSON file that command line tools read to offer and complete those commands without loading them. - * - *

The commands are those {@code runCommand} runs: every {@link ApplicationCommand} the - * {@link ApplicationContextCommandRegistry} finds on the classpath of the process, which the Grails - * Gradle plugin makes the application's classpath plus its {@code grailsCli} configuration.

- * - * @since 9.0 - */ -@CompileStatic -final class ApplicationCommandManifestWriter { - - /** - * The version of the file's format, written to the file, raised when a reader could misread a - * file of the new format. - */ - static final int FORMAT_VERSION = 1 - - private ApplicationCommandManifestWriter() { - } - - /** - * Writes the manifest of the commands on the classpath. - * - * @param args the path of the file to write - */ - static void main(String[] args) { - if (args.length != 1) { - throw new IllegalArgumentException('Expected the path of the manifest file to write as the only argument') - } - write(new ApplicationContextCommandRegistry().findCommands(), new File(args[0])) - } - - /** - * Writes the manifest of the given commands, sorted by name. - * - * @param commands the commands to describe - * @param file the file to write, whose directory is created when it does not exist - */ - static void write(Collection commands, File file) { - List> described = commands - .sort(false) { ApplicationCommand command -> command.name } - .collect { ApplicationCommand command -> describe(command) } - file.parentFile?.mkdirs() - file.setText(JsonOutput.prettyPrint(JsonOutput.toJson([version: FORMAT_VERSION, commands: described])), 'UTF-8') - } - - private static Map describe(ApplicationCommand command) { - Class type = command.class - List> arguments = declaredArguments(type).collect { CommandParameter argument -> - [name: argument.name(), description: argument.description(), required: argument.required(), - values: argument.values().toList()] as Map - } - List> options = declaredOptions(type).collect { CommandOption option -> - [name: option.name(), description: option.description(), flag: option.flag(), - values: option.values().toList()] as Map - } - // the class lets a build tool find the command's source, whose changes may change this description - [name: command.name, description: command.description ?: '', className: type.name, arguments: arguments, - options: options] as Map - } - - // Class#getAnnotationsByType would read both forms at once, but the static compiler links it with an - // Object[] return type, which the JDK method does not have, so the call fails with NoSuchMethodError. - // getAnnotation returns a single annotation and links correctly. - private static List declaredArguments(Class type) { - CommandParameter single = type.getAnnotation(CommandParameter) - single ? [single] : (type.getAnnotation(CommandParameters)?.value()?.toList() ?: []) - } - - private static List declaredOptions(Class type) { - CommandOption single = type.getAnnotation(CommandOption) - single ? [single] : (type.getAnnotation(CommandOptions)?.value()?.toList() ?: []) - } -} diff --git a/grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandOption.groovy b/grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandOption.groovy deleted file mode 100644 index 68fdf718310..00000000000 --- a/grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandOption.groovy +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.grails.core.cli - -import java.lang.annotation.Documented -import java.lang.annotation.ElementType -import java.lang.annotation.Repeatable -import java.lang.annotation.Retention -import java.lang.annotation.RetentionPolicy -import java.lang.annotation.Target - -/** - * Describes one option of an {@link ApplicationCommand}, so that command line tools can show it in the - * command's help and complete it. - * - *

An option is given as {@code --name=value}, or as {@code --name} when it is a {@link #flag()}:

- * - *
- * @CommandOption(name = 'groupClassName', description = 'Also generate role group classes with this name')
- * @CommandOption(name = 'uiOnly', flag = true, description = 'Configure the plugin without generating classes')
- * class SetupUsersCommand implements GrailsApplicationCommand { ... }
- * 
- * - *

The annotation only describes the option. The command still reads it from the command line, for - * example through {@code executionContext.commandLine}. A command that declares no option accepts any - * options.

- * - * @since 9.0 - */ -@Documented -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.TYPE) -@Repeatable(CommandOptions) -@interface CommandOption { - - /** - * @return the name of the option, without the leading {@code --} - */ - String name() - - /** - * @return the description of the option, shown in the command's help - */ - String description() default '' - - /** - * @return whether the option is a flag, given without a value - */ - boolean flag() default false - - /** - * @return the values offered when the option's value is completed; none when it takes any value - */ - String[] values() default [] -} diff --git a/grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandOptions.groovy b/grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandOptions.groovy deleted file mode 100644 index d9c37db45c2..00000000000 --- a/grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandOptions.groovy +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.grails.core.cli - -import java.lang.annotation.Documented -import java.lang.annotation.ElementType -import java.lang.annotation.Retention -import java.lang.annotation.RetentionPolicy -import java.lang.annotation.Target - -/** - * Holds the {@link CommandOption} annotations of a command that declares more than one. - * - * @since 9.0 - */ -@Documented -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.TYPE) -@interface CommandOptions { - - /** - * @return the options of the command - */ - CommandOption[] value() -} diff --git a/grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandParameter.groovy b/grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandParameter.groovy deleted file mode 100644 index 9d515d2af46..00000000000 --- a/grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandParameter.groovy +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.grails.core.cli - -import java.lang.annotation.Documented -import java.lang.annotation.ElementType -import java.lang.annotation.Repeatable -import java.lang.annotation.Retention -import java.lang.annotation.RetentionPolicy -import java.lang.annotation.Target - -/** - * Describes one positional argument of an {@link ApplicationCommand}, so that command line tools can - * show it in the command's help and complete it. - * - *

Declare one annotation per argument, in the order the arguments are given on the command line:

- * - *
- * @CommandParameter(name = 'package', description = 'The package of the domain classes')
- * @CommandParameter(name = 'user-class-name', description = 'The name of the user class')
- * class SetupUsersCommand implements GrailsApplicationCommand { ... }
- * 
- * - *

The annotation only describes the argument. The command still reads its value from the command - * line, for example through {@code args} of {@link GrailsApplicationCommand}. A command that declares - * no argument accepts any arguments.

- * - * @since 9.0 - */ -@Documented -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.TYPE) -@Repeatable(CommandParameters) -@interface CommandParameter { - - /** - * @return the name of the argument, shown in the command's help - */ - String name() - - /** - * @return the description of the argument, shown in the command's help - */ - String description() default '' - - /** - * @return whether the command needs the argument - */ - boolean required() default true - - /** - * @return the values offered when the argument is completed; none when the argument takes any value - */ - String[] values() default [] -} diff --git a/grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandParameters.groovy b/grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandParameters.groovy deleted file mode 100644 index c770c53f87d..00000000000 --- a/grails-core/src/cli/groovy/org/apache/grails/core/cli/CommandParameters.groovy +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.grails.core.cli - -import java.lang.annotation.Documented -import java.lang.annotation.ElementType -import java.lang.annotation.Retention -import java.lang.annotation.RetentionPolicy -import java.lang.annotation.Target - -/** - * Holds the {@link CommandParameter} annotations of a command that declares more than one. - * - * @since 9.0 - */ -@Documented -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.TYPE) -@interface CommandParameters { - - /** - * @return the arguments of the command, in the order they are given on the command line - */ - CommandParameter[] value() -} diff --git a/grails-core/src/test-cli/groovy/org/apache/grails/core/cli/ApplicationCommandManifestWriterSpec.groovy b/grails-core/src/test-cli/groovy/org/apache/grails/core/cli/ApplicationCommandManifestWriterSpec.groovy deleted file mode 100644 index f9cba7e1b4b..00000000000 --- a/grails-core/src/test-cli/groovy/org/apache/grails/core/cli/ApplicationCommandManifestWriterSpec.groovy +++ /dev/null @@ -1,181 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.grails.core.cli - -import java.nio.file.Path - -import groovy.json.JsonSlurper - -import spock.lang.Specification -import spock.lang.TempDir - -class ApplicationCommandManifestWriterSpec extends Specification { - - @TempDir - Path tempDir - - private ClassLoader originalContextClassLoader - private URLClassLoader factoryClassLoader - - void setup() { - originalContextClassLoader = Thread.currentThread().contextClassLoader - } - - void cleanup() { - Thread.currentThread().contextClassLoader = originalContextClassLoader - factoryClassLoader?.close() - } - - void 'the manifest lists the commands on the classpath, sorted by name, with their descriptions'() { - given: - declareCommands(UndescribedManifestCommand, DescribedManifestCommand) - - when: - Map manifest = writeManifest() - - then: - manifest.version == ApplicationCommandManifestWriter.FORMAT_VERSION - - and: 'every command the registry finds, in name order' - List names = manifest.commands*.name - names == names.toSorted() - names.containsAll(['described-manifest', 'undescribed-manifest']) - - and: - with(command(manifest, 'described-manifest')) { - description == 'Sets up the users of the application' - className == DescribedManifestCommand.name - } - - and: "a command without a description of its own is described by the trait's default, its name" - with(command(manifest, 'undescribed-manifest')) { - description == 'undescribed-manifest' - arguments == [] - options == [] - } - } - - void 'the manifest describes the declared arguments in the order they are given and the declared options'() { - given: - declareCommands(DescribedManifestCommand) - - when: - Map described = command(writeManifest(), 'described-manifest') - - then: - described.arguments == [ - [name: 'package', description: 'The package of the domain classes', required: true, values: []], - [name: 'user-class-name', description: 'The name of the user class', required: false, values: []] - ] - - and: - described.options == [ - [name: 'groupClassName', description: 'Also generate role group classes', flag: false, values: []], - [name: 'uiOnly', description: 'Configure without generating classes', flag: true, values: []], - [name: 'format', description: '', flag: false, values: ['json', 'yaml']] - ] - } - - void 'a command may declare a single argument and a single option'() { - given: - declareCommands(SingleDeclarationManifestCommand) - - when: - Map described = command(writeManifest(), 'single-declaration-manifest') - - then: - described.arguments == [[name: 'domain-class', description: '', required: true, values: ['Book', 'Author']]] - described.options == [[name: 'force', description: '', flag: true, values: []]] - } - - void 'the directory of the manifest is created when it does not exist'() { - given: - declareCommands(UndescribedManifestCommand) - File manifestFile = tempDir.resolve('build/grails/commands.json').toFile() - - when: - ApplicationCommandManifestWriter.main(manifestFile.path) - - then: - manifestFile.isFile() - } - - void 'the writer needs the path of the file to write'() { - when: - ApplicationCommandManifestWriter.main(args as String[]) - - then: - IllegalArgumentException e = thrown() - e.message == 'Expected the path of the manifest file to write as the only argument' - - where: - args << [[], ['one.json', 'two.json']] - } - - private void declareCommands(Class... commandTypes) { - File metaInf = tempDir.resolve('classes/META-INF').toFile() - assert metaInf.mkdirs() - new File(metaInf, 'grails-cli.factories').text = "${ApplicationCommand.name}=${commandTypes*.name.join(',')}" - factoryClassLoader = new URLClassLoader([tempDir.resolve('classes').toUri().toURL()] as URL[], getClass().classLoader) - Thread.currentThread().contextClassLoader = factoryClassLoader - } - - private Map writeManifest() { - File manifestFile = tempDir.resolve('commands.json').toFile() - ApplicationCommandManifestWriter.main(manifestFile.path) - (Map) new JsonSlurper().parse(manifestFile) - } - - private static Map command(Map manifest, String name) { - (Map) manifest.commands.find { it.name == name } - } -} - -class UndescribedManifestCommand implements ApplicationCommand { - - @Override - boolean handle(ExecutionContext executionContext) { - true - } -} - -@CommandParameter(name = 'package', description = 'The package of the domain classes') -@CommandParameter(name = 'user-class-name', description = 'The name of the user class', required = false) -@CommandOption(name = 'groupClassName', description = 'Also generate role group classes') -@CommandOption(name = 'uiOnly', description = 'Configure without generating classes', flag = true) -@CommandOption(name = 'format', values = ['json', 'yaml']) -class DescribedManifestCommand implements ApplicationCommand { - - final String description = 'Sets up the users of the application' - - @Override - boolean handle(ExecutionContext executionContext) { - true - } -} - -@CommandParameter(name = 'domain-class', values = ['Book', 'Author']) -@CommandOption(name = 'force', flag = true) -class SingleDeclarationManifestCommand implements ApplicationCommand { - - @Override - boolean handle(ExecutionContext executionContext) { - true - } -} diff --git a/grails-doc/src/en/guide/commandLine.adoc b/grails-doc/src/en/guide/commandLine.adoc index 181bba0827c..b66755c6cd0 100644 --- a/grails-doc/src/en/guide/commandLine.adoc +++ b/grails-doc/src/en/guide/commandLine.adoc @@ -195,107 +195,6 @@ features: This "grails-forge-cli.yml" configuration sets the default values for various aspects of the Grails web application, including the application type, default package, test framework, source language, build tool, GORM implementation, servlet implementation, and a list of enabled features. -[[projectCommands]] -=== Commands of the Project - -Inside a Grails project, the Grails Forge CLI also offers the commands of the application and of its -plugins: the commands in `grails-app/commands` and those every plugin of the build brings with it, such -as `s2-quickstart` from the Spring Security plugin or the `dbm-*` commands of database migration. The -interactive prompt completes their names, the options they declare and the values those options take: - -[source,shell] ----- -$ grails-forge-cli -grails> s2- -s2-create-persistent-token s2-create-role-hierarchy-entry s2-quickstart -grails> s2-quickstart com.example User Role -- ---groupClassName --help --salt --uiOnly ----- - -Each command shows its usage with `--help`, and runs through the project's `runCommand` Gradle task, -as `./gradlew runCommand -Pargs="s2-quickstart com.example User Role"` would run it. The Forge CLI hands -the arguments to the task in a file, so an argument with spaces or quotes reaches `runCommand` as typed, -on every operating system, and the command reads it as `runCommand` always has: trimmed, with a pair of -double quotes around it removed, and left out when it is empty. In a build of several projects, the task -runs in the project of the current directory only, not also in the projects below it. The Forge CLI's -own commands keep their name: a project command named like one of them is not offered. - -A command runs in the development environment unless `-D` says otherwise. `-D` sets a system property -of the command: it reaches `runCommand` among the arguments, as `-Dname=value`, and `runCommand` starts -the command with the property set, as `./gradlew runCommand -Pargs="dbm-update -Dgrails.env=test"` does: - -[source,shell] ----- -grails> dbm-update -Dgrails.env=test ----- - -The Forge CLI learns the commands from the project's build: the Grails Gradle plugin's -`generateCommandManifest` task writes them to `build/grails/commands.json`, with the directories whose -files decide them: - -* the directory of every project of the build, for its `.gradle`, `.gradle.kts`, `.properties` and - `.toml` files, such as `build.gradle`, `settings.gradle`, `gradle.properties` and `libs.versions.toml`; -* everything under the `gradle` and `buildSrc` directories of the build's root, under - `grails-app/commands`, `src/main/resources/META-INF` and `src/cli/resources/META-INF` of every - project, and under every included build, also one included with `pluginManagement { includeBuild }`, apart from what builds and tools - write outside a `src` or `grails-app` directory, such as `build`, `.gradle` and `.kotlin`, and from - `node_modules`; -* everything under the directories of the `cli` source set of every project that has one, such as - `src/cli/groovy` and `src/cli/resources`, where a command a plugin project of the build adds registers - itself; -* the source file of each command it lists that one of the projects of the build compiles, such as a - command of a plugin project in `src/main/groovy`; -* the names of the files in the main Java and Groovy sources of every project, such as `src/main/groovy`, - that are named like a command, `AuditCommand.groovy` or `AuditCommand.java`, so a command added there - counts, while editing or adding another source does not. - -Each time it runs the task, the Forge CLI records those files, with their times and sizes, and the -directories the manifest names, in the project's `.gradle/grails` directory, so they are still watched -after `./gradlew clean` removes the manifest. It runs the task again when one of them is added or deleted, or -its time or size changes, whether the time goes forward or back, and when a manifest has no record, as -one written by `./gradlew generateCommandManifest` alone. So a plugin added to the build brings its commands the next -time a project command is asked for. A change the Forge CLI cannot see, such as a new snapshot of a -plugin, or a command in the main sources of a file not named after it, such as a class that becomes a -command, is picked up by running `./gradlew generateCommandManifest`. - -The build runs only when the interactive prompt starts or a command the Forge CLI does not have is -named, also after the Forge CLI's own options, as in `grails-forge-cli -x dbm-update`. Its own commands, -such as `create-controller`, never run it, even in a project they do not apply to, and neither do its -options alone, such as `--help`, or a command line after `--`. `grails-forge-cli --help` lists the -project's commands from the file the build last wrote. When a command at the prompt runs the build, the -prompt then completes the commands it found. - -Since the build is code of the project, the Forge CLI runs it as `./gradlew` would. In a checkout you -do not trust, set the environment variable `GRAILS_FORGE_RUN_BUILD`, or the system property -`grails.forge.runBuild`, to `false`. The Forge CLI then never runs the build on its own: it offers the -commands of the file the build last wrote, which `./gradlew generateCommandManifest` writes, and runs the -build only for a project command you name. - -[source,shell] ----- -$ GRAILS_FORGE_RUN_BUILD=false grails-forge-cli ----- - -When the build fails, as it does while the application does not compile or in a project of a Grails -release without `generateCommandManifest`, the Forge CLI says so once and keeps offering the commands of -the file the build last wrote, if any. It does not run the build for them again until one of the files -above changes, also after `./gradlew clean`. While the build fails, editing a file named like a command -counts as a change too, so fixing a new command that did not compile runs the build again. Run -`./gradlew generateCommandManifest` to see what went wrong. A command of the file that the Forge CLI cannot offer, such as one declaring an option named -`help`, is reported and left out, and the others are offered. - -The build runs through the Gradle wrapper of the project, or of the build it belongs to: the wrapper in -the first directory above it with a `settings.gradle` or `settings.gradle.kts` file. A project whose build has no wrapper has no -commands offered, and no `gradle` found on the path is run instead. - -A command that describes its arguments and options, as <> -shows, has them checked and completed. A command that declares no options takes any arguments and -options, and is given the words of its command line in the order they were typed, as `runCommand` -gives them. - -The `grails` command of an SDKMAN! installation starts the profile CLI by default. Start the Forge CLI -with `grails-forge-cli`, with `grails -t forge`, or by default with `GRAILS_PREFERRED_SHELL=forge`. - === Grails Default Package Configuration The project's default package is determined based on the project's name. For instance, running the following command: diff --git a/grails-doc/src/en/guide/commandLine/creatingCustomCommands.adoc b/grails-doc/src/en/guide/commandLine/creatingCustomCommands.adoc index b8dafa00da8..79f00b6a598 100644 --- a/grails-doc/src/en/guide/commandLine/creatingCustomCommands.adoc +++ b/grails-doc/src/en/guide/commandLine/creatingCustomCommands.adoc @@ -212,94 +212,8 @@ Now you can run the `greet` command with arguments and options: ./gradlew runCommand -Pargs="greet Alice --loud" ---- -`args` is split into words as a shell would split it. A tool that passes arguments it cannot quote for -every shell can put them in a JSON file as an array of strings and name it with `argsFile`, a path -relative to the project directory or an absolute one. Each reaches the command's command line as it is in -the file, after those of `args`: - -[source,shell] ----- -echo '["greet", "Alice and Bob"]' > greet-args.json -./gradlew runCommand -PargsFile=greet-args.json ----- - -However they are given, the command reads the arguments through Grails' command line parser: each is -trimmed, a pair of double quotes around one is removed, an empty one is left out, and an option's value -is everything after the first `=`, as in `--filter=status=active`. A word that starts with `-` is an -option, and one whose name holds a space, such as `--dev mode`, is refused as invalid. An empty argument -after an option without a value is that option's empty value. An argument `-Dname=value` sets a -system property: `runCommand` starts the command with it set, so `-Dgrails.env=test` runs the command in -the test environment, and the command also finds it in the system properties of its command line. - -In a build of several projects, `./gradlew runCommand` from the root runs the task in every project that -has it. Name one project's directory, absolute or relative to the root of the build, with the -`grailsCommandProject` property to run it there only; the other command tasks and -`generateCommandManifest` take it too. In the other projects the task is skipped together with the -tasks it would depend on, so a project that does not compile leaves the command of another alone: - -[source,shell] ----- -./gradlew runCommand -Pargs="greet Alice" -PgrailsCommandProject=app ----- - This allows you to create more versatile and interactive custom commands for your Grails application. -**Describing Arguments and Options** - -A command can describe the arguments and options it takes with the -link:{api}org/apache/grails/core/cli/CommandParameter.html[CommandParameter] and -link:{api}org/apache/grails/core/cli/CommandOption.html[CommandOption] annotations. Tools that offer the -commands of a project, such as the <>, show them in the -command's help, check them and complete them. Declare one `@CommandParameter` per argument, in the order -the arguments are given, and one `@CommandOption` per option: - -[source,groovy] ----- -// grails-app/commands/com/example/GreetCommand.groovy -package com.example - -import org.apache.grails.core.cli.CommandOption -import org.apache.grails.core.cli.CommandParameter -import org.apache.grails.core.cli.GrailsApplicationCommand - -@CommandParameter(name = 'name', required = false, description = 'Who to greet') -@CommandOption(name = 'loud', flag = true, description = 'Greet in capitals') -@CommandOption(name = 'language', values = ['en', 'de', 'fr'], description = 'The language of the greeting') -class GreetCommand implements GrailsApplicationCommand { - - String getDescription() { - 'Greet the user with options' - } - - boolean handle() { - String name = args ? args[0] : 'user' - boolean loud = executionContext.commandLine.hasOption('loud') - String language = executionContext.commandLine.optionValue('language') ?: 'en' - // ... - true - } -} ----- - -An argument is required unless it declares `required = false`. An option is given as `--name value` or -`--name=value`, or as `--name` when it is a `flag`. `values` lists the values offered when the argument -or option is completed, in either form: - -[source,shell] ----- -grails> greet Alice --language -de en fr -grails> greet Alice --language= ---language=de --language=en --language=fr ----- - -The Forge CLI passes a declared option on as `--name=value`, after the arguments. The annotations only describe the command: it still reads its arguments and options from the -command line as before. - -A command that declares no `@CommandParameter` takes any arguments, and one that declares no -`@CommandOption` takes any options and is given its command line in the order it was typed, so a command -without the annotations works as it always has. - In summary, creating custom commands in Grails using the `GrailsApplicationCommand` trait is a powerful way to extend your application's functionality beyond the web interface. You can define the command's name, description, and logic, and then execute it from the command line, optionally passing arguments and options as needed. **Using the `executionContext` in the Grails Custom Commands** @@ -462,14 +376,6 @@ dependencies { # or, via the automatically registered per-command task: ./gradlew auditReport ---- -+ -Inside the application, the <> offers and completes the -command as well, and runs it through `runCommand`: -+ -[source,shell] ----- -grails-forge-cli audit-report ----- If an application disables the automatic discovery (`grails { cliAutoProvision = false }`), it declares the companion explicitly instead: diff --git a/grails-doc/src/en/guide/introduction/whatsNew.adoc b/grails-doc/src/en/guide/introduction/whatsNew.adoc index c0df18baf1b..94e52dc8599 100644 --- a/grails-doc/src/en/guide/introduction/whatsNew.adoc +++ b/grails-doc/src/en/guide/introduction/whatsNew.adoc @@ -369,28 +369,6 @@ breakdown. The Grails Data plugin itself (`grails-data-hibernate5` or `grails-data-hibernate7`) is an application dependency only: generated builds no longer place it on the `buildscript` classpath or in `buildSrc`, where Gradle's own Groovy would run the compile-time transformations the plugin carries while compiling the build scripts. See the xref:upgrading#upgrading80x[upgrade guide] for the equivalent change in an existing build. -==== Plugin Commands in the Grails Forge CLI - -Inside a Grails project, the Grails Forge CLI now offers the commands of the application and of its -plugins next to its own, with Tab completion at its prompt. Adding the Spring Security plugin to the -build is enough to type `s2-quickstart`: - -[source,shell] ----- -$ grails-forge-cli -grails> s2-quickstart com.example User Role --groupClassName=RoleGroup ----- - -The Forge CLI reads the commands from a manifest that the project's build writes with the new -`generateCommandManifest` task, so it never loads the project's classpath itself, and runs a command -through the project's `runCommand` task. A command describes the arguments and options it takes with -the new `@CommandParameter` and `@CommandOption` annotations, which the Forge CLI shows in the command's -help, checks and completes. Commands without them, such as those of plugins built for an earlier -release, are offered too and take any arguments. - -See <> and -<>. - ==== GSP Tag Library Improvements Grails {grailsMajorVersion} continues the move toward method-based TagLib handlers while preserving compatibility with existing closure-based tags. diff --git a/grails-forge/grails-forge-cli/build.gradle b/grails-forge/grails-forge-cli/build.gradle index 3aa89fa82db..aab6c01bf02 100644 --- a/grails-forge/grails-forge-cli/build.gradle +++ b/grails-forge/grails-forge-cli/build.gradle @@ -90,7 +90,6 @@ dependencies { implementation 'org.apache.grails:grails-core' implementation 'org.apache.grails.bootstrap:grails-bootstrap' // grails.codegen.model, for add-property implementation 'org.apache.groovy:groovy' - implementation 'org.apache.groovy:groovy-json' // reads the command manifest of a project // renders the compiled pages, without a web request implementation 'org.apache.grails.views:grails-gsp-core' diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/CommandLineCompleter.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/CommandLineCompleter.groovy index e7564e8a9eb..f33d061bef6 100644 --- a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/CommandLineCompleter.groovy +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/CommandLineCompleter.groovy @@ -18,8 +18,6 @@ */ package org.grails.forge.cli -import java.util.function.Supplier - import groovy.transform.CompileStatic import org.jline.reader.Candidate @@ -32,43 +30,22 @@ import picocli.CommandLine.Model.CommandSpec /** * Completes the word under the cursor at the interactive prompt from the picocli commands, their * subcommands and their options. - * - *

The commands can be given as a supplier, so the prompt completes the commands offered now, such as - * those of a project whose build changed since the prompt started.

*/ @CompileStatic class CommandLineCompleter implements Completer { - private final Supplier specs + private final CommandSpec spec CommandLineCompleter(CommandSpec spec) { - Objects.requireNonNull(spec, 'spec') - this.specs = { -> spec } as Supplier - } - - CommandLineCompleter(Supplier specs) { - this.specs = Objects.requireNonNull(specs, 'specs') + this.spec = Objects.requireNonNull(spec, 'spec') } @Override void complete(LineReader reader, ParsedLine line, List candidates) { - List words = new ArrayList<>(line.words()) - int wordIndex = line.wordIndex() - String word = wordIndex < words.size() ? words[wordIndex] : '' - int separator = word.indexOf('=') - // picocli completes the value of an option only as a word of its own, so --name=value is completed - // as --name value and the values are offered with the option in front of them - String prefix = '' - if (word.startsWith('--') && separator > 0) { - prefix = word.substring(0, separator + 1) - words.set(wordIndex, word.substring(0, separator)) - words.add(wordIndex + 1, word.substring(separator + 1)) - wordIndex++ - } List suggestions = [] - AutoComplete.complete(specs.get(), words.toArray(new String[0]), wordIndex, 0, line.cursor(), suggestions) + AutoComplete.complete(spec, line.words().toArray(new String[0]), line.wordIndex(), 0, line.cursor(), suggestions) suggestions.each { CharSequence suggestion -> - candidates.add(new Candidate(prefix + suggestion)) + candidates.add(new Candidate(suggestion.toString())) } } } diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/ForgeCli.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/ForgeCli.groovy index 8f5c7d1b9c3..9a1025c3c8a 100644 --- a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/ForgeCli.groovy +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/ForgeCli.groovy @@ -19,13 +19,10 @@ package org.grails.forge.cli import java.util.function.BiFunction -import java.util.function.Supplier import groovy.transform.CompileStatic -import org.jline.terminal.Terminal import org.springframework.context.ApplicationContext import org.springframework.context.ConfigurableApplicationContext import picocli.CommandLine -import picocli.CommandLine.Model.OptionSpec import org.grails.forge.cli.command.AddPropertyCommand import org.grails.forge.cli.command.BaseCommand import org.grails.forge.cli.command.CodeGenCommand @@ -35,10 +32,7 @@ import org.grails.forge.cli.command.CreateInterceptorCommand import org.grails.forge.cli.command.CreateJobCommand import org.grails.forge.cli.command.CreateServiceCommand import org.grails.forge.cli.command.CreateTagLibCommand -import org.grails.forge.cli.command.ProjectCommand -import org.grails.forge.cli.command.ProjectCommands import org.grails.forge.io.ConsoleOutput -import org.grails.forge.io.FileSystemOutputHandler /** * Runs the Forge CLI against the Grails application {@link Application} starts: builds the picocli @@ -62,24 +56,6 @@ final class ForgeCli { AddPropertyCommand ] as List> - /** - * The names of the code-gen commands, which are the Forge CLI's own even where they do not apply, so - * naming one never runs the project's build - */ - private static final Set CODE_GEN_COMMAND_NAMES = CODE_GEN_COMMANDS - .collect { Class type -> type.getAnnotation(CommandLine.Command).name() } - .toSet().asImmutable() - - /** - * The environment variable that, set to {@code false}, keeps the Forge CLI from running the build of the - * project on its own to update the project's commands: it offers those of a manifest the build already - * wrote, and runs the build only for a project command the user names. - */ - static final String RUN_BUILD_VARIABLE = 'GRAILS_FORGE_RUN_BUILD' - - /** The system property that does what {@link #RUN_BUILD_VARIABLE} does, and wins over it */ - static final String RUN_BUILD_PROPERTY = 'grails.forge.runBuild' - private static final BiFunction EXCEPTION_HANDLER = { Throwable e, CommandLine commandLine -> BaseCommand command = commandLine.getCommand() command.err(e.message) @@ -118,94 +94,18 @@ final class ForgeCli { */ static int execute(ApplicationContext context, String... args) { boolean noOpConsole = args.length > 0 && args[0].startsWith('update-cli-config') - CommandLine commandLine = createForgeCommandLine(context, noOpConsole) - // the project's build runs only when the command line names a command that is not the Forge CLI's: - // its own commands never wait for the build, and its options alone, such as --help, list the - // project's commands from a manifest the build already wrote - String command = commandName(commandLine, args) - if (command == null) { - if (args.length > 0) { - addProjectCommands(commandLine, false) - } - } - else if (!commandLine.subcommands.containsKey(command) && !CODE_GEN_COMMAND_NAMES.contains(command)) { - addProjectCommands(commandLine, true) - } - return commandLine.execute(args) - } - - /** - * The command a command line names: its first word that is neither one of the Forge CLI's options - * nor the value of one. None follows {@code --}, after which picocli takes every word as a parameter. - */ - private static String commandName(CommandLine commandLine, String... args) { - for (int i = 0; i < args.length; i++) { - String word = args[i] - if (word == '--') { - return null - } - if (!word.startsWith('-')) { - return word - } - OptionSpec option = commandLine.commandSpec.findOption(word) - if (option != null && option.arity().min() > 0) { - i++ - } - } - null + return createCommandLine(context, noOpConsole).execute(args) } /** * Runs the interactive shell on a running context until the user leaves it. */ static void interactive(ApplicationContext context) { - interactive(context, null) - } - - /** - * Runs the interactive shell on a running context until the user leaves it or its input ends. - * - * @param terminal the terminal the shell reads and writes, or {@code null} for the terminal of the JVM - */ - static void interactive(ApplicationContext context, Terminal terminal) { CommandLine commandLine = createCommandLine(context, true) - // a line may run the build and change the project's commands, so the prompt then completes those - // of the manifest as it is, read quietly, since a problem with it was reported already - CommandLine[] offered = [commandLine] as CommandLine[] - Supplier current = { -> offered[0] } as Supplier - new InteractiveShell(commandLine, current, { String[] commandArgs -> runLine(context, offered, commandArgs) }, - EXCEPTION_HANDLER, InteractiveShell.DEFAULT_PROMPT, terminal).start() - } - - /** - * Runs a line entered at the prompt, then makes the commands it offers those of the project's - * manifest as it is now. A problem reading them keeps those offered before, so the prompt goes on. - * - * @param offered holds the command line the prompt completes from - */ - private static void runLine(ApplicationContext context, CommandLine[] offered, String[] commandArgs) { - execute(context, commandArgs) - try { - CommandLine refreshed = createForgeCommandLine(context, true) - addProjectCommands(refreshed, false, ConsoleOutput.NOOP) - offered[0] = refreshed - } - catch (RuntimeException ignored) { - // the next line that runs reports it - } + new InteractiveShell(commandLine, { String[] commandArgs -> execute(context, commandArgs) }, EXCEPTION_HANDLER).start() } - /** - * Builds the command line with every command it offers in the current directory: the Forge CLI's - * own, and the commands of the Grails project there, if any. - */ static CommandLine createCommandLine(ApplicationContext context, boolean noOpConsole) { - CommandLine commandLine = createForgeCommandLine(context, noOpConsole) - addProjectCommands(commandLine, true) - return commandLine - } - - private static CommandLine createForgeCommandLine(ApplicationContext context, boolean noOpConsole) { ForgeCommand root = context.getBean(ForgeCommand) CommandLine commandLine = new CommandLine(root, new GrailsPicocliFactory(context)) commandLine.executionExceptionHandler = { Exception ex, CommandLine failed, CommandLine.ParseResult parseResult -> @@ -230,39 +130,4 @@ final class ForgeCli { return commandLine } - /** - * Whether the build of the project may run to update the project's commands: unless - * {@link #RUN_BUILD_PROPERTY}, or else {@link #RUN_BUILD_VARIABLE}, is {@code false}. - */ - private static boolean buildMayRun() { - String setting = System.getProperty(RUN_BUILD_PROPERTY) ?: System.getenv(RUN_BUILD_VARIABLE) - !'false'.equalsIgnoreCase(setting?.trim()) - } - - /** - * Adds the commands of the Grails project in the current directory, which run through the project's - * build. A command named like one of the Forge CLI's own is left out: the Forge CLI's command runs. - * - * @param runBuild whether the project's build may run to update the commands - */ - private static void addProjectCommands(CommandLine commandLine, boolean runBuild) { - addProjectCommands(commandLine, runBuild, (ForgeCommand) commandLine.getCommand()) - } - - private static void addProjectCommands(CommandLine commandLine, boolean runBuild, ConsoleOutput console) { - runBuild = runBuild && buildMayRun() - File projectDir - try { - projectDir = FileSystemOutputHandler.getDefaultBaseDirectory() - } - catch (IOException ignored) { - return - } - for (ProjectCommand command : new ProjectCommands(projectDir).load(console, runBuild)) { - if (!commandLine.subcommands.containsKey(command.name)) { - commandLine.addSubcommand(command.name, command.commandSpec) - } - } - } - } diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/InteractiveShell.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/InteractiveShell.groovy index 759333e619f..8d86582208c 100644 --- a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/InteractiveShell.groovy +++ b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/InteractiveShell.groovy @@ -29,24 +29,20 @@ import org.jline.reader.impl.DefaultParser import org.jline.terminal.Terminal import org.jline.terminal.TerminalBuilder import picocli.CommandLine -import picocli.CommandLine.Model.CommandSpec import java.util.function.BiFunction import java.util.function.Consumer -import java.util.function.Supplier import static picocli.CommandLine.Help.Ansi.AUTO @CompileStatic class InteractiveShell { - static final String DEFAULT_PROMPT = '@|blue grails>|@ ' + private static final String DEFAULT_PROMPT = '@|blue grails>|@ ' private final CommandLine commandLine - private final Supplier offered private final Consumer executor private final BiFunction onError private final String prompt - private final Terminal terminal InteractiveShell(CommandLine commandLine, Consumer executor, @@ -58,41 +54,7 @@ class InteractiveShell { Consumer executor, BiFunction onError, String prompt) { - this(commandLine, { -> commandLine } as Supplier, executor, onError, prompt) - } - - /** - * @param commandLine the command line whose help and colors the prompt starts with - * @param offered the command line of the commands offered now, which the prompt completes - * @param executor runs each line entered - * @param onError reports what ends the prompt - * @param prompt the prompt, with picocli's markup - */ - InteractiveShell(CommandLine commandLine, - Supplier offered, - Consumer executor, - BiFunction onError, - String prompt) { - this(commandLine, offered, executor, onError, prompt, null) - } - - /** - * @param commandLine the command line whose help and colors the prompt starts with - * @param offered the command line of the commands offered now, which the prompt completes - * @param executor runs each line entered - * @param onError reports what ends the prompt - * @param prompt the prompt, with picocli's markup - * @param terminal the terminal the prompt reads and writes, or {@code null} for the terminal of the JVM - */ - InteractiveShell(CommandLine commandLine, - Supplier offered, - Consumer executor, - BiFunction onError, - String prompt, - Terminal terminal) { - this.terminal = terminal this.commandLine = commandLine - this.offered = offered this.executor = executor this.onError = onError this.prompt = prompt @@ -100,10 +62,10 @@ class InteractiveShell { void start() { try { - Terminal terminal = this.terminal ?: TerminalBuilder.terminal() + Terminal terminal = TerminalBuilder.terminal() LineReader reader = LineReaderBuilder.builder() .terminal(terminal) - .completer(new CommandLineCompleter({ -> offered.get().commandSpec } as Supplier)) + .completer(new CommandLineCompleter(commandLine.commandSpec)) .parser(new DefaultParser()) .variable(LineReader.LIST_MAX, 50) // max tab completion candidates .build() diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommand.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommand.groovy deleted file mode 100644 index c5236152905..00000000000 --- a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommand.groovy +++ /dev/null @@ -1,215 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.grails.forge.cli.command - -import java.nio.charset.StandardCharsets -import java.nio.file.Files -import java.nio.file.Path -import java.util.concurrent.Callable - -import groovy.json.JsonOutput -import groovy.transform.CompileStatic - -import picocli.CommandLine -import picocli.CommandLine.Model.CommandSpec -import picocli.CommandLine.Model.OptionSpec -import picocli.CommandLine.Model.PositionalParamSpec - -/** - * A command of the project, from the command manifest the project's build writes: one of the - * application's own commands or a command of one of its plugins. It is offered and completed like - * the Forge CLI's own commands, and runs through the project's {@code runCommand} Gradle task. - * - *

Arguments and options the command declares are checked and completed. A command that declares - * no arguments takes any, and one that declares no options takes any options and is given the words of - * its command line in the order they were typed, so a command that does not describe itself runs as it - * does through {@code runCommand}.

- * - *

The arguments reach {@code runCommand} in a file, so they need no quoting for the shell or the - * wrapper script. {@code -D} sets a system property of the command, such as {@code -Dgrails.env=test}: - * it is passed among the arguments, as {@code -Dname=value}, and {@code runCommand} starts the - * command with it set.

- */ -@CompileStatic -class ProjectCommand extends BaseCommand implements Callable { - - /** The task that runs a command of the project */ - static final String RUN_COMMAND_TASK = 'runCommand' - - /** The project property {@code runCommand} reads the arguments of the command from */ - static final String ARGS_FILE_PROPERTY = 'argsFile' - - private static final String PASS_THROUGH_LABEL = 'ARGS' - - private static final String SYSTEM_PROPERTY_OPTION = '-D' - - final String name - - private final ProjectGradle gradle - private final CommandSpec commandSpec - private final OptionSpec systemProperties - private final boolean declaresOptions - - /** - * @param command the command, as described in the manifest - * @param gradle runs the project's build - */ - ProjectCommand(Map command, ProjectGradle gradle) { - this.name = command.name as String - this.gradle = gradle - this.declaresOptions = command.options as boolean - this.systemProperties = OptionSpec.builder(SYSTEM_PROPERTY_OPTION) - .paramLabel('=') - .type(Map) - .auxiliaryTypes(String, String) - .description('Sets a system property of the command, such as -Dgrails.env=test to run it in the test environment.') - .build() - this.commandSpec = createSpec(command) - this.spec = commandSpec - } - - /** - * @return the picocli model of the command, which is what the command line runs and completes - */ - CommandSpec getCommandSpec() { - commandSpec - } - - @Override - Integer call() throws Exception { - List commandArguments = [name] - commandArguments.addAll(givenArguments()) - // only its owner can read it, where the file system allows that, since the arguments can hold a password - Path argumentsFile = Files.createTempFile('grails-command-arguments', '.json') - try { - Files.writeString(argumentsFile, JsonOutput.toJson(commandArguments), StandardCharsets.UTF_8) - return gradle.run([RUN_COMMAND_TASK, "-P${ARGS_FILE_PROPERTY}=${argumentsFile.toAbsolutePath()}".toString()]) - } - finally { - Files.deleteIfExists(argumentsFile) - } - } - - private List givenSystemProperties() { - Map given = (systemProperties.getValue() ?: [:]) as Map - given.collect { String property, String value -> "${SYSTEM_PROPERTY_OPTION}${property}=${value}".toString() } - } - - private List givenArguments() { - CommandLine.ParseResult parsed = commandSpec.commandLine().parseResult - if (!declaresOptions) { - return typedArguments(parsed.originalArgs()) - } - List given = [] - for (PositionalParamSpec positional : commandSpec.positionalParameters()) { - if (parsed.hasMatchedPositional(positional)) { - given.addAll(positional.stringValues()) - } - } - for (OptionSpec option : commandSpec.options()) { - if (option.usageHelp() || option.versionHelp() || option.is(systemProperties) || !parsed.hasMatchedOption(option)) { - continue - } - given << (option.arity().max() == 0 - ? option.longestName() - : "${option.longestName()}=${option.stringValues().first()}".toString()) - } - given.addAll(parsed.unmatched()) - given.addAll(givenSystemProperties()) - given - } - - /** - * The words typed after the command's name: picocli cannot tell which word of an undeclared option - * is its value, so the command gets them in their order, as it does from {@code runCommand}. Only - * {@code -D name=value}, given as two words, is joined into the one word {@code runCommand} reads. - */ - private List typedArguments(List commandLine) { - List typed = [] - List afterName = commandLine.subList(commandLine.indexOf(name) + 1, commandLine.size()) - for (int i = 0; i < afterName.size(); i++) { - String word = afterName[i] - if (word == SYSTEM_PROPERTY_OPTION && i + 1 < afterName.size()) { - typed << SYSTEM_PROPERTY_OPTION + afterName[++i] - } - else { - typed << word - } - } - typed - } - - private CommandSpec createSpec(Map command) { - CommandSpec created = CommandSpec.wrapWithoutInspection(this) - created.name(name) - created.addOption(OptionSpec.builder('-h', '--help') - .usageHelp(true) - .description('Show this help message and exit.') - .build()) - created.addOption(systemProperties) - String description = command.description as String - if (description) { - created.usageMessage().description(description) - } - - List> arguments = (command.arguments ?: []) as List> - arguments.eachWithIndex { Map argument, int index -> - boolean required = argument.required != false - created.addPositional(PositionalParamSpec.builder() - .index(String.valueOf(index)) - .paramLabel(argument.name as String) - .description((argument.description ?: '') as String) - .arity(required ? '1' : '0..1') - .required(required) - .type(String) - .completionCandidates(candidates(argument)) - .build()) - } - if (!arguments) { - created.addPositional(PositionalParamSpec.builder() - .index('0..*') - .paramLabel(PASS_THROUGH_LABEL) - .description('The arguments of the command') - .arity('0..*') - .type(String[]) - .build()) - } - - List> options = (command.options ?: []) as List> - for (Map option : options) { - boolean flag = option.flag == true - created.addOption(OptionSpec.builder("--${option.name}".toString()) - .description((option.description ?: '') as String) - .arity(flag ? '0' : '1') - .type(flag ? boolean : String) - .paramLabel((option.name as String).toUpperCase(Locale.ENGLISH)) - .completionCandidates(candidates(option)) - .build()) - } - if (!options) { - created.parser().unmatchedArgumentsAllowed(true) - } - created - } - - private static Iterable candidates(Map declaration) { - List values = (declaration.values ?: []) as List - values ? values.asImmutable() : null - } -} diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy deleted file mode 100644 index 35a15e8687d..00000000000 --- a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectCommands.groovy +++ /dev/null @@ -1,508 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.grails.forge.cli.command - -import java.nio.file.Files -import java.nio.file.Path - -import groovy.json.JsonException -import groovy.json.JsonOutput -import groovy.json.JsonSlurper -import groovy.transform.CompileStatic -import groovy.transform.EqualsAndHashCode - -import org.grails.forge.io.ConsoleOutput - -/** - * Finds the commands of the Grails project in a directory: those of the application and of its - * plugins, which {@code runCommand} runs. - * - *

The project's build describes them in a manifest, written by the Grails Gradle plugin's - * {@code generateCommandManifest} task, which also names the directories and files that decide the - * commands. Each time the build runs, the files of those directories are recorded, with their times and - * sizes; the manifest is read as long as they are unchanged, and otherwise the task is run first, so a - * plugin added to the build brings its commands with it. When the build fails, the commands of the last - * manifest are kept, and the build is not run for them again until one of those files changes.

- */ -@CompileStatic -class ProjectCommands { - - /** The task of the Grails Gradle plugin that writes the manifest */ - static final String MANIFEST_TASK = 'generateCommandManifest' - - /** The project property that tells the task where to write the manifest */ - static final String MANIFEST_PROPERTY = 'grailsCommandManifest' - - /** Where the manifest is kept, relative to the project directory */ - static final String MANIFEST_PATH = 'build/grails/commands.json' - - /** - * Where the files the commands depend on are recorded when the build runs, relative to the project - * directory: outside the build directory, so that cleaning the build does not forget a failed build - */ - static final String RECORD_PATH = '.gradle/grails/commands.record' - - /** Kept next to the record when the build could not write the manifest, so it is not run again for nothing */ - static final String FAILED_PATH = '.gradle/grails/commands.failed' - - /** - * A copy of the inputs the last manifest named, kept next to the record, so they are still watched when - * cleaning the build removed the manifest - */ - static final String INPUTS_PATH = '.gradle/grails/commands.inputs' - - /** The newest version of the manifest's format this CLI reads */ - static final int FORMAT_VERSION = 1 - - private static final List BUILD_FILE_SUFFIXES = ['.gradle', '.gradle.kts', '.properties', '.toml'] - - /** What builds and tools write, rather than read, outside a directory of sources */ - private static final List OUTPUT_DIRS = ['build', '.gradle', '.kotlin', 'out', 'bin', '.git', '.idea', '.settings'] - - /** Directories whose files are sources, so a package below them named like an output is read */ - private static final List SOURCE_DIRS = ['src', 'grails-app'] - - /** How the file of a command is named, after the class whose name gives the command its own */ - private static final List COMMAND_FILE_SUFFIXES = ['Command.groovy', 'Command.java'] - - /** What tools write anywhere, sources included */ - private static final List TOOL_FILES = ['node_modules', '.DS_Store'] - - private final File projectDir - private final ProjectGradle gradle - - ProjectCommands(File projectDir) { - this(projectDir, new ProjectGradle(projectDir)) - } - - ProjectCommands(File projectDir, ProjectGradle gradle) { - this.projectDir = projectDir - this.gradle = gradle - } - - /** - * @return whether the directory holds a Grails project built with Gradle - */ - boolean isGrailsProject() { - new File(projectDir, 'grails-app').isDirectory() && - (new File(projectDir, 'build.gradle').isFile() || new File(projectDir, 'build.gradle.kts').isFile()) - } - - /** - * Reads the commands of the project, after updating the manifest when it is missing or one of the - * files it depends on changed. - * - * @param console where to say that the build is being run, and why the commands could not be read - * @return the commands, or none when the directory holds no Grails project or none could be read - */ - List load(ConsoleOutput console) { - load(console, true) - } - - /** - * Reads the commands of the project. - * - * @param console where to say that the build is being run, and why the commands could not be read - * @param runBuild whether the manifest may be updated by running the build; when not, a manifest - * that is there is read as it is, and none means no commands - * @return the commands, or none when the directory holds no Grails project or none could be read - */ - List load(ConsoleOutput console, boolean runBuild) { - if (!grailsProject) { - return [] - } - if (!gradle.available) { - console.warning('The build of the project has no Gradle wrapper, so its commands are not offered.') - return [] - } - File manifest = new File(projectDir, MANIFEST_PATH) - if (runBuild && outdated(manifest)) { - update(manifest, console) - } - manifest.isFile() ? read(manifest, console) : [] - } - - private void update(File manifest, ConsoleOutput console) { - // the files are recorded before the build runs, so one changed while it runs counts next time - Inputs before = inputsOf(manifest) - List recorded = snapshotOrNull(before, false) - // a failed build is recorded with the contents of the files named like a command too, so fixing - // a new command that did not compile runs the build again, while nothing else changed does not - List recordedWhole = snapshotOrNull(before, true) - console.out('Reading the commands of the project from its build...') - String problem - try { - int exitCode = gradle.runSilently([MANIFEST_TASK, "-P${MANIFEST_PROPERTY}=${MANIFEST_PATH}".toString()]) - problem = exitCode == 0 ? null : "its build failed with exit code ${exitCode}" - } - catch (IOException e) { - problem = e.message - } - if (problem == null && !manifest.isFile()) { - problem = 'its build wrote no manifest' - } - if (problem == null) { - // a manifest naming other files is recorded as they are now, unless one of the files known - // before the build changed while it ran: then the record from before makes the build run again - Inputs after = inputsOf(manifest) - boolean changedDuringBuild = after != before && snapshotOrNull(before, false) != recorded - save(after == before || changedDuringBuild ? recorded : snapshotOrNull(after, false), null, console) - keepInputs(manifest) - return - } - save(recordedWhole, problem, console) - String kept = manifest.isFile() ? 'The commands it listed before are offered' : 'Its commands are not offered' - console.warning("Could not read the commands of the project: ${problem}. ${kept}, and the build is not run " + - 'for them again until a build file changes. A project of Grails 9 or later lists them with its ' + - "${MANIFEST_TASK} Gradle task; run it to see what went wrong.") - } - - /** - * Records the files the commands depend on, and whether the build failed. When they cannot be - * written, the build is run again next time, which is said. - */ - private void save(List recorded, String problem, ConsoleOutput console) { - File record = new File(projectDir, RECORD_PATH) - File failed = new File(projectDir, FAILED_PATH) - if (recorded == null) { - console.warning('Could not read the files the commands of the project depend on. The build is run for ' + - 'them again next time.') - return - } - try { - Files.createDirectories(record.parentFile.toPath()) - Files.write(record.toPath(), recorded.collect { String line -> line + '\n' }.join('').getBytes('UTF-8')) - if (problem == null) { - Files.deleteIfExists(failed.toPath()) - } - else { - Files.write(failed.toPath(), (problem + '\n').getBytes('UTF-8')) - } - } - catch (IOException e) { - console.warning("Could not record what the commands of the project depend on in ${record.parentFile}: " + - "${e.message ?: e.class.simpleName}. The build is run for them again next time.") - } - } - - private List read(File manifest, ConsoleOutput console) { - List commands - try { - Object parsed = new JsonSlurper().parse(manifest, 'UTF-8') - if (!(parsed instanceof Map)) { - throw new IllegalArgumentException('it is not a JSON object') - } - Map content = (Map) parsed - Object version = content.version - if (!(version instanceof Integer)) { - throw new IllegalArgumentException('it has no format version') - } - if ((int) version > FORMAT_VERSION) { - throw new IllegalArgumentException("it was written in format ${version}, by a newer Grails, and this " + - "Grails Forge CLI reads up to format ${FORMAT_VERSION}") - } - if (!(content.commands instanceof List)) { - throw new IllegalArgumentException('it has no list of commands') - } - commands = (List) content.commands - } - catch (JsonException | IOException | RuntimeException e) { - console.warning("Could not read the commands of the project from ${manifest}, so they are not offered: ${e.message}") - return [] - } - // a command picocli cannot take, such as one declaring an option twice, costs only that command - List created = [] - for (Object command : commands) { - try { - created << create(command) - } - catch (RuntimeException e) { - console.warning("The command ${nameOf(command)} of the project is not offered: ${e.message}") - } - } - created - } - - private ProjectCommand create(Object command) { - if (!(command instanceof Map) || !(((Map) command).name instanceof String) || !((Map) command).name) { - throw new IllegalArgumentException("it has no name: ${command}") - } - new ProjectCommand((Map) command, gradle) - } - - private static String nameOf(Object command) { - Object name = command instanceof Map ? ((Map) command).name : null - name instanceof String && name ? (String) name : 'without a name' - } - - /** - * Whether the manifest needs the build to run: there is none and the build did not fail last time, - * nothing was recorded, or a recorded file changed. - */ - private boolean outdated(File manifest) { - if (!manifest.isFile() && !new File(projectDir, FAILED_PATH).isFile()) { - return true - } - File record = new File(projectDir, RECORD_PATH) - try { - // without a record, as when the project's own build wrote the manifest, a file deleted since - // cannot be told from one that was never there - boolean failedLast = new File(projectDir, FAILED_PATH).isFile() - return !record.isFile() || Files.readAllLines(record.toPath()) != snapshot(inputsOf(manifest), failedLast) - } - catch (IOException | RuntimeException ignored) { - return true - } - } - - /** - * Keeps a copy of the inputs the manifest names, or forgets the last one when it names none. - */ - private void keepInputs(File manifest) { - Path kept = new File(projectDir, INPUTS_PATH).toPath() - try { - Object parsed = new JsonSlurper().parse(manifest, 'UTF-8') - Object inputs = parsed instanceof Map ? ((Map) parsed).inputs : null - if (inputs instanceof Map) { - Files.createDirectories(kept.parent) - Files.write(kept, JsonOutput.toJson(inputs).getBytes('UTF-8')) - } - else { - Files.deleteIfExists(kept) - } - } - catch (JsonException | IOException | RuntimeException ignored) { - // without the copy, a manifest removed by cleaning the build is watched through the project's inputs - } - } - - /** - * The directories the commands depend on: those the manifest names; without a manifest, as after - * cleaning the build, those the last manifest named; or else those of the project and the root of - * its build. - */ - private Inputs inputsOf(File manifest) { - if (manifest.isFile()) { - Object parsed = parseOrNull(manifest) - Inputs named = inputsFrom(parsed instanceof Map ? ((Map) parsed).inputs : null) - if (named != null) { - return named - } - } - else { - File kept = new File(projectDir, INPUTS_PATH) - Inputs named = kept.isFile() ? inputsFrom(parseOrNull(kept)) : null - if (named != null) { - return named - } - } - File rootDir = gradle.rootDir - new Inputs([projectDir, rootDir].unique(), - [new File(rootDir, 'gradle'), new File(rootDir, 'buildSrc'), - new File(projectDir, 'grails-app/commands'), new File(projectDir, 'src/main/resources/META-INF')], - [new File(projectDir, 'src/cli')], - [new File(projectDir, 'src/main/groovy'), new File(projectDir, 'src/main/java')], []) - } - - private static Object parseOrNull(File file) { - try { - return new JsonSlurper().parse(file, 'UTF-8') - } - catch (JsonException | IOException | RuntimeException ignored) { - // the manifest is reported when it is read - return null - } - } - - /** The inputs a manifest names, or none when it names none or they are not lists of absolute paths */ - private static Inputs inputsFrom(Object inputs) { - if (!(inputs instanceof Map)) { - return null - } - Map given = (Map) inputs - List buildDirectories = files(given.buildDirectories) - List sourceDirectories = files(given.sourceDirectories) - List commandDirectories = optionalFiles(given, 'commandDirectories') - List commandFileDirectories = optionalFiles(given, 'commandFileDirectories') - List sourceFiles = optionalFiles(given, 'sourceFiles') - if (buildDirectories == null || sourceDirectories == null || commandDirectories == null || - commandFileDirectories == null || sourceFiles == null) { - return null - } - new Inputs(buildDirectories, sourceDirectories, commandDirectories, commandFileDirectories, sourceFiles) - } - - /** A list a manifest written by an earlier Grails may not have, which then names no files */ - private static List optionalFiles(Map inputs, String key) { - inputs.containsKey(key) ? files(inputs[key]) : [] - } - - /** The files of a list of absolute paths, or none when one is not such a path, such as a mistyped one */ - private static List files(Object paths) { - if (!(paths instanceof List)) { - return null - } - List files = [] - for (Object path : (List) paths) { - if (!(path instanceof String) || !absolute((String) path)) { - return null - } - files << new File((String) path) - } - files - } - - private static boolean absolute(String path) { - try { - return Path.of(path).absolute && Path.of(path).nameCount > 0 - } - catch (RuntimeException ignored) { - return false - } - } - - /** - * What the files the commands depend on are now: the build files directly in each build directory, - * every file and directory below each source directory, apart from what builds and tools write next - * to a build script, and below each directory of commands, all of which are sources. Each such file - * is listed with its time and size, so a deleted, added, replaced or changed file changes the list, - * whatever the clock said when it was written. Below each directory of command files, the files - * named like a command are listed by name only, or, after a failed build, with their times and sizes. - */ - private static List snapshot(Inputs inputs, boolean wholeCommandFiles) { - List lines = [] - for (File dir : inputs.buildDirectories) { - File[] files = dir.listFiles() - if (files == null) { - lines << "absent\t${dir.absolutePath}".toString() - continue - } - for (File file : files.sort(false) { File it -> it.name }) { - if (file.isFile() && BUILD_FILE_SUFFIXES.any { String suffix -> file.name.endsWith(suffix) }) { - lines << describe(file) - } - } - } - for (File dir : inputs.sourceDirectories) { - // the sources of commands are sources from the start; a tree of a build becomes so below src - walk(dir, lines, dir.name == 'META-INF' || (dir.name == 'commands' && dir.parentFile?.name == 'grails-app')) - } - for (File dir : inputs.commandDirectories) { - walk(dir, lines, true) - } - // a new command there registers itself, and names its file after itself; other sources are left out - for (File dir : inputs.commandFileDirectories) { - walkNames(dir, lines, wholeCommandFiles) - } - for (File file : inputs.sourceFiles) { - walk(file, lines, true) - } - lines - } - - /** The record of the files, or none when they cannot be read, such as one of a path the system refuses */ - private static List snapshotOrNull(Inputs inputs, boolean wholeCommandFiles) { - try { - return snapshot(inputs, wholeCommandFiles) - } - catch (RuntimeException ignored) { - return null - } - } - - private static void walk(File file, List lines, boolean inSources) { - Path path = file.toPath() - if (Files.isSymbolicLink(path)) { - // not followed, so a link to a directory above it cannot loop - String target - try { - target = Files.readSymbolicLink(path).toString() - } - catch (IOException | UnsupportedOperationException ignored) { - target = '' - } - lines << "link\t${file.absolutePath}\t${target}".toString() - } - else if (file.isDirectory()) { - File[] children = file.listFiles() - if (children == null) { - lines << "unreadable\t${file.absolutePath}".toString() - return - } - lines << "directory\t${file.absolutePath}".toString() - for (File child : children.sort(false) { File it -> it.name }) { - boolean output = !inSources && child.name in OUTPUT_DIRS && child.isDirectory() - if (!output && !(child.name in TOOL_FILES)) { - walk(child, lines, inSources || child.name in SOURCE_DIRS) - } - } - } - else if (file.isFile()) { - lines << describe(file) - } - else { - lines << "absent\t${file.absolutePath}".toString() - } - } - - /** - * The files below the directory named like a command: by name only, so editing one changes nothing, - * or else with their times and sizes - */ - private static void walkNames(File dir, List lines, boolean whole) { - if (Files.isSymbolicLink(dir.toPath())) { - return - } - File[] children = dir.listFiles() - if (children == null) { - return - } - for (File child : children.sort(false) { File it -> it.name }) { - if (child.isDirectory()) { - walkNames(child, lines, whole) - } - else if (COMMAND_FILE_SUFFIXES.any { String suffix -> child.name.endsWith(suffix) }) { - lines << (whole ? describe(child) : "named\t${child.absolutePath}".toString()) - } - } - } - - private static String describe(File file) { - "file\t${file.absolutePath}\t${file.lastModified()}\t${file.length()}".toString() - } - - @EqualsAndHashCode - private static final class Inputs { - - final List buildDirectories - final List sourceDirectories - final List commandDirectories - final List commandFileDirectories - final List sourceFiles - - Inputs(List buildDirectories, List sourceDirectories, List commandDirectories, - List commandFileDirectories, List sourceFiles) { - this.buildDirectories = buildDirectories - this.sourceDirectories = sourceDirectories - this.commandDirectories = commandDirectories - this.commandFileDirectories = commandFileDirectories - this.sourceFiles = sourceFiles - } - } -} diff --git a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectGradle.groovy b/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectGradle.groovy deleted file mode 100644 index 51d06338374..00000000000 --- a/grails-forge/grails-forge-cli/src/main/groovy/org/grails/forge/cli/command/ProjectGradle.groovy +++ /dev/null @@ -1,134 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.grails.forge.cli.command - -import groovy.transform.CompileStatic - -/** - * Runs Gradle for a project through the Gradle wrapper of its build: the wrapper in the project - * directory, or the one in the root directory of the build the project belongs to, the first - * directory above it with a settings file. No wrapper outside the build is ever run, and neither is - * a {@code gradle} found on the path. - * - *

Every run names the project with the Grails Gradle plugin's {@code grailsCommandProject} property, - * so the tasks it runs run in that project only, and not also in the projects below it.

- */ -@CompileStatic -class ProjectGradle { - - private static final boolean WINDOWS = System.getProperty('os.name', '').toLowerCase(Locale.ENGLISH).contains('win') - - private static final List SETTINGS_FILES = ['settings.gradle', 'settings.gradle.kts'] - - /** The project property of the Grails Gradle plugin that runs its command tasks in one project only */ - static final String COMMAND_PROJECT_PROPERTY = 'grailsCommandProject' - - private final File projectDir - private final File wrapper - private final File rootDir - - ProjectGradle(File projectDir) { - this.projectDir = projectDir - this.wrapper = findWrapper(projectDir) - this.rootDir = findSettingsDir(projectDir) ?: projectDir - } - - /** - * @return whether the project's build has a Gradle wrapper to run - */ - boolean isAvailable() { - wrapper != null - } - - /** - * @return the root directory of the build the project belongs to: the first directory, from the - * project's upward, with a settings file, as Gradle finds it, or the project directory when there is none - */ - File getRootDir() { - rootDir - } - - /** - * Runs Gradle in the project quietly, with its output shown. - * - * @param arguments the tasks and arguments to run - * @return the exit code of Gradle - * @throws IOException when the wrapper cannot be started - */ - int run(List arguments) { - start(arguments) { ProcessBuilder builder -> builder.inheritIO() } - } - - /** - * Runs Gradle in the project quietly, with its output hidden. - * - * @param arguments the tasks and arguments to run - * @return the exit code of Gradle - * @throws IOException when the wrapper cannot be started - */ - int runSilently(List arguments) { - start(arguments) { ProcessBuilder builder -> - builder.redirectOutput(ProcessBuilder.Redirect.DISCARD).redirectError(ProcessBuilder.Redirect.DISCARD) - } - } - - private int start(List arguments, Closure redirect) { - if (wrapper == null) { - throw new IOException("The build of ${projectDir} has no Gradle wrapper") - } - List command = [wrapper.path, '-p', projectDir.path, '-q', '--console=plain', - "-P${COMMAND_PROJECT_PROPERTY}=${projectDir.path}".toString()] - command.addAll(arguments) - ProcessBuilder builder = new ProcessBuilder(command).directory(projectDir) - redirect.call(builder) - try { - return builder.start().waitFor() - } - catch (IllegalArgumentException e) { - // ProcessBuilder refuses some arguments of a batch file, such as one with a quote inside it - throw new IOException("Could not run ${wrapper}: ${e.message}", e) - } - catch (InterruptedException e) { - Thread.currentThread().interrupt() - throw new IOException("Gradle was interrupted while running ${arguments}", e) - } - } - - private static File findWrapper(File projectDir) { - String name = WINDOWS ? 'gradlew.bat' : 'gradlew' - File own = new File(projectDir, name) - if (own.isFile()) { - return own - } - // the build's root is the first directory with a settings file, as Gradle finds it; a wrapper - // further up belongs to something else - File settingsDir = findSettingsDir(projectDir) - File rootWrapper = settingsDir == null ? null : new File(settingsDir, name) - rootWrapper?.isFile() ? rootWrapper : null - } - - private static File findSettingsDir(File projectDir) { - for (File dir = projectDir; dir != null; dir = dir.parentFile) { - if (SETTINGS_FILES.any { new File(dir, it).isFile() }) { - return dir - } - } - null - } -} diff --git a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/CommandLineCompleterSpec.groovy b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/CommandLineCompleterSpec.groovy index fd24596875c..2139ee616aa 100644 --- a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/CommandLineCompleterSpec.groovy +++ b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/CommandLineCompleterSpec.groovy @@ -26,7 +26,6 @@ import org.jline.terminal.Terminal import org.jline.terminal.TerminalBuilder import org.springframework.context.ConfigurableApplicationContext import picocli.CommandLine -import picocli.CommandLine.Model.CommandSpec import spock.lang.AutoCleanup import spock.lang.Shared import spock.lang.Specification @@ -73,28 +72,13 @@ class CommandLineCompleterSpec extends Specification { void 'the completer needs a command to complete from'() { when: - new CommandLineCompleter((CommandSpec) null) + new CommandLineCompleter(null) then: NullPointerException e = thrown() e.message == 'spec' } - void 'commands given by a supplier are completed as they are when the word is completed'() { - given: 'the commands of the prompt, which gain one after the completer is made' - CommandSpec offered = CommandSpec.create().addSubcommand('hello', CommandSpec.create()) - CommandLineCompleter completer = new CommandLineCompleter({ -> offered } as java.util.function.Supplier) - LineReader reader = LineReaderBuilder.builder().terminal(terminal).build() - - when: - offered.addSubcommand('help-me', CommandSpec.create()) - List candidates = [] - completer.complete(reader, new DefaultParser().parse('he', 2), candidates) - - then: - candidates*.value() as Set == ['hello', 'help-me'] as Set - } - private List complete(String typed) { LineReader reader = LineReaderBuilder.builder().terminal(terminal).build() List candidates = [] diff --git a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy b/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy deleted file mode 100644 index 9a13cd3d980..00000000000 --- a/grails-forge/grails-forge-cli/src/test/groovy/org/grails/forge/cli/ProjectCommandsSpec.groovy +++ /dev/null @@ -1,1306 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.grails.forge.cli - -import java.nio.file.Files -import java.util.concurrent.LinkedBlockingQueue -import java.util.concurrent.TimeUnit -import java.nio.file.attribute.PosixFilePermission - -import groovy.json.JsonOutput -import groovy.json.JsonSlurper - -import org.jline.reader.Candidate -import org.jline.reader.LineReader -import org.jline.reader.LineReaderBuilder -import org.jline.reader.Parser -import org.jline.reader.impl.DefaultParser -import org.jline.terminal.Terminal -import org.jline.terminal.TerminalBuilder -import org.springframework.context.ConfigurableApplicationContext -import picocli.CommandLine - -import org.grails.build.parsing.CommandLineParser -import org.grails.forge.cli.command.CreateAppCommand -import spock.lang.AutoCleanup -import spock.lang.IgnoreIf -import spock.lang.Shared -import spock.lang.Specification -import spock.lang.Timeout -import spock.util.environment.RestoreSystemProperties - -// the project's build is a shell script standing in for the Gradle wrapper -@IgnoreIf({ os.windows }) -class ProjectCommandsSpec extends Specification { - - private static final Map SECURITY_QUICKSTART = [ - name : 's2-quickstart', - description: 'Creates domain classes and updates config settings for the Spring Security plugin', - arguments : [ - [name: 'package', description: 'The package of the domain classes', required: false, values: []], - [name: 'user-class-name', description: 'The name of the user class', required: false, values: []], - [name: 'role-class-name', description: 'The name of the role class', required: false, values: []] - ], - options : [ - [name: 'groupClassName', description: 'Also generate role group classes', flag: false, values: []], - [name: 'uiOnly', description: 'Configure without generating classes', flag: true, values: []] - ] - ] - - private static final Map CREATE_TOKEN = [ - name : 's2-create-persistent-token', - description: 'Creates a persistent token domain class', - arguments : [[name: 'domain-class-name', description: 'The name of the domain class', required: true, values: []]], - options : [[name: 'format', description: 'The format', flag: false, values: ['json', 'yaml']]] - ] - - private static final Map URL_MAPPINGS_REPORT = [ - name: 'url-mappings-report', description: 'url-mappings-report', arguments: [], options: [] - ] - - private static final Map AUDIT = [name: 'audit', description: 'Audits the application', arguments: [], options: []] - - private static final List MANIFEST_TASK = ['generateCommandManifest', '-PgrailsCommandManifest=build/grails/commands.json'] - - /** Files the commands depend on, in every place the Forge CLI watches without a manifest naming them */ - private static final List WATCHED_FILES = [ - 'build.gradle', 'settings.gradle', 'settings.gradle.kts', 'gradle.properties', 'dependencies.gradle', - 'libs.versions.toml', 'gradle/libs.versions.toml', 'gradle/publishing.gradle', - 'gradle/wrapper/gradle-wrapper.properties', 'buildSrc/build.gradle', - 'buildSrc/src/main/groovy/Conventions.groovy', 'buildSrc/src/main/groovy/com/acme/build/Conventions.groovy', - 'grails-app/commands/example/HelloCommand.groovy', 'grails-app/commands/com/acme/build/DeployCommand.groovy', - 'src/main/resources/META-INF/grails-cli.factories' - ] - - /** What a terminal writes to ask the emulator for the position of the cursor */ - private static final String CURSOR_POSITION_QUESTION = '\u001b[6n' - - /** What the prompt writes when it is ready for a line: it turns on bracketed paste */ - private static final String PROMPT_SHOWN = '\u001b[?2004h' - - private static final Map PLUGIN_CREATE_APP = [ - name: 'create-app', description: 'A plugin command named like a Forge command', arguments: [], options: [] - ] - - @Shared - @AutoCleanup - ConfigurableApplicationContext context = Application.builder().run() - - @Shared - @AutoCleanup - Terminal terminal = TerminalBuilder.builder() - .type(Terminal.TYPE_DUMB) - .streams(new ByteArrayInputStream(new byte[0]), new ByteArrayOutputStream()) - .build() - - File dir = Files.createTempDirectory('forge-project-commands').toFile().canonicalFile - File manifest = new File(dir, 'build/grails/commands.json') - ByteArrayOutputStream output = new ByteArrayOutputStream() - - private String previousUserDir - private PrintStream previousOut - private PrintStream previousErr - - void setup() { - previousUserDir = System.getProperty('user.dir') - System.setProperty('user.dir', dir.path) - previousOut = System.out - previousErr = System.err - PrintStream captured = new PrintStream(output, true) - System.out = captured - System.err = captured - assert new File(dir, 'grails-app/commands').mkdirs() - new File(dir, 'build.gradle').text = "plugins { id 'org.apache.grails.gradle.grails-web' }\n" - writeGradleWrapper() - } - - void cleanup() { - System.out = previousOut - System.err = previousErr - System.setProperty('user.dir', previousUserDir) - dir.deleteDir() - } - - void 'the commands of a project without a manifest are read from its build first'() { - given: - buildWrites(SECURITY_QUICKSTART, URL_MAPPINGS_REPORT) - - when: - CommandLine commandLine = ForgeCli.createCommandLine(context, false) - - then: "the build is run once, quietly, in the project directory" - gradleInvocations() == [prefix() + MANIFEST_TASK] - output.toString().contains('Reading the commands of the project from its build...') - - and: "the project's commands are offered next to the Forge CLI's own" - commandLine.subcommands.keySet().containsAll(['create-app', 's2-quickstart', 'url-mappings-report']) - } - - void 'a manifest whose files are unchanged is read without running the build'() { - given: - writeManifest(SECURITY_QUICKSTART) - - when: - CommandLine commandLine = ForgeCli.createCommandLine(context, false) - - then: - gradleInvocations() == [] - commandLine.subcommands.containsKey('s2-quickstart') - } - - void 'the manifest is written again when #path is #change'() { - given: 'the file there when the Forge CLI last ran the build, unless it is added later' - File file = new File(dir, path) - if (change != 'added') { - file.parentFile.mkdirs() - file.text = 'a=1' - file.setLastModified(System.currentTimeMillis() - 3_600_000) - } - writeManifest(URL_MAPPINGS_REPORT) - long recorded = file.lastModified() - buildWrites(SECURITY_QUICKSTART) - - when: - switch (change) { - case 'added': - file.parentFile.mkdirs() - file.text = 'a=1' - break - case 'changed in size with its time kept': - file.text = 'a=12' - file.setLastModified(recorded) - break - case 'replaced by an older copy': - file.text = 'b=2' - file.setLastModified(recorded - 60_000) - break - case 'deleted': - assert file.delete() - break - } - CommandLine commandLine = ForgeCli.createCommandLine(context, false) - - then: - gradleInvocations() == [prefix() + MANIFEST_TASK] - commandLine.subcommands.containsKey('s2-quickstart') - - and: 'the change is recorded, so the build is not run again for it' - ForgeCli.createCommandLine(context, false) - gradleInvocations().size() == 1 - - where: - [path, change] << [WATCHED_FILES, ['added', 'changed in size with its time kept', 'replaced by an older copy', 'deleted']] - .combinations() - .findAll { String path, String change -> !(path == 'build.gradle' && change in ['added', 'deleted']) } - } - - void 'the manifest is written again when the #removed directory is removed'() { - given: - File file = new File(dir, "${removed}/example/Hello.groovy") - file.parentFile.mkdirs() - file.text = 'class Hello {}' - writeManifest(URL_MAPPINGS_REPORT) - buildWrites(SECURITY_QUICKSTART) - - when: - assert new File(dir, removed).deleteDir() - ForgeCli.createCommandLine(context, false) - - then: - gradleInvocations() == [prefix() + MANIFEST_TASK] - - where: - removed << ['grails-app/commands', 'buildSrc', 'gradle', 'src/main/resources/META-INF'] - } - - void 'a project without the directories the commands depend on does not run the build while nothing changed'() { - given: - assert new File(dir, 'grails-app/commands').deleteDir() - writeManifest(URL_MAPPINGS_REPORT) - - when: - CommandLine commandLine = ForgeCli.createCommandLine(context, false) - - then: - gradleInvocations() == [] - commandLine.subcommands.containsKey('url-mappings-report') - } - - void 'a manifest the build wrote without the Forge CLI is written again, as a file may have been deleted since'() { - given: - writeManifestOfTheBuild(URL_MAPPINGS_REPORT) - buildWrites(SECURITY_QUICKSTART) - - when: - CommandLine commandLine = ForgeCli.createCommandLine(context, false) - - then: - gradleInvocations() == [prefix() + MANIFEST_TASK] - commandLine.subcommands.containsKey('s2-quickstart') - - and: 'the files are recorded, so it is not run again' - ForgeCli.createCommandLine(context, false) - gradleInvocations().size() == 1 - } - - void 'what builds and tools write next to a build script, and the sources the build compiles, do not make the manifest outdated'() { - given: - new File(dir, 'buildSrc/build.gradle').with { parentFile.mkdirs(); text = '' } - writeManifest(URL_MAPPINGS_REPORT) - - when: - ['buildSrc/build/classes/Conventions.class', 'buildSrc/.gradle/file-system.probe', 'buildSrc/.kotlin/sessions/a', - 'buildSrc/bin/main/Conventions.class', 'buildSrc/out/Conventions.class', 'build/libs/app.jar', - '.gradle/file-system.probe', 'src/main/groovy/example/Service.groovy', 'grails-app/controllers/Home.groovy', - 'README.md'].each { String path -> - File written = new File(dir, path) - written.parentFile.mkdirs() - written.text = 'written' - } - ForgeCli.createCommandLine(context, false) - - then: - gradleInvocations() == [] - } - - void 'a source package named like an output directory is watched like any other'() { - given: - File command = new File(dir, "grails-app/commands/com/acme/${name}/DeployCommand.groovy") - command.parentFile.mkdirs() - writeManifest(URL_MAPPINGS_REPORT) - - when: - command.text = 'class DeployCommand {}' - ForgeCli.createCommandLine(context, false) - - then: - gradleInvocations() == [prefix() + MANIFEST_TASK] - - where: - name << ['build', 'bin', 'out'] - } - - void 'a file dated in the future makes the build run once, not on every start'() { - given: - File future = new File(dir, 'gradle.properties') - future.text = 'a=1' - future.setLastModified(System.currentTimeMillis() + 86_400_000) - - when: - ForgeCli.createCommandLine(context, false) - ForgeCli.createCommandLine(context, false) - - then: - gradleInvocations().size() == 1 - } - - void 'a file changed while the build runs makes the build run again next time, with a manifest #naming'() { - given: 'a build during which a command is saved' - if (namesInputs) { - buildWritesWithInputs([buildDirectories: [dir.path], sourceDirectories: [new File(dir, 'grails-app/commands').path]], - URL_MAPPINGS_REPORT) - } - else { - buildWrites(URL_MAPPINGS_REPORT) - } - new File(dir, 'during-build.sh').text = 'printf "class Hello {}" > "$1/grails-app/commands/Hello.groovy"\n' - - when: - ForgeCli.createCommandLine(context, false) - new File(dir, 'during-build.sh').delete() - ForgeCli.createCommandLine(context, false) - ForgeCli.createCommandLine(context, false) - - then: - gradleInvocations().size() == 2 - - where: - namesInputs << [false, true] - naming = namesInputs ? 'naming its inputs' : 'naming no inputs' - } - - void 'a first build whose manifest names its inputs runs once while nothing changes'() { - given: - buildWritesWithInputs([buildDirectories: [dir.path], sourceDirectories: [new File(dir, 'grails-app/commands').path]], - URL_MAPPINGS_REPORT) - - when: - 3.times { ForgeCli.createCommandLine(context, false) } - - then: - gradleInvocations().size() == 1 - } - - void "the inputs of the last manifest are still watched when cleaning the build removed it after a failed build"() { - given: "a manifest naming a plugin project's commands" - File command = new File(dir, 'plugins/audit/grails-app/commands/AuditCommand.groovy') - command.parentFile.mkdirs() - command.text = 'class AuditCommand {}' - buildWritesWithInputs([buildDirectories: [dir.path], sourceDirectories: [command.parentFile.path]], - URL_MAPPINGS_REPORT, AUDIT) - ForgeCli.createCommandLine(context, false) - - and: 'a change to the command that does not compile' - command.text = 'class AuditCommand { broken' - new File(dir, 'gradle-exit-code').text = '1' - ForgeCli.createCommandLine(context, false) - forgetInvocations() - - when: 'the build is cleaned, and the Forge CLI starts while the command is still broken' - new File(dir, 'build').deleteDir() - ForgeCli.createCommandLine(context, false) - - then: 'the failed build is not run again for nothing' - gradleInvocations() == [] - - when: 'the command is fixed' - command.text = 'class AuditCommand { /* fixed */ }' - new File(dir, 'gradle-exit-code').delete() - CommandLine commandLine = ForgeCli.createCommandLine(context, false) - - then: - gradleInvocations() == [prefix() + MANIFEST_TASK] - commandLine.subcommands.containsKey('audit') - } - - void "a command added to a plugin project's main sources is offered and runs, while other sources change nothing"() { - given: "a manifest naming the main sources of a plugin project next to the application" - File sources = new File(dir, 'plugins/audit/src/main/groovy') - File service = new File(sources, 'com/acme/AuditService.groovy') - service.parentFile.mkdirs() - service.text = 'class AuditService {}' - Map inputs = [buildDirectories: [dir.path], sourceDirectories: [], commandFileDirectories: [sources.path]] - buildWritesWithInputs(inputs, URL_MAPPINGS_REPORT) - ForgeCli.createCommandLine(context, false) - forgetInvocations() - - when: 'a command class is added' - new File(sources, 'com/acme/AuditCommand.groovy').text = 'class AuditCommand {}' - buildWritesWithInputs(inputs, URL_MAPPINGS_REPORT, AUDIT) - int exitCode = ForgeCli.execute(context, 'audit') - - then: 'the build runs once, and the new command runs instead of being rejected' - exitCode == 0 - gradleInvocations()*.get(5) == ['generateCommandManifest', 'runCommand'] - commandArguments() == ['audit'] - - when: 'another source is changed and one is added' - forgetInvocations() - service.text = 'class AuditService { void audit() {} }' - new File(sources, 'com/acme/Helper.groovy').text = 'class Helper {}' - ForgeCli.createCommandLine(context, false) - - then: - gradleInvocations() == [] - - when: 'the command is removed' - new File(sources, 'com/acme/AuditCommand.groovy').delete() - ForgeCli.createCommandLine(context, false) - - then: - gradleInvocations() == [prefix() + MANIFEST_TASK] - } - - void "a new command in a plugin project's main sources that did not compile runs the build again once it is fixed"() { - given: "a manifest naming the main sources of a plugin project next to the application" - File sources = new File(dir, 'plugins/audit/src/main/groovy') - File command = new File(sources, 'com/acme/AuditCommand.groovy') - command.parentFile.mkdirs() - Map inputs = [buildDirectories: [dir.path], sourceDirectories: [], commandFileDirectories: [sources.path]] - buildWritesWithInputs(inputs, URL_MAPPINGS_REPORT) - ForgeCli.createCommandLine(context, false) - forgetInvocations() - - when: 'a command is added that does not compile' - command.text = 'class AuditCommand { broken' - new File(dir, 'gradle-exit-code').text = '1' - int rejected = ForgeCli.execute(context, 'audit') - ForgeCli.createCommandLine(context, false) - - then: 'the failed build runs once, not again while nothing changes' - rejected != 0 - gradleInvocations() == [prefix() + MANIFEST_TASK] - - when: 'the command is fixed' - forgetInvocations() - command.text = 'class AuditCommand { /* fixed */ }' - new File(dir, 'gradle-exit-code').delete() - buildWritesWithInputs(inputs, URL_MAPPINGS_REPORT, AUDIT) - int exitCode = ForgeCli.execute(context, 'audit') - - then: 'the build runs again, and the command runs' - exitCode == 0 - gradleInvocations()*.get(5) == ['generateCommandManifest', 'runCommand'] - - when: 'the command is edited once the build has succeeded' - forgetInvocations() - command.text = 'class AuditCommand { /* edited */ }' - ForgeCli.createCommandLine(context, false) - - then: 'it is watched by name again' - gradleInvocations() == [] - } - - void 'the directories the manifest names are watched, such as those of the other projects of the build'() { - given: 'a manifest naming a plugin project next to the application' - File plugin = new File(dir, 'plugins/audit') - new File(plugin, 'grails-app/commands').mkdirs() - new File(plugin, 'build.gradle').text = '' - buildWritesWithInputs([buildDirectories: [dir.path, plugin.path], - sourceDirectories: [new File(dir, 'grails-app/commands').path, new File(plugin, 'grails-app/commands').path]], - URL_MAPPINGS_REPORT) - ForgeCli.createCommandLine(context, false) - forgetInvocations() - - when: 'the plugin gains a command' - new File(plugin, 'grails-app/commands/AuditCommand.groovy').text = 'class AuditCommand {}' - ForgeCli.createCommandLine(context, false) - - then: - gradleInvocations() == [prefix() + MANIFEST_TASK] - - when: "the plugin's build changes" - new File(plugin, 'build.gradle').text = 'plugins {}' - ForgeCli.createCommandLine(context, false) - - then: - gradleInvocations().size() == 2 - } - - void 'the source of a command the manifest names is watched, wherever it is'() { - given: - File source = new File(dir, 'plugins/audit/src/main/groovy/com/acme/AuditCommand.groovy') - source.parentFile.mkdirs() - source.text = 'class AuditCommand {}' - buildWritesWithInputs([buildDirectories: [dir.path], sourceDirectories: [], sourceFiles: [source.path]], URL_MAPPINGS_REPORT) - ForgeCli.createCommandLine(context, false) - forgetInvocations() - - when: 'the command gains an option' - source.text = 'class AuditCommand { /* @CommandOption */ }' - ForgeCli.createCommandLine(context, false) - - then: - gradleInvocations() == [prefix() + MANIFEST_TASK] - } - - void "a command a plugin project of the build adds to its cli source set is offered and runs"() { - given: "a manifest naming the cli source set of a plugin project next to the application" - File plugin = new File(dir, 'plugins/audit') - File groovy = new File(plugin, 'src/cli/groovy') - File resources = new File(plugin, 'src/cli/resources') - new File(resources, 'META-INF').mkdirs() - groovy.mkdirs() - Map inputs = [buildDirectories: [dir.path], sourceDirectories: [], commandDirectories: [groovy.path, resources.path]] - buildWritesWithInputs(inputs, URL_MAPPINGS_REPORT) - ForgeCli.createCommandLine(context, false) - forgetInvocations() - - when: 'a command class is added, in a package named like an output directory' - File command = new File(groovy, 'com/acme/build/AuditCommand.groovy') - command.parentFile.mkdirs() - command.text = 'class AuditCommand {}' - buildWritesWithInputs(inputs, URL_MAPPINGS_REPORT, AUDIT) - int exitCode = ForgeCli.execute(context, 'audit') - ForgeCli.createCommandLine(context, false) - - then: 'the build runs once, and the new command runs instead of being rejected' - exitCode == 0 - gradleInvocations()*.get(5) == ['generateCommandManifest', 'runCommand'] - commandArguments() == ['audit'] - - when: 'a command is registered by hand in the factories file' - forgetInvocations() - new File(resources, 'META-INF/grails-cli.factories').text = - 'org.apache.grails.core.cli.ApplicationCommand=com.acme.ExportCommand\n' - ForgeCli.createCommandLine(context, false) - ForgeCli.createCommandLine(context, false) - - then: - gradleInvocations() == [prefix() + MANIFEST_TASK] - } - - void 'what tools write in an included build does not make the manifest outdated, wherever the build is'() { - given: 'an included build, below a directory named src, with a subproject that has no build file of its own' - File included = new File(dir, 'src/shared') - new File(included, 'lib/src/main/groovy/com/acme/build').mkdirs() - new File(included, 'lib/src/main/webapp').mkdirs() - new File(included, 'settings.gradle.kts').text = 'include("lib")' - buildWritesWithInputs([buildDirectories: [dir.path], sourceDirectories: [included.path]], URL_MAPPINGS_REPORT) - ForgeCli.createCommandLine(context, false) - forgetInvocations() - - when: - ['lib/build/classes/A.class', 'lib/.gradle/probe', '.gradle/probe', '.kotlin/sessions/a', 'lib/bin/A.class', - 'node_modules/.cache/x', 'lib/src/main/webapp/node_modules/.cache/x', '.DS_Store', 'lib/.settings/prefs'].each { String path -> - File written = new File(included, path) - written.parentFile.mkdirs() - written.text = 'written' - } - ForgeCli.createCommandLine(context, false) - - then: - gradleInvocations() == [] - - when: 'a source of the included build changes, in a package named build' - new File(included, 'lib/src/main/groovy/com/acme/build/Plugin.groovy').text = 'class Plugin {}' - ForgeCli.createCommandLine(context, false) - - then: - gradleInvocations().size() == 1 - } - - void 'a manifest naming #what as an input is read with the inputs of the project and its build instead'() { - given: - buildWritesWithInputs([buildDirectories: paths, sourceDirectories: []], URL_MAPPINGS_REPORT) - - when: - CommandLine first = ForgeCli.createCommandLine(context, false) - ForgeCli.createCommandLine(context, false) - - then: - first.subcommands.containsKey('url-mappings-report') - gradleInvocations().size() == 1 - - where: - what | paths - 'a relative path' | ['relative/dir'] - 'the root' | ['/'] - 'an empty path' | [''] - 'a path with NUL' | ['/tmp/a\u0000b'] - } - - void 'a link that loops is recorded, not followed'() { - given: - File source = new File(dir, 'buildSrc/src/main/groovy') - source.mkdirs() - Files.createSymbolicLink(new File(source, 'loop').toPath(), new File(dir, 'buildSrc').toPath()) - - when: - ForgeCli.createCommandLine(context, false) - CommandLine commandLine = ForgeCli.createCommandLine(context, false) - - then: - gradleInvocations().size() == 1 - commandLine.subcommands.containsKey('create-app') - } - - void 'a directory without a Grails project has no project commands and runs no build'() { - given: - new File(dir, 'grails-app').deleteDir() - buildWrites(SECURITY_QUICKSTART) - - when: - CommandLine commandLine = ForgeCli.createCommandLine(context, false) - - then: - !commandLine.subcommands.containsKey('s2-quickstart') - gradleInvocations() == [] - } - - void "the Forge CLI's own commands run without reading the project's commands"() { - given: - buildWrites(SECURITY_QUICKSTART) - - expect: - ForgeCli.execute(context, 'create-app', '--help') == 0 - gradleInvocations() == [] - } - - void "naming one of the Forge CLI's code generation commands never runs the build, even where it does not apply"() { - given: 'a project without the configuration the code generation commands need' - buildWrites(SECURITY_QUICKSTART) - - when: - ForgeCli.execute(context, 'create-controller', 'Book') - - then: - gradleInvocations() == [] - } - - void 'no command follows --, so it runs no build'() { - given: - buildWrites(URL_MAPPINGS_REPORT) - - when: - ForgeCli.execute(context, '--', 'url-mappings-report') - - then: - gradleInvocations() == [] - } - - void "a project command named like one of the Forge CLI's commands does not replace it"() { - given: - writeManifest(PLUGIN_CREATE_APP, URL_MAPPINGS_REPORT) - - when: - CommandLine commandLine = ForgeCli.createCommandLine(context, false) - - then: - commandLine.subcommands['create-app'].commandSpec.userObject() instanceof CreateAppCommand - commandLine.subcommands.containsKey('url-mappings-report') - } - - void '--help lists the commands the build already wrote, even when they are outdated, without running the build'() { - given: - writeManifest(SECURITY_QUICKSTART) - new File(dir, 'build.gradle') << '// changed\n' - buildWrites(URL_MAPPINGS_REPORT) - - when: - int exitCode = ForgeCli.execute(context, '--help') - - then: - exitCode == 0 - output.toString().contains('s2-quickstart') - !output.toString().contains('url-mappings-report') - gradleInvocations() == [] - } - - void '--help without a manifest lists only the Forge CLI commands and runs no build'() { - given: - buildWrites(SECURITY_QUICKSTART) - - when: - ForgeCli.execute(context, '--help') - - then: - output.toString().contains('create-app') - !output.toString().contains('s2-quickstart') - gradleInvocations() == [] - } - - void 'a project command runs through runCommand with its arguments, then its options'() { - given: - writeManifest(SECURITY_QUICKSTART) - - when: - int exitCode = ForgeCli.execute(context, 's2-quickstart', '--uiOnly', 'com.example', 'User', 'Role', - '--groupClassName=RoleGroup') - - then: - exitCode == 0 - gradleInvocations().size() == 1 - gradleInvocations().first().take(6) == prefix() + 'runCommand' - commandArguments() == ['s2-quickstart', 'com.example', 'User', 'Role', '--groupClassName=RoleGroup', '--uiOnly'] - - and: 'the file of the arguments is gone once the command has run' - !new File(argumentsFile()).exists() - } - - void 'an option value given as a separate argument is passed as one option'() { - given: - writeManifest(SECURITY_QUICKSTART) - - when: - ForgeCli.execute(context, 's2-quickstart', 'com.example', 'User', 'Role', '--groupClassName', 'RoleGroup') - - then: - commandArguments() == ['s2-quickstart', 'com.example', 'User', 'Role', '--groupClassName=RoleGroup'] - } - - void 'an option value with equals signs in it is passed whole, as #given'() { - given: - writeManifest(SECURITY_QUICKSTART) - - when: - ForgeCli.execute(context, (['s2-quickstart', 'com.example', 'User', 'Role'] + given) as String[]) - - then: - commandArguments() == ['s2-quickstart', 'com.example', 'User', 'Role', '--groupClassName=a=b=c'] - - and: 'the parser runCommand reads them with gives the command the whole value' - with(new CommandLineParser().parse(commandArguments() as String[])) { - commandName == 's2-quickstart' - remainingArgs == ['com.example', 'User', 'Role'] - optionValue('groupClassName') == 'a=b=c' - } - - where: - given << [['--groupClassName', 'a=b=c'], ['--groupClassName=a=b=c']] - } - - void 'a command that describes no options is given the words in the order they were typed'() { - given: - writeManifest(declared) - - when: 'an undeclared option whose value is a word of its own' - ForgeCli.execute(context, 'url-mappings-report', '--format', 'json', 'first') - - then: 'as runCommand gives them, so the command reads json as the value of --format' - commandArguments() == ['url-mappings-report', '--format', 'json', 'first'] - - where: - declared << [URL_MAPPINGS_REPORT, URL_MAPPINGS_REPORT + [arguments: [[name: 'first', required: false]]]] - } - - void 'arguments are passed on unchanged, whatever quotes and spaces they hold'() { - given: - writeManifest(URL_MAPPINGS_REPORT) - List arguments = ['two words', 'say "hi"', 'it\'s "x" y', '', '%PATH% & | <>'] - - when: - ForgeCli.execute(context, (['url-mappings-report'] + arguments) as String[]) - - then: - commandArguments() == ['url-mappings-report'] + arguments - } - - @RestoreSystemProperties - void 'with grails.forge.runBuild=false the build runs only for a command the user names'() { - given: 'a project whose build has not described its commands' - System.setProperty('grails.forge.runBuild', 'false') - buildWrites(URL_MAPPINGS_REPORT) - - when: 'the prompt starts, and a word that is no command of the Forge CLI is named' - ForgeCli.createCommandLine(context, true) - int unknown = ForgeCli.execute(context, ['url-mappings-report'] as String[]) - - then: 'the build does not run, so the Forge CLI knows no such command' - gradleInvocations().empty - unknown != 0 - - when: 'the user runs the build that describes the commands, which then are out of date' - writeManifestOfTheBuild(URL_MAPPINGS_REPORT) - ForgeCli.execute(context, ['url-mappings-report'] as String[]) - - then: 'the command of that manifest runs, and the build is not run to describe them again' - gradleInvocations().size() == 1 - gradleInvocations().first().contains('runCommand') - } - - void 'the file that passes the arguments, which can hold a password, is readable by its owner only, and removed after the command'() { - given: - writeManifest(URL_MAPPINGS_REPORT) - - when: - ForgeCli.execute(context, ['url-mappings-report', 'secret'] as String[]) - - then: - gradleInvocations().last().contains('argsFileMode:-rw-------') - !new File(argumentsFile()).exists() - } - - void '-D sets a system property of the command, passed among its arguments as runCommand reads it, not on the command line of Gradle'() { - given: - writeManifest(SECURITY_QUICKSTART, URL_MAPPINGS_REPORT) - - when: - ForgeCli.execute(context, typed as String[]) - - then: - gradleInvocations().first().take(7) == prefix() + ['runCommand', "-PargsFile=${argumentsFile()}".toString()] - gradleInvocations().first().count { it.startsWith('-D') } == 0 - commandArguments() == passed - - and: 'the parser runCommand uses reads it as a system property' - new CommandLineParser().parse(commandArguments() as String[]).systemProperties == ['grails.env': 'test', query: 'a=b'] - - where: - typed || passed - ['s2-quickstart', '-Dgrails.env=test', 'com.example', '-D', 'query=a=b'] || ['s2-quickstart', 'com.example', '-Dgrails.env=test', '-Dquery=a=b'] - ['url-mappings-report', '-Dgrails.env=test', 'first', '-Dquery=a=b'] || ['url-mappings-report', '-Dgrails.env=test', 'first', '-Dquery=a=b'] - ['url-mappings-report', 'first', '-D', 'grails.env=test', '-D', 'query=a=b'] || ['url-mappings-report', 'first', '-Dgrails.env=test', '-Dquery=a=b'] - } - - void "a project command after the Forge CLI's own options runs the build that finds it"() { - given: - buildWrites(URL_MAPPINGS_REPORT) - - when: - int exitCode = ForgeCli.execute(context, '-x', 'url-mappings-report') - - then: - exitCode == 0 - gradleInvocations()*.get(5) == ['generateCommandManifest', 'runCommand'] - } - - void 'the exit code of the command is the exit code of the build'() { - given: - writeManifest(URL_MAPPINGS_REPORT) - new File(dir, 'gradle-exit-code').text = '3' - - expect: - ForgeCli.execute(context, 'url-mappings-report') == 3 - } - - void 'a missing required argument or an undeclared option is reported without running the build'() { - given: - writeManifest(CREATE_TOKEN, SECURITY_QUICKSTART) - - expect: - ForgeCli.execute(context, arguments as String[]) != 0 - gradleInvocations() == [] - - where: - arguments << [['s2-create-persistent-token'], ['s2-quickstart', '--unknown']] - } - - void 'the help of a project command shows what it declares'() { - given: - writeManifest(SECURITY_QUICKSTART) - - when: - ForgeCli.execute(context, 's2-quickstart', '--help') - - then: 'the usage, as picocli wraps it, read as one line' - String help = output.toString().split(/\s+/).join(' ') - help.contains('Creates domain classes and updates config settings for the Spring Security plugin') - help.contains('[package] [user-class-name] [role-class-name]') - help.contains('The package of the domain classes') - help.contains('--groupClassName=GROUPCLASSNAME') - help.contains('--uiOnly') - gradleInvocations() == [] - } - - void 'a build that cannot write the manifest, such as one of an earlier Grails, is reported once, and the Forge CLI still runs'() { - given: - new File(dir, 'gradle-exit-code').text = '1' - - when: - CommandLine commandLine = ForgeCli.createCommandLine(context, false) - - then: - output.toString().contains('Could not read the commands of the project: its build failed with exit code 1. ' + - 'Its commands are not offered, and the build is not run for them again until a build file changes. ' + - 'A project of Grails 9 or later lists them with its generateCommandManifest Gradle task; run it to see what went wrong.') - commandLine.subcommands.containsKey('create-app') - !commandLine.subcommands.containsKey('s2-quickstart') - - when: 'the Forge CLI starts again in the same project' - ForgeCli.createCommandLine(context, false) - - then: 'the build is not run again' - gradleInvocations().size() == 1 - - when: 'the build directory is cleaned' - new File(dir, 'build').deleteDir() - ForgeCli.createCommandLine(context, false) - - then: 'the failed build is still remembered' - gradleInvocations().size() == 1 - - when: 'a build file changes, and the build works again' - new File(dir, 'gradle-exit-code').delete() - buildWrites(SECURITY_QUICKSTART) - new File(dir, 'build.gradle') << '// changed\n' - commandLine = ForgeCli.createCommandLine(context, false) - - then: - gradleInvocations().size() == 2 - commandLine.subcommands.containsKey('s2-quickstart') - !new File(dir, '.gradle/grails/commands.failed').exists() - } - - void 'a failed build keeps the commands of the last manifest, and is not run again for them'() { - given: 'an outdated manifest, and a build that fails, as while the application does not compile' - writeManifest(URL_MAPPINGS_REPORT) - new File(dir, 'build.gradle') << '// changed\n' - new File(dir, 'gradle-exit-code').text = '1' - - when: - CommandLine commandLine = ForgeCli.createCommandLine(context, false) - - then: - commandLine.subcommands.containsKey('url-mappings-report') - output.toString().contains('The commands it listed before are offered') - - when: - new File(dir, 'gradle-exit-code').delete() - int exitCode = ForgeCli.execute(context, 'url-mappings-report') - - then: 'the command runs without the build being tried again' - exitCode == 0 - gradleInvocations()*.get(5) == ['generateCommandManifest', 'runCommand'] - } - - void 'a wrapper that cannot be started is reported, and the Forge CLI still runs'() { - given: - writeManifest(URL_MAPPINGS_REPORT) - new File(dir, 'build.gradle') << '// changed\n' - Files.setPosixFilePermissions(new File(dir, 'gradlew').toPath(), [PosixFilePermission.OWNER_READ] as Set) - - when: - CommandLine commandLine = ForgeCli.createCommandLine(context, false) - - then: - output.toString().contains('Could not read the commands of the project: ') - commandLine.subcommands.containsKey('create-app') - commandLine.subcommands.containsKey('url-mappings-report') - } - - void 'a build whose manifest task is up to date is not run again'() { - given: 'a build file changed in a way that leaves the classpath, and so the manifest, as it was' - writeManifest(URL_MAPPINGS_REPORT) - new File(dir, 'build.gradle') << '// changed\n' - - when: - ForgeCli.createCommandLine(context, false) - ForgeCli.createCommandLine(context, false) - - then: - gradleInvocations().size() == 1 - } - - void 'a manifest #problem is reported, and the Forge CLI still runs'() { - given: - buildWritesContent(content) - - when: - CommandLine commandLine = ForgeCli.createCommandLine(context, false) - - then: - output.toString().contains("Could not read the commands of the project from ${manifest}, so they are not offered: ${message}") - commandLine.subcommands.containsKey('create-app') - - and: 'the build that wrote it is not run again for nothing' - ForgeCli.createCommandLine(context, false) - gradleInvocations().size() == 1 - - where: - problem | content || message - 'of a newer format' | [version: 2, commands: [URL_MAPPINGS_REPORT]] || 'it was written in format 2, by a newer Grails' - 'without a format version' | [commands: [URL_MAPPINGS_REPORT]] || 'it has no format version' - 'whose commands are not a list' | [version: 1, commands: 'oops'] || 'it has no list of commands' - 'that is not an object' | [URL_MAPPINGS_REPORT] || 'it is not a JSON object' - } - - void 'a command that cannot be offered, #problem, costs only that command'() { - given: - buildWritesContent([version: 1, commands: [command, URL_MAPPINGS_REPORT]]) - - when: - CommandLine commandLine = ForgeCli.createCommandLine(context, false) - - then: - output.toString().contains("The command ${name} of the project is not offered: ") - commandLine.subcommands.containsKey('url-mappings-report') - !commandLine.subcommands.containsKey('broken') - - where: - problem | command || name - 'as it has no name' | [description: 'nameless'] || 'without a name' - 'as an option takes --help' | [name: 'broken', options: [[name: 'help', flag: true]]] || 'broken' - 'as it declares an option twice' | [name: 'broken', options: [[name: 'format', flag: false], [name: 'format', flag: true]]] || 'broken' - } - - void 'a record that cannot be written is reported, and the Forge CLI still runs'() { - given: 'a file where the directory of the record would be, as a directory the user cannot write' - new File(dir, '.gradle').text = 'not a directory' - buildWrites(URL_MAPPINGS_REPORT) - - when: - CommandLine commandLine = ForgeCli.createCommandLine(context, false) - - then: - output.toString().contains('Could not record what the commands of the project depend on in ') - output.toString().contains('The build is run for them again next time.') - commandLine.subcommands.containsKey('url-mappings-report') - } - - void 'a build directory the Forge CLI cannot write is reported, and the Forge CLI still runs'() { - given: - buildWrites(URL_MAPPINGS_REPORT) - new File(dir, 'build').text = 'not a directory' - - when: - CommandLine commandLine = ForgeCli.createCommandLine(context, false) - - then: - output.toString().contains('Could not read the commands of the project: ') - commandLine.subcommands.containsKey('create-app') - } - - void 'a manifest that is not JSON is reported'() { - given: - manifest.parentFile.mkdirs() - manifest.text = 'not json' - - when: - CommandLine commandLine = ForgeCli.createCommandLine(context, false) - - then: - output.toString().contains("Could not read the commands of the project from ${manifest}, so they are not offered") - commandLine.subcommands.containsKey('create-app') - } - - @Timeout(120) - void 'after a line that runs the build, the prompt completes the commands the build found'() { - given: 'the prompt starts with the commands of the last manifest' - writeManifest(URL_MAPPINGS_REPORT) - - when: 'a plugin is added while it waits, a line names a command the Forge CLI does not have, which runs the build, the next completes s2-q, and the last runs no build' - prompt({ - buildWrites(URL_MAPPINGS_REPORT, SECURITY_QUICKSTART) - new File(dir, 'build.gradle') << '// a plugin added\n' - }, 's2-quickstart --help\n', 's2-q\t\n', '--help\n') - - then: 'the build ran once, and the completed line ran the command it found' - gradleInvocations()*.get(5) == ['generateCommandManifest', 'runCommand'] - commandArguments() == ['s2-quickstart'] - } - - void 'the prompt completes project commands, their options and the values they declare'() { - given: - writeManifest(SECURITY_QUICKSTART, CREATE_TOKEN) - CommandLine commandLine = ForgeCli.createCommandLine(context, true) - - expect: - complete(commandLine, 's2-').containsAll(['s2-quickstart', 's2-create-persistent-token']) - complete(commandLine, 's2-quickstart --').containsAll(['--groupClassName', '--uiOnly', '--help']) - complete(commandLine, 's2-create-persistent-token --format ').containsAll(['json', 'yaml']) - complete(commandLine, 's2-create-persistent-token --format=').containsAll(['--format=json', '--format=yaml']) - complete(commandLine, 's2-create-persistent-token --format=j').contains('--format=json') - } - - void 'a project of a multi-project build runs the wrapper at the root of the build'() { - given: 'the project in a subdirectory of a build whose wrapper and settings are at its root' - File project = moveProjectInto('app') - new File(dir, 'settings.gradle').text = "include 'app'\n" - buildWrites(URL_MAPPINGS_REPORT) - - when: - int exitCode = ForgeCli.execute(context, 'url-mappings-report') - - then: 'the tasks run in that project only' - exitCode == 0 - gradleInvocations()*.take(6) == [prefix(project) + 'generateCommandManifest', prefix(project) + 'runCommand'] - new File(project, 'build/grails/commands.json').isFile() - commandArguments() == ['url-mappings-report'] - - when: "a build file of the root changes" - new File(dir, 'gradle.properties').text = 'a=1' - ForgeCli.createCommandLine(context, false) - - then: - gradleInvocations().size() == 3 - } - - void 'a wrapper above the root of the build is never run'() { - given: 'a project with a settings file of its own but no wrapper, inside a directory that has one' - File project = moveProjectInto('app') - new File(project, 'settings.gradle').text = '' - writeRawManifest(new File(project, 'build/grails/commands.json'), URL_MAPPINGS_REPORT) - - when: - CommandLine commandLine = ForgeCli.createCommandLine(context, false) - - then: - !commandLine.subcommands.containsKey('url-mappings-report') - output.toString().contains('The build of the project has no Gradle wrapper, so its commands are not offered.') - gradleInvocations() == [] - } - - void 'a wrapper above a project without a settings file is never run'() { - given: - File project = moveProjectInto('app') - buildWrites(URL_MAPPINGS_REPORT) - - when: - CommandLine commandLine = ForgeCli.createCommandLine(context, false) - - then: - !commandLine.subcommands.containsKey('url-mappings-report') - gradleInvocations() == [] - !new File(project, 'build/grails/commands.json').exists() - } - - /** - * Moves the project into a subdirectory of the directory that keeps the Gradle wrapper, and makes - * it the current directory. - */ - private File moveProjectInto(String name) { - File project = new File(dir, name) - assert project.mkdirs() - assert new File(dir, 'grails-app').renameTo(new File(project, 'grails-app')) - assert new File(dir, 'build.gradle').renameTo(new File(project, 'build.gradle')) - System.setProperty('user.dir', project.path) - project - } - - /** The arguments the Forge CLI runs the wrapper with before the tasks, for the project in the directory */ - private static List prefix(File project) { - ['-p', project.path, '-q', '--console=plain', "-PgrailsCommandProject=${project.path}".toString()] - } - - private List prefix() { - prefix(dir) - } - - /** Has the stand-in build write a manifest of the commands, naming no directories it depends on */ - private void buildWrites(Map... commands) { - buildWritesContent([version: 1, commands: commands.toList()]) - } - - /** Has the stand-in build write a manifest of the commands, naming the directories it depends on */ - private void buildWritesWithInputs(Map inputs, Map... commands) { - buildWritesContent([version: 1, commands: commands.toList(), inputs: inputs]) - } - - private void buildWritesContent(Object content) { - new File(dir, 'manifest-source.json').text = JsonOutput.toJson(content) - } - - /** - * Leaves the manifest as an earlier run of the Forge CLI left it: written by the build, with the files - * it depends on recorded. What that run did is forgotten. - */ - private void writeManifest(Map... commands) { - buildWrites(commands) - ForgeCli.createCommandLine(context, false) - assert manifest.isFile() - forgetInvocations() - } - - /** Writes the manifest as the project's own build leaves it, without the Forge CLI's record */ - private void writeManifestOfTheBuild(Map... commands) { - writeRawManifest(manifest, commands) - } - - private static void writeRawManifest(File file, Map... commands) { - file.parentFile.mkdirs() - file.text = JsonOutput.toJson([version: 1, commands: commands.toList()]) - } - - private void forgetInvocations() { - new File(dir, 'gradle-invocations.txt').delete() - output.reset() - } - - private List> gradleInvocations(File wrapperDir = dir) { - File log = new File(wrapperDir, 'gradle-invocations.txt') - if (!log.isFile()) { - return [] - } - List> invocations = [] - List current = [] - for (String line : log.readLines()) { - if (line == '--') { - invocations << current - current = [] - } else { - current << line - } - } - invocations - } - - /** - * The arguments the last command ran with, from the file runCommand was given, as the stand-in - * wrapper logged it. - */ - private List commandArguments() { - String logged = gradleInvocations().last().find { String argument -> argument.startsWith('argsFile:') } - (List) new JsonSlurper().parseText(logged.substring('argsFile:'.length())) - } - - private String argumentsFile() { - gradleInvocations().last().find { String argument -> argument.startsWith('-PargsFile=') }.substring('-PargsFile='.length()) - } - - private void writeGradleWrapper(File wrapperDir = dir) { - File wrapper = new File(wrapperDir, 'gradlew') - wrapper.text = '''\ - #!/bin/sh - dir=$(cd "$(dirname "$0")" && pwd) - project=$dir; previous='' - for argument in "$@"; do [ "$previous" = '-p' ] && project=$argument; previous=$argument; done - { for argument in "$@"; do - printf '%s\\n' "$argument" - case "$argument" in -PargsFile=*) - printf 'argsFile:%s\\n' "$(cat "${argument#-PargsFile=}")" - printf 'argsFileMode:%s\\n' "$(ls -l "${argument#-PargsFile=}" | cut -c1-10)";; esac - done; echo '--'; } >> "$dir/gradle-invocations.txt" - case " $* " in - *" generateCommandManifest "*) - if [ -f "$dir/during-build.sh" ]; then sh "$dir/during-build.sh" "$project"; fi - if [ -f "$dir/manifest-source.json" ]; then - mkdir -p "$project/build/grails" && cp "$dir/manifest-source.json" "$project/build/grails/commands.json" || exit 1 - fi;; - esac - if [ -f "$dir/gradle-exit-code" ]; then exit "$(cat "$dir/gradle-exit-code")"; fi - exit 0 - '''.stripIndent() - Files.setPosixFilePermissions(wrapper.toPath(), [PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE, - PosixFilePermission.OWNER_EXECUTE] as Set) - } - - /** - * Runs the interactive prompt on a terminal that a user types the given lines on, each once the prompt - * shows, and then {@code exit}. - * - * @param whileWaiting runs once the prompt first shows, before a line is typed - */ - private void prompt(Closure whileWaiting, String... lines) { - Deque toType = new ArrayDeque(lines.toList() + ['exit\n']) - LinkedBlockingQueue typed = new LinkedBlockingQueue() - boolean[] waited = [false] as boolean[] - // what the terminal writes, read as a user and their terminal emulator would - OutputStream screen = new OutputStream() { - private final StringBuilder recent = new StringBuilder() - - @Override - synchronized void write(int b) { - recent.append((char) (b & 0xff)) - if (endsWith(CURSOR_POSITION_QUESTION)) { - // the emulator answers where the cursor is - type('\u001b[1;1R') - } - else if (endsWith(PROMPT_SHOWN) && !toType.empty) { - if (!waited[0]) { - waited[0] = true - whileWaiting.call() - } - type(toType.poll()) - } - } - - private boolean endsWith(String text) { - recent.length() >= text.length() && recent.substring(recent.length() - text.length()) == text - } - - private void type(String text) { - text.getBytes('UTF-8').each { byte b -> typed.put(b & 0xff) } - } - } - InputStream keyboard = new InputStream() { - @Override - int read() { - Integer next = typed.poll(30, TimeUnit.SECONDS) - next == null ? -1 : next - } - } - // a terminal that completes on tab, as a dumb one does not - Terminal terminal = TerminalBuilder.builder().system(false).type('xterm').streams(keyboard, screen).build() - try { - ForgeCli.interactive(context, terminal) - } - finally { - terminal.close() - } - } - - private List complete(CommandLine commandLine, String typed) { - LineReader reader = LineReaderBuilder.builder().terminal(terminal).build() - List candidates = [] - new CommandLineCompleter(commandLine.commandSpec) - .complete(reader, new DefaultParser().parse(typed, typed.length(), Parser.ParseContext.COMPLETE), candidates) - candidates*.value() - } -} diff --git a/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTask.groovy b/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTask.groovy deleted file mode 100644 index c7ec95fc42d..00000000000 --- a/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTask.groovy +++ /dev/null @@ -1,150 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.grails.gradle.plugin.commands - -import groovy.json.JsonOutput -import groovy.json.JsonSlurper -import groovy.transform.CompileStatic - -import org.gradle.api.file.RegularFileProperty -import org.gradle.api.provider.ListProperty -import org.gradle.api.tasks.Input -import org.gradle.api.tasks.JavaExec -import org.gradle.api.tasks.OutputFile -import org.gradle.api.tasks.TaskAction -import org.gradle.work.DisableCachingByDefault - -import javax.inject.Inject - -/** - * Writes the commands the application can run with {@code runCommand}, with their descriptions, - * arguments and options, to a JSON file. Command line tools such as the Grails Forge CLI read the file - * to offer those commands and complete them without loading the application's classpath themselves. - * - *

Runs on the classpath {@code runCommand} uses, so the file lists exactly the commands - * {@code runCommand} can run. The task is up to date while that classpath is unchanged.

- * - *

The file also names, under {@code inputs}, the directories whose files decide the commands without - * being on the classpath yet: those whose build files decide the build, those that hold the sources - * of commands or of the build's logic, those of the {@code cli} source sets, where every source can be a - * command, those of the main sources, where a file named like a command can add one, and the source files - * of the listed commands found in the projects of the build. A tool reading the file runs the task again when one of their files changes.

- * - * @since 9.0 - */ -@CompileStatic -@DisableCachingByDefault(because = 'Reading the command metadata is cheaper than restoring it from the build cache') -abstract class ApplicationCommandManifestTask extends JavaExec { - - /** The class in the {@code grails-core-cli} artifact that writes the file */ - public static final String MANIFEST_WRITER_CLASS = 'org.apache.grails.core.cli.ApplicationCommandManifestWriter' - - @Inject - ApplicationCommandManifestTask() { - mainClass.set(MANIFEST_WRITER_CLASS) - buildDirectories.convention([]) - sourceDirectories.convention([]) - commandDirectories.convention([]) - commandFileDirectories.convention([]) - commandSourceRoots.convention([]) - } - - /** - * @return the JSON file the commands are written to - */ - @OutputFile - abstract RegularFileProperty getManifestFile() - - /** - * @return the directories whose build files, such as {@code build.gradle} and - * {@code gradle.properties}, decide the build: those of every project of the build - */ - @Input - abstract ListProperty getBuildDirectories() - - /** - * @return the directories whose files, at any depth, decide the commands or the build: the sources of - * the commands of every project, the build's {@code gradle} and {@code buildSrc} directories, and - * every included build - */ - @Input - abstract ListProperty getSourceDirectories() - - /** - * @return the directories of the {@code cli} source set of every project of the build, which hold - * commands and what they use, so a command added there, which registers itself, counts too - */ - @Input - abstract ListProperty getCommandDirectories() - - /** - * @return the directories of the main Java and Groovy sources of every project of the build, where a - * command compiled from a new file, named after its class as a command is, registers itself; a tool - * watches the names of those files, not what they hold - */ - @Input - abstract ListProperty getCommandFileDirectories() - - /** - * @return the source directories of every project of the build, where the source of a command the - * manifest lists is looked for by its class, so a change to it counts too - */ - @Input - abstract ListProperty getCommandSourceRoots() - - /** - * The source files of the listed commands found in the projects of the build; a command of a plugin - * from a repository has none. - */ - private List commandSources(Object commands) { - List found = [] - for (Object command : (commands instanceof List ? (List) commands : [])) { - Object className = command instanceof Map ? ((Map) command).className : null - if (!(className instanceof String)) { - continue - } - // a nested class is in the file of its outermost class - String path = ((String) className).replaceFirst(/\$.*/, '').replace('.', '/') - for (String root : commandSourceRoots.get()) { - for (String extension : ['.groovy', '.java', '.kt']) { - File source = new File(root, path + extension) - if (source.isFile()) { - found << source.absolutePath - } - } - } - } - found.unique().sort() - } - - @Override - @TaskAction - void exec() { - // the file is the task's output, so it is given to the writer here rather than as an - // argument input, which would make the absolute path part of the task's inputs - File file = manifestFile.get().asFile - args(file.absolutePath) - super.exec() - Map manifest = (Map) new JsonSlurper().parse(file, 'UTF-8') - manifest.inputs = [buildDirectories: buildDirectories.get(), sourceDirectories: sourceDirectories.get(), - commandDirectories: commandDirectories.get(), - commandFileDirectories: commandFileDirectories.get(), sourceFiles: commandSources(manifest.commands)] - file.setText(JsonOutput.prettyPrint(JsonOutput.toJson(manifest)), 'UTF-8') - } -} diff --git a/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/GrailsCliGradlePlugin.groovy b/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/GrailsCliGradlePlugin.groovy index a1ed8b6f63f..a5e23fdcbe9 100644 --- a/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/GrailsCliGradlePlugin.groovy +++ b/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/commands/GrailsCliGradlePlugin.groovy @@ -20,8 +20,6 @@ package org.grails.gradle.plugin.commands import java.util.jar.JarFile -import groovy.json.JsonException -import groovy.json.JsonSlurper import groovy.transform.CompileDynamic import groovy.transform.CompileStatic import groovy.transform.PackageScope @@ -38,7 +36,6 @@ import org.gradle.api.artifacts.component.ModuleComponentIdentifier import org.gradle.api.artifacts.component.ProjectComponentIdentifier import org.gradle.api.artifacts.result.ResolvedArtifactResult import org.gradle.api.artifacts.result.ResolvedComponentResult -import org.gradle.api.file.Directory import org.gradle.api.file.FileCollection import org.gradle.api.provider.Provider import org.gradle.api.tasks.JavaExec @@ -55,8 +52,6 @@ import org.grails.gradle.plugin.util.ClasspathUtils import org.grails.gradle.plugin.util.SourceSets import org.grails.io.support.FactoriesLoaderSupport import org.grails.build.parsing.CommandLineParser -import org.grails.build.parsing.DefaultCommandLine -import org.grails.build.parsing.ParseException /** * Configures the CLI tier of a Grails application or plugin project: the {@code grailsCli} @@ -110,42 +105,6 @@ class GrailsCliGradlePlugin implements Plugin { /** The internal probe configuration used to detect companion `-cli` modules */ public static final String GRAILS_CLI_DETECT_CONFIGURATION = 'grailsCliDetect' - /** - * The task that writes the commands {@code runCommand} can run to {@link #COMMAND_MANIFEST_PATH}, - * which the Grails Forge CLI reads to offer them. - */ - public static final String COMMAND_MANIFEST_TASK = 'generateCommandManifest' - - /** Where {@link #COMMAND_MANIFEST_TASK} writes the commands, relative to the build directory */ - public static final String COMMAND_MANIFEST_PATH = 'grails/commands.json' - - /** - * Set this project property to the path of a file, relative to the project directory, to have - * {@link #COMMAND_MANIFEST_TASK} write the commands there instead of {@link #COMMAND_MANIFEST_PATH}. - */ - public static final String COMMAND_MANIFEST_PROPERTY = 'grailsCommandManifest' - - /** - * The project property with the arguments of a command task, such as {@code runCommand}, as one - * command line that is split into arguments as a shell would. - */ - public static final String ARGS_PROPERTY = 'args' - - /** - * The project property with the path of a JSON file holding the arguments of a command task as an - * array of strings, each passed on unchanged. They follow those of {@link #ARGS_PROPERTY}. - */ - public static final String ARGS_FILE_PROPERTY = 'argsFile' - - /** - * Set this project property to the directory of a project, absolute or relative to the root of the - * build, to run {@code runCommand}, - * {@link #COMMAND_MANIFEST_TASK} and the other command tasks only in that project. Without it, a task - * named without its project path runs in the project Gradle starts in and in every project below it - * that has the task. - */ - public static final String COMMAND_PROJECT_PROPERTY = 'grailsCommandProject' - @Override void apply(Project project) { // self-sufficient when applied standalone: the auto-provisioning behavior is configured @@ -163,8 +122,6 @@ class GrailsCliGradlePlugin implements Plugin { configureRunScript(project) configureRunCommand(project) - - configureCommandManifest(project) } /** @@ -413,126 +370,6 @@ class GrailsCliGradlePlugin implements Plugin { dependency } - /** - * Returns the arguments a command task passes to its command: those of {@link #ARGS_PROPERTY}, split - * as a shell would, then those of the file {@link #ARGS_FILE_PROPERTY} names, unchanged. - * - * @param project the project the command task belongs to - * @return the arguments, empty when neither property is set - */ - static List commandArguments(Project project) { - List arguments = [] - def args = project.findProperty(ARGS_PROPERTY) - if (args) { - arguments.addAll(CommandLineParser.translateCommandline(args as String)) - } - def argsFile = project.findProperty(ARGS_FILE_PROPERTY) - if (argsFile) { - String content = project.providers.fileContents(project.layout.projectDirectory.file(argsFile as String)).asText.orNull - if (content == null) { - throw new GradleException("The file of the '${ARGS_FILE_PROPERTY}' property, ${argsFile}, does not exist.") - } - Object parsed - try { - parsed = new JsonSlurper().parseText(content) - } - catch (JsonException e) { - throw new GradleException("The file of the '${ARGS_FILE_PROPERTY}' property, ${argsFile}, is not JSON: ${e.message}", e) - } - if (!(parsed instanceof List) || ((List) parsed).any { !(it instanceof String) }) { - throw new GradleException("The file of the '${ARGS_FILE_PROPERTY}' property, ${argsFile}, must hold a JSON array of strings.") - } - arguments.addAll((List) parsed) - } - arguments - } - - /** - * Returns the arguments of a command task, from {@link #commandArguments}, and prepares the task for - * them: the system properties they name as {@code -Dname=value} are set on the command's JVM when - * the task runs, after every other setting, so {@code -Dgrails.env=test} among them chooses the - * environment; and the task runs only in the project {@link #COMMAND_PROJECT_PROPERTY} names, when - * it is set. - * - * @param task the command task - * @param project the project the task belongs to - * @return the arguments, to which the task adds the application class - */ - @CompileDynamic - static List commandTaskArguments(JavaExec task, Project project) { - List arguments = commandArguments(project) - Map systemProperties = commandSystemProperties(arguments) - if (systemProperties) { - // set when the task runs, so no setting of grails.env made for every JavaExec task wins over it - task.doFirst { JavaExec running -> running.systemProperties(systemProperties) } - } - runOnlyInNamedProject(task, project) - new ArrayList(arguments) - } - - /** - * Returns the system properties among a command's arguments, as the command's own command line - * reads them: each argument {@code -Dname=value}, after the parser has trimmed it and removed the - * quotes around it. - * - * @param arguments the arguments of the command - * @return the properties by name; none when the parser refuses the arguments, as the command then will - */ - static Map commandSystemProperties(List arguments) { - Map properties = new LinkedHashMap() - try { - new CommandLineParser().parse(new BuildCommandLine(), arguments as String[]).systemProperties.each { Object name, Object value -> - properties[name as String] = value as String - } - } - catch (ParseException ignored) { - // the command reports it when it parses the same arguments - } - properties - } - - /** - * Skips the task unless {@link #COMMAND_PROJECT_PROPERTY} is unset or names the directory of the - * task's project. A skipped task depends on nothing and has no classpath, so the build neither - * compiles nor resolves anything for it: a project below the named one that does not compile - * leaves the named one's command alone. - * - *

Call it after the task's classpath is set.

- */ - @CompileDynamic - static void runOnlyInNamedProject(org.gradle.api.Task task, Project project) { - if (isNamedCommandProject(project)) { - return - } - task.onlyIf("the ${COMMAND_PROJECT_PROPERTY} property names this project".toString()) { false } - task.setDependsOn([]) - if (task instanceof JavaExec) { - task.classpath = project.files() - } - } - - /** - * Returns whether the commands of the project run: when {@link #COMMAND_PROJECT_PROPERTY} is unset, - * or names the directory of the project, either absolute or relative to the root of the build. - */ - private static boolean isNamedCommandProject(Project project) { - def named = project.findProperty(COMMAND_PROJECT_PROPERTY) - !named || project.rootProject.file(named as String).canonicalFile == project.projectDir.canonicalFile - } - - /** - * A command line that keeps the environment it reads to itself, where the command line the - * command reads sets {@code grails.env} on its JVM. Here that JVM is the one that configures the - * build, whose other tasks read {@code grails.env} for their own environment. - */ - @CompileStatic - private static class BuildCommandLine extends DefaultCommandLine { - - @Override - void setEnvironment(String environment) { - } - } - /** * Returns the application main class for a task that runs against the application, or fails the task with * an explanation when the project has none, as is the case for a plugin without an {@code Application} class. @@ -585,7 +422,11 @@ class GrailsCliGradlePlugin implements Plugin { it.classpath = fileCollection it.command = commandName it.systemProperty(Environment.KEY, System.getProperty(Environment.KEY, Environment.DEVELOPMENT.getName())) - List args = GrailsCliGradlePlugin.commandTaskArguments(it, project) + List args = [] + def otherArgs = project.findProperty('args') + if (otherArgs) { + args.addAll(CommandLineParser.translateCommandline(otherArgs as String)) + } def appClassProvider = GrailsGradlePlugin.getMainClassProvider(project) @@ -717,7 +558,11 @@ class GrailsCliGradlePlugin implements Plugin { // with Grails' plugin management and bean wiring when running CLI scripts via Gradle it.systemProperty('spring.devtools.restart.enabled', 'false') - List args = GrailsCliGradlePlugin.commandTaskArguments(it, project) + List args = [] + def otherArgs = project.findProperty('args') + if (otherArgs) { + args.addAll(CommandLineParser.translateCommandline(otherArgs as String)) + } def appClassProvider = GrailsGradlePlugin.getMainClassProvider(project) @@ -746,7 +591,11 @@ class GrailsCliGradlePlugin implements Plugin { // with Grails' plugin management and bean wiring when running CLI commands via Gradle it.systemProperty('spring.devtools.restart.enabled', 'false') - List args = GrailsCliGradlePlugin.commandTaskArguments(it, project) + List args = [] + def otherArgs = project.findProperty('args') + if (otherArgs) { + args.addAll(CommandLineParser.translateCommandline(otherArgs as String)) + } def appClassProvider = GrailsGradlePlugin.getMainClassProvider(project) @@ -760,69 +609,4 @@ class GrailsCliGradlePlugin implements Plugin { } } - /** - * Registers {@link #COMMAND_MANIFEST_TASK}, which describes the commands of {@code runCommand}'s - * classpath without starting the application. - */ - @CompileDynamic - protected void configureCommandManifest(Project project) { - if (!project.tasks.names.contains(COMMAND_MANIFEST_TASK)) { - def manifestTask = project.tasks.register(COMMAND_MANIFEST_TASK, ApplicationCommandManifestTask) - // the configuration cache cannot keep the project, so the mapping holds only its directory - Directory projectDirectory = project.layout.projectDirectory - manifestTask.configure { - it.description = 'Writes the Grails commands of the application, which the Grails Forge CLI offers' - it.manifestFile.convention(project.providers.gradleProperty(COMMAND_MANIFEST_PROPERTY) - .map { String path -> projectDirectory.file(path) } - .orElse(project.layout.buildDirectory.file(COMMAND_MANIFEST_PATH))) - } - project.afterEvaluate { - manifestTask.configure { - SourceSet mainSourceSet = SourceSets.findMainSourceSet(project) - it.classpath = mainSourceSet.runtimeClasspath + project.configurations.getByName(GRAILS_CLI_CLASSPATH_CONFIGURATION) - GrailsCliGradlePlugin.runOnlyInNamedProject(it, project) - } - } - // every project of the build is known only once all are evaluated - project.gradle.projectsEvaluated { gradle -> - Project root = gradle.rootProject - List projectDirs = root.allprojects*.projectDir - List includedBuildDirs = gradle.includedBuilds*.projectDir - List buildDirectories = ([root.projectDir] + projectDirs) - .collect { File dir -> dir.absolutePath }.unique().sort() - // the hand-written registrations in either META-INF are merged into the generated ones - List sourceDirectories = ([new File(root.projectDir, 'gradle'), new File(root.projectDir, 'buildSrc')] + - projectDirs.collectMany { File dir -> - [new File(dir, 'grails-app/commands'), new File(dir, 'src/main/resources/META-INF'), - new File(dir, 'src/cli/resources/META-INF')] - } + includedBuildDirs) - .collect { File dir -> dir.absolutePath }.unique().sort() - // a command compiled from the cli source set registers itself, so a new one adds a command - List commandDirectories = root.allprojects.collectMany { Project each -> - def sourceSets = each.extensions.findByType(org.gradle.api.tasks.SourceSetContainer) - SourceSet cli = sourceSets?.findByName(GrailsCliArtifactGradlePlugin.CLI_SOURCE_SET_NAME) - cli ? cli.allSource.srcDirs*.absolutePath : [] - }.unique().sort() - // so is one compiled from the main sources, which is named after its class like any command - List commandFileDirectories = root.allprojects.collectMany { Project each -> - def sourceSets = each.extensions.findByType(org.gradle.api.tasks.SourceSetContainer) - SourceSet main = sourceSets?.findByName(SourceSet.MAIN_SOURCE_SET_NAME) - main ? main.allJava.srcDirs*.absolutePath : [] - }.unique().sort() - // every source set, as a plugin keeps its commands in its cli source set, src/cli/groovy - List commandSourceRoots = root.allprojects.collectMany { Project each -> - def sourceSets = each.extensions.findByType(org.gradle.api.tasks.SourceSetContainer) - sourceSets ? sourceSets.collectMany { SourceSet set -> set.allSource.srcDirs*.absolutePath } : [] - }.unique().sort() - manifestTask.configure { - it.buildDirectories.set(buildDirectories) - it.sourceDirectories.set(sourceDirectories) - it.commandDirectories.set(commandDirectories) - it.commandFileDirectories.set(commandFileDirectories) - it.commandSourceRoots.set(commandSourceRoots) - } - } - } - } - } diff --git a/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTaskSpec.groovy b/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTaskSpec.groovy deleted file mode 100644 index 6b43fb5f4d2..00000000000 --- a/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/ApplicationCommandManifestTaskSpec.groovy +++ /dev/null @@ -1,178 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.grails.gradle.plugin.commands - -import java.nio.file.Path - -import groovy.json.JsonSlurper -import org.gradle.testkit.runner.TaskOutcome - -import org.grails.gradle.plugin.core.GradleSpecification - -class ApplicationCommandManifestTaskSpec extends GradleSpecification { - - void 'the manifest is written by the grails-core-cli writer on the classpath runCommand uses'() { - given: - def runner = setupTestResourceProject('command-manifest') - Path manifest = runner.projectDir.toPath().resolve('app/build/grails/commands.json') - - when: - def result = executeTask(':app:generateCommandManifest') - - then: - assertTaskSuccess('generateCommandManifest', result) - - and: 'the writer was given only the file to write, which is in the build directory' - Map written = read(manifest) - written.arguments == '1' - Path.of(written.file).toRealPath() == manifest.toRealPath() - - and: "the application's own classes and resources are on the classpath next to its grailsCli dependencies" - written.runtimeMarker == 'from the application' - } - - void 'the manifest is up to date until the classpath changes'() { - given: - def runner = setupTestResourceProject('command-manifest') - executeTask(':app:generateCommandManifest') - - when: - def unchanged = executeTask(':app:generateCommandManifest') - - then: - unchanged.task(':app:generateCommandManifest').outcome == TaskOutcome.UP_TO_DATE - - when: 'a resource of the application changes' - runner.projectDir.toPath().resolve('app/src/main/resources/runtime-marker.txt').toFile().text = 'changed' - def changed = executeTask(':app:generateCommandManifest') - - then: - changed.task(':app:generateCommandManifest').outcome == TaskOutcome.SUCCESS - read(runner.projectDir.toPath().resolve('app/build/grails/commands.json')).runtimeMarker == 'changed' - } - - void 'the manifest is written where the grailsCommandManifest property says'() { - given: - def runner = setupTestResourceProject('command-manifest') - Path manifest = runner.projectDir.toPath().resolve('app/elsewhere/commands.json') - - when: - def result = executeTask(':app:generateCommandManifest', ['-PgrailsCommandManifest=elsewhere/commands.json']) - - then: - assertTaskSuccess('generateCommandManifest', result) - Path.of(read(manifest).file).toRealPath() == manifest.toRealPath() - !runner.projectDir.toPath().resolve('app/build/grails/commands.json').toFile().exists() - } - - void 'the manifest names the directories whose files decide the commands and the build'() { - given: - def runner = setupTestResourceProject('command-manifest') - File root = runner.projectDir.canonicalFile - - when: - executeTask(':app:generateCommandManifest') - Map> inputs = (Map>) read(root.toPath().resolve('app/build/grails/commands.json')).inputs - - then: 'the directories of every project, for their build files' - inputs.buildDirectories.collect { new File(it).canonicalFile } as Set == - [root, new File(root, 'app'), new File(root, 'manifest-writer')] as Set - - and: 'the sources of commands of every project, and the logic of the build' - inputs.sourceDirectories.collect { new File(it).canonicalFile }.containsAll([ - new File(root, 'gradle'), new File(root, 'buildSrc'), - new File(root, 'app/grails-app/commands'), new File(root, 'app/src/main/resources/META-INF'), - new File(root, 'app/src/cli/resources/META-INF'), - new File(root, 'manifest-writer/grails-app/commands'), - new File(root, 'manifest-writer/src/cli/resources/META-INF')]) - - and: 'the directories of the cli source sets, where a new source can be a new command' - inputs.commandDirectories.collect { new File(it).canonicalFile } as Set == - [new File(root, 'app/src/cli/java'), new File(root, 'app/src/cli/resources')] as Set - - and: 'the main sources of every project, where a file named like a command can add one' - inputs.commandFileDirectories.collect { new File(it).canonicalFile } as Set == - [new File(root, 'app/src/main/java'), new File(root, 'manifest-writer/src/main/java')] as Set - - and: 'every included build, also those that bring plugins of the build' - inputs.sourceDirectories.collect { new File(it).canonicalFile }.containsAll([new File(root, 'shared'), new File(root, 'build-logic')]) - - and: 'the sources of the listed commands found in the projects of the build, in any source set' - inputs.sourceFiles.collect { new File(it).canonicalFile } == [new File(root, 'app/src/cli/java/org/example/FakeCommand.java')] - } - - void 'the task runs only in the project the grailsCommandProject property names'() { - given: - def runner = setupTestResourceProject('command-manifest') - File app = new File(runner.projectDir, 'app') - - when: 'another project is named' - def other = executeTask(':app:generateCommandManifest', ["-PgrailsCommandProject=${new File(runner.projectDir, 'manifest-writer').absolutePath}".toString()]) - - then: - other.task(':app:generateCommandManifest').outcome == TaskOutcome.SKIPPED - !new File(app, 'build/grails/commands.json').exists() - - when: 'the application is named' - def named = executeTask(':app:generateCommandManifest', ["-PgrailsCommandProject=${app.absolutePath}".toString()]) - - then: - named.task(':app:generateCommandManifest').outcome == TaskOutcome.SUCCESS - new File(app, 'build/grails/commands.json').exists() - } - - void 'a project below the named one that does not compile leaves the named one alone'() { - given: 'a project below the application whose source does not compile' - def runner = setupTestResourceProject('command-manifest') - File root = runner.projectDir - new File(root, 'settings.gradle') << "\ninclude 'app:broken'\n" - File broken = new File(root, 'app/broken') - new File(broken, 'src/main/java/org/example').mkdirs() - new File(broken, 'build.gradle').text = """ - plugins { - id 'java' - id 'org.apache.grails.gradle.grails-cli' - } - """.stripIndent() - new File(broken, 'src/main/java/org/example/Broken.java').text = 'class Broken { not java }' - - when: 'the command list of the application is written by name, from its directory' - def manifest = executeTask('generateCommandManifest', ['-p', 'app', '-PgrailsCommandProject=app']) - - then: 'the project below is skipped, and nothing of it is compiled' - manifest.task(':app:generateCommandManifest').outcome == TaskOutcome.SUCCESS - manifest.task(':app:broken:generateCommandManifest').outcome == TaskOutcome.SKIPPED - !manifest.tasks*.path.contains(':app:broken:compileJava') - - when: 'a command of the application is planned the same way' - ['app', 'app/broken'].each { - new File(root, "${it}/build.gradle") << "\ntasks.register('findMainClass', org.grails.gradle.plugin.run.FindMainClassTask)\n" - } - def command = executeTask('runCommand', ['-m', '-p', 'app', '-PgrailsCommandProject=app']) - List planned = command.output.readLines().findAll { it.startsWith(':') }*.tokenize(' ')*.first() - - then: 'the project below brings only its own command task, which is skipped, and nothing it would depend on' - planned.containsAll([':app:compileJava', ':app:runCommand']) - planned.findAll { it.startsWith(':app:broken:') } == [':app:broken:runCommand'] - } - - private static Map read(Path file) { - (Map) new JsonSlurper().parse(file.toFile(), 'UTF-8') - } -} diff --git a/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/CommandArgumentsSpec.groovy b/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/CommandArgumentsSpec.groovy deleted file mode 100644 index 1f9aa176917..00000000000 --- a/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/commands/CommandArgumentsSpec.groovy +++ /dev/null @@ -1,211 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.grails.gradle.plugin.commands - -import groovy.json.JsonOutput - -import org.gradle.api.GradleException -import org.gradle.api.Project -import org.gradle.api.tasks.JavaExec -import org.gradle.testfixtures.ProjectBuilder -import spock.lang.Specification -import spock.util.environment.RestoreSystemProperties -import spock.lang.TempDir - -class CommandArgumentsSpec extends Specification { - - @TempDir - File projectDir - - void 'without arguments a command gets none'() { - expect: - GrailsCliGradlePlugin.commandArguments(project([:])) == [] - } - - void 'the args property is split as a shell would split it'() { - expect: - GrailsCliGradlePlugin.commandArguments(project(args: 'url-mappings-report "two words" \'say "hi"\'')) == - ['url-mappings-report', 'two words', 'say "hi"'] - } - - void 'the arguments of the args file are passed on unchanged, after those of the args property'() { - given: 'arguments a command line could not carry without quoting' - List arguments = ['two words', 'it\'s "x" y', '', '%PATH% & | <>', '--format', 'json'] - new File(projectDir, 'arguments.json').text = JsonOutput.toJson(arguments) - - expect: - GrailsCliGradlePlugin.commandArguments(project(args: 'url-mappings-report', argsFile: 'arguments.json')) == - ['url-mappings-report'] + arguments - } - - void 'an args file that #problem is reported'() { - given: - if (content != null) { - new File(projectDir, 'arguments.json').text = content - } - - when: - GrailsCliGradlePlugin.commandArguments(project(argsFile: 'arguments.json')) - - then: - GradleException e = thrown() - e.message.contains(message) - - where: - problem | content || message - 'does not exist' | null || 'does not exist' - 'is not JSON' | 'not json' || 'is not JSON' - 'is not an array of strings' | '["a", 1]' || 'must hold a JSON array of strings' - 'is an object' | '{"a": "b"}' || 'must hold a JSON array of strings' - } - - void 'an args file given by its absolute path, as the Grails Forge CLI gives it, is read'() { - given: - File elsewhere = new File(projectDir, 'elsewhere/arguments.json') - elsewhere.parentFile.mkdirs() - elsewhere.text = JsonOutput.toJson(['greet', 'Alice']) - - expect: - GrailsCliGradlePlugin.commandArguments(project(argsFile: elsewhere.absolutePath)) == ['greet', 'Alice'] - } - - void 'the system properties among the arguments are set on the command JVM when it runs, and stay among the arguments'() { - given: 'arguments with properties in every form the command line parser reads' - List given = ['dbm-update', '-Dgrails.env=test', '-Dquery=a=b', ' -Dtrimmed=yes ', '"-Dquoted=yes"', - '-D', '-Dnovalue', 'first'] - new File(projectDir, 'arguments.json').text = JsonOutput.toJson(given) - Project project = project(argsFile: 'arguments.json') - JavaExec task = project.tasks.register('commandTask', JavaExec).get() - task.systemProperty('grails.env', 'development') - - when: - List arguments = GrailsCliGradlePlugin.commandTaskArguments(task, project) - - then: 'nothing is set while the build is configured, where a setting for every JavaExec task could replace it' - arguments == given - task.systemProperties == ['grails.env': 'development'] - - when: 'the task starts' - task.actions.first().execute(task) - - then: - task.systemProperties == ['grails.env': 'test', query: 'a=b', trimmed: 'yes', quoted: 'yes'] - task.onlyIf.isSatisfiedBy(task) - } - - void 'arguments the command line parser refuses set no system property'() { - given: - new File(projectDir, 'arguments.json').text = JsonOutput.toJson(['report', '-Dgrails.env=test', '--dev mode']) - Project project = project(argsFile: 'arguments.json') - JavaExec task = project.tasks.register('commandTask', JavaExec).get() - - int actions = task.actions.size() - - when: - GrailsCliGradlePlugin.commandTaskArguments(task, project) - - then: 'no action is added to set any' - task.actions.size() == actions - } - - @RestoreSystemProperties - void 'configuring a command task with #given leaves the environment of the build alone'() { - given: - System.setProperty('grails.env', 'development') - new File(projectDir, 'arguments.json').text = JsonOutput.toJson(given) - Project project = project(argsFile: 'arguments.json') - JavaExec task = project.tasks.register('commandTask', JavaExec).get() - - when: - GrailsCliGradlePlugin.commandTaskArguments(task, project) - task.actions.first().execute(task) - - then: 'the JVM that configures the build keeps its environment' - System.getProperty('grails.env') == 'development' - - and: 'the command JVM gets the one it names' - task.systemProperties == properties - - where: - given || properties - ['dbm-update', '-Dgrails.env=test'] || ['grails.env': 'test'] - ['test', 'dbm-update', '-Dother=value'] || [other: 'value'] - } - - void 'a command task of a project the grailsCommandProject property does not name depends on nothing'() { - given: - Project project = project(grailsCommandProject: new File(projectDir, 'other').absolutePath) - JavaExec task = project.tasks.register('commandTask', JavaExec).get() - task.dependsOn('classes') - task.classpath = project.files('classes') - - when: - GrailsCliGradlePlugin.commandTaskArguments(task, project) - - then: 'nothing is compiled or resolved for it' - task.dependsOn.empty - task.classpath.empty - !task.onlyIf.isSatisfiedBy(task) - } - - void 'a relative grailsCommandProject is the directory below the root of the build'() { - given: - Project root = ProjectBuilder.builder().withProjectDir(projectDir).build() - File appDir = new File(projectDir, 'apps/app') - appDir.mkdirs() - Project app = ProjectBuilder.builder().withName('app').withProjectDir(appDir).withParent(root).build() - app.extensions.extraProperties.set('grailsCommandProject', named) - JavaExec task = app.tasks.register('commandTask', JavaExec).get() - - when: - GrailsCliGradlePlugin.commandTaskArguments(task, app) - - then: - task.onlyIf.isSatisfiedBy(task) == runs - - where: - named || runs - 'apps/app' || true - 'apps/other' || false - 'app' || false - } - - void 'a command task runs only in the project the grailsCommandProject property names: #named'() { - given: - Project project = project(grailsCommandProject: new File(projectDir, named).absolutePath) - JavaExec task = project.tasks.register('commandTask', JavaExec).get() - - when: - GrailsCliGradlePlugin.commandTaskArguments(task, project) - - then: - task.onlyIf.isSatisfiedBy(task) == runs - - where: - named || runs - '' || true - 'other' || false - } - - private Project project(Map properties) { - Project project = ProjectBuilder.builder().withProjectDir(projectDir).build() - properties.each { String name, String value -> project.extensions.extraProperties.set(name, value) } - project - } -} diff --git a/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/app/build.gradle b/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/app/build.gradle deleted file mode 100644 index 516571119a4..00000000000 --- a/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/app/build.gradle +++ /dev/null @@ -1,13 +0,0 @@ -plugins { - id 'java' - id 'org.apache.grails.gradle.grails-cli' -} - -// commands of a plugin live in its cli source set -sourceSets { - cli -} - -dependencies { - grailsCli project(':manifest-writer') -} diff --git a/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/app/src/cli/java/org/example/FakeCommand.java b/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/app/src/cli/java/org/example/FakeCommand.java deleted file mode 100644 index d5c9d6189b2..00000000000 --- a/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/app/src/cli/java/org/example/FakeCommand.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.example; - -/** Stands in for the source of a command the manifest lists. */ -public class FakeCommand { -} diff --git a/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/app/src/main/resources/runtime-marker.txt b/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/app/src/main/resources/runtime-marker.txt deleted file mode 100644 index 3c4f04f6f47..00000000000 --- a/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/app/src/main/resources/runtime-marker.txt +++ /dev/null @@ -1 +0,0 @@ -from the application diff --git a/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/build-logic/settings.gradle b/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/build-logic/settings.gradle deleted file mode 100644 index 6ef2ba8769a..00000000000 --- a/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/build-logic/settings.gradle +++ /dev/null @@ -1 +0,0 @@ -rootProject.name = 'build-logic' diff --git a/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/gradle.properties b/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/gradle.properties deleted file mode 100644 index d1e27431f11..00000000000 --- a/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/gradle.properties +++ /dev/null @@ -1,3 +0,0 @@ -version=1.0.0 -group=org.example.test -grailsCliAutoProvision=false diff --git a/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/manifest-writer/build.gradle b/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/manifest-writer/build.gradle deleted file mode 100644 index 075ba3d5638..00000000000 --- a/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/manifest-writer/build.gradle +++ /dev/null @@ -1,3 +0,0 @@ -plugins { - id 'java' -} diff --git a/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/manifest-writer/src/main/java/org/apache/grails/core/cli/ApplicationCommandManifestWriter.java b/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/manifest-writer/src/main/java/org/apache/grails/core/cli/ApplicationCommandManifestWriter.java deleted file mode 100644 index be41a293a92..00000000000 --- a/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/manifest-writer/src/main/java/org/apache/grails/core/cli/ApplicationCommandManifestWriter.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.grails.core.cli; - -import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; - -/** - * Stands in for the writer of grails-core-cli: records the arguments it was given and what its - * classpath held, so the spec can check what the manifest task runs it with. - */ -public final class ApplicationCommandManifestWriter { - - private ApplicationCommandManifestWriter() { - } - - public static void main(String[] args) throws IOException { - String runtimeMarker; - try (InputStream input = ApplicationCommandManifestWriter.class.getClassLoader().getResourceAsStream("runtime-marker.txt")) { - runtimeMarker = input == null ? "absent" : new String(input.readAllBytes(), StandardCharsets.UTF_8).trim(); - } - Path file = Path.of(args[0]); - Files.createDirectories(file.getParent()); - // JSON, as the real writer writes, with what the spec checks in place of the commands - Files.writeString(file, "{\"version\": 1, \"commands\": [{\"name\": \"fake\", \"className\": \"org.example.FakeCommand\"}," - + " {\"name\": \"elsewhere\", \"className\": \"org.example.NotInTheBuild\"}], \"arguments\": \"" + args.length + "\", \"file\": \"" - + file.toString().replace("\\", "\\\\") + "\", \"runtimeMarker\": \"" + runtimeMarker + "\"}\n"); - } -} diff --git a/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/settings.gradle b/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/settings.gradle deleted file mode 100644 index 5eabdad5c73..00000000000 --- a/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/settings.gradle +++ /dev/null @@ -1,9 +0,0 @@ -pluginManagement { - includeBuild 'build-logic' -} - -rootProject.name = 'command-manifest' - -include 'manifest-writer', 'app' - -includeBuild 'shared' diff --git a/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/shared/settings.gradle b/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/shared/settings.gradle deleted file mode 100644 index e1f3db7ca90..00000000000 --- a/grails-gradle/plugins/src/test/resources/test-projects/command-manifest/shared/settings.gradle +++ /dev/null @@ -1 +0,0 @@ -rootProject.name = 'shared' diff --git a/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2CreatePersistentTokenCommand.groovy b/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2CreatePersistentTokenCommand.groovy index 3bc7e86753d..615db6fc799 100644 --- a/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2CreatePersistentTokenCommand.groovy +++ b/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2CreatePersistentTokenCommand.groovy @@ -18,7 +18,6 @@ package org.apache.grails.security.cli import grails.build.logging.ConsoleLogger import grails.build.logging.GrailsConsole import grails.codegen.model.Model -import org.apache.grails.core.cli.CommandParameter import org.apache.grails.core.cli.GrailsApplicationCommand import groovy.transform.CompileStatic @@ -30,13 +29,9 @@ import groovy.transform.CompileStatic * * @since 6.0.0 */ -@CommandParameter(name = 'domain-class-name', - description = 'The fully qualified name of the persistent token domain class, e.g. com.yourapp.PersistentLogin') @CompileStatic class S2CreatePersistentTokenCommand implements GrailsApplicationCommand, CommandLineHelper, SkipBootstrap { - String description = 'Creates a persistent token domain class for remember-me logins.' - @Delegate ConsoleLogger consoleLogger = GrailsConsole.getInstance() diff --git a/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2CreateRoleHierarchyEntryCommand.groovy b/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2CreateRoleHierarchyEntryCommand.groovy index 8cca9a5a336..1411817642e 100644 --- a/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2CreateRoleHierarchyEntryCommand.groovy +++ b/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2CreateRoleHierarchyEntryCommand.groovy @@ -18,7 +18,6 @@ package org.apache.grails.security.cli import grails.build.logging.ConsoleLogger import grails.build.logging.GrailsConsole import grails.codegen.model.Model -import org.apache.grails.core.cli.CommandParameter import org.apache.grails.core.cli.GrailsApplicationCommand import groovy.transform.CompileStatic @@ -29,13 +28,9 @@ import groovy.transform.CompileStatic * * @since 6.0.0 */ -@CommandParameter(name = 'domain-class-name', - description = 'The fully qualified name of the role hierarchy domain class, e.g. com.yourapp.RoleHierarchyEntry') @CompileStatic class S2CreateRoleHierarchyEntryCommand implements GrailsApplicationCommand, CommandLineHelper, SkipBootstrap { - String description = 'Creates a domain class for a persistent role hierarchy.' - private static final String USAGE_MESSAGE = ''' ./grailsw s2-create-role-hierarchy-entry [DOMAIN CLASS NAME] diff --git a/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2QuickstartCommand.groovy b/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2QuickstartCommand.groovy index 3e8fc44c1f6..9a7cab86840 100644 --- a/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2QuickstartCommand.groovy +++ b/grails-spring-security/plugin/src/cli/groovy/org/apache/grails/security/cli/S2QuickstartCommand.groovy @@ -18,15 +18,13 @@ package org.apache.grails.security.cli import grails.build.logging.ConsoleLogger import grails.build.logging.GrailsConsole import grails.codegen.model.Model -import org.apache.grails.core.cli.CommandOption -import org.apache.grails.core.cli.CommandParameter import org.apache.grails.core.cli.GrailsApplicationCommand import groovy.transform.CompileStatic /** * Creates domain classes and updates config settings for the Spring Security plugin. * Usage: ./grailsw s2-quickstart [DOMAIN_CLASS_PACKAGE] [USER_CLASS_NAME] [ROLE_CLASS_NAME] [REQUEST_MAP_CLASS_NAME] --groupClassName=[GROUP_CLASS_NAME] or - * ./grailsw s2-quickstart --uiOnly + * s2-quickstart --ui-only * * For Example: * 1. ./grailsw s2-quickstart com.yourapp User Role --groupClassName=RoleGroup @@ -36,16 +34,6 @@ import groovy.transform.CompileStatic * * @since 6.0.0 */ -@CommandParameter(name = 'domain-class-package', required = false, - description = 'The package of the domain classes') -@CommandParameter(name = 'user-class-name', required = false, description = 'The name of the user class') -@CommandParameter(name = 'role-class-name', required = false, description = 'The name of the role class') -@CommandParameter(name = 'requestmap-class-name', required = false, - description = 'The name of the request map class, when request maps secure the URLs') -@CommandOption(name = 'groupClassName', description = 'Also creates role group classes with this name') -@CommandOption(name = 'uiOnly', flag = true, - description = 'Configures the plugin without creating domain classes, as LDAP or CAS authentication needs') -@CommandOption(name = 'salt', flag = true, description = 'Creates a password encoder listener that salts the passwords') @CompileStatic class S2QuickstartCommand implements GrailsApplicationCommand, CommandLineHelper, SkipBootstrap { diff --git a/grails-spring-security/plugin/src/test-cli/groovy/org/apache/grails/security/cli/SecurityCommandManifestSpec.groovy b/grails-spring-security/plugin/src/test-cli/groovy/org/apache/grails/security/cli/SecurityCommandManifestSpec.groovy deleted file mode 100644 index fed2aad8d1a..00000000000 --- a/grails-spring-security/plugin/src/test-cli/groovy/org/apache/grails/security/cli/SecurityCommandManifestSpec.groovy +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.grails.security.cli - -import java.nio.file.Path - -import groovy.json.JsonSlurper - -import org.apache.grails.core.cli.ApplicationCommandManifestWriter -import spock.lang.Specification -import spock.lang.TempDir - -class SecurityCommandManifestSpec extends Specification { - - @TempDir - Path tempDir - - void 's2-quickstart describes its arguments and options'() { - when: - Map quickstart = command('s2-quickstart') - - then: - quickstart.description == 'Creates domain classes and updates config settings for the Spring Security plugin.' - quickstart.arguments*.name == ['domain-class-package', 'user-class-name', 'role-class-name', 'requestmap-class-name'] - quickstart.arguments*.required == [false, false, false, false] - - and: 'the options the command reads' - quickstart.options.collectEntries { [(it.name): it.flag] } == [groupClassName: false, uiOnly: true, salt: true] - } - - void '#name takes the domain class name it creates'() { - when: - Map described = command(name) - - then: - described.description == description - described.arguments*.name == ['domain-class-name'] - described.arguments*.required == [true] - described.options == [] - - where: - name | description - 's2-create-persistent-token' | 'Creates a persistent token domain class for remember-me logins.' - 's2-create-role-hierarchy-entry' | 'Creates a domain class for a persistent role hierarchy.' - } - - private Map command(String name) { - File manifest = tempDir.resolve('commands.json').toFile() - ApplicationCommandManifestWriter.main(manifest.path) - Map found = (Map) ((Map) new JsonSlurper().parse(manifest)).commands.find { it.name == name } - assert found : "${name} is not in the manifest" - found - } -} From 839685d6559cf949471bf24ef6eaf373df962e03 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Thu, 19 Nov 2026 10:51:10 -0800 Subject: [PATCH 092/115] test: assert the asynchronous requests' outcome on the spec thread, and an included action's save The asynchronous requests ran in Thread.start, where a failed assertStatus never reached Spock, and the join could time out silently. They now run through a CompletableFuture that the spec releases and checks for HTTP 200. The request made while the first one waits must answer 200 with an open session of its own, not merely one whose description differs. An action included from a view saves after the outer action has flushed, so only the include's own flush writes it. A test in the MongoDB example application keeps it written. --- .../OsivRequestSessionSpec.groovy | 26 +++++++++++--- .../functional/tests/PlayerController.groovy | 8 +++++ .../grails-app/views/player/includeSave.gsp | 19 +++++++++++ .../tests/OpenSessionInViewSpec.groovy | 34 ++++++++++++++++--- 4 files changed, 77 insertions(+), 10 deletions(-) create mode 100644 grails-test-examples/mongodb/base/grails-app/views/player/includeSave.gsp diff --git a/grails-test-examples/datasources/src/integration-test/groovy/functionaltests/OsivRequestSessionSpec.groovy b/grails-test-examples/datasources/src/integration-test/groovy/functionaltests/OsivRequestSessionSpec.groovy index 843f6544a0c..9c0c7255ac4 100644 --- a/grails-test-examples/datasources/src/integration-test/groovy/functionaltests/OsivRequestSessionSpec.groovy +++ b/grails-test-examples/datasources/src/integration-test/groovy/functionaltests/OsivRequestSessionSpec.groovy @@ -18,11 +18,15 @@ */ package functionaltests +import java.util.concurrent.CompletableFuture import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit +import java.util.function.Supplier import datasources.RequestSessions import grails.testing.mixin.integration.Integration import org.apache.grails.testing.http.client.HttpClientSupport +import org.apache.grails.testing.http.client.TestHttpResponse import spock.lang.Specification import spock.util.concurrent.PollingConditions @@ -47,20 +51,32 @@ class OsivRequestSessionSpec extends Specification implements HttpClientSupport given: RequestSessions.release = new CountDownLatch(1) RequestSessions.asyncSecondarySession = null - final asyncRequest = Thread.start { http('/requestSession/promise').assertStatus(200) } - expect: 'the session the action ran with is closed once the request has gone asynchronous' + when: 'the request goes asynchronous and waits' + CompletableFuture asyncResponse = CompletableFuture.supplyAsync({ http('/requestSession/promise') } as Supplier) + + then: 'the session the action ran with is closed once the request has gone asynchronous' new PollingConditions(timeout: 10).eventually { assert RequestSessions.asyncSecondarySession != null assert !RequestSessions.asyncSecondarySession.open } - and: 'a later request gets a session of its own' - !http('/requestSession/current').body().startsWith("${System.identityHashCode(RequestSessions.asyncSecondarySession)}/") + when: 'another request runs while the first one waits' + TestHttpResponse later = http('/requestSession/current') + + then: 'it gets an open session of its own' + later.assertStatus(200) + later.body() ==~ /\d+\/open/ + !later.body().startsWith("${System.identityHashCode(RequestSessions.asyncSecondarySession)}/") + + when: 'the first request is released' + RequestSessions.release.countDown() + + then: 'it completes successfully' + asyncResponse.get(30, TimeUnit.SECONDS).assertStatus(200) cleanup: RequestSessions.release.countDown() - asyncRequest?.join(30000) } private Map pageLines(String path) { diff --git a/grails-test-examples/mongodb/base/grails-app/controllers/functional/tests/PlayerController.groovy b/grails-test-examples/mongodb/base/grails-app/controllers/functional/tests/PlayerController.groovy index 01a7959c6e2..bf0fa776721 100644 --- a/grails-test-examples/mongodb/base/grails-app/controllers/functional/tests/PlayerController.groovy +++ b/grails-test-examples/mongodb/base/grails-app/controllers/functional/tests/PlayerController.groovy @@ -31,6 +31,14 @@ class PlayerController { render text: Player.list().size().toString() } + def includeSave() { + } + + def saveFromView() { + new Player(name: 'From View', team: Team.findByName('Rovers')).save() + render text: 'saved' + } + def save(String name, String teamId) { render text: new Player(name: name, team: Team.get(teamId)).save().id.toString() } diff --git a/grails-test-examples/mongodb/base/grails-app/views/player/includeSave.gsp b/grails-test-examples/mongodb/base/grails-app/views/player/includeSave.gsp new file mode 100644 index 00000000000..64d6f1f5383 --- /dev/null +++ b/grails-test-examples/mongodb/base/grails-app/views/player/includeSave.gsp @@ -0,0 +1,19 @@ +<%-- + ~ Licensed to the Apache Software Foundation (ASF) under one + ~ or more contributor license agreements. See the NOTICE file + ~ distributed with this work for additional information + ~ regarding copyright ownership. The ASF licenses this file + ~ to you under the Apache License, Version 2.0 (the + ~ "License"); you may not use this file except in compliance + ~ with the License. You may obtain a copy of the License at + ~ + ~ https://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, + ~ software distributed under the License is distributed on an + ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + ~ KIND, either express or implied. See the License for the + ~ specific language governing permissions and limitations + ~ under the License. + --%> + diff --git a/grails-test-examples/mongodb/base/src/integration-test/groovy/functional/tests/OpenSessionInViewSpec.groovy b/grails-test-examples/mongodb/base/src/integration-test/groovy/functional/tests/OpenSessionInViewSpec.groovy index 8aa589285fb..8c1412c5f16 100644 --- a/grails-test-examples/mongodb/base/src/integration-test/groovy/functional/tests/OpenSessionInViewSpec.groovy +++ b/grails-test-examples/mongodb/base/src/integration-test/groovy/functional/tests/OpenSessionInViewSpec.groovy @@ -19,7 +19,10 @@ package functional.tests +import java.util.concurrent.CompletableFuture import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit +import java.util.function.Supplier import org.springframework.beans.factory.annotation.Autowired import org.springframework.context.ApplicationContext @@ -89,6 +92,15 @@ class OpenSessionInViewSpec extends Specification implements HttpClientSupport { Player.withNewSession { Player.get(playerId).name } == 'Player 0' } + void "an action included from a view has its save written"() { + when: 'the view, rendered after the outer action has flushed, includes an action that saves without a flush' + String body = http('/player/includeSave').assertStatus(200).body() + + then: + body.contains('saved') + Player.withNewSession { Player.countByName('From View') } == 1 + } + void "an include uses the session of the request and leaves it open for the rest of the page"() { when: final page = pageLines('/requestSession/page?include=true') @@ -105,9 +117,11 @@ class OpenSessionInViewSpec extends Specification implements HttpClientSupport { given: RequestSessions.release = new CountDownLatch(1) RequestSessions.asyncSession = null - final asyncRequest = Thread.start { http(path).assertStatus(200) } - expect: 'the session the action ran with is closed once the request has gone asynchronous' + when: 'the request goes asynchronous and waits' + CompletableFuture asyncResponse = CompletableFuture.supplyAsync({ http(path) } as Supplier) + + then: 'the session the action ran with is closed once the request has gone asynchronous' new PollingConditions(timeout: 10).eventually { assert RequestSessions.asyncSession != null assert !RequestSessions.asyncSession.connected @@ -116,12 +130,22 @@ class OpenSessionInViewSpec extends Specification implements HttpClientSupport { and: 'what the action saved was flushed then, as when an action returns' Player.withNewSession { Player.findByName('Saved before going asynchronous') } != null - and: 'a later request gets a session of its own' - !http('/requestSession/current').body().startsWith("${System.identityHashCode(RequestSessions.asyncSession)}/") + when: 'another request runs while the first one waits' + TestHttpResponse later = http('/requestSession/current') + + then: 'it gets an open session of its own' + later.assertStatus(200) + later.body() ==~ /\d+\/open/ + !later.body().startsWith("${System.identityHashCode(RequestSessions.asyncSession)}/") + + when: 'the first request is released' + RequestSessions.release.countDown() + + then: 'it completes successfully' + asyncResponse.get(30, TimeUnit.SECONDS).assertStatus(200) cleanup: RequestSessions.release.countDown() - asyncRequest?.join(30000) where: how | path From d950748839a451fcf56e33c14b3f4d4fa4e90d39 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Thu, 19 Nov 2026 12:56:43 -0800 Subject: [PATCH 093/115] docs(async): a MongoDB action that must handle a write failure flushes before going asynchronous What an action saves is written when the request goes asynchronous, as when it returns, but a failure then only reaches the log and the response still succeeds. An action that needs to handle it flushes or commits first. --- grails-doc/src/en/guide/async/asyncRequests.adoc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/grails-doc/src/en/guide/async/asyncRequests.adoc b/grails-doc/src/en/guide/async/asyncRequests.adoc index b383615a119..84bf34e9bfe 100644 --- a/grails-doc/src/en/guide/async/asyncRequests.adoc +++ b/grails-doc/src/en/guide/async/asyncRequests.adoc @@ -36,6 +36,8 @@ grails: version: 3.0 ---- +NOTE: With GORM for MongoDB, what an action saves is written when the request goes asynchronous, as when the action returns. A failure to write it then only reaches the log, though, and the response still succeeds. So an action that needs to handle a database failure itself should flush, or commit, before it returns a `Promise` or calls `startAsync()`. + ==== Async Models From 91a58343aca5baed7afe882f255311644a11a849 Mon Sep 17 00:00:00 2001 From: Mattias Reichel Date: Thu, 19 Nov 2026 22:17:59 +0100 Subject: [PATCH 094/115] test(hibernate5): a request that goes asynchronous closes the session of another datasource without a flush A change an action makes to an instance of another datasource outside a transaction is not written when the request goes asynchronous. Under the default COMMIT flush mode, it is not written when the action returns either. --- .../support/MultiDataSourceSessionSpec.groovy | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/grails-data-hibernate5/grails-plugin/src/test/groovy/org/grails/plugin/hibernate/support/MultiDataSourceSessionSpec.groovy b/grails-data-hibernate5/grails-plugin/src/test/groovy/org/grails/plugin/hibernate/support/MultiDataSourceSessionSpec.groovy index 5abd227b701..e6531896e6f 100644 --- a/grails-data-hibernate5/grails-plugin/src/test/groovy/org/grails/plugin/hibernate/support/MultiDataSourceSessionSpec.groovy +++ b/grails-data-hibernate5/grails-plugin/src/test/groovy/org/grails/plugin/hibernate/support/MultiDataSourceSessionSpec.groovy @@ -21,6 +21,7 @@ package org.grails.plugin.hibernate.support import grails.gorm.annotation.Entity import org.grails.datastore.mapping.core.DatastoreUtils import org.grails.orm.hibernate.HibernateDatastore +import org.hibernate.FlushMode import org.hibernate.Session import org.hibernate.SessionFactory import org.hibernate.dialect.H2Dialect @@ -239,6 +240,38 @@ class MultiDataSourceSessionSpec extends Specification { defaultSession?.close() } + def "a request that goes asynchronous closes the session of another datasource without a flush"() { + given: "a book of the other datasource" + OsivBook.withTransaction { new OsivBook(title: 'Before').save() } + + and: "a request that changes it without a flush" + def interceptor = new GrailsOpenSessionInViewInterceptor() + interceptor.setHibernateDatastore(datastore) + WebRequest webRequest = webRequest() + SessionFactory secondarySf = datastore.getDatastoreForConnection('secondary').sessionFactory + interceptor.preHandle(webRequest) + Session defaultSession = boundSession(datastore.sessionFactory) + Session secondarySession = boundSession(secondarySf) + OsivBook book = OsivBook.findByTitle('Before') + book.title = 'Changed before going asynchronous' + + expect: "the default flush mode, under which the change is not written when an action returns either" + secondarySession.hibernateFlushMode == FlushMode.COMMIT + + when: "the request goes asynchronous" + interceptor.afterConcurrentHandlingStarted(webRequest) + + then: "the change is not written, and the session is closed" + OsivBook.withNewSession { OsivBook.findByTitle('Before') } != null + OsivBook.withNewSession { OsivBook.findByTitle('Changed before going asynchronous') } == null + !TransactionSynchronizationManager.hasResource(secondarySf) + !secondarySession.open + + cleanup: + defaultSession?.close() + OsivBook.withTransaction { OsivBook.list()*.delete(flush: true) } + } + def "CRUD operations work on secondary datasource with OSIV"() { given: "OSIV interceptor configured and preHandle called" def interceptor = new GrailsOpenSessionInViewInterceptor() From 17b3fcbfb2f4cc81598eae1ff7c6be4fd8a376c1 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Thu, 19 Nov 2026 14:45:32 -0800 Subject: [PATCH 095/115] Update Spring Boot to 4.1.3 Spring Boot 4.1.3 is released. Follow the versions it now manages: - Hibernate 7 is strictly re-declared at Boot's 7.4.15.Final (still on hibernate-models 1.1.2). - ASM 9.11.1: Jetty 12.1.15 brings it in, so the BOM manages it to stay ahead of the classpath. - Drop the CVE-2026-113602 scan exclusion for json-smart: Boot now manages 2.6.1, which rejects a trailing 0x1A in strict mode (#305). - Refresh the comments that name Boot's versions. Boot's Jackson 2.21.8 and 3.1.8 now carry the CVE-2026-110879 fix; the 2.22.4 pin stays for Groovy YAML, and the 3.2.4 pin keeps applications on the 3.2 line. --- .../scan/VulnerabilityScanPlugin.groovy | 6 +-- dependencies.gradle | 44 +++++++++---------- 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/build-logic/vulnerability-scan/src/main/groovy/org/apache/grails/buildsrc/scan/VulnerabilityScanPlugin.groovy b/build-logic/vulnerability-scan/src/main/groovy/org/apache/grails/buildsrc/scan/VulnerabilityScanPlugin.groovy index 81dfc115a43..44a1df034c1 100644 --- a/build-logic/vulnerability-scan/src/main/groovy/org/apache/grails/buildsrc/scan/VulnerabilityScanPlugin.groovy +++ b/build-logic/vulnerability-scan/src/main/groovy/org/apache/grails/buildsrc/scan/VulnerabilityScanPlugin.groovy @@ -83,10 +83,6 @@ class VulnerabilityScanPlugin implements Plugin { 'and no fixed release exists. A long pause can terminate the pause-detector dispatch thread ' + 'and leave its queue growing, see https://github.com/LatencyUtils/LatencyUtils/issues/22. ' + 'Remove once a patched release is available or micrometer no longer requires LatencyUtils'), - new VulnerabilityExclusion('CVE-2026-113602', 'net.minidev:json-smart', - 'Managed by spring-boot-dependencies; every release through 2.6.0, the newest, is affected and ' + - 'no fixed release exists. Strict-mode parsing accepts a trailing 0x1A character. Remove ' + - 'once a patched release ships'), new VulnerabilityExclusion('CVE-2026-110974', 'com.vaadin.external.google:android-json', 'Pulled transitively via spring-boot-cli; com.vaadin.external.google:android-json is unmaintained ' + 'and 0.0.20131108.vaadin1, its newest release, is affected. Remove once spring-boot-cli no ' + @@ -97,7 +93,7 @@ class VulnerabilityScanPlugin implements Plugin { 'CVE-2026-22747. Remove once Sonatype narrows its affected range'), new VulnerabilityExclusion('CVE-2026-47842', 'org.springframework.security:spring-security-crypto', 'Sonatype flags 7.1.1, which https://spring.io/security/cve-2026-47842 lists as fixed. ' + - 'Spring Boot 4.1.2 manages 7.1.2, past that fixed release. The exclusion stays until ' + + 'Spring Boot 4.1.3 manages 7.1.3, past that fixed release. The exclusion stays until ' + 'Sonatype narrows its affected range past the version Boot manages. ' + 'Remove once Sonatype corrects its affected range; reassess on any Spring Security upgrade'), ].asImmutable() diff --git a/dependencies.gradle b/dependencies.gradle index 4f1dad07c14..7bc2dfd1dea 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -25,7 +25,7 @@ ext { 'ant.version' : '1.10.18', 'asciidoctor-gradle-jvm.version': '4.0.5', 'asciidoctorj.version' : '3.0.1', - 'asm.version' : '9.10.1', + 'asm.version' : '9.11.1', 'asset-pipeline-gradle.version' : '5.2.3', 'commons-text.version' : '1.15.0', 'directory-watcher.version' : '0.19.1', @@ -39,7 +39,7 @@ ext { 'jna.version' : '5.19.1', 'maven-model.version' : '3.9.16', 'objenesis.version' : '3.6', - 'spring-boot.version' : '4.1.2', + 'spring-boot.version' : '4.1.3', ] // Note: the name of the dependency must be the prefix of the property name so properties in the pom are resolved correctly @@ -77,7 +77,7 @@ ext { 'checker-qual.version' : '3.55.1', 'commons-codec.version' : '1.22.1', 'geb-spock.version' : '8.0.1', - // Pinned as a drift tripwire even though 25.1 matches Spring Boot 4.1.2: graphql-java- + // Pinned as a drift tripwire even though 25.1 matches Spring Boot 4.1.3: graphql-java- // extended-scalars is not managed by Spring Boot. Keeping this pin makes a later Boot // graphql-java bump force a compatibility check of extended-scalars, not an equal-version // upgrade. See https://github.com/apache/grails-core/issues/15674 @@ -86,25 +86,25 @@ ext { 'groovy.version' : '5.1.3', // Security: 33.7.2 addresses CVE-2026-102554 (uncontrolled resource consumption). 'guava.version' : '33.7.2-jre', - // Groovy YAML 5.1.0 requires a minimum of Jackson 2.22.1, and 2.22.4 also overrides the - // 2.21.7 Spring Boot 4.1.2 manages: it adds the StreamReadConstraints length limit to - // GregorianCalendar/Duration (CVE-2026-68497) and restricts the URI schemes - // java.nio.file.Path deserialization accepts (CVE-2026-19032). Jackson 2 only releases - // jackson-annotations on the minor, hence the separate 2.22 below. 2.22.3 also fixes - // CVE-2026-89425, CVE-2026-89407, CVE-2026-91777 and CVE-2026-91776, and 2.22.4 fixes - // CVE-2026-110879 (type id validation in jackson-databind), which 2.21.7 does not carry. + // Groovy YAML 5.1.0 requires a minimum of Jackson 2.22.1, so this overrides the 2.21.8 + // Spring Boot 4.1.3 manages. 2.22.4 is the first 2.22 release with the fixes 2.21.8 also + // carries: the StreamReadConstraints length limit for GregorianCalendar/Duration + // (CVE-2026-68497), the URI schemes java.nio.file.Path deserialization accepts + // (CVE-2026-19032), CVE-2026-89425, CVE-2026-89407, CVE-2026-91777, CVE-2026-91776 and the + // type id validation in jackson-databind (CVE-2026-110879). Jackson 2 only releases + // jackson-annotations on the minor, hence the separate 2.22 below. 'jackson-2-bom.version' : '2.22.4', 'jackson2-annotations.version' : '2.22', - // Security: overrides Jackson 3.1.7 from Spring Boot 4.1.2. The 3.2.4 BOM aligns the family - // and fixes CVE-2026-89425, CVE-2026-89407, CVE-2026-91777, CVE-2026-91776 and - // CVE-2026-110879, which 3.1.7 does not carry, and CVE-2026-110729, which affects only - // jackson-core 3.2.0 to 3.2.3. + // Overrides Jackson 3.1.8 from Spring Boot 4.1.3, which keeps applications on the 3.2 line + // earlier Grails 8.0 releases moved them to. The 3.2.4 BOM aligns the family and fixes + // CVE-2026-89425, CVE-2026-89407, CVE-2026-91777, CVE-2026-91776 and CVE-2026-110879, + // which 3.1.8 fixes too, and CVE-2026-110729, which affects only jackson-core 3.2.0 to 3.2.3. 'jackson-bom.version' : '3.2.4', 'jquery.version' : '4.0.0', - // Security: overrides the 1.5.38 Spring Boot 4.1.2 manages. 1.6.3 strips slashes from MDC + // Security: overrides the 1.5.38 Spring Boot 4.1.3 manages. 1.6.3 strips slashes from MDC // values before SiftingAppender uses them as discriminating keys (CVE-2026-19880), and // 1.6.5 completes that fix (CVE-2026-104721). The 1.6.x line keeps SLF4J 2.0.x and JDK 11. - // Spring Boot 4.1.2 manages SLF4J 2.0.20, so SLF4J is not pinned. + // Spring Boot 4.1.3 manages SLF4J 2.0.20, so SLF4J is not pinned. 'logback.version' : '1.6.5', // Security: overrides the 5.8.1 Spring Boot 4.1 manages. 5.9.2 stopped ProxySettings // .toString() writing the SOCKS5 proxy credentials into the application log @@ -294,7 +294,7 @@ ext { // not an equal-version upgrade of extended-scalars - see graphql-java.version. 'graphql-java.version', // The Hibernate 7 BOM strictly re-declares Boot's version so enforcedPlatform consumers - // are forced to it; the pin matches Spring Boot 4.1.2 on purpose. + // are forced to it; the pin matches Spring Boot 4.1.3 on purpose. 'hibernate.version', ] @@ -309,7 +309,7 @@ ext { if (project.name in ['grails-bom', 'grails-hibernate5-bom'] || project.name.startsWith('grails-data-hibernate5')) { customBomVersions = [ 'liquibase-hibernate.version': '4.27.0', - 'liquibase.version' : '4.27.0', // liquibase-hibernate5 stops at 4.27.0, and Boot 4.1.2's Liquibase 5.0.4 is a new major. + 'liquibase.version' : '4.27.0', // liquibase-hibernate5 stops at 4.27.0, and Boot 4.1.3's Liquibase 5.0.4 is a new major. 'hibernate.version' : '5.6.15.Final', ] combinedVersions += customBomVersions @@ -351,15 +351,15 @@ ext { customBomVersions = [ 'cache-ri-impl.version' : '1.1.1', 'derby.version' : '10.17.1.0', - // Spring Boot 4.1.2 selects Hibernate ORM 7.4.13.Final. This BOM strictly re-declares + // Spring Boot 4.1.3 selects Hibernate ORM 7.4.15.Final. This BOM strictly re-declares // that version so enforcedPlatform consumers are forced to it; a platform() import - // is not enforced. 7.4.13.Final still requires hibernate-models 1.1.2. + // is not enforced. 7.4.15.Final still requires hibernate-models 1.1.2. 'hibernate-models.version' : '1.1.2', 'hibernate-tools.version' : '7.3.13.Final', - 'hibernate.version' : '7.4.13.Final', + 'hibernate.version' : '7.4.15.Final', 'jandex.version' : '3.6.0', 'liquibase-test-harness.version': '1.0.11', - 'liquibase.version' : '4.27.0', // liquibase-hibernate5 stops at 4.27.0, and Boot 4.1.2's Liquibase 5.0.4 is a new major. + 'liquibase.version' : '4.27.0', // liquibase-hibernate5 stops at 4.27.0, and Boot 4.1.3's Liquibase 5.0.4 is a new major. 'mockito-inline.version' : '5.2.0', ] combinedVersions += customBomVersions From f5d3a791643a917e15917bd2ee885e9a864f21e8 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Thu, 19 Nov 2026 14:48:28 -0800 Subject: [PATCH 096/115] Update Spring Boot to 4.2.0 Spring Boot 4.2.0 is released. Move from 4.2.0-RC2 to the GA, which brings Spring Framework 7.1.0, Spring Security 7.2.0, Spring Data 2026.1.0 and Hibernate ORM 7.4.15.Final, and name the GA Spring Security version in the CVE-2026-47842 scan exclusion. --- .../apache/grails/buildsrc/scan/VulnerabilityScanPlugin.groovy | 2 +- dependencies.gradle | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/build-logic/vulnerability-scan/src/main/groovy/org/apache/grails/buildsrc/scan/VulnerabilityScanPlugin.groovy b/build-logic/vulnerability-scan/src/main/groovy/org/apache/grails/buildsrc/scan/VulnerabilityScanPlugin.groovy index 6b146735914..ce6bcbd07a5 100644 --- a/build-logic/vulnerability-scan/src/main/groovy/org/apache/grails/buildsrc/scan/VulnerabilityScanPlugin.groovy +++ b/build-logic/vulnerability-scan/src/main/groovy/org/apache/grails/buildsrc/scan/VulnerabilityScanPlugin.groovy @@ -93,7 +93,7 @@ class VulnerabilityScanPlugin implements Plugin { 'CVE-2026-22747. Remove once Sonatype narrows its affected range'), new VulnerabilityExclusion('CVE-2026-47842', 'org.springframework.security:spring-security-crypto', 'Sonatype flagged 7.1.1, but https://spring.io/security/cve-2026-47842 lists only 7.1.0 on ' + - 'the 7.1 line as affected and 7.1.1 as fixed. Spring Boot 4.2.0-RC2 manages 7.2.0-RC1, which ' + + 'the 7.1 line as affected and 7.1.1 as fixed. Spring Boot 4.2.0 manages 7.2.0, which ' + 'the advisory does not list and which carries the fix (the AesCbcBytesEncryptor and ' + 'AesGcmBytesEncryptor replacements 7.1.1 introduced). Remove once Sonatype corrects its ' + 'affected range; reassess on any Spring Security upgrade'), diff --git a/dependencies.gradle b/dependencies.gradle index 452d0e4844e..2b38004f195 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -39,7 +39,7 @@ ext { 'jna.version' : '5.19.1', 'maven-model.version' : '3.9.16', 'objenesis.version' : '3.6', - 'spring-boot.version' : '4.2.0-RC2', + 'spring-boot.version' : '4.2.0', ] // Note: the name of the dependency must be the prefix of the property name so properties in the pom are resolved correctly From 97d88c275489e949176711135c4daea8135a4650 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Thu, 19 Nov 2026 15:08:41 -0800 Subject: [PATCH 097/115] fix(hibernate5): close the default session when a Grails request goes asynchronous Spring's interceptor only unbinds the default session when asynchronous handling starts, and closes it from the interceptors it registers for a Callable or DeferredResult. A promise or startAsync() in a Grails controller starts asynchronous handling without them, so the session was left open. When Grails started it (GrailsApplicationAttributes.ASYNC_STARTED) and the interceptor opened the session, unbind and close it, as for the other datasources. A Callable or DeferredResult still leaves it to Spring. --- .../GrailsOpenSessionInViewInterceptor.java | 40 ++++++++++++++++-- .../support/MultiDataSourceSessionSpec.groovy | 41 +++++++++++++++++++ 2 files changed, 78 insertions(+), 3 deletions(-) diff --git a/grails-data-hibernate5/grails-plugin/src/main/groovy/org/grails/plugin/hibernate/support/GrailsOpenSessionInViewInterceptor.java b/grails-data-hibernate5/grails-plugin/src/main/groovy/org/grails/plugin/hibernate/support/GrailsOpenSessionInViewInterceptor.java index 6b1a4a1bf90..a41c985d959 100644 --- a/grails-data-hibernate5/grails-plugin/src/main/groovy/org/grails/plugin/hibernate/support/GrailsOpenSessionInViewInterceptor.java +++ b/grails-data-hibernate5/grails-plugin/src/main/groovy/org/grails/plugin/hibernate/support/GrailsOpenSessionInViewInterceptor.java @@ -56,6 +56,9 @@ */ public class GrailsOpenSessionInViewInterceptor extends OpenSessionInViewInterceptor { + /** The value of {@code GrailsApplicationAttributes.ASYNC_STARTED}, which is in a module this plugin does not use. */ + private static final String GRAILS_ASYNC_STARTED = "org.grails.ASYNC_STARTED"; + protected FlushMode hibernateFlushMode = FlushMode.MANUAL; private final List additionalSessionFactories = new ArrayList<>(); @@ -180,8 +183,12 @@ public void afterCompletion(WebRequest request, Exception ex) throws DataAccessE /** * Called instead of {@link #postHandle} and {@link #afterCompletion} on the thread that started asynchronous - * handling. The parent class unbinds the default session; the sessions this interceptor opened for the other - * datasources are closed here, so that the thread goes back to its pool without them. + * handling. The sessions this interceptor opened for the other datasources are closed here, so that the thread + * goes back to its pool without them. + * + *

The parent class only unbinds the default session, and leaves closing it to the interceptors it registers for + * a {@code Callable} or {@code DeferredResult}. A promise or {@code startAsync()} in a Grails controller starts + * asynchronous handling without them, so the default session this interceptor opened is closed here too. */ @Override public void afterConcurrentHandlingStarted(WebRequest request) { @@ -189,10 +196,37 @@ public void afterConcurrentHandlingStarted(WebRequest request) { closeAdditionalSessions(request); } finally { - super.afterConcurrentHandlingStarted(request); + if (isGrailsAsync(request) && !isParticipating(request) && + TransactionSynchronizationManager.hasResource(getSessionFactory())) { + SessionHolder sessionHolder = + (SessionHolder) TransactionSynchronizationManager.unbindResource(getSessionFactory()); + if (logger.isDebugEnabled()) { + logger.debug("Closing Hibernate Session in OpenSessionInViewInterceptor as asynchronous handling starts"); + } + SessionFactoryUtils.closeSession(sessionHolder.getSession()); + } + else { + super.afterConcurrentHandlingStarted(request); + } } } + /** + * @return whether Grails started asynchronous handling, for a promise or {@code startAsync()}: both set + * {@code GrailsApplicationAttributes.ASYNC_STARTED} + */ + private static boolean isGrailsAsync(WebRequest request) { + return request.getAttribute(GRAILS_ASYNC_STARTED, WebRequest.SCOPE_REQUEST) != null; + } + + /** + * @return whether the default session was already bound when the request started, so that it belongs to + * whoever bound it + */ + private boolean isParticipating(WebRequest request) { + return request.getAttribute(getParticipateAttributeName(), WebRequest.SCOPE_REQUEST) != null; + } + private void closeAdditionalSessions(WebRequest request) { for (int i = additionalSessionFactories.size() - 1; i >= 0; i--) { AdditionalSessionFactoryConfig config = additionalSessionFactories.get(i); diff --git a/grails-data-hibernate5/grails-plugin/src/test/groovy/org/grails/plugin/hibernate/support/MultiDataSourceSessionSpec.groovy b/grails-data-hibernate5/grails-plugin/src/test/groovy/org/grails/plugin/hibernate/support/MultiDataSourceSessionSpec.groovy index e6531896e6f..45c7c741c1d 100644 --- a/grails-data-hibernate5/grails-plugin/src/test/groovy/org/grails/plugin/hibernate/support/MultiDataSourceSessionSpec.groovy +++ b/grails-data-hibernate5/grails-plugin/src/test/groovy/org/grails/plugin/hibernate/support/MultiDataSourceSessionSpec.groovy @@ -213,6 +213,47 @@ class MultiDataSourceSessionSpec extends Specification { defaultSession?.close() } + def "a request that Grails makes asynchronous closes its default session too"() { + given: "the request has opened its sessions" + def interceptor = new GrailsOpenSessionInViewInterceptor() + interceptor.setHibernateDatastore(datastore) + WebRequest webRequest = webRequest() + interceptor.preHandle(webRequest) + Session defaultSession = boundSession(datastore.sessionFactory) + + when: "the action returns a promise or calls startAsync(), which Spring's asynchronous interceptors never see" + webRequest.setAttribute('org.grails.ASYNC_STARTED', true, WebRequest.SCOPE_REQUEST) + interceptor.afterConcurrentHandlingStarted(webRequest) + + then: "the default session is unbound and closed" + !TransactionSynchronizationManager.hasResource(datastore.sessionFactory) + !defaultSession.open + + cleanup: + if (defaultSession?.open) { + defaultSession.close() + } + } + + def "a request that Spring makes asynchronous leaves its default session open for Spring to close"() { + given: "the request has opened its sessions" + def interceptor = new GrailsOpenSessionInViewInterceptor() + interceptor.setHibernateDatastore(datastore) + WebRequest webRequest = webRequest() + interceptor.preHandle(webRequest) + Session defaultSession = boundSession(datastore.sessionFactory) + + when: "the action returns a Callable or DeferredResult, whose processing rebinds the session and closes it" + interceptor.afterConcurrentHandlingStarted(webRequest) + + then: "the default session is unbound, and left open" + !TransactionSynchronizationManager.hasResource(datastore.sessionFactory) + defaultSession.open + + cleanup: + defaultSession?.close() + } + def "a session of another datasource bound before the request stays bound and open when the request goes asynchronous"() { given: "a pre-bound session for the secondary datasource" def interceptor = new GrailsOpenSessionInViewInterceptor() From fd34e1652686aebc46b055ce5eb8f5ae9d37ad7e Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Thu, 19 Nov 2026 15:34:40 -0800 Subject: [PATCH 098/115] fix(hibernate5): flush the other datasources' sessions at the asynchronous handoff when Hibernate allows it postHandle flushes the sessions of the other datasources unless their flush mode is MANUAL or COMMIT. When a request went asynchronous before this fix, they stayed bound and the next request's postHandle flushed them, which with hibernate.allow_update_outside_transaction wrote an action's pending update. Closing them without a flush lost it. Flush them first when postHandle would and Hibernate allows the flush: inside a transaction, or with updates outside one allowed. Under the default setting Hibernate refuses that flush, so none is attempted, and MANUAL and COMMIT still flush nothing. --- .../GrailsOpenSessionInViewInterceptor.java | 44 ++++++++- .../support/MultiDataSourceSessionSpec.groovy | 90 +++++++++++++++++++ 2 files changed, 130 insertions(+), 4 deletions(-) diff --git a/grails-data-hibernate5/grails-plugin/src/main/groovy/org/grails/plugin/hibernate/support/GrailsOpenSessionInViewInterceptor.java b/grails-data-hibernate5/grails-plugin/src/main/groovy/org/grails/plugin/hibernate/support/GrailsOpenSessionInViewInterceptor.java index a41c985d959..575475c8e29 100644 --- a/grails-data-hibernate5/grails-plugin/src/main/groovy/org/grails/plugin/hibernate/support/GrailsOpenSessionInViewInterceptor.java +++ b/grails-data-hibernate5/grails-plugin/src/main/groovy/org/grails/plugin/hibernate/support/GrailsOpenSessionInViewInterceptor.java @@ -24,6 +24,7 @@ import org.hibernate.FlushMode; import org.hibernate.Session; import org.hibernate.SessionFactory; +import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.springframework.dao.DataAccessException; import org.springframework.dao.DataAccessResourceFailureException; @@ -174,7 +175,7 @@ public void postHandle(WebRequest request, ModelMap model) throws DataAccessExce @Override public void afterCompletion(WebRequest request, Exception ex) throws DataAccessException { try { - closeAdditionalSessions(request); + closeAdditionalSessions(request, false); } finally { super.afterCompletion(request, ex); @@ -184,7 +185,8 @@ public void afterCompletion(WebRequest request, Exception ex) throws DataAccessE /** * Called instead of {@link #postHandle} and {@link #afterCompletion} on the thread that started asynchronous * handling. The sessions this interceptor opened for the other datasources are closed here, so that the thread - * goes back to its pool without them. + * goes back to its pool without them. Each is flushed first when {@link #postHandle} would flush it and Hibernate + * allows the flush: inside a transaction, or with {@code hibernate.allow_update_outside_transaction}. * *

The parent class only unbinds the default session, and leaves closing it to the interceptors it registers for * a {@code Callable} or {@code DeferredResult}. A promise or {@code startAsync()} in a Grails controller starts @@ -193,7 +195,7 @@ public void afterCompletion(WebRequest request, Exception ex) throws DataAccessE @Override public void afterConcurrentHandlingStarted(WebRequest request) { try { - closeAdditionalSessions(request); + closeAdditionalSessions(request, true); } finally { if (isGrailsAsync(request) && !isParticipating(request) && @@ -227,7 +229,8 @@ private boolean isParticipating(WebRequest request) { return request.getAttribute(getParticipateAttributeName(), WebRequest.SCOPE_REQUEST) != null; } - private void closeAdditionalSessions(WebRequest request) { + private void closeAdditionalSessions(WebRequest request, boolean flushFirst) { + RuntimeException firstFlushException = null; for (int i = additionalSessionFactories.size() - 1; i >= 0; i--) { AdditionalSessionFactoryConfig config = additionalSessionFactories.get(i); SessionFactory sf = config.sessionFactory; @@ -237,6 +240,22 @@ private void closeAdditionalSessions(WebRequest request) { SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sf); if (sessionHolder != null) { Session session = sessionHolder.getSession(); + if (flushFirst && isFlushAllowed(session)) { + try { + if (logger.isDebugEnabled()) { + logger.debug("Flushing additional Hibernate session for datasource '" + config.connectionName + "' as asynchronous handling starts"); + } + session.flush(); + } + catch (RuntimeException flushEx) { + if (firstFlushException == null) { + firstFlushException = flushEx; + } + else { + firstFlushException.addSuppressed(flushEx); + } + } + } TransactionSynchronizationManager.unbindResource(sf); if (logger.isDebugEnabled()) { logger.debug("Closing additional Hibernate Session for datasource '" + config.connectionName + "' in OpenSessionInViewInterceptor"); @@ -249,6 +268,23 @@ private void closeAdditionalSessions(WebRequest request) { } } } + if (firstFlushException != null) { + throw firstFlushException; + } + } + + /** + * @return whether {@link #postHandle} would flush the session, and Hibernate would let it: a session it would not + * flush (MANUAL or COMMIT) is not flushed here either, and one outside a transaction only when + * {@code hibernate.allow_update_outside_transaction} is set, since Hibernate refuses that flush otherwise + */ + private static boolean isFlushAllowed(Session session) { + FlushMode flushMode = session.getHibernateFlushMode(); + if (flushMode == FlushMode.MANUAL || flushMode == FlushMode.COMMIT) { + return false; + } + return ((SharedSessionContractImplementor) session).isTransactionInProgress() || + session.getSessionFactory().getSessionFactoryOptions().isAllowOutOfTransactionUpdateOperations(); } private static void incrementParticipateCount(WebRequest request, SessionFactory sf) { diff --git a/grails-data-hibernate5/grails-plugin/src/test/groovy/org/grails/plugin/hibernate/support/MultiDataSourceSessionSpec.groovy b/grails-data-hibernate5/grails-plugin/src/test/groovy/org/grails/plugin/hibernate/support/MultiDataSourceSessionSpec.groovy index 45c7c741c1d..4b75a7ff296 100644 --- a/grails-data-hibernate5/grails-plugin/src/test/groovy/org/grails/plugin/hibernate/support/MultiDataSourceSessionSpec.groovy +++ b/grails-data-hibernate5/grails-plugin/src/test/groovy/org/grails/plugin/hibernate/support/MultiDataSourceSessionSpec.groovy @@ -213,6 +213,68 @@ class MultiDataSourceSessionSpec extends Specification { defaultSession?.close() } + def "a request that goes asynchronous flushes another datasource's session under AUTO when updates outside a transaction are allowed"() { + given: "a datastore that flushes in AUTO mode and allows updates outside a transaction, with a book on its secondary datasource" + HibernateDatastore autoFlush = new HibernateDatastore(DatastoreUtils.createPropertyResolver(config + [ + 'hibernate.flush.mode': 'AUTO', + 'hibernate.allow_update_outside_transaction': 'true', + 'dataSource.url': 'jdbc:h2:mem:outsideTxDefaultDb;LOCK_TIMEOUT=10000', + 'dataSources.secondary': [url: 'jdbc:h2:mem:outsideTxSecondaryDb;LOCK_TIMEOUT=10000'], + ]), OutsideTransactionBook) + SessionFactory secondarySf = autoFlush.getDatastoreForConnection('secondary').sessionFactory + Serializable bookId = OutsideTransactionBook.withNewTransaction { new OutsideTransactionBook(title: 'Before').save(flush: true).id } + + and: "a request that changes it without a flush" + def interceptor = new GrailsOpenSessionInViewInterceptor() + interceptor.setHibernateDatastore(autoFlush) + WebRequest webRequest = webRequest() + interceptor.preHandle(webRequest) + Session secondarySession = boundSession(secondarySf) + secondarySession.get(OutsideTransactionBook, bookId).title = 'After' + + when: "the request goes asynchronous" + interceptor.afterConcurrentHandlingStarted(webRequest) + + then: "the change is written, as postHandle would write it, and the session is closed" + titleOf(secondarySf, OutsideTransactionBook, bookId) == 'After' + !secondarySession.open + !TransactionSynchronizationManager.hasResource(secondarySf) + + cleanup: + autoFlush?.close() + } + + def "a request that goes asynchronous does not flush another datasource's session under AUTO when Hibernate would refuse it"() { + given: "a datastore that flushes in AUTO mode with the default setting, which refuses a flush outside a transaction" + HibernateDatastore autoFlush = new HibernateDatastore(DatastoreUtils.createPropertyResolver(config + [ + 'hibernate.flush.mode': 'AUTO', + 'dataSource.url': 'jdbc:h2:mem:insideTxDefaultDb;LOCK_TIMEOUT=10000', + 'dataSources.secondary': [url: 'jdbc:h2:mem:insideTxSecondaryDb;LOCK_TIMEOUT=10000'], + ]), InsideTransactionBook) + SessionFactory secondarySf = autoFlush.getDatastoreForConnection('secondary').sessionFactory + Serializable bookId = InsideTransactionBook.withNewTransaction { new InsideTransactionBook(title: 'Before').save(flush: true).id } + + and: "a request that changes it without a flush" + def interceptor = new GrailsOpenSessionInViewInterceptor() + interceptor.setHibernateDatastore(autoFlush) + WebRequest webRequest = webRequest() + interceptor.preHandle(webRequest) + Session secondarySession = boundSession(secondarySf) + secondarySession.get(InsideTransactionBook, bookId).title = 'After' + + when: "the request goes asynchronous" + interceptor.afterConcurrentHandlingStarted(webRequest) + + then: "nothing is flushed or thrown, and the session is closed" + notThrown(Exception) + titleOf(secondarySf, InsideTransactionBook, bookId) == 'Before' + !secondarySession.open + !TransactionSynchronizationManager.hasResource(secondarySf) + + cleanup: + autoFlush?.close() + } + def "a request that Grails makes asynchronous closes its default session too"() { given: "the request has opened its sessions" def interceptor = new GrailsOpenSessionInViewInterceptor() @@ -346,6 +408,16 @@ class MultiDataSourceSessionSpec extends Specification { interceptor.afterCompletion(webRequest, null) } + private static String titleOf(SessionFactory sessionFactory, Class type, Serializable id) { + Session session = sessionFactory.openSession() + try { + session.get(type, id).title + } + finally { + session.close() + } + } + private WebRequest webRequest() { Map attributes = [:] Stub(WebRequest) { @@ -375,3 +447,21 @@ class OsivBook { class OsivAuthor { String name } + +@Entity +class OutsideTransactionBook { + String title + + static mapping = { + datasource 'secondary' + } +} + +@Entity +class InsideTransactionBook { + String title + + static mapping = { + datasource 'secondary' + } +} From 5065b39331655a2f37c4cf19c1b9dd36f77e4f6b Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Thu, 19 Nov 2026 16:24:46 -0800 Subject: [PATCH 099/115] Link What's New to the content negotiation upgrade notes by anchor What's New still named section 26 for the upgrade notes of grails.mime.negotiation.spring, which 9.0.x has renumbered twice since; they are now section 31. It links them by their anchor, upgradingSpringFormatNegotiation, so a renumbering cannot leave the reference stale again. The other sections the sentence lists moved by two when 9.0.x added the Spring Boot 4.2 and graphql-java 26 notes as sections 2 and 3, so they are now 6 to 13 and 15 to 18. --- grails-doc/src/en/guide/introduction/whatsNew.adoc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/grails-doc/src/en/guide/introduction/whatsNew.adoc b/grails-doc/src/en/guide/introduction/whatsNew.adoc index 9de63e5dc98..592aec42553 100644 --- a/grails-doc/src/en/guide/introduction/whatsNew.adoc +++ b/grails-doc/src/en/guide/introduction/whatsNew.adoc @@ -173,7 +173,8 @@ With `grails.mime.negotiation.spring: true`, which Grails 10 makes the default, `grails.mime` settings of the `Accept` header decide what Spring MVC writes for an action too, such as a `ResponseEntity` it returns. See <>. -See xref:upgrading#upgrading90x[Upgrading from Grails 8 to Grails 9], sections 4 to 11, 13 to 16 and 26. +See xref:upgrading#upgrading90x[Upgrading from Grails 8 to Grails 9], sections 6 to 13 and 15 to 18, and +<>. ==== Hibernate 7 Criteria Queries Can Be Extended From b094af1d8ae74579b245baa6dcb17bf93343b294 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Thu, 19 Nov 2026 16:36:22 -0800 Subject: [PATCH 100/115] HTML-encode the attribute values that GSP tags write g:form, g:formActionSubmit, g:checkBox, g:actionSubmit, g:actionSubmitImage, g:datePicker, g:radio, g:radioGroup, g:select (the noSelection key), g:field (type), g:withTag, g:external, g:paginate and g:renderException wrote some attribute values as they were given. They now encode them with the HTML encoder that the tags already use for their other attributes. A value encoded with encodeAsHTML() before it reaches the tag is written once: the encoder skips a value it has already encoded, and the name of the checkbox's hidden field and the action in a submit button's name are built from the encoded value. The URL of g:form and g:formActionSubmit and the class of g:paginate are encoded after the tag builds them, as the href of g:link is. The upgrade guide describes the change. --- .../src/en/guide/upgrading/upgrading60x.adoc | 20 ++ .../plugins/web/taglib/RenderTagLib.groovy | 4 +- .../web/taglib/ApplicationTagLib.groovy | 4 +- .../plugins/web/taglib/FormTagLib.groovy | 102 ++++++----- .../web/taglib/UrlMappingTagLib.groovy | 3 +- .../taglib/TagAttributeEncodingSpec.groovy | 173 ++++++++++++++++++ .../errors/ErrorsViewStackTracePrinter.groovy | 8 +- 7 files changed, 261 insertions(+), 53 deletions(-) create mode 100644 grails-gsp/plugin/src/test/groovy/org/grails/web/taglib/TagAttributeEncodingSpec.groovy diff --git a/grails-doc/src/en/guide/upgrading/upgrading60x.adoc b/grails-doc/src/en/guide/upgrading/upgrading60x.adoc index a48f49dbaf4..cf26bd93f5c 100644 --- a/grails-doc/src/en/guide/upgrading/upgrading60x.adoc +++ b/grails-doc/src/en/guide/upgrading/upgrading60x.adoc @@ -1208,3 +1208,23 @@ As of Grails 7.0.18, the list of bindable properties that Grails generates for a * Request data bound into an association that was loaded lazily, as `book.properties = params` binds the nested `author` parameters into the book's author, is bound by the list generated for the association's class. Previously an association that was still a proxy was bound without restriction. * A class that gets the list now carries it as a new `public static final` field. A class that implements `Serializable` without declaring a `serialVersionUID`, such as a `Validateable` object kept in a persisted HTTP session or in a distributed cache, therefore gets a new default `serialVersionUID`, and instances serialized before the upgrade do not deserialize after it. Declare a `serialVersionUID` on such a class. * A controller compiled against a command object that was compiled without the list, by an earlier version of Grails or without the data binding library, warns that the command object is bound without restriction and should be recompiled. + +==== 24. More GSP tags HTML-encode the attribute values they write + +These tags now HTML-encode the values they write into attributes, as `` and `` already did and as these tags already encoded their other attributes: + +* `` and ``: the URL in `action` and `formaction`, and the hidden fields that a `RequestDataValueProcessor` adds to a form. +* ``: `name`, `value`, `form`, and the `id` taken from the name, on the checkbox and on its hidden field. +* `` and ``: `value`, the `action` in the button's name, and `src`. +* ``: `name`, `id` and `selectDateClass`, on the labels, the hidden field and each select. +* `` and ``: `name`, and on `` the `id` taken from it. +* ``: the key of `noSelection`. +* ``: `type`. +* ``: the values of `attrs` that are not closures. +* ``: `uri`. +* ``: `class`, on the current step. +* ``: `detailsClass`, `stackClass`, `filenameClass`, `snippetClass`, `lineClass`, `lineNumberClass` and `lineErrorClass`. + +The browser decodes the attribute, so a value without a character reference reaches it unchanged. The markup changes, though: an `&` in the URL of a form is written as `+&+`, as `` writes it in an `href`. + +A value encoded with `encodeAsHTML()` before it reaches the tag is written once, not encoded again. There are two exceptions. The tag builds the URL of `` and `` from its attributes, and joins the class of `` with `currentStep`, and only then encodes the result, so a value encoded beforehand is encoded twice there, as it already was in the `href` of ``. Pass those values unencoded. A character reference written in the page, as in `+name="a&b"+`, is encoded as well: write the character itself. diff --git a/grails-gsp/grails-web-gsp-taglib/src/main/groovy/org/grails/plugins/web/taglib/RenderTagLib.groovy b/grails-gsp/grails-web-gsp-taglib/src/main/groovy/org/grails/plugins/web/taglib/RenderTagLib.groovy index cf0f3bca8ae..5594804c606 100644 --- a/grails-gsp/grails-web-gsp-taglib/src/main/groovy/org/grails/plugins/web/taglib/RenderTagLib.groovy +++ b/grails-gsp/grails-web-gsp-taglib/src/main/groovy/org/grails/plugins/web/taglib/RenderTagLib.groovy @@ -92,7 +92,7 @@ class RenderTagLib implements TagLibrary { def currentOut = out int statusCode = request.getAttribute('jakarta.servlet.error.status_code') as int currentOut << """

Error ${prettyPrintStatus(statusCode)}

-
+
URI
${htmlEncoder.encode(WebUtils.getForwardURI(request) ?: request.getAttribute('jakarta.servlet.error.request_uri'))}
""" @@ -109,7 +109,7 @@ class RenderTagLib implements TagLibrary { def trace = errorsViewStackTracePrinter.prettyPrint(exception.cause ?: exception, attrs) if (StringUtils.hasText(trace.trim())) { currentOut << '

Trace

' - currentOut << """
"""
+            currentOut << """
"""
             currentOut << htmlEncoder.encode(trace)
             currentOut << '
' } diff --git a/grails-gsp/plugin/src/main/groovy/org/grails/plugins/web/taglib/ApplicationTagLib.groovy b/grails-gsp/plugin/src/main/groovy/org/grails/plugins/web/taglib/ApplicationTagLib.groovy index 96844c286e2..ee91fa204c7 100644 --- a/grails-gsp/plugin/src/main/groovy/org/grails/plugins/web/taglib/ApplicationTagLib.groovy +++ b/grails-gsp/plugin/src/main/groovy/org/grails/plugins/web/taglib/ApplicationTagLib.groovy @@ -338,7 +338,7 @@ class ApplicationTagLib implements ApplicationContextAware, InitializingBean, Gr // Allow attrs to overwrite any constants attrs.each { typeInfo.remove(it.key) } - out << writer(processedUrl(uri, request), typeInfo, attrs) + out << writer(processedUrl(uri, request).encodeAsHTML(), typeInfo, attrs) out << '\r\n' } @@ -414,7 +414,7 @@ class ApplicationTagLib implements ApplicationContextAware, InitializingBean, Gr writer << '"' } else { - writer << " $k=\"$v\"" + writer << " $k=\"${v.encodeAsHTML()}\"" } } writer << '>' diff --git a/grails-gsp/plugin/src/main/groovy/org/grails/plugins/web/taglib/FormTagLib.groovy b/grails-gsp/plugin/src/main/groovy/org/grails/plugins/web/taglib/FormTagLib.groovy index d88e8209db4..e16e979e26d 100644 --- a/grails-gsp/plugin/src/main/groovy/org/grails/plugins/web/taglib/FormTagLib.groovy +++ b/grails-gsp/plugin/src/main/groovy/org/grails/plugins/web/taglib/FormTagLib.groovy @@ -179,7 +179,7 @@ class FormTagLib implements ApplicationContextAware, InitializingBean, TagLibrar attrs.value = processFormFieldValueIfNecessary(attrs.name, attrs.value, attrs.type) - out << "' } @@ -237,25 +237,28 @@ class FormTagLib implements ApplicationContextAware, InitializingBean, TagLibrar value = processFormFieldValueIfNecessary(name, value, 'checkbox') hiddenValue = processFormFieldValueIfNecessary("_${name}", hiddenValue, 'hidden') + // The hidden field name is built from the encoded name, so that a name encoded before it reached the tag + // is not encoded a second time in one of the two fields. The encoded name holds no other dots than the name. + String htmlName = encodeAttributeValue(name).toString() def hiddenFieldName - if (name.indexOf('.') == -1) { - hiddenFieldName = "_${name}" + if (htmlName.indexOf('.') == -1) { + hiddenFieldName = "_${htmlName}" } else { - def lastDot = name.lastIndexOf('.') - hiddenFieldName = name[0..lastDot] + '_' - if (lastDot + 1 != name.length()) { - hiddenFieldName += name[(lastDot + 1)..-1] + def lastDot = htmlName.lastIndexOf('.') + hiddenFieldName = htmlName[0..lastDot] + '_' + if (lastDot + 1 != htmlName.length()) { + hiddenFieldName += htmlName[(lastDot + 1)..-1] } } out << " writer << "\n" } + hiddenFields.each { key, value -> writer << "\n" } } /** @@ -546,7 +558,7 @@ class FormTagLib implements ApplicationContextAware, InitializingBean, TagLibrar value = processFormFieldValueIfNecessary("_action_${action}", value, 'submit') booleanToAttribute(attrs, 'disabled') - out << "Day") - out.println("") - out.println("") - out.println("") - out.println("") + def htmlName = encodeAttributeValue(name) + def htmlId = encodeAttributeValue(id) + out.println("") + out.println("") + out.println("") + out.println("") + out.println("") // Change this hidden to use requestDataValueProcessor def dateStructValue = processFormFieldValueIfNecessary("${name}", 'date.struct', 'hidden') - out.println("") + out.println("") // create day select if (precision >= PRECISION_RANKINGS['day']) { - out.println("') } // create month select if (precision >= PRECISION_RANKINGS['month']) { - out.println("') @@ -855,7 +869,7 @@ class FormTagLib implements ApplicationContextAware, InitializingBean, TagLibrar // create year select if (precision >= PRECISION_RANKINGS['year']) { - out.println("') } // do hour select if (precision >= PRECISION_RANKINGS['hour']) { - out.println(" :') @@ -916,7 +930,7 @@ class FormTagLib implements ApplicationContextAware, InitializingBean, TagLibrar // do minute select if (precision >= PRECISION_RANKINGS['minute']) { - out.println("') } @@ -949,7 +963,7 @@ class FormTagLib implements ApplicationContextAware, InitializingBean, TagLibrar def renderNoSelectionOptionImpl(out, noSelectionKey, noSelectionValue, value) { // If a label for the '--Please choose--' first item is supplied, write it out - out << "" + out << "" } /** @@ -1269,9 +1283,9 @@ class FormTagLib implements ApplicationContextAware, InitializingBean, TagLibrar def checked = attrs.remove('checked') ? true : false value = processFormFieldValueIfNecessary(name, "${value?.toString()?.encodeAsHTML()}", 'radio') - out << " def it = new Expando() def radioWriter = new FastStringWriter() - radioWriter << " if (currentstep == i) { - writer << "${i}" + writer << "${i}" } else { linkParams.offset = (i - 1) * max diff --git a/grails-gsp/plugin/src/test/groovy/org/grails/web/taglib/TagAttributeEncodingSpec.groovy b/grails-gsp/plugin/src/test/groovy/org/grails/web/taglib/TagAttributeEncodingSpec.groovy new file mode 100644 index 00000000000..12f6d56d7c9 --- /dev/null +++ b/grails-gsp/plugin/src/test/groovy/org/grails/web/taglib/TagAttributeEncodingSpec.groovy @@ -0,0 +1,173 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.web.taglib + +import jakarta.servlet.http.HttpServletRequest + +import org.springframework.core.io.FileSystemResource +import org.springframework.web.servlet.support.RequestDataValueProcessor +import spock.lang.Specification + +import grails.testing.web.taglib.TagLibUnitTest +import org.grails.core.io.StaticResourceLocator +import org.grails.plugins.web.DefaultGrailsTagDateHelper +import org.grails.plugins.web.taglib.ApplicationTagLib +import org.grails.plugins.web.taglib.FormTagLib +import org.grails.plugins.web.taglib.RenderTagLib +import org.grails.plugins.web.taglib.UrlMappingTagLib +import org.grails.web.errors.ErrorsViewStackTracePrinter + +class TagAttributeEncodingSpec extends Specification implements TagLibUnitTest { + + private static final String VALUE = 'a"b' || '
' || '' || '' || '' || '' || '' || '' || '' || ' value="' + ENCODED + '" ' + 'g:checkBox' | 'form' | '' || '' + 'g:actionSubmit' | 'value' | '' || '' || '' || '' || '' || '' || ' class="' + ENCODED + '">' + 'g:select' | 'noSelection key' | '' || '' + 'g:radio' | 'name' | '' || '${it.radio}' || '' + 'g:field' | 'type' | '' || 'x' || 'x' + 'g:external' | 'uri of a script' | '' || '