From 4923f74168f27cedea6dd29faa4de1ea6b58c92a Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Fri, 20 Nov 2026 16:50:16 -0800 Subject: [PATCH 1/3] Resolve Grails formats with Spring MVC's content negotiation by default grails.mime.negotiation.spring now defaults to true, as Grails 9 announced (#16698). Settings.MIME_NEGOTIATION_SPRING_DEFAULT and the configuration metadata say true, and every reader of the setting follows the constant. The URL mappings plugin no longer turns a missing configuration into false: it falls back to the default. The specs that pin the Grails rules set the setting to false, which keeps them: RequestAndResponseMimeTypesApiSpec, GrailsFormatNegotiationRespondSpec, ProblemDetailContentNegotiationSpec, and a new XmlProblemDetailGrailsRulesSpec for the lenient reading of an Accept header Spring cannot read, which GrailsFormatNegotiationRespondSpec now covers for a map too. SpringFormatNegotiationSpec and UrlMappingsGrailsPluginSpec state the new default, and ControllerJsonSerializationSpec expects a map to be answered 406 for an Accept header Spring cannot read. --- .../main/groovy/grails/config/Settings.groovy | 6 +-- .../mime/SpringFormatNegotiationSpec.groovy | 20 ++++++--- .../RequestAndResponseMimeTypesApiSpec.groovy | 3 +- .../contentneg/SpringWrittenController.groovy | 40 ----------------- .../GrailsFormatNegotiationSpec.groovy | 0 .../ControllerJsonSerializationSpec.groovy | 4 +- .../SpringFormatNegotiationRespondSpec.groovy | 19 +++++++- .../mapping/UrlMappingsGrailsPlugin.groovy | 6 ++- .../UrlMappingsGrailsPluginSpec.groovy | 4 +- ...itional-spring-configuration-metadata.json | 4 +- ...ProblemDetailContentNegotiationSpec.groovy | 7 ++- .../plugins/xml/XmlProblemDetailSpec.groovy | 43 ++++++++++++------- 12 files changed, 80 insertions(+), 76 deletions(-) delete mode 100644 grails-test-examples/app1/grails-app/controllers/functionaltests/contentneg/SpringWrittenController.groovy rename grails-test-examples/{app1/src/integration-test/groovy/functionaltests/contentneg => spring-format-negotiation/src/integration-test/groovy/springformatnegotiation}/GrailsFormatNegotiationSpec.groovy (100%) diff --git a/grails-core/src/main/groovy/grails/config/Settings.groovy b/grails-core/src/main/groovy/grails/config/Settings.groovy index 5cc2daa272e..ecd7494fbe7 100644 --- a/grails-core/src/main/groovy/grails/config/Settings.groovy +++ b/grails-core/src/main/groovy/grails/config/Settings.groovy @@ -223,12 +223,12 @@ interface Settings { 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 9, - * and {@code true} from Grails 10. + * The default of {@link #MIME_NEGOTIATION_SPRING}, as a property placeholder reads it: {@code true} from Grails 10, + * and {@code false} in Grails 9. Set the setting to {@code false} to keep Grails' own rules. * * @since 9.0 */ - String MIME_NEGOTIATION_SPRING_DEFAULT = 'false' + String MIME_NEGOTIATION_SPRING_DEFAULT = 'true' /** * The default scope for controllers 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 bd5ed8518f5..5eafc3335ea 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 @@ -97,14 +97,24 @@ class SpringFormatNegotiationSpec extends Specification { 'built for the request in a context without one' | false } - void "without the setting, Spring MVC's manager is left as it is"() { + void "with the setting false, Spring MVC's manager is left as it is"() { when: - start([:]) + start((Settings.MIME_NEGOTIATION_SPRING): false) then: manager().getClass() == ContentNegotiationManager } + void "without the setting, Spring MVC's manager resolves the Grails formats, as Grails 10 does by default"() { + when: + start([:]) + request('Accept': '*/*, application/json') + + then: + manager() instanceof GrailsContentNegotiationManager + GrailsWebRequest.lookup().currentResponse.format == 'json' + } + 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) @@ -429,7 +439,7 @@ class SpringFormatNegotiationSpec extends Specification { 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"() { + void "Spring MVC's parameter negotiation of the format parameter still answers #format for an endpoint, as it does with the setting false"() { given: start((Settings.MIME_NEGOTIATION_SPRING): true, (ParameterConfiguration.ENABLED): true, (ParameterConfiguration.NAME): 'format') def request = request('Accept': 'application/json') @@ -547,9 +557,9 @@ class SpringFormatNegotiationSpec extends Specification { true | false || 'all' } - void 'without the setting, the Grails 9 rules resolve the formats'() { + void 'with the setting false, the Grails 9 rules resolve the formats'() { given: - start([:]) + start((Settings.MIME_NEGOTIATION_SPRING): false) request('Accept': '*/*, application/json') expect: 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 9534626371a..36a048afab4 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 @@ -44,7 +44,7 @@ import spock.lang.Specification import spock.lang.Unroll /** - * Tests for mime type resolution + * Tests for mime type resolution by the Grails rules, which {@code grails.mime.negotiation.spring: false} keeps. */ class RequestAndResponseMimeTypesApiSpec extends Specification{ def responseMimeTypesApiInstance @@ -557,6 +557,7 @@ class RequestAndResponseMimeTypesApiSpec extends Specification{ String applicationConfigText = ''' grails.mime.file.extensions = true // enables the parsing of file extensions from URLs into the request format grails.mime.use.accept.header = true +grails.mime.negotiation.spring = false // these features pin the Grails rules grails.mime.types = [ all: '*/*', html: ['text/html','application/xhtml+xml'], 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 deleted file mode 100644 index 9dd265d1850..00000000000 --- a/grails-test-examples/app1/grails-app/controllers/functionaltests/contentneg/SpringWrittenController.groovy +++ /dev/null @@ -1,40 +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 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 - * the Grails rules negotiate without {@code grails.mime.negotiation.spring}. - */ -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/src/integration-test/groovy/functionaltests/contentneg/GrailsFormatNegotiationSpec.groovy b/grails-test-examples/spring-format-negotiation/src/integration-test/groovy/springformatnegotiation/GrailsFormatNegotiationSpec.groovy similarity index 100% rename from grails-test-examples/app1/src/integration-test/groovy/functionaltests/contentneg/GrailsFormatNegotiationSpec.groovy rename to grails-test-examples/spring-format-negotiation/src/integration-test/groovy/springformatnegotiation/GrailsFormatNegotiationSpec.groovy diff --git a/grails-test-suite-web/src/test/groovy/grails/rest/web/ControllerJsonSerializationSpec.groovy b/grails-test-suite-web/src/test/groovy/grails/rest/web/ControllerJsonSerializationSpec.groovy index eae9e9edb42..38d1adb6765 100644 --- a/grails-test-suite-web/src/test/groovy/grails/rest/web/ControllerJsonSerializationSpec.groovy +++ b/grails-test-suite-web/src/test/groovy/grails/rest/web/ControllerJsonSerializationSpec.groovy @@ -391,7 +391,7 @@ class ControllerJsonSerializationSpec extends Specification implements Controlle response.json.detail == 'taken' } - void 'a client whose Accept header Spring MVC cannot read is answered with the status alone for #description, and as in Grails 8 for any other value'() { + void 'a client whose Accept header Spring MVC cannot read is answered with the status alone for #description'() { given: 'a quality value above 1' response.format = null request.addHeader('Accept', 'application/json;q=2') @@ -406,7 +406,7 @@ class ControllerJsonSerializationSpec extends Specification implements Controlle where: description | value || status | body 'a problem' | org.springframework.http.ProblemDetail.forStatus(org.springframework.http.HttpStatus.CONFLICT) || 409 | '' - 'a map' | [title: 'Grails'] || 200 | '{"title":"Grails"}' + 'a map' | [title: 'Grails'] || 406 | '' } void 'a client that accepts only #accept is answered as one that asks for #format'() { 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 2dae949093f..5da6774c3df 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 @@ -233,12 +233,15 @@ class SpringFormatNegotiationParameterRespondSpec extends Specification implemen } /** - * Without {@code grails.mime.negotiation.spring}, the Grails 9 rules resolve the format {@code respond} answers in. + * With {@code grails.mime.negotiation.spring: false}, 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 } + { config -> + config[Settings.WEB_RENDERING_JSON_SPRING] = true + config[Settings.MIME_NEGOTIATION_SPRING] = false + } } void 'the request of the action a web unit test calls is not marked as handled by a URL mapping, as in Grails 9'() { @@ -287,6 +290,18 @@ class GrailsFormatNegotiationRespondSpec extends Specification implements Contro model.title == null } + void 'a client whose Accept header Spring MVC cannot read is answered as Grails 9 reads it, leniently'() { + given: 'a quality value above 1' + request.addHeader('Accept', 'application/json;q=2') + + when: + controller.respond([title: 'Grails'], formats: ['json']) + + then: + response.status == 200 + response.contentAsString == '{"title":"Grails"}' + } + void 'a client that accepts any type of a version is answered 406, as in Grails 9'() { given: request.addHeader('Accept', '*/*;v=2') 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 550a34cf3c4..620038216d1 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 @@ -134,8 +134,10 @@ class UrlMappingsGrailsPlugin extends Plugin { 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 + boolean springNegotiationDefault = Boolean.valueOf(Settings.MIME_NEGOTIATION_SPRING_DEFAULT) + Boolean springNegotiation = grailsApplication.config?.getProperty(Settings.MIME_NEGOTIATION_SPRING, Boolean, + springNegotiationDefault) + boolean markHandledRequests = springNegotiation != null ? springNegotiation : springNegotiationDefault // 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 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 8ae5d239f59..a8ac1de7ed0 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 @@ -105,7 +105,7 @@ class UrlMappingsGrailsPluginSpec extends Specification { !registry.containsBeanDefinition('urlMappingsTargetSource') } - void "the handler mapping marks the requests its mappings handle only with grails.mime.negotiation.spring: #setting"() { + void "the handler mapping marks the requests its mappings handle unless grails.mime.negotiation.spring is false: #setting"() { given: def beanFactory = new DefaultListableBeanFactory() def config = new PropertySourcesConfig(setting == null ? [:] : [(Settings.MIME_NEGOTIATION_SPRING): setting]) @@ -125,7 +125,7 @@ class UrlMappingsGrailsPluginSpec extends Specification { where: setting || marks - null || null + null || true false || null true || true } 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 c3842951ce1..2eca58440d9 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,8 +135,8 @@ { "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 10 makes it the default.", - "defaultValue": false + "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. The default from Grails 10; set it to false to keep the Grails rules.", + "defaultValue": true }, { "name": "grails.converters.encoding", diff --git a/grails-xml/src/test/groovy/org/grails/plugins/xml/ProblemDetailContentNegotiationSpec.groovy b/grails-xml/src/test/groovy/org/grails/plugins/xml/ProblemDetailContentNegotiationSpec.groovy index 881e4092135..671317d4b07 100644 --- a/grails-xml/src/test/groovy/org/grails/plugins/xml/ProblemDetailContentNegotiationSpec.groovy +++ b/grails-xml/src/test/groovy/org/grails/plugins/xml/ProblemDetailContentNegotiationSpec.groovy @@ -38,10 +38,15 @@ import spock.lang.Specification /** * A problem is written in the type Spring MVC's content negotiation selects for it, as the application configures it: a * strategy of its own, the format parameter spring.mvc.contentnegotiation.favor-parameter reads, the Accept header and a - * default content type, in the order Spring MVC asks them. + * default content type, in the order Spring MVC asks them. With {@code grails.mime.negotiation.spring: false}, which + * leaves the application's manager as it is; by default, Grails reads the format parameter in its place. */ class ProblemDetailContentNegotiationSpec extends Specification implements ControllerUnitTest { + Closure doWithConfig() {{ config -> + config.grails.mime.negotiation.spring = false + }} + @Configuration static class Negotiation { diff --git a/grails-xml/src/test/groovy/org/grails/plugins/xml/XmlProblemDetailSpec.groovy b/grails-xml/src/test/groovy/org/grails/plugins/xml/XmlProblemDetailSpec.groovy index d2b97b0b031..acf73ffb492 100644 --- a/grails-xml/src/test/groovy/org/grails/plugins/xml/XmlProblemDetailSpec.groovy +++ b/grails-xml/src/test/groovy/org/grails/plugins/xml/XmlProblemDetailSpec.groovy @@ -202,22 +202,6 @@ class XmlProblemDetailSpec extends Specification implements ControllerUnitTest

{ + + Closure doWithConfig() {{ config -> + config.grails.mime.negotiation.spring = false + }} + + void 'the page respond names for a problem renders it for a client whose Accept header Spring MVC cannot read, as Grails 8 read it'() { + given: 'the HTML page of the name, and a header Grails reads as Grails 8 read it' + views['/problemXml/conflict.gsp'] = '

${problemDetail.detail}

' + request.addHeader('Accept', 'text/html;q=2') + request.addHeader('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko)') + + when: + controller.respond(ProblemDetail.forStatusAndDetail(HttpStatus.CONFLICT, 'taken'), view: 'conflict', + formats: ['html', 'xml', 'json']) + + then: + response.status == 409 + view == 'conflict' + model.problemDetail.detail == 'taken' + } +} + @Artefact('Controller') class ProblemXmlController { } From 0be2e1849737e86cf9482e3cbf27fff76cfe81ae Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Fri, 20 Nov 2026 16:50:16 -0800 Subject: [PATCH 2/3] Run the format negotiation test app on the default The spring-format-negotiation test app no longer sets grails.mime.negotiation.spring, so its SpringFormatNegotiationSpec runs the Grails 10 default end to end. app1's GrailsFormatNegotiationSpec, which pins the Grails rules, moves to that app with @TestPropertySource(grails.mime.negotiation.spring=false), along with the end of app1's SpringWrittenController, which only it used. app1 has GORM, where a second test context breaks every later GORM spec; the format negotiation app has none. app1 now runs the default too. --- .../grails-app/conf/application.yml | 3 --- .../init/springformatnegotiation/Application.groovy | 2 +- .../GrailsFormatNegotiationSpec.groovy | 8 +++++--- .../SpringFormatNegotiationSpec.groovy | 3 ++- 4 files changed, 8 insertions(+), 8 deletions(-) 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 f451db25241..5d0f4ea4e45 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 @@ -18,9 +18,6 @@ grails: profile: web codegen: defaultPackage: springformatnegotiation - mime: - negotiation: - spring: true info: app: name: '@info.app.name@' 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 index 4d5a4e04800..d6a6e63e1f3 100644 --- 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 @@ -27,7 +27,7 @@ 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. + * {@code grails.mime.negotiation.spring} does by default. */ @CompileStatic class Application extends GrailsAutoConfiguration { diff --git a/grails-test-examples/spring-format-negotiation/src/integration-test/groovy/springformatnegotiation/GrailsFormatNegotiationSpec.groovy b/grails-test-examples/spring-format-negotiation/src/integration-test/groovy/springformatnegotiation/GrailsFormatNegotiationSpec.groovy index 26073054254..0c6696cb875 100644 --- a/grails-test-examples/spring-format-negotiation/src/integration-test/groovy/springformatnegotiation/GrailsFormatNegotiationSpec.groovy +++ b/grails-test-examples/spring-format-negotiation/src/integration-test/groovy/springformatnegotiation/GrailsFormatNegotiationSpec.groovy @@ -16,8 +16,9 @@ * 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 @@ -25,11 +26,12 @@ 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. + * With {@code grails.mime.negotiation.spring: false}, Grails resolves formats with its own rules, and Spring MVC writes + * the values of actions as the {@code Accept} header asks, as in Grails 9 by default. */ @Integration @Tag('http-client') +@TestPropertySource(properties = 'grails.mime.negotiation.spring=false') 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'() { 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 ea570a0c924..2baa8b815fd 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 @@ -25,7 +25,8 @@ import grails.testing.mixin.integration.Integration import org.apache.grails.testing.http.client.HttpClientSupport /** - * With {@code grails.mime.negotiation.spring}, which this application's configuration sets, Grails formats are resolved with Spring MVC's content negotiation, and + * With {@code grails.mime.negotiation.spring}, the default this application keeps, 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. */ From 76f842193f94cbaffa40b0409edd88f8c4752c6c Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Fri, 20 Nov 2026 16:50:16 -0800 Subject: [PATCH 3/3] Document Spring MVC content negotiation as the Grails 10 default The content negotiation section describes Spring MVC's manager as how Grails resolves formats, with false keeping the Grails rules, and notes that the Accept header rules above it are those rules. The configuration option gives the new default. The Grails 9 to 10 upgrade guide gains section 3 on the change, with the answers that differ and how to keep the Grails rules, and its announced-defaults table lists the setting as changed. The grails-upgrade skill covers the change the same way. --- .../en/guide/conf/config/builtInOptions.adoc | 2 +- .../guide/theWebLayer/contentNegotiation.adoc | 27 ++++++---- .../src/en/guide/upgrading/upgrading100x.adoc | 50 ++++++++++++++++++- .../upgrade/skills/grails-upgrade/SKILL.md | 31 ++++++++++-- 4 files changed, 94 insertions(+), 16 deletions(-) diff --git a/grails-doc/src/en/guide/conf/config/builtInOptions.adoc b/grails-doc/src/en/guide/conf/config/builtInOptions.adoc index 8d4385c2b36..d308b12a14b 100644 --- a/grails-doc/src/en/guide/conf/config/builtInOptions.adoc +++ b/grails-doc/src/en/guide/conf/config/builtInOptions.adoc @@ -35,7 +35,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; true from Grails 10). 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: true; `false` keeps the Grails rules, the Grails 9 default). 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 77317acd7f5..1c0fb6588ac 100644 --- a/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc +++ b/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc @@ -217,6 +217,12 @@ where the action responds in XML (see link:{controllersRef}respond.html[respond] +NOTE: The `Accept` header rules above are the Grails rules, which `grails.mime.negotiation.spring: false` keeps. By +default, Grails resolves the `Accept` header with Spring MVC's content negotiation manager: `*/*` is still the `all` +format, but a header that contains no configured type, or that Spring cannot read, results in no format, which +`respond` answers with `406 Not Acceptable`, and the types are taken in Spring MVC's order. See +<> for every rule that changes. + ==== Request format vs. Response format @@ -266,12 +272,12 @@ 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: +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. By default, Grails resolves `request.format`, +`response.format`, `withFormat` and the format `respond` writes in with that manager too, so that the two answer a +request alike. Set `grails.mime.negotiation.spring` to `false` to resolve the Grails formats by the Grails rules the +sections above describe, as Grails 9 does by default, and leave Spring MVC's manager as Spring MVC configures it: [source,yaml] .grails-app/conf/application.yml @@ -279,11 +285,10 @@ Grails formats with Spring MVC's manager too: grails: mime: negotiation: - spring: true + spring: false ---- -Grails {grailsMajorVersion} keeps the Grails rules by default, and Grails 10 makes the setting the default. With the -setting: +With the setting, which is `true` from Grails 10: * 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 @@ -351,7 +356,9 @@ type is configured for in the first configured format |=== <>, in the guide to -upgrading from Grails 8 to Grails 9, lists how each answer changes. +upgrading from Grails 8 to Grails 9, lists how each answer changes, and +<>, in the guide +to upgrading from Grails 9 to Grails 10, how to keep the Grails rules. ==== Testing Content Negotiation diff --git a/grails-doc/src/en/guide/upgrading/upgrading100x.adoc b/grails-doc/src/en/guide/upgrading/upgrading100x.adoc index f607600d1c9..b1de2cc6ded 100644 --- a/grails-doc/src/en/guide/upgrading/upgrading100x.adoc +++ b/grails-doc/src/en/guide/upgrading/upgrading100x.adoc @@ -21,7 +21,7 @@ under the License. This guide outlines the changes introduced in Grails 10 and the steps required to upgrade your application. -Grails 10 is in development. It does not differ from Grails 9 yet, so there is nothing to change in an application today. Each change that an application has to act on is added to this guide, and to the `grails-upgrade` agent skill, with the pull request that makes it. +Grails 10 is in development. Each change that an application has to act on is added to this guide, and to the `grails-upgrade` agent skill, with the pull request that makes it. [TIP] ==== @@ -43,7 +43,7 @@ Upgrade the application to the last Grails 9 release before moving to Grails 10, ==== 2. Defaults That Grails 9 Announced for Grails 10 -Grails 9 added settings that keep the Grails 8 behavior by default, and its upgrade guide announces which defaults Grails 10 changes. None of them has changed on Grails 10 yet. Set each one explicitly while the application is on Grails 9, so that the upgrade changes nothing unannounced: +Grails 9 added settings that keep the Grails 8 behavior by default, and its upgrade guide announces which defaults Grails 10 changes. Of them, only `grails.mime.negotiation.spring` has changed on Grails 10 yet, as <> describes. Set each one explicitly while the application is on Grails 9, so that the upgrade changes nothing unannounced: [options="header",cols="2,3,1,1"] |=== @@ -76,6 +76,52 @@ Grails 9 added settings that keep the Grails 8 behavior by default, and its upgr | The values of actions annotated `@ResponseBody`, written by Spring MVC as the body | `false` | `true` + +| `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`, changed (see <>) |=== The Grails 9 guide also announces that the web starter and `grails-web-boot` stop bringing `org.apache.grails:grails-xml` in Grails 10, and that `@Resource` then defaults to JSON only. An application that serves or reads XML declares the module itself. + +[[upgradingSpringFormatNegotiationDefault]] +==== 3. Grails Formats Are Resolved with Spring MVC Content Negotiation + +`grails.mime.negotiation.spring` is now `true` by 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 <>). An application that does not set +the setting answers some requests differently than on Grails 9, among them: + +* `Accept: */*, application/json` is the `json` format, where it was `all`, since Spring MVC takes a more specific type +ahead of a less specific one of equal quality. +* 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`, and `respond` answers +`406 Not Acceptable`, where Grails 9 answered as for a client that accepts any type. +* A `format` parameter or URL extension no type is configured for is not acceptable, where Grails 9 answered in the +first configured format. +* `/book/show/1.json` of an action that returns a `ResponseEntity` 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. + +<>, in the guide to +upgrading from Grails 8 to Grails 9, lists every kind of answer that changes. A Spring MVC endpoint, and Spring +Security's filters before a URL mapping has matched a request, negotiate as before. + +===== Required Actions + +* An application that set `grails.mime.negotiation.spring: true` on Grails 9 answers as it did, and can remove the +setting. +* An application that did not set it checks the answers above, and code that reads `response.format` or +`response.mimeType` allows for `null`. +* To keep the Grails rules, set the setting to `false`: ++ +[source,yaml] +.grails-app/conf/application.yml +---- +grails: + mime: + negotiation: + spring: false +---- diff --git a/grails-skills/upgrade/skills/grails-upgrade/SKILL.md b/grails-skills/upgrade/skills/grails-upgrade/SKILL.md index f1ae25866b5..08a2cf56d95 100644 --- a/grails-skills/upgrade/skills/grails-upgrade/SKILL.md +++ b/grails-skills/upgrade/skills/grails-upgrade/SKILL.md @@ -1,6 +1,6 @@ --- name: grails-upgrade -description: Guide for upgrading Grails applications from Grails 9.x to Grails 10 and from one Grails 10 release to a later one, covering the changes of each Grails 10 release and the defaults that Grails 9 announced for Grails 10 for Spring JSON rendering, the JSON converter, Jackson request binding, Spring MVC exception handlers, response bodies, and the grails-xml module +description: Guide for upgrading Grails applications from Grails 9.x to Grails 10 and from one Grails 10 release to a later one, covering the changes of each Grails 10 release, Grails formats resolved with Spring MVC content negotiation, and the defaults that Grails 9 announced for Grails 10 for Spring JSON rendering, the JSON converter, Jackson request binding, Spring MVC exception handlers, response bodies, and the grails-xml module license: Apache-2.0 --- @@ -26,6 +26,7 @@ Activate this skill when: - Updating a Grails 10 application to a later Grails 10 release. - Reviewing a Grails 10 upgrade branch or pull request. - Fixing tests, build failures, runtime failures, or behavior changes after moving from Grails 9.x to Grails 10. +- Fixing a request answered `406 Not Acceptable`, or in another format than on Grails 9, now that `grails.mime.negotiation.spring` resolves Grails formats with Spring MVC's content negotiation by default. - Setting `grails.web.rendering.json.spring`, `grails.converters.json.legacy`, `grails.databinding.json.jackson`, `grails.exceptionresolver.springHandlers` or `grails.controllers.responseBody` explicitly ahead of the upgrade. - Deciding whether an application declares `grails-xml` itself. @@ -59,7 +60,7 @@ Do not assume `https://grails.apache.org/docs/latest/` points at Grails 10. Chec ## Upgrade Baseline -Grails 10 is in development and does not differ from Grails 9 yet: the upgrade guide lists no change an application has to act on. Read the guide of the target version before relying on that, since each change is added to it, and to this skill, with the pull request that makes it. +Grails 10 is in development. Read the upgrade guide of the target version, since each change an application has to act on is added to it, and to this skill, with the pull request that makes it. Verify these before touching application code: @@ -76,7 +77,7 @@ Verify these before touching application code: ## Defaults Grails 9 Announced for Grails 10 -The Grails 9 upgrade guide announces these defaults for Grails 10. The Grails 10 guide of the target version says which of them have changed; until it lists a setting, its default is the Grails 9 one. +The Grails 9 upgrade guide announces these defaults for Grails 10. The Grails 10 guide of the target version says which of them have changed; until it lists a setting, its default is the Grails 9 one. `grails.mime.negotiation.spring` has changed: see [Grails Formats Resolved with Spring MVC Content Negotiation](#grails-formats-resolved-with-spring-mvc-content-negotiation). | Setting | What it governs | Grails 9 default | Announced for Grails 10 | |---------|-----------------|------------------|-------------------------| @@ -85,6 +86,7 @@ The Grails 9 upgrade guide announces these defaults for Grails 10. The Grails 10 | `grails.databinding.json.jackson` | JSON request bodies bound to command objects and domain classes | `false` | `true` | | `grails.exceptionresolver.springHandlers` | Exceptions actions throw, resolved by Spring MVC `@ExceptionHandler` methods | `false` | `true` | | `grails.controllers.responseBody` | The values of actions annotated `@ResponseBody`, written by Spring MVC as the body | `false` | `true` | +| `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`, changed in Grails 10 | - Search `application.yml` and `application.groovy` for each setting. One that is set keeps its value through the upgrade. One that is missing takes the new default once Grails 10 changes it. - The three JSON settings change together, so that `respond`, `render ... as JSON`, `request.JSON` and data binding write and read JSON with the same mapper. Test them together. @@ -99,11 +101,34 @@ The Grails 9 upgrade guide announces these defaults for Grails 10. The Grails 10 The `grails-upgrade` skill of the last Grails 9 release describes what each setting changes and how to check an application with it. +## Grails Formats Resolved with Spring MVC Content Negotiation + +`grails.mime.negotiation.spring` is `true` by default from Grails 10. `request.format`, `response.format`, `withFormat` and the format `respond` writes in are resolved with Spring MVC's content negotiation manager, so 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`. A Spring MVC endpoint, and Spring Security's filters before a URL mapping has matched a request, negotiate as before. + +- Search `application.yml` and `application.groovy` for `grails.mime.negotiation.spring`. An application that set it to `true` on Grails 9 answers as it did; remove the setting. One that set it to `false` keeps the Grails rules. +- Where it is not set, check these answers, which change: + - `Accept: */*, application/json` is the `json` format, where it was `all`: Spring MVC takes a more specific type ahead of a less specific one of equal quality, so `withFormat` runs the `json` block rather than its first. + - A request that accepts no configured type, such as `Accept: image/png`, or whose `Accept` header Spring cannot read, such as `application/json;q=abc`, has no format: `response.format` and `response.mimeType` are `null`, and `respond` answers `406`. Code that reads either must allow for `null`. + - A `format` parameter or URL extension no type is configured for is not acceptable, where Grails 9 answered in the first configured format. + - A type with the `+json` or `+xml` suffix 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. + - The URL extension decides what Spring MVC writes for an action: `/book/show/1.json` of an action that returns a `ResponseEntity` is JSON whatever the `Accept` header asks for, and `/book/show/1.html` of one that returns a map is `406`. +- To keep the Grails 9 answers, set the setting to `false`: + + ```yaml + grails: + mime: + negotiation: + spring: false + ``` + +The section "Opt Into Spring MVC Content Negotiation for Grails Formats" of the Grails 9 upgrade guide lists every kind of answer that changes. + ## Verification Checklist - [ ] The application was on the last Grails 9 release, with its tests passing, before the upgrade. - [ ] Every announced setting the application depends on is set explicitly. - [ ] `grails-xml` is declared if the application serves or reads XML. +- [ ] Requests answered `406` or in another format than on Grails 9 have been checked against Spring MVC content negotiation, or `grails.mime.negotiation.spring: false` keeps the Grails rules. - [ ] The test suite passes on the target Grails 10 version. - [ ] The upgrade guide of the target version has been read section by section.