jdaugherty commented on code in PR #16453:
URL: https://github.com/apache/grails-core/pull/16453#discussion_r4166256828


##########
grails-interceptors/src/test/groovy/grails/artefact/InterceptorSpec.groovy:
##########
@@ -535,6 +537,52 @@ class InterceptorSpec extends Specification {
         '/grails/foo/x'       | true
     }
 
+    void "Test getModel returns null when neither MODEL_AND_VIEW nor 
TEMPLATE_MODEL is set"() {
+        given: "an interceptor with no model attributes on the request"
+        def i = new TestInterceptor()
+        GrailsWebMockUtil.bindMockWebRequest()
+
+        expect: "getModel returns null"
+        i.model == null
+    }
+
+    void "Test getModel returns modelMap from ModelAndView when MODEL_AND_VIEW 
is set"() {
+        given: "an interceptor with a ModelAndView on the request"
+        def i = new TestInterceptor()
+        def webRequest = GrailsWebMockUtil.bindMockWebRequest()
+        def mav = new ModelAndView()
+        mav.addObject('foo', 'bar')
+        webRequest.setAttribute(GrailsApplicationAttributes.MODEL_AND_VIEW, 
mav, 0)
+
+        expect: "getModel returns the ModelAndView's model map"
+        i.model == [foo: 'bar']
+    }
+
+    void "Test getModel falls back to TEMPLATE_MODEL when no ModelAndView is 
set"() {
+        given: "an interceptor with only TEMPLATE_MODEL on the request"
+        def i = new TestInterceptor()
+        def webRequest = GrailsWebMockUtil.bindMockWebRequest()
+        Map<String, Object> templateModel = [baz: 'qux']
+        webRequest.setAttribute(GrailsApplicationAttributes.TEMPLATE_MODEL, 
templateModel, 0)

Review Comment:
   Thanks, this is what I was after: the new feature fails when the previous 
`getModel()` is restored. In f9e483ceef I added the `bean:` plus `model:` case 
and the same changes in the hibernate7 copy of the app.



##########
grails-interceptors/src/main/groovy/grails/artefact/Interceptor.groovy:
##########
@@ -145,7 +145,11 @@ trait Interceptor implements ResponseRenderer, 
ResponseRedirector, RequestForwar
     @Generated
     Map<String, Object> getModel() {
         def modelAndView = (ModelAndView) 
currentRequestAttributes().getAttribute(GrailsApplicationAttributes.MODEL_AND_VIEW,
 0)
-        return modelAndView?.modelMap
+        if (modelAndView != null) {
+            return modelAndView.modelMap
+        }
+        // Fallback: when render template: ..., model: ... is used, the model 
is stored in TEMPLATE_MODEL
+        return (Map<String, Object>) 
currentRequestAttributes().getAttribute(GrailsApplicationAttributes.TEMPLATE_MODEL,
 0)

Review Comment:
   Thanks. In f9e483ceef I tightened the wording and added that returning 
`false` from `after()` does not stop a template response.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to