Skip to content

Conversation

@codeconsole
Copy link
Contributor

@codeconsole codeconsole commented Nov 6, 2025

Why?

Grails by default doesn't use mime types to render json. It encourages using $format. If you want to handle multiple mime types with the same controller while supporting ids with decimals, this becomes impossible due to how regular expression url mappings are handled in Grails. This PR offers the capability of restricting format to not include periods by specifying the $id attribute to be greedy with the + symbol.

Problem

Currently when a url parameter is specified prior prior to a $format parameter, the parameter only consumes everything up until the first dot. This results in undesired behavior on sites that's use parameters for usernames that include 1 or more periods.

For instance

"/$controller/$action?/$id?(.$format)?"
/user/show/bob.smith.json results in controller = 'UserController' id = 'bob'. format = 'smith.json'

The solution

Introduce the + symbol after a url parameter to make it greedy.

Syntax:

  • "/$id+(.$format)?" - Required id, greedy (splits at last dot)
  • "/$id+?(.$format)?" - Optional id, greedy (splits at last dot)

Examples with this Fix

"/$controller/$action?/$id+?(.$format)?"

/user/show/est.test.json → id=test.test, format=json
/user/show/test.json → id=test, format=json
/user/show/foo.bar.baz.xml → id=foo.bar.baz, format=xml

@codeconsole codeconsole assigned codeconsole and unassigned jdaugherty and matrei Nov 6, 2025
@jdaugherty
Copy link
Contributor

jdaugherty commented Nov 6, 2025

This regex change does not appear to compile:

Caused by:
java.util.regex.PatternSyntaxException: Dangling meta character '' near index 3
^/(
)(.(*))?/??$
^

@codeconsole codeconsole marked this pull request as draft November 6, 2025 16:46
@codeconsole
Copy link
Contributor Author

This regex change does not appear to compile:

Caused by: java.util.regex.PatternSyntaxException: Dangling meta character '' near index 3 ^/()(.(*))?/??$ ^

@jdaugherty accidentally added a couple parens when I fixed codenarc spacing. Should be good now

@codeconsole codeconsole added this to the grails:7.1.0 milestone Nov 6, 2025
@codeconsole codeconsole marked this pull request as ready for review November 6, 2025 18:16
@jdaugherty
Copy link
Contributor

We should update the new features for 7.1.x to mention this. thank you for your contribution

@codeconsole codeconsole merged commit b4dcd8a into apache:7.1.x Nov 11, 2025
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants