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


##########
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:
   Added a `NOTE` block and annotated code example to 
`grails-doc/src/en/guide/theWebLayer/interceptors/definingInterceptors.adoc` 
(after the existing `after()` example, lines 48–59) that explains the timing 
distinction between view rendering and template rendering:
   
   - **View rendering** (`render(view: ..., model: [...])`) — Spring MVC defers 
the actual render until after `after()` returns, so modifications to `model` 
and `view` in `after()` are reflected in the final response. This is the 
existing documented behavior.
   - **Template rendering** (`render(template: ..., model: [...])`) — 
`ResponseRenderer` processes the template and writes the response immediately 
inside the controller's `render()` call, before `after()` is invoked. The model 
is stored on the request as `GrailsApplicationAttributes.TEMPLATE_MODEL` (the 
fallback branch of `Interceptor#getModel()`), so `after()` can *read* it, but 
any modifications will not affect the already-written response.
   
   The explanation is backed by the source code (`ResponseRenderer.groovy` sets 
`TEMPLATE_MODEL` synchronously, `Interceptor#getModel()` reads it as a 
fallback) and by the passing integration test `ModelInterceptorIntSpec` that 
was already in the repository confirming this exact behavior.



-- 
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