Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions grails-test-examples/issue-15819/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!--
SPDX-License-Identifier: Apache-2.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.
-->

# Response rendering regression (#15819)

Functional coverage based on the
[end-application reproducer](https://github.com/ruthst00/15819-response-renderer-overloads).
The application registers a Spring `RequestContextFilter` at order `-100`, after
Grails' request filter, and serves a small GSP fragment from `/search/results`.
Keep the fragment small and do not flush the response: committing it can mask
the unwanted conventional view resolution that caused the original failure.

There is deliberately no `results.gsp` or `upload.gsp`. The tests cover template
rendering with AJAX and normal GET requests and multipart uploads behind a late
request-context filter. `/search/upload` reads a `title` form field and a `file`
upload, and renders the title, original filename and uploaded content in the fragment.

`GrailsDispatcherServletRequestBindingSpec` provides the dispatcher regression
coverage on `7.0.x`, including multipart resolution, request publication, nested
dispatches and asynchronous processing. It has a distinct name so merge-up can
retain the existing dispatcher and hidden-method specs on `8.0.x`.

The tests use real HTTP requests against the embedded application; no browser,
database, or Docker container is needed. From the repository root, run:

```bash
./gradlew :grails-test-examples-issue-15819:integrationTest
```
44 changes: 44 additions & 0 deletions grails-test-examples/issue-15819/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

plugins {
id 'org.apache.grails.buildsrc.properties'
id 'org.apache.grails.buildsrc.dependency-validator'
id 'org.apache.grails.buildsrc.compile'
id 'org.apache.grails.buildsrc.vulnerability-scan'
}

version = '0.1'
group = 'issue15819'

apply plugin: 'org.apache.grails.gradle.grails-web'
apply plugin: 'org.apache.grails.gradle.grails-gsp'

dependencies {
implementation platform(project(':grails-bom'))
implementation 'org.apache.grails:grails-dependencies-starter-web'

integrationTestImplementation 'org.apache.grails.testing:grails-testing-support-core'
integrationTestImplementation 'org.apache.grails:grails-testing-support-http-client'
}

apply {
from rootProject.layout.projectDirectory.file('gradle/functional-test-config.gradle')
from rootProject.layout.projectDirectory.file('gradle/grails-extension-gradle-config.gradle')
}
23 changes: 23 additions & 0 deletions grails-test-examples/issue-15819/grails-app/conf/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

grails:
profile: web
views:
gsp:
encoding: UTF-8
info:
app:
name: issue-15819
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package spring

import org.springframework.boot.web.servlet.FilterRegistrationBean
import org.springframework.web.filter.RequestContextFilter

beans = {
// Reproduce an application filter replacing the thread binding after Grails' request filter.
lateRequestContextFilter(FilterRegistrationBean) {
filter = new RequestContextFilter()
order = -100
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package issue15819

import grails.compiler.GrailsCompileStatic
import org.springframework.web.multipart.MultipartHttpServletRequest

@GrailsCompileStatic
class SearchController {

def results() {
render(template: 'results', model: [results: ['Grails', 'Groovy', 'Spring Boot']])
}

def upload() {
def file = ((MultipartHttpServletRequest) request).getFile('file')
render(template: 'results', model: [results: [params.title, file.originalFilename, new String(file.bytes, 'UTF-8')]])
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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 issue15819

class UrlMappings {

static mappings = {
"/search/$action"(controller: 'search')
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package issue15819

import groovy.transform.CompileStatic

import grails.boot.GrailsApp
import grails.boot.config.GrailsAutoConfiguration

@CompileStatic
class Application extends GrailsAutoConfiguration {

static void main(String[] args) {
GrailsApp.run(Application, args)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<%--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
--%>
<ul id="results">
<g:each in="${results}" var="result">
<li>${result}</li>
</g:each>
</ul>
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package issue15819

import spock.lang.Issue
import spock.lang.Specification
import spock.lang.Tag
import spock.lang.Unroll

import grails.testing.mixin.integration.Integration
import org.apache.grails.testing.http.client.HttpClientSupport
import org.apache.grails.testing.http.client.MultipartBody

@Integration
@Tag('http-client')
@Issue('https://github.com/apache/grails-core/issues/15819')
class ResponseRendererFunctionalSpec extends Specification implements HttpClientSupport {

@Unroll
void 'an upload renders only the template fragment with ajax=#ajax'() {
given:
def multipart = MultipartBody.builder()
.addPart('title', 'Upload title')
.addPart('file', 'results.txt', 'text/plain', 'Uploaded content')
.build()

when:
def response = httpPostMultipart(ajax ? ['X-Requested-With': 'XMLHttpRequest'] : [:], '/search/upload', multipart)

then:
response.assertStatus(200)
response.headerValue('Content-Type').startsWith('text/html')
response.body().replaceAll(/\s+/, ' ').trim() ==
'<ul id="results"> <li>Upload title</li> <li>results.txt</li> <li>Uploaded content</li> </ul>'

where:
ajax << [false, true]
}

@Unroll
void '#action renders only the _results.gsp template fragment with ajax=#ajax'() {
when:
def response = http(ajax ? ['X-Requested-With': 'XMLHttpRequest'] : [:], "/search/$action")

then: 'no conventional action view is resolved and no layout or error page is appended'
response.assertStatus(200)
response.headerValue('Content-Type').startsWith('text/html')
response.body().replaceAll(/\s+/, ' ').trim() ==
'<ul id="results"> <li>Grails</li> <li>Groovy</li> <li>Spring Boot</li> </ul>'

where:
action | ajax
'results' | false
'results' | true
}
}
2 changes: 2 additions & 0 deletions grails-web-mvc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ dependencies {
api 'org.apache.groovy:groovy'

compileOnly 'jakarta.servlet:jakarta.servlet-api'
compileOnly 'org.apache.grails.views:grails-gsp-core'
compileOnly 'org.springframework:spring-test', {
// MockHttpServletRequest/Response/Context used in many classes
}

testImplementation 'jakarta.servlet:jakarta.servlet-api'
testImplementation 'org.apache.grails.views:grails-gsp-core'
testImplementation 'org.springframework:spring-test'

testImplementation 'org.apache.groovy:groovy-test-junit5'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import org.springframework.web.servlet.DispatcherServlet

import grails.util.Holders
import org.grails.web.context.ServletEnvironmentGrailsApplicationDiscoveryStrategy
import org.grails.web.util.GrailsApplicationAttributes
import org.grails.web.util.WebUtils

/**
Expand All @@ -54,18 +55,21 @@ class GrailsDispatcherServlet extends DispatcherServlet implements ServletContex

@Override
protected ServletRequestAttributes buildRequestAttributes(HttpServletRequest request, HttpServletResponse response, RequestAttributes previousAttributes) {
if (previousAttributes == null || !(previousAttributes instanceof GrailsWebRequest)) {
return buildGrailsWebRequest(request, response)
}
else {
GrailsWebRequest webRequest = (GrailsWebRequest) previousAttributes
if (webRequest.isActive()) {
return webRequest
}
else {
return buildGrailsWebRequest(request, response)
if (previousAttributes instanceof GrailsWebRequest) {
GrailsWebRequest previousWebRequest = (GrailsWebRequest) previousAttributes
if (previousWebRequest.isActive()) {
return previousWebRequest
}
}
GrailsWebRequest webRequest = buildGrailsWebRequest(request, response)
if (previousAttributes != null) {
// A later filter replaced the binding. Publish the dispatch's instance so URL mappings
// and rendering agree, while preserving the request and response wrappers seen here.
// FrameworkServlet passes null on Callable threads, which need their own instance
// without replacing the attribute still used by the dispatch.
request.setAttribute(GrailsApplicationAttributes.WEB_REQUEST, webRequest)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Request tags are raw and unchanged. A filter sets params.tags[0] to filter. A later wrapper supplies wrapped and values, and a late filter replaces the thread binding. The controller should receive filter and values. It receives wrapped and values, because the new request is published without the filter's edit. Carry that edit onto the published request.

}
return webRequest
}

protected GrailsWebRequest buildGrailsWebRequest(HttpServletRequest request, HttpServletResponse response) {
Expand Down
Loading