From e7fa993be415655a89560d0342a9cffaa3fa8438 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Fri, 20 Nov 2026 13:53:47 -0700 Subject: [PATCH 1/7] Describe every format Spring MVC's manager maps as the format fallback With grails.mime.negotiation.spring, a format grails.mime.types does not list is written in the type Spring MVC's content negotiation manager maps it to. The guide, the upgrade notes, the upgrade skill and the Javadoc said only spring.mvc.contentnegotiation.media-types fed that fallback, but the manager also holds the defaults Spring MVC registers for its message converters (json, and xml, atom, rss, smile, cbor and yaml where their libraries are present), and it matches a format whatever its case. Say so, and pin the json default, also as JSON, in SpringFormatNegotiationSpec. --- .../guide/theWebLayer/contentNegotiation.adoc | 18 +++++++++++------- .../src/en/guide/upgrading/upgrading90x.adoc | 6 ++++-- .../GrailsContentNegotiationStrategy.groovy | 8 +++++--- ...ailsFormatContentNegotiationStrategy.groovy | 4 ++-- .../mime/SpringFormatNegotiationSpec.groovy | 14 ++++++++++++++ .../upgrade/skills/grails-upgrade/SKILL.md | 2 +- 6 files changed, 37 insertions(+), 15 deletions(-) diff --git a/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc b/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc index 77317acd7f5..647099f5f7a 100644 --- a/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc +++ b/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc @@ -305,11 +305,15 @@ 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, 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. +Spring MVC's content negotiation manager maps is in the type the manager maps it to, while `respond` answers it with +`406 Not Acceptable`. The manager maps the formats `spring.mvc.contentnegotiation.media-types` adds and the defaults +Spring MVC registers for its message converters: `json`, and `xml`, `atom`, `rss`, `smile`, `cbor` and `yaml` where +their libraries are on the classpath. It matches a format whatever its case, so `?format=JSON`, which +`grails.mime.types` does not list, is written as `application/json` through Spring MVC's `json`. 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. * `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 @@ -330,8 +334,8 @@ no URL mapping handles. Each rule of Grails' negotiation becomes: |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, 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. +Spring MVC's content negotiation manager maps, such as one `spring.mvc.contentnegotiation.media-types` adds, 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 1348d2ae5db..1f184a6e251 100644 --- a/grails-doc/src/en/guide/upgrading/upgrading90x.adoc +++ b/grails-doc/src/en/guide/upgrading/upgrading90x.adoc @@ -1584,8 +1584,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. A format that -`spring.mvc.contentnegotiation.media-types` maps, such as `plain: text/plain`, decides what Spring MVC writes, as +`withFormat` answers it from the `Accept` header, as for a format it has no block for. A format that Spring MVC's +content negotiation manager maps, such as one `spring.mvc.contentnegotiation.media-types` adds (`plain: text/plain`) or +one of the defaults Spring MVC registers for its message converters (`json`, and `xml`, `atom`, `rss`, `smile`, `cbor` +and `yaml` where their libraries are on the classpath), whatever its case, 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. 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 9348ecf1a3b..1c32d080f83 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 @@ -261,9 +261,11 @@ class GrailsContentNegotiationStrategy { } /** - * 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}. + * The media type Spring MVC's content negotiation manager maps a format to, whatever its case, 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}. The manager maps the formats + * {@code spring.mvc.contentnegotiation.media-types} adds and the defaults Spring MVC registers for the message + * converters present, such as {@code json}. * * @param format the format * @return the media type, as a list, or {@code null} where the manager maps no type to the format 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 0ca63b474fc..9e6a99db250 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 @@ -37,8 +37,8 @@ import org.grails.web.servlet.mvc.GrailsWebRequest * 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 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. + * manager maps it to, such as one {@code spring.mvc.contentnegotiation.media-types} adds or a default Spring MVC + * registers for the message converters present, and a request that names a format neither maps is not acceptable. * * @since 9.0 */ 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..4ef298f31fe 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 @@ -468,6 +468,20 @@ class SpringFormatNegotiationSpec extends Specification { 'and with parameter negotiation, still not acceptable' | true | 'unknown' || 'not acceptable' } + void "a format grails.mime.types does not list is written in the type Spring MVC maps it to by default, as #format"() { + given: 'grails.mime.types without json, which Spring MVC maps by default for its JSON message converter' + start((Settings.MIME_NEGOTIATION_SPRING): true, 'grails.mime.types': [all: '*/*', xml: 'application/xml']) + def request = request('Accept': 'application/xml') + request.addParameter('format', format) + handledBy(request, urlMapping()) + + expect: + resolve(request) == [MediaType.APPLICATION_JSON] + + where: + format << ['json', 'JSON'] + } + 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'], diff --git a/grails-skills/upgrade/skills/grails-upgrade/SKILL.md b/grails-skills/upgrade/skills/grails-upgrade/SKILL.md index 1951c40d335..fe014d5a7e8 100644 --- a/grails-skills/upgrade/skills/grails-upgrade/SKILL.md +++ b/grails-skills/upgrade/skills/grails-upgrade/SKILL.md @@ -449,7 +449,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. 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 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's content negotiation manager maps, such as one `spring.mvc.contentnegotiation.media-types` adds or a default Spring MVC registers for its message converters (`json`, and `xml`, `atom`, `rss`, `smile`, `cbor` and `yaml` where their libraries are present), whatever its case (`?format=JSON`), 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. From 3364f39843f528e7bdd3e042ec80da91d207560d Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Fri, 20 Nov 2026 16:36:28 -0800 Subject: [PATCH 2/7] Prefer a +xml type to an unconfigured problem+xml, as the Grails rules do The Grails rules read application/problem+xml, where no configured type names it, as application/xml, and so put a later +xml type of equal quality ahead of it: Accept: application/problem+xml, application/atom+xml is the atom format. With grails.mime.negotiation.spring the +xml preference looked only for application/xml and text/xml, so the same header was the xml format. preferringSuffixedXml becomes a method of the strategy, which knows the configured types, and also takes an unconfigured problem+xml as the XML type. SpringFormatNegotiationSpec gains the header at equal and lower quality, and FormatNegotiationComparisonSpec sends it under every configuration: only what Spring MVC writes for it differs, as it does for application/xml, application/atom+xml. --- ...lsAcceptHeaderContentNegotiationStrategy.groovy | 2 +- .../mime/GrailsContentNegotiationStrategy.groovy | 14 ++++++++++---- .../mime/FormatNegotiationComparisonSpec.groovy | 4 ++++ .../web/mime/SpringFormatNegotiationSpec.groovy | 2 ++ 4 files changed, 17 insertions(+), 5 deletions(-) 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 41ece6b7506..07cca0b5d76 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 @@ -71,6 +71,6 @@ class GrailsAcceptHeaderContentNegotiationStrategy extends HeaderContentNegotiat } 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 + return handled ? grailsStrategy.get().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 1c32d080f83..83319a02abe 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 @@ -411,16 +411,22 @@ class GrailsContentNegotiationStrategy { /** * 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. + * suffix of the quality of the XML type, the first {@code application/xml} or {@code text/xml}, or + * {@code application/problem+xml} where no configured type names it, since Grails reads that problem type as + * {@code application/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) { + List preferringSuffixedXml(List mediaTypes) { + boolean problemXmlConfigured = mimeTypes.any { MimeType configured -> + configured?.name?.equalsIgnoreCase(MediaType.APPLICATION_PROBLEM_XML_VALUE) + } int xml = mediaTypes.findIndexOf { MediaType mediaType -> - mediaType.type == 'application' && mediaType.subtype == 'xml' || mediaType.type == 'text' && mediaType.subtype == 'xml' + mediaType.type == 'application' && mediaType.subtype == 'xml' || mediaType.type == 'text' && mediaType.subtype == 'xml' || + !problemXmlConfigured && mediaType.equalsTypeAndSubtype(MediaType.APPLICATION_PROBLEM_XML) } if (xml < 0) { return mediaTypes 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 ad7d002b1a5..c26ce1ec92e 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 @@ -118,6 +118,7 @@ class FormatNegotiationComparisonSpec extends Specification { [accept: 'application/vnd.api+json'], [accept: 'application/soap+xml'], [accept: 'application/xml, application/atom+xml'], + [accept: 'application/problem+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'], @@ -156,6 +157,7 @@ class FormatNegotiationComparisonSpec extends Specification { '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 | Spring MVC: [application/xml, application/atom+xml] -> [application/atom+xml, application/xml]', + 'defaults | Accept: application/problem+xml, application/atom+xml | Spring MVC: [application/problem+xml, application/atom+xml] -> [application/atom+xml, application/problem+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', @@ -191,6 +193,7 @@ class FormatNegotiationComparisonSpec extends Specification { '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 | Spring MVC: [application/xml, application/atom+xml] -> [application/atom+xml, application/xml]', + 'merged vendor type | Accept: application/problem+xml, application/atom+xml | Spring MVC: [application/problem+xml, application/atom+xml] -> [application/atom+xml, application/problem+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', @@ -232,6 +235,7 @@ class FormatNegotiationComparisonSpec extends Specification { '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/problem+xml, application/atom+xml | Spring MVC: [application/problem+xml, application/atom+xml] -> [application/atom+xml, application/problem+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 -> *', 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 4ef298f31fe..33e03e05768 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 @@ -337,6 +337,8 @@ class SpringFormatNegotiationSpec extends Specification { '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'] + 'application/problem+xml, application/atom+xml' || ['atom', 'xml'] | ['application/atom+xml', 'application/problem+xml'] + 'application/problem+xml;q=0.5, application/atom+xml;q=0.4' || ['xml', 'atom'] | ['application/problem+xml;q=0.5', 'application/atom+xml;q=0.4'] } void 'a wildcard is of the all format, which withFormat answers with the first block compatible with it: #result for #accept'() { From f82e6fd2862ef3f2be0b20f367a205a453b9f43c Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sat, 21 Nov 2026 12:41:38 -0800 Subject: [PATCH 3/7] Cover a problem+xml that grails.mime.types names, and one only Spring maps preferringSuffixedXml takes application/problem+xml as the XML type a +xml type comes ahead of only where no type of grails.mime.types names it, as DefaultAcceptHeaderParser reads it. No test covered the configured case: with grails.mime.types naming application/problem+xml, Accept: application/problem+xml, application/atom+xml is the problem format and keeps the order of the header, for Grails and for what Spring MVC writes. A problem mapping in Spring MVC alone leaves it the atom format, with application/problem+xml behind atom, as Grails reads it as application/xml. --- .../mime/SpringFormatNegotiationSpec.groovy | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) 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 33e03e05768..37d959def9d 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 @@ -341,6 +341,31 @@ class SpringFormatNegotiationSpec extends Specification { 'application/problem+xml;q=0.5, application/atom+xml;q=0.4' || ['xml', 'atom'] | ['application/problem+xml;q=0.5', 'application/atom+xml;q=0.4'] } + void 'a problem type grails.mime.types names keeps the order of the header, as a format of its own, for Grails and for what Spring MVC writes'() { + given: + start((Settings.MIME_NEGOTIATION_SPRING): true, 'grails.mime.types': [all: '*/*', xml: 'application/xml', + atom: 'application/atom+xml', problem: 'application/problem+xml']) + def request = request('Accept': 'application/problem+xml, application/atom+xml') + handledBy(request, urlMapping()) + + expect: + GrailsWebRequest.lookup().currentResponse.mimeTypes*.extension == ['problem', 'atom'] + GrailsWebRequest.lookup().currentResponse.format == 'problem' + manager().resolveMediaTypes(new ServletWebRequest(request))*.toString() == ['application/problem+xml', 'application/atom+xml'] + } + + void "a problem type only Spring MVC's media types map is the XML type a +xml type comes ahead of, as Grails reads it as XML"() { + given: 'problem mapped to application/problem+xml in Spring MVC only, as spring.mvc.contentnegotiation.media-types.problem maps it' + start((Settings.MIME_NEGOTIATION_SPRING): true, (ParameterConfiguration.PROBLEM): true) + def request = request('Accept': 'application/problem+xml, application/atom+xml') + handledBy(request, urlMapping()) + + expect: + GrailsWebRequest.lookup().currentResponse.mimeTypes*.extension == ['atom', 'xml'] + GrailsWebRequest.lookup().currentResponse.format == 'atom' + manager().resolveMediaTypes(new ServletWebRequest(request))*.toString() == ['application/atom+xml', 'application/problem+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) @@ -664,16 +689,25 @@ class SpringFormatNegotiationSpec extends Specification { /** 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' + /** + * Maps the {@code problem} format to {@code application/problem+xml}, as + * {@code spring.mvc.contentnegotiation.media-types.problem} does. + */ + static final String PROBLEM = 'test.parameter.negotiation.problem' + private final boolean enabled private final String name private final boolean plain + private final boolean problem + ParameterConfiguration(ConfigurableEnvironment environment) { enabled = environment.getProperty(ENABLED, Boolean, false) name = environment.getProperty(NAME, 'mediaType') plain = environment.getProperty(PLAIN, Boolean, false) + problem = environment.getProperty(PROBLEM, Boolean, false) } @Override @@ -684,6 +718,9 @@ class SpringFormatNegotiationSpec extends Specification { if (plain) { configurer.mediaType('plain', MediaType.TEXT_PLAIN) } + if (problem) { + configurer.mediaType('problem', MediaType.APPLICATION_PROBLEM_XML) + } } } } From d86b855656cc0537912b8f55491bd577629453e4 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sat, 21 Nov 2026 12:42:24 -0800 Subject: [PATCH 4/7] Pin the case-insensitive fallback for the data formats Grails registers Grails registers the data formats of grails.mime.types (json, xml, hal, atom, rss, csv and text) with Spring MVC's content negotiation manager as format aliases, and the fallback looks a format up there whatever its case. With the default grails.mime.types, ?format=HAL, ?format=CSV and ?format=Text are written as application/hal+json, text/csv and text/plain for what Spring MVC writes, while Grails resolves no format for them. --- .../mime/SpringFormatNegotiationSpec.groovy | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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 37d959def9d..125c3d3adf7 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 @@ -509,6 +509,24 @@ class SpringFormatNegotiationSpec extends Specification { format << ['json', 'JSON'] } + void "a data format Grails registers with Spring MVC is written in its type whatever its case, while Grails resolves no format for #format"() { + given: 'the default grails.mime.types, whose data formats Grails registers with Spring MVC as format aliases' + start((Settings.MIME_NEGOTIATION_SPRING): true) + def request = request('Accept': 'application/xml') + request.addParameter('format', format) + handledBy(request, urlMapping()) + + expect: + resolve(request) == [MediaType.parseMediaType(expected)] + GrailsWebRequest.lookup().currentResponse.format == null + + where: + format || expected + 'HAL' || 'application/hal+json' + 'CSV' || 'text/csv' + 'Text' || 'text/plain' + } + 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'], From cea86e4d5b274ff618e5ef6b9d65981c933abca0 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sat, 21 Nov 2026 12:45:16 -0800 Subject: [PATCH 5/7] Name what Spring MVC's manager maps as the format fallback precisely The fallback takes a format grails.mime.types does not list from Spring MVC's content negotiation manager. Besides the formats spring.mvc.contentnegotiation.media-types adds, the manager maps the data formats Grails registers with it (json, xml, hal, atom, rss, csv and text, where grails.mime.types lists them), which the case-insensitive lookup also finds as ?format=HAL, and those Spring MVC registers for the types its message converters support, which depend on the converters, not on the libraries on the classpath. The guide, the upgrade note, the upgrade skill and the Javadoc of springMediaTypesOf now say so, naming Spring MVC's defaults by example. The upgrade note says "a format only" the manager maps, so it no longer reads as if respond answered ?format=json with 406, and it links the format resolution section by a new anchor, upgradingGrailsFormatResolution. --- .../en/guide/theWebLayer/contentNegotiation.adoc | 9 +++++---- .../src/en/guide/upgrading/upgrading90x.adoc | 16 +++++++++------- .../mime/GrailsContentNegotiationStrategy.groovy | 7 ++++--- .../upgrade/skills/grails-upgrade/SKILL.md | 2 +- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc b/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc index 647099f5f7a..4f32f370346 100644 --- a/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc +++ b/grails-doc/src/en/guide/theWebLayer/contentNegotiation.adoc @@ -306,10 +306,11 @@ that accepts any type, with the `all` format. 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, but what Spring MVC writes for a format `grails.mime.types` does not list and Spring MVC's content negotiation manager maps is in the type the manager maps it to, while `respond` answers it with -`406 Not Acceptable`. The manager maps the formats `spring.mvc.contentnegotiation.media-types` adds and the defaults -Spring MVC registers for its message converters: `json`, and `xml`, `atom`, `rss`, `smile`, `cbor` and `yaml` where -their libraries are on the classpath. It matches a format whatever its case, so `?format=JSON`, which -`grails.mime.types` does not list, is written as `application/json` through Spring MVC's `json`. The `all` format, +`406 Not Acceptable`. The manager maps the data formats Grails registers with it, those among `json`, `xml`, `hal`, +`atom`, `rss`, `csv` and `text` that `grails.mime.types` lists, the formats `spring.mvc.contentnegotiation.media-types` +adds, and those Spring MVC registers for the types its message converters support, such as `json`. It matches a +format whatever its case, which `grails.mime.types` does not, so `?format=JSON` and `?format=HAL` are written as +`application/json` and `application/hal+json`. 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 diff --git a/grails-doc/src/en/guide/upgrading/upgrading90x.adoc b/grails-doc/src/en/guide/upgrading/upgrading90x.adoc index 1f184a6e251..7ad7b7a969d 100644 --- a/grails-doc/src/en/guide/upgrading/upgrading90x.adoc +++ b/grails-doc/src/en/guide/upgrading/upgrading90x.adoc @@ -1129,6 +1129,7 @@ Adding `spring-boot-starter-hateoas` alone does not register a Grails renderer f `EntityModel` values. Return those models from a Spring MVC controller or provide an explicit Grails renderer for the desired media type. +[[upgradingGrailsFormatResolution]] ==== 15. Grails Format Resolution and Spring MVC Content Negotiation Grails format resolution -- `withFormat`, `respond`, and `response.format` -- applies the `grails.mime.*` @@ -1584,13 +1585,14 @@ 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. A format that Spring MVC's -content negotiation manager maps, such as one `spring.mvc.contentnegotiation.media-types` adds (`plain: text/plain`) or -one of the defaults Spring MVC registers for its message converters (`json`, and `xml`, `atom`, `rss`, `smile`, `cbor` -and `yaml` where their libraries are on the classpath), whatever its case, 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. +`withFormat` answers it from the `Accept` header, as for a format it has no block for. A format only Spring MVC's +content negotiation manager maps, such as one `spring.mvc.contentnegotiation.media-types` adds (`plain: text/plain`), +one Spring MVC registers for the types its message converters support, or, in another case, as `?format=JSON` or +`?format=HAL`, a data format Grails registers with it (see +<>), 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 MVC read only the `Accept` header: `/book/show/1.json` is answered in JSON whatever the `Accept` header asks for, and 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 83319a02abe..a408300ee12 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 @@ -263,9 +263,10 @@ class GrailsContentNegotiationStrategy { /** * The media type Spring MVC's content negotiation manager maps a format to, whatever its case, 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}. The manager maps the formats - * {@code spring.mvc.contentnegotiation.media-types} adds and the defaults Spring MVC registers for the message - * converters present, such as {@code json}. + * that type, as without {@code grails.mime.negotiation.spring}. The manager maps the data formats Grails + * registers with it, the formats {@code spring.mvc.contentnegotiation.media-types} adds, and those Spring MVC + * registers for the types its message converters support, such as {@code json}; since the lookup ignores case, a + * data format {@code grails.mime.types} lists, named in another case, such as {@code HAL}, is mapped too. * * @param format the format * @return the media type, as a list, or {@code null} where the manager maps no type to the format diff --git a/grails-skills/upgrade/skills/grails-upgrade/SKILL.md b/grails-skills/upgrade/skills/grails-upgrade/SKILL.md index fe014d5a7e8..d6d83553a33 100644 --- a/grails-skills/upgrade/skills/grails-upgrade/SKILL.md +++ b/grails-skills/upgrade/skills/grails-upgrade/SKILL.md @@ -449,7 +449,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. A format only Spring MVC's content negotiation manager maps, such as one `spring.mvc.contentnegotiation.media-types` adds or a default Spring MVC registers for its message converters (`json`, and `xml`, `atom`, `rss`, `smile`, `cbor` and `yaml` where their libraries are present), whatever its case (`?format=JSON`), 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 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's content negotiation manager maps, such as one `spring.mvc.contentnegotiation.media-types` adds, one Spring MVC registers for the types its message converters support, or a data format Grails registers with it in another case (`?format=JSON`, `?format=HAL`), 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. From fdccd5b1688f6975787ae91dab8ba0063cbc3ef0 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sat, 21 Nov 2026 17:26:21 -0800 Subject: [PATCH 6/7] Never move an unconfigured problem+xml ahead of another XML type For a request a URL mapping handles, preferringSuffixedXml runs once for what Spring MVC writes and again when Grails resolves its formats. An unconfigured application/problem+xml was both a +xml type the first pass moved ahead of application/xml and the XML type the second pass anchored on, so Accept: application/xml, application/atom+xml, application/problem+xml had Spring MVC write problem+xml while the format was atom. DefaultAcceptHeaderParser reads such a problem type as application/xml, one of the XML types, which the +xml preference never moves. Treat it the same way: it is still the XML type a +xml type comes ahead of, but it is not moved itself, so a second pass changes nothing and Spring MVC writes application/atom+xml for that header, the atom format. The Spring-only problem feature of SpringFormatNegotiationSpec becomes data-driven over that header, the upgrade note's text/xml example, application/xml, application/problem+xml and problem+xml between xml and atom, and FormatNegotiationComparisonSpec sends the header under every configuration, where only what Spring MVC writes differs. --- .../GrailsContentNegotiationStrategy.groovy | 23 +++++++++++++------ .../FormatNegotiationComparisonSpec.groovy | 4 ++++ .../mime/SpringFormatNegotiationSpec.groovy | 19 ++++++++++----- 3 files changed, 33 insertions(+), 13 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 a408300ee12..aea52000a9b 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 @@ -415,27 +415,26 @@ class GrailsContentNegotiationStrategy { * suffix of the quality of the XML type, the first {@code application/xml} or {@code text/xml}, or * {@code application/problem+xml} where no configured type names it, since Grails reads that problem type as * {@code application/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. + * of those before it, as Grails 9 orders them. Such an unconfigured {@code application/problem+xml} is one of the + * XML types, so it is not moved ahead of another, and the order is the same however often it is applied. * * @param mediaTypes the media types in Spring MVC's order * @return the media types in the order Grails reads them * @since 9.0 */ List preferringSuffixedXml(List mediaTypes) { - boolean problemXmlConfigured = mimeTypes.any { MimeType configured -> + boolean problemXmlAsXml = !mimeTypes.any { MimeType configured -> configured?.name?.equalsIgnoreCase(MediaType.APPLICATION_PROBLEM_XML_VALUE) } - int xml = mediaTypes.findIndexOf { MediaType mediaType -> - mediaType.type == 'application' && mediaType.subtype == 'xml' || mediaType.type == 'text' && mediaType.subtype == 'xml' || - !problemXmlConfigured && mediaType.equalsTypeAndSubtype(MediaType.APPLICATION_PROBLEM_XML) - } + int xml = mediaTypes.findIndexOf { MediaType mediaType -> isXmlType(mediaType, problemXmlAsXml) } 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) { + if (mediaType.subtypeSuffix?.equalsIgnoreCase('xml') && mediaType.qualityValue >= quality && + !isXmlType(mediaType, problemXmlAsXml)) { ordered.remove(mediaType) ordered.add(xml, mediaType) } @@ -443,6 +442,16 @@ class GrailsContentNegotiationStrategy { return ordered } + /** + * @return whether Grails reads the media type as XML: {@code application/xml}, {@code text/xml}, or, where no + * configured type names it, {@code application/problem+xml}, which Grails reads as {@code application/xml} + */ + private static boolean isXmlType(MediaType mediaType, boolean problemXmlAsXml) { + return mediaType.type == 'application' && mediaType.subtype == 'xml' || + mediaType.type == 'text' && mediaType.subtype == 'xml' || + problemXmlAsXml && mediaType.equalsTypeAndSubtype(MediaType.APPLICATION_PROBLEM_XML) + } + /** * 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 c26ce1ec92e..88c40d35d5f 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 @@ -119,6 +119,7 @@ class FormatNegotiationComparisonSpec extends Specification { [accept: 'application/soap+xml'], [accept: 'application/xml, application/atom+xml'], [accept: 'application/problem+xml, application/atom+xml'], + [accept: 'application/xml, application/atom+xml, application/problem+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'], @@ -158,6 +159,7 @@ class FormatNegotiationComparisonSpec extends Specification { 'defaults | Accept: application/soap+xml | withFormat(pdf, html): pdf -> null', 'defaults | Accept: application/xml, application/atom+xml | Spring MVC: [application/xml, application/atom+xml] -> [application/atom+xml, application/xml]', 'defaults | Accept: application/problem+xml, application/atom+xml | Spring MVC: [application/problem+xml, application/atom+xml] -> [application/atom+xml, application/problem+xml]', + 'defaults | Accept: application/xml, application/atom+xml, application/problem+xml | Spring MVC: [application/xml, application/atom+xml, application/problem+xml] -> [application/atom+xml, application/xml, application/problem+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', @@ -194,6 +196,7 @@ class FormatNegotiationComparisonSpec extends Specification { 'merged vendor type | Accept: application/soap+xml | withFormat(pdf, html): pdf -> null', '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: application/problem+xml, application/atom+xml | Spring MVC: [application/problem+xml, application/atom+xml] -> [application/atom+xml, application/problem+xml]', + 'merged vendor type | Accept: application/xml, application/atom+xml, application/problem+xml | Spring MVC: [application/xml, application/atom+xml, application/problem+xml] -> [application/atom+xml, application/xml, application/problem+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', @@ -236,6 +239,7 @@ class FormatNegotiationComparisonSpec extends Specification { '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/problem+xml, application/atom+xml | Spring MVC: [application/problem+xml, application/atom+xml] -> [application/atom+xml, application/problem+xml]', + 'without all | Accept: application/xml, application/atom+xml, application/problem+xml | Spring MVC: [application/xml, application/atom+xml, application/problem+xml] -> [application/atom+xml, application/xml, application/problem+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 -> *', 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 125c3d3adf7..fd3e1367691 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 @@ -354,16 +354,23 @@ class SpringFormatNegotiationSpec extends Specification { manager().resolveMediaTypes(new ServletWebRequest(request))*.toString() == ['application/problem+xml', 'application/atom+xml'] } - void "a problem type only Spring MVC's media types map is the XML type a +xml type comes ahead of, as Grails reads it as XML"() { + void "a problem type only Spring MVC's media types map is one of the XML types, which a +xml type comes ahead of and which is not moved itself: #accept"() { given: 'problem mapped to application/problem+xml in Spring MVC only, as spring.mvc.contentnegotiation.media-types.problem maps it' start((Settings.MIME_NEGOTIATION_SPRING): true, (ParameterConfiguration.PROBLEM): true) - def request = request('Accept': 'application/problem+xml, application/atom+xml') + def request = request('Accept': accept) handledBy(request, urlMapping()) - expect: - GrailsWebRequest.lookup().currentResponse.mimeTypes*.extension == ['atom', 'xml'] - GrailsWebRequest.lookup().currentResponse.format == 'atom' - manager().resolveMediaTypes(new ServletWebRequest(request))*.toString() == ['application/atom+xml', 'application/problem+xml'] + expect: 'the format and the type Spring MVC writes agree, as the Grails rules read the header' + GrailsWebRequest.lookup().currentResponse.format == format + manager().resolveMediaTypes(new ServletWebRequest(request))*.toString() == resolved + + where: + accept || format | resolved + 'application/problem+xml, application/atom+xml' || 'atom' | ['application/atom+xml', 'application/problem+xml'] + 'application/xml, application/atom+xml, application/problem+xml' || 'atom' | ['application/atom+xml', 'application/xml', 'application/problem+xml'] + 'text/xml, application/atom+xml, application/problem+xml' || 'atom' | ['application/atom+xml', 'text/xml', 'application/problem+xml'] + 'application/xml, application/problem+xml' || 'xml' | ['application/xml', 'application/problem+xml'] + 'application/problem+xml, application/xml, application/atom+xml' || 'atom' | ['application/atom+xml', 'application/problem+xml', 'application/xml'] } void 'a wildcard is of the all format, which withFormat answers with the first block compatible with it: #result for #accept'() { From 26fb5844361521f6efce79d55f24761a1e03feeb Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Sat, 21 Nov 2026 20:57:25 -0800 Subject: [PATCH 7/7] Move a repeated +xml type from its own place ahead of the XML type preferringSuffixedXml took each +xml type behind the XML type out of the list with remove(Object), which removes the first equal type. Where the header lists a type twice, that was the one ahead of the XML type, so the first pass, which decides what Spring MVC writes, left the type behind it, and the second, when Grails resolves its formats, moved it again: with the default grails.mime.types, Accept: application/atom+xml, application/xml, application/atom+xml had Spring MVC write application/xml while the format was atom, and application/xml, application/atom+xml, application/atom+xml was the xml format. With problem+xml mapped in Spring MVC only, the same happened with an unconfigured problem+xml as the XML type. #16996 brought the method in on 9.0.x. Each type is now removed from its own index. After one pass no +xml type of the quality of the XML type is left behind it, so a second pass changes nothing, and the format and the type Spring MVC writes agree. SpringFormatNegotiationSpec covers the repeated type with the default types and with the Spring-only problem mapping, and FormatNegotiationComparisonSpec sends application/atom+xml, application/xml, application/atom+xml under every configuration, where only what Spring MVC writes differs. --- .../web/mime/GrailsContentNegotiationStrategy.groovy | 10 +++++++--- .../web/mime/FormatNegotiationComparisonSpec.groovy | 4 ++++ .../grails/web/mime/SpringFormatNegotiationSpec.groovy | 4 ++++ 3 files changed, 15 insertions(+), 3 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 aea52000a9b..f387e3c9183 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 @@ -416,7 +416,9 @@ class GrailsContentNegotiationStrategy { * {@code application/problem+xml} where no configured type names it, since Grails reads that problem type as * {@code application/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. Such an unconfigured {@code application/problem+xml} is one of the - * XML types, so it is not moved ahead of another, and the order is the same however often it is applied. + * XML types, so it is not moved ahead of another. A type the header lists more than once is moved from its own + * place, so no {@code +xml} type of the quality of the XML type is left behind it, and the order is the same however + * often it is applied. * * @param mediaTypes the media types in Spring MVC's order * @return the media types in the order Grails reads them @@ -432,10 +434,12 @@ class GrailsContentNegotiationStrategy { } double quality = mediaTypes[xml].qualityValue List ordered = new ArrayList<>(mediaTypes) - for (MediaType mediaType in mediaTypes.subList(xml + 1, mediaTypes.size())) { + for (int i = xml + 1; i < mediaTypes.size(); i++) { + MediaType mediaType = mediaTypes[i] if (mediaType.subtypeSuffix?.equalsIgnoreCase('xml') && mediaType.qualityValue >= quality && !isXmlType(mediaType, problemXmlAsXml)) { - ordered.remove(mediaType) + // still at i: each type moved before it was taken from between the XML type and i and put at the XML type + ordered.removeAt(i) ordered.add(xml, mediaType) } } 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 88c40d35d5f..fddd402ee92 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 @@ -120,6 +120,7 @@ class FormatNegotiationComparisonSpec extends Specification { [accept: 'application/xml, application/atom+xml'], [accept: 'application/problem+xml, application/atom+xml'], [accept: 'application/xml, application/atom+xml, application/problem+xml'], + [accept: 'application/atom+xml, 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'], @@ -160,6 +161,7 @@ class FormatNegotiationComparisonSpec extends Specification { 'defaults | Accept: application/xml, application/atom+xml | Spring MVC: [application/xml, application/atom+xml] -> [application/atom+xml, application/xml]', 'defaults | Accept: application/problem+xml, application/atom+xml | Spring MVC: [application/problem+xml, application/atom+xml] -> [application/atom+xml, application/problem+xml]', 'defaults | Accept: application/xml, application/atom+xml, application/problem+xml | Spring MVC: [application/xml, application/atom+xml, application/problem+xml] -> [application/atom+xml, application/xml, application/problem+xml]', + 'defaults | Accept: application/atom+xml, application/xml, application/atom+xml | Spring MVC: [application/atom+xml, application/xml, application/atom+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', @@ -197,6 +199,7 @@ class FormatNegotiationComparisonSpec extends Specification { '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: application/problem+xml, application/atom+xml | Spring MVC: [application/problem+xml, application/atom+xml] -> [application/atom+xml, application/problem+xml]', 'merged vendor type | Accept: application/xml, application/atom+xml, application/problem+xml | Spring MVC: [application/xml, application/atom+xml, application/problem+xml] -> [application/atom+xml, application/xml, application/problem+xml]', + 'merged vendor type | Accept: application/atom+xml, application/xml, application/atom+xml | Spring MVC: [application/atom+xml, application/xml, application/atom+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', @@ -240,6 +243,7 @@ class FormatNegotiationComparisonSpec extends Specification { 'without all | Accept: application/xml, application/atom+xml | Spring MVC: [application/xml, application/atom+xml] -> [application/atom+xml, application/xml]', 'without all | Accept: application/problem+xml, application/atom+xml | Spring MVC: [application/problem+xml, application/atom+xml] -> [application/atom+xml, application/problem+xml]', 'without all | Accept: application/xml, application/atom+xml, application/problem+xml | Spring MVC: [application/xml, application/atom+xml, application/problem+xml] -> [application/atom+xml, application/xml, application/problem+xml]', + 'without all | Accept: application/atom+xml, application/xml, application/atom+xml | Spring MVC: [application/atom+xml, application/xml, application/atom+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 -> *', 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 fd3e1367691..34784d0394a 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 @@ -339,6 +339,8 @@ class SpringFormatNegotiationSpec extends Specification { 'application/json, application/atom+xml' || ['json', 'atom'] | ['application/json', 'application/atom+xml'] 'application/problem+xml, application/atom+xml' || ['atom', 'xml'] | ['application/atom+xml', 'application/problem+xml'] 'application/problem+xml;q=0.5, application/atom+xml;q=0.4' || ['xml', 'atom'] | ['application/problem+xml;q=0.5', 'application/atom+xml;q=0.4'] + 'application/atom+xml, application/xml, application/atom+xml' || ['atom', 'xml'] | ['application/atom+xml', 'application/atom+xml', 'application/xml'] + 'application/xml, application/atom+xml, application/atom+xml' || ['atom', 'xml'] | ['application/atom+xml', 'application/atom+xml', 'application/xml'] } void 'a problem type grails.mime.types names keeps the order of the header, as a format of its own, for Grails and for what Spring MVC writes'() { @@ -371,6 +373,8 @@ class SpringFormatNegotiationSpec extends Specification { 'text/xml, application/atom+xml, application/problem+xml' || 'atom' | ['application/atom+xml', 'text/xml', 'application/problem+xml'] 'application/xml, application/problem+xml' || 'xml' | ['application/xml', 'application/problem+xml'] 'application/problem+xml, application/xml, application/atom+xml' || 'atom' | ['application/atom+xml', 'application/problem+xml', 'application/xml'] + 'application/atom+xml, application/problem+xml, application/atom+xml' || 'atom' | ['application/atom+xml', 'application/atom+xml', 'application/problem+xml'] + 'application/problem+xml, application/atom+xml, application/atom+xml' || 'atom' | ['application/atom+xml', 'application/atom+xml', 'application/problem+xml'] } void 'a wildcard is of the all format, which withFormat answers with the first block compatible with it: #result for #accept'() {