diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/GroupByFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/GroupByFilter.java index 5c13ebdee..8418c0815 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/GroupByFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/GroupByFilter.java @@ -1,7 +1,9 @@ package com.hubspot.jinjava.lib.filter; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Objects; import com.google.common.collect.LinkedListMultimap; @@ -50,18 +52,24 @@ public Object filter(Object var, JinjavaInterpreter interpreter, String... args) ForLoop loop = ObjectIterator.getLoop(var); Multimap groupBuckets = LinkedListMultimap.create(); + Map groupMapRaw = new HashMap<>(); while (loop.hasNext()) { Object val = loop.next(); - String grouper = Objects.toString(interpreter.resolveProperty(val, attr)); + Object resolvedProperty = interpreter.resolveProperty(val, attr); + String grouper = Objects.toString(resolvedProperty); groupBuckets.put(grouper, val); + + if (!groupMapRaw.containsKey(grouper)) { + groupMapRaw.put(grouper, resolvedProperty); + } } List groups = new ArrayList<>(); for (String grouper : groupBuckets.keySet()) { List list = Lists.newArrayList(groupBuckets.get(grouper)); - groups.add(new Group(grouper, list)); + groups.add(new Group(grouper, groupMapRaw.get(grouper), list)); } return groups; @@ -69,10 +77,12 @@ public Object filter(Object var, JinjavaInterpreter interpreter, String... args) public static class Group { private final String grouper; + private final Object grouperObject; private final List list; - public Group(String grouper, List list) { + public Group(String grouper, Object grouperObject, List list) { this.grouper = grouper; + this.grouperObject = grouperObject; this.list = list; } @@ -80,6 +90,10 @@ public String getGrouper() { return grouper; } + public Object getGrouperObject() { + return grouperObject; + } + public List getList() { return list; }