Repository: PebbleTemplates/pebble
Pull request #229 (2016-09-28 23:32:43)

Issue 222: Raw filter should call toString on non-string inputs


Patch:
diff --git a/src/main/java/com/mitchellbosecke/pebble/extension/escaper/RawFilter.java b/src/main/java/com/mitchellbosecke/pebble/extension/escaper/RawFilter.java
index 1bdb293f..06048c88 100644
--- a/src/main/java/com/mitchellbosecke/pebble/extension/escaper/RawFilter.java
+++ b/src/main/java/com/mitchellbosecke/pebble/extension/escaper/RawFilter.java
@@ -20,10 +20,8 @@ public class RawFilter implements Filter {
     }
 
     public Object apply(Object inputObject, Map<String, Object> args) {
-        if(inputObject instanceof String){
-            return new SafeString((String) inputObject);
-        }
-        return inputObject;
+        return inputObject == null ? null : new SafeString(inputObject.toString());
+
     }
 
 }


Test patch:
diff --git a/src/test/java/com/mitchellbosecke/pebble/EscaperExtensionTest.java b/src/test/java/com/mitchellbosecke/pebble/EscaperExtensionTest.java
index 4a0cac46..c254506d 100644
--- a/src/test/java/com/mitchellbosecke/pebble/EscaperExtensionTest.java
+++ b/src/test/java/com/mitchellbosecke/pebble/EscaperExtensionTest.java
@@ -29,7 +29,6 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotEquals;
 
 public class EscaperExtensionTest extends AbstractTest {
-
     @Test
     public void testEscapeHtml() throws PebbleException, IOException {
         PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build();
@@ -126,28 +125,6 @@ public class EscaperExtensionTest extends AbstractTest {
         assertEquals("<br />", writer.toString());
     }
 
-    @Test
-    public void testRawFilter() throws PebbleException, IOException {
-        PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build();
-        PebbleTemplate template = pebble.getTemplate("{{ text | upper | raw }}");
-        Map<String, Object> context = new HashMap<>();
-        context.put("text", "<br />");
-        Writer writer = new StringWriter();
-        template.evaluate(writer, context);
-        assertEquals("<BR />", writer.toString());
-    }
-
-    @Test
-    public void testRawFilterNotBeingLast() throws PebbleException, IOException {
-        PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build();
-        PebbleTemplate template = pebble.getTemplate("{{ text | raw | upper}}");
-        Map<String, Object> context = new HashMap<>();
-        context.put("text", "<br />");
-        Writer writer = new StringWriter();
-        template.evaluate(writer, context);
-        assertEquals("&lt;BR /&gt;", writer.toString());
-    }
-
     @Test
     public void testEscapeIntoAbbreviate() throws PebbleException, IOException {
         PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build();
@@ -207,18 +184,6 @@ public class EscaperExtensionTest extends AbstractTest {
         assertEquals("<&lt;br&gt;>", writer.toString());
     }
 
-    @Test
-    public void testRawFilterWithinAutoescapeToken() throws PebbleException, IOException {
-        PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false)
-                .autoEscaping(false).build();
-        PebbleTemplate template = pebble.getTemplate("{% autoescape 'html' %}{{ text|raw }}{% endautoescape %}");
-        Map<String, Object> context = new HashMap<>();
-        context.put("text", "<br />");
-        Writer writer = new StringWriter();
-        template.evaluate(writer, context);
-        assertEquals("<br />", writer.toString());
-    }
-
     @Test
     public void testAutoEscapingParentFunction() throws PebbleException, IOException {
         PebbleEngine pebble = new PebbleEngine.Builder().strictVariables(false).build();
diff --git a/src/test/java/com/mitchellbosecke/pebble/extension/escaper/RawFilterTest.java b/src/test/java/com/mitchellbosecke/pebble/extension/escaper/RawFilterTest.java
new file mode 100644
index 00000000..7e312c07
--- /dev/null
+++ b/src/test/java/com/mitchellbosecke/pebble/extension/escaper/RawFilterTest.java
@@ -0,0 +1,81 @@
+package com.mitchellbosecke.pebble.extension.escaper;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.Test;
+
+import com.mitchellbosecke.pebble.PebbleEngine;
+import com.mitchellbosecke.pebble.error.PebbleException;
+import com.mitchellbosecke.pebble.loader.StringLoader;
+import com.mitchellbosecke.pebble.template.PebbleTemplate;
+
+public class RawFilterTest {
+	@Test
+    public void testRawFilter() throws PebbleException, IOException {
+        PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build();
+        PebbleTemplate template = pebble.getTemplate("{{ text | upper | raw }}");
+        Map<String, Object> context = new HashMap<>();
+        context.put("text", "<br />");
+        Writer writer = new StringWriter();
+        template.evaluate(writer, context);
+        assertEquals("<BR />", writer.toString());
+    }
+
+    @Test
+    public void testRawFilterNotBeingLast() throws PebbleException, IOException {
+        PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build();
+        PebbleTemplate template = pebble.getTemplate("{{ text | raw | upper}}");
+        Map<String, Object> context = new HashMap<>();
+        context.put("text", "<br />");
+        Writer writer = new StringWriter();
+        template.evaluate(writer, context);
+        assertEquals("&lt;BR /&gt;", writer.toString());
+    }
+    
+    @Test
+    public void testRawFilterWithinAutoescapeToken() throws PebbleException, IOException {
+        PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false)
+                .autoEscaping(false).build();
+        PebbleTemplate template = pebble.getTemplate("{% autoescape 'html' %}{{ text|raw }}{% endautoescape %}");
+        Map<String, Object> context = new HashMap<>();
+        context.put("text", "<br />");
+        Writer writer = new StringWriter();
+        template.evaluate(writer, context);
+        assertEquals("<br />", writer.toString());
+    }
+    
+    @Test
+    public void testRawFilterWithJsonObject() throws PebbleException, IOException {
+    	PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build();
+        PebbleTemplate template = pebble.getTemplate("{{ text | raw }}");
+        Map<String, Object> context = new HashMap<>();
+        context.put("text", new JsonObject());
+        Writer writer = new StringWriter();
+        template.evaluate(writer, context);
+        assertEquals(JsonObject.JSON_VALUE, writer.toString());
+    }
+    
+    @Test
+    public void testRawFilterWithNullObject() throws PebbleException, IOException {
+    	PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build();
+        PebbleTemplate template = pebble.getTemplate("{{ text | raw }}");
+        Writer writer = new StringWriter();
+        template.evaluate(writer, new HashMap<String, Object>());
+        assertEquals("", writer.toString());
+    }
+    
+    private class JsonObject {
+    	public static final String JSON_VALUE = "{\"menu\": {\"id\": \"file\",\"value\": \"File\",\"popup\": {\"menuitem\": [{\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"},{\"value\": \"Open\", \"onclick\": \"OpenDoc()\"},{\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}]}}}";
+    	
+    	@Override
+    	public String toString() {
+    		return JSON_VALUE;
+    	}
+    }
+}