Grails 6.2.3 Redirect issue when redirected to a url created using LinkGenerator.link
Expected Behavior
If a url is created using grailsLinkGenerator
String url = grailsLinkGenerator.link(controller: 'menu', action: 'home')
and the url (/red/menu/home) is used to redirect like:
redirect(url: url)
404 occurs as the redirect appends the application name again to the url, e.g.,
http://localhost:8080/red/red/menu/home
This looks to be a regression and asit works in Grails 5. Attaching a Grails 5 and Grails 6 application for bug reproduction.
Actual Behaviour
Redirect should not append the application name again.
Steps To Reproduce
Please checkout the following Grails 6.2.3 project: https://github.com/imranmir/redirect-issue Boot run the application Hit the following url: http://localhost:8080/red/test/index This should ideally have redirected to http://localhost:8080/red/menu/home, but instead it tries to redirect to http://localhost:8080/red/red/menu/home
Environment Information
OSX openjdk 11.0.27
Example Application
https://github.com/imranmir/redirect-issue
Version
6.2.3
Working redirect on Grails 5 application: https://github.com/imranmir/green-app-
The bug must have been introduced in Grails 6.1.2 and it seems to also be present in Grails 7.0.0 ...
Hi,
In your code, you forget absolute:false.
The bug introduced between grails 5.2X (https://github.com/apache/grails-core/blob/5.2.x/grails-web-url-mappings/src/main/groovy/grails/web/mapping/ResponseRedirector.groovy#L125) and grails 5.3.X (https://github.com/apache/grails-core/blob/5.3.x/grails-web-url-mappings/src/main/groovy/grails/web/mapping/ResponseRedirector.groovy#L125)
Probably a dumb question, but may I ask the use case for first creating a url with the LinkGenerator and then passing that url to redirect() (where the LinkGenerator will be invoked again) instead of calling redirect() directly?
From the example application provided: https://github.com/imranmir/redirect-issue
class TestController {
LinkGenerator grailsLinkGenerator
def index() {
def url = grailsLinkGenerator.link(controller: 'menu', action: 'home')
redirect(url: url)
}
// vs
def index2() {
redirect(controller: 'menu', action: 'home')
}
}
The linkGenerator.link call comes from a legacy helper method that’s responsible for generating links dynamically. Redirecting with that link is just one of the things it does — the same link is also passed to JavaScript and used in a few other places. So we end up reusing that generated link in redirect() instead of calling redirect() directly.
Fixed by https://github.com/apache/grails-core/pull/15227
Reverts changes for https://github.com/apache/grails-core/issues/11673