From eef9bc899f859445bbcf93980c7df2df02ed97b3 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Thu, 13 Jun 2019 01:40:15 +0300 Subject: [PATCH] Avoid NPE in FreeMarkerView.getModelAttributes() in spring-webflux This commit declares the model method parameter as @Nullable and adds defensive guards against a null model argument. Closes gh-23105 --- .../reactive/result/view/freemarker/FreeMarkerView.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/freemarker/FreeMarkerView.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/freemarker/FreeMarkerView.java index 923a5b5405..e07d06db69 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/freemarker/FreeMarkerView.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/freemarker/FreeMarkerView.java @@ -219,17 +219,17 @@ public class FreeMarkerView extends AbstractUrlBasedView { * @see org.springframework.web.reactive.result.view.AbstractView#getModelAttributes(Map, ServerWebExchange) */ @Override - protected Mono> getModelAttributes(Map model, - ServerWebExchange exchange) { + protected Mono> getModelAttributes( + @Nullable Map model, ServerWebExchange exchange) { if (this.exposeSpringMacroHelpers) { - if (model.containsKey(SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE)) { + if (model != null && model.containsKey(SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE)) { throw new IllegalStateException( "Cannot expose bind macro helper '" + SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE + "' because of an existing model object of the same name"); } // Make a defensive copy of the model. - Map attributes = new HashMap<>(model); + Map attributes = (model != null ? new HashMap<>(model) : new HashMap<>()); // Expose RequestContext instance for Spring macros. attributes.put(SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE, new RequestContext( exchange, attributes, obtainApplicationContext(), getRequestDataValueProcessor()));