Search code examples
javaspring-bootfreemarker

Freemarker error when ?join on null field since changing SpringBoot dependency


Solved: the error was already there, I only assumed I recently created it...


I have a project where I'm using SpringMVC and Freemarker.
In my controller I add an attribute to the model named activities like this:

@GetMapping("/activities/{floorId}")
public String activities(Model model, @PathVariable String floorId) {
    model.addAttribute("activities", activityService.getActivitiesForFloor(floorId));
    return "floor/activities";
}

activities has a field called involvedTiles which sometimes is null. This is perfectly fine.
In my Freemarker view I use the modelattribute like this:

<#list activities as activity>
<tr>
    <td>${activity.id}</td>
    <td>${activity.involvedTiles?join(", ")}</td>
</tr>
</#list>

This has always worked (it never gave an error). But now I suddenly get this error:

2016-10-21 13:56:17,163 ERROR [http-nio-8080-exec-10] o.a.c.c.C.[.[.[.[dispatcherServlet] [DirectJDKLog.java:181] Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is freemarker.core.InvalidReferenceException: The following has evaluated to null or missing:
==> activity.involvedTiles  [in template "floor/activities.ftl" at line 51, column 27]

----
Tip: It's the step after the last dot that caused this error, not those before it.
----
Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present#deleted-channelwhen-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??
----
...etc...

I understand I'm getting this error, because activity.involvedTiles is null and I try to use it with the ?join() function. But I'm not understanding why this suddenly stopped working. The only thing I can think of is that I've updated my SpringBoot dependency from 1.3.0.RELEASE to 1.4.1.RELEASE. In SpringBoot 1.3.0 they use org.freemarker 2.3.23 and in SpringBoot 1.4.1 they use org.freemarker 2.3.25-incubating.
My question: did Freemarker change something and is that why I'm getting this error?


I checked the site of freemarker 2.3.23 and freemarker 2.3.25 but couldn't find something that explains this error.


Solution

  • ?join was never intended to tolerate a null, and there was no such bug (accidentally allowing null) either as far as I know. Also, I have tried this with FreeMarker 2.3.21 (a really old version) and it still doesn't allow null there.