Repository: PebbleTemplates/pebble
Pull request #569 (2021-01-01 19:15:23)

#568: Support array of List in sort filter


Patch:
diff --git a/pebble/src/main/java/com/mitchellbosecke/pebble/extension/core/SortFilter.java b/pebble/src/main/java/com/mitchellbosecke/pebble/extension/core/SortFilter.java
index 13d6bcad..44c7c9c6 100644
--- a/pebble/src/main/java/com/mitchellbosecke/pebble/extension/core/SortFilter.java
+++ b/pebble/src/main/java/com/mitchellbosecke/pebble/extension/core/SortFilter.java
@@ -8,9 +8,11 @@
  */
 package com.mitchellbosecke.pebble.extension.core;
 
+import com.mitchellbosecke.pebble.error.PebbleException;
 import com.mitchellbosecke.pebble.extension.Filter;
 import com.mitchellbosecke.pebble.template.EvaluationContext;
 import com.mitchellbosecke.pebble.template.PebbleTemplate;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
@@ -29,7 +31,16 @@ public class SortFilter implements Filter {
     if (input == null) {
       return null;
     }
-    List<Comparable> collection = (List<Comparable>) input;
+
+    List<Comparable> collection;
+    if (input instanceof List) {
+      collection = (List<Comparable>) input;
+    } else if (input instanceof Comparable[]) {
+      collection = Arrays.asList((Comparable[]) input);
+    } else {
+      throw new PebbleException(null, "Unsupported input type for sort filter", lineNumber,
+          self.getName());
+    }
     Collections.sort(collection);
     return collection;
   }


Test patch:
diff --git a/pebble/src/test/java/com/mitchellbosecke/pebble/ArraySyntaxTest.java b/pebble/src/test/java/com/mitchellbosecke/pebble/ArraySyntaxTest.java
index b8fdbe2e..e01f680c 100644
--- a/pebble/src/test/java/com/mitchellbosecke/pebble/ArraySyntaxTest.java
+++ b/pebble/src/test/java/com/mitchellbosecke/pebble/ArraySyntaxTest.java
@@ -8,22 +8,20 @@
  */
 package com.mitchellbosecke.pebble;
 
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.fail;
+
 import com.mitchellbosecke.pebble.error.ParserException;
 import com.mitchellbosecke.pebble.error.PebbleException;
 import com.mitchellbosecke.pebble.loader.StringLoader;
 import com.mitchellbosecke.pebble.template.PebbleTemplate;
-
-import org.junit.jupiter.api.Test;
-
 import java.io.IOException;
 import java.io.StringWriter;
 import java.io.Writer;
 import java.util.HashMap;
 import java.util.Map;
-
-import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.fail;
+import org.junit.jupiter.api.Test;
 
 class ArraySyntaxTest {
 
@@ -146,7 +144,7 @@ class ArraySyntaxTest {
     Map<String, Object> context = new HashMap<>();
     context.put("three", new Object() {
 
-      public Integer number = 3;
+      public final Integer number = 3;
     });
     context.put("numbers", new HashMap<String, Object>() {
 
@@ -154,7 +152,7 @@ class ArraySyntaxTest {
         this.put("four", new String[]{"4"});
         this.put("five", new Object() {
 
-          private String value = "five";
+          private final String value = "five";
 
           public String getValue() {
             return this.value;
@@ -334,6 +332,20 @@ class ArraySyntaxTest {
     assertEquals("[0, 1, 2, 3]", writer.toString());
   }
 
+  @Test
+  void testSortFilterFromArray() throws PebbleException, IOException {
+
+    PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader())
+        .strictVariables(false).build();
+
+    String source = "{{ 'q,g,s,c,w' | split(',') | sort }}";
+    PebbleTemplate template = pebble.getTemplate(source);
+
+    Writer writer = new StringWriter();
+    template.evaluate(writer, new HashMap<>());
+    assertEquals("[c, g, q, s, w]", writer.toString());
+  }
+
   @Test
   void testForTag() throws PebbleException, IOException {
 
@@ -804,7 +816,7 @@ class ArraySyntaxTest {
       {
         this.put("first-name", new Object() {
 
-          private String name = "Bob";
+          private final String name = "Bob";
 
           public String getName() {
             return this.name;