Uploaded image for project: 'Apache Freemarker'
  1. Apache Freemarker
  2. FREEMARKER-42

?first sequence operator does not work for SortedSets

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 2.3.23
    • Fix Version/s: 2.3.26-incubating
    • Component/s: None
    • Labels:
      None
    • Environment:
      JBoss, Linux, JDK8

      Description

      ?first should work for SortedSets given that there's an actual .first() method on the object.

      Offending line of code:

      <a href="<@s.url action="index" report=(reportsMenu[key]?first).name />"><@s.text name=key /></a>
      

      Working alternative:

      <a href="<@s.url action="index" report=(reportsMenu[key].first()).name />"><@s.text name=key /></a>
      

      Stacktrace:

      10:45:00,549 ERROR [freemarker.runtime] (http-0.0.0.0:8443-1) Error executing FreeMarker template: freemarker.template.TemplateModelException: Underlying collection is not a list, it's java.util.TreeSet
      
      ----
      FTL stack trace ("~" means nesting-related):
             	- Failed at: @s.url action="index" report=(reports...  [in template "mx/widgets/modules/reports/menu/headeradminnavmenu.ftl" at line 12, column 18]
             	- Reached through: @mx inc="menu/headeradminnavmenu"  [in template "mx/macros/widgets.ftl" in macro "moduleHeaderWide" at line 667, column 13]
             	- Reached through: @_widgets.moduleHeaderWide module="re...  [in template "mx/widgets/modules/reports/header.ftl" at line 1, column 1]
             	- Reached through: @mx inc="header"  [in template "mx/decorators/default.ftl" at line 14, column 9]
      

        Activity

        Hide
        ddekany Daniel Dekany added a comment -

        FTL doesn't work with Java types directly, it has its own simple type system, and the built-ins and operators operate on that. So utilizing that the underling wrapped object is a SortedSet would be a rather dirty thing. However, we can probably get away without that, because wrapped Set-s are #list-able, so ?first could just start iterating over the value and stop after the first item. Surely then it also works with non-sorted sets too, which is not ideal, but then again, they are all #list-able already.

        BTW, with the default object wrapper configuration, where the forceLegacyNonListCollections property is true, ?first actually works on Set-s. But leaving forceLegacyNonListCollections on true is not recommended, it's only backward compatible.

        Show
        ddekany Daniel Dekany added a comment - FTL doesn't work with Java types directly, it has its own simple type system, and the built-ins and operators operate on that. So utilizing that the underling wrapped object is a SortedSet would be a rather dirty thing. However, we can probably get away without that, because wrapped Set -s are #list -able, so ?first could just start iterating over the value and stop after the first item. Surely then it also works with non-sorted sets too, which is not ideal, but then again, they are all #list -able already. BTW, with the default object wrapper configuration, where the forceLegacyNonListCollections property is true , ?first actually works on Set -s. But leaving forceLegacyNonListCollections on true is not recommended, it's only backward compatible.
        Hide
        trumpetx David Greene added a comment -

        That makes sense. I figured it was something along those lines and the fix might be something like sequence.iterator().next() regardless of underlying type. I would suspect that it would be just fine (and expected) that a HashSet would have an unpredictable order instead of throwing an exception.

        Show
        trumpetx David Greene added a comment - That makes sense. I figured it was something along those lines and the fix might be something like sequence.iterator().next() regardless of underlying type. I would suspect that it would be just fine (and expected) that a HashSet would have an unpredictable order instead of throwing an exception.
        Hide
        ddekany Daniel Dekany added a comment -

        Fixed in the 2.3-gae branch head.

        Show
        ddekany Daniel Dekany added a comment - Fixed in the 2.3-gae branch head.

          People

          • Assignee:
            Unassigned
            Reporter:
            trumpetx David Greene
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development