Repository: rollbar/rollbar-java
Pull request #28 (2016-06-20 17:32:38)

Frame json cleanup, and obfuscation fix.
If a customer is obfuscating their code, because of the reflection being used for BodyContents, the payload is invalid, this removes the reflection and cleans up the json payload a bit.


Patch:
diff --git a/README.md b/README.md
index 26967bd..fe82565 100644
--- a/README.md
+++ b/README.md
@@ -36,7 +36,7 @@ dependency to your pom file:
 <dependency>
   <groupId>com.rollbar</groupId>
    <artifactId>rollbar</artifactId>
-   <version>0.5.2</version>
+   <version>0.5.3</version>
 </dependency>
 </dependencies>
 ```
@@ -44,7 +44,7 @@ dependency to your pom file:
 ### Gradle
 
 ```groovy
-compile('com.rollbar:rollbar:0.5.2')
+compile('com.rollbar:rollbar:0.5.3')
 ```
 
 ## Usage
diff --git a/pom.xml b/pom.xml
index 890cd92..66c5e83 100644
--- a/pom.xml
+++ b/pom.xml
@@ -9,7 +9,7 @@
   <packaging>pom</packaging>
   <modelVersion>4.0.0</modelVersion>
   <properties>
-    <rollbar.version>0.5.2</rollbar.version>
+    <rollbar.version>0.5.3</rollbar.version>
   </properties>
 
   <name>java-rollbar</name>
diff --git a/rollbar-payload/src/main/java/com/rollbar/payload/data/body/Body.java b/rollbar-payload/src/main/java/com/rollbar/payload/data/body/Body.java
index f41ce09..4765e1f 100644
--- a/rollbar-payload/src/main/java/com/rollbar/payload/data/body/Body.java
+++ b/rollbar-payload/src/main/java/com/rollbar/payload/data/body/Body.java
@@ -164,10 +164,6 @@ public class Body implements JsonSerializable {
     }
 
     private String key() {
-        return toSnakeCase(contents.getClass().getSimpleName());
-    }
-
-    private static String toSnakeCase(String simpleName) {
-        return StringUtils.join("_", simpleName.split("(?=\\p{Lu})")).toLowerCase();
+        return contents.getKeyName();
     }
 }
diff --git a/rollbar-payload/src/main/java/com/rollbar/payload/data/body/BodyContents.java b/rollbar-payload/src/main/java/com/rollbar/payload/data/body/BodyContents.java
index 149c7e2..1df81bb 100644
--- a/rollbar-payload/src/main/java/com/rollbar/payload/data/body/BodyContents.java
+++ b/rollbar-payload/src/main/java/com/rollbar/payload/data/body/BodyContents.java
@@ -4,4 +4,5 @@ package com.rollbar.payload.data.body;
  * A marker interface for the contents of the rollbar body.
  */
 public interface BodyContents {
+    String getKeyName();
 }
diff --git a/rollbar-payload/src/main/java/com/rollbar/payload/data/body/CrashReport.java b/rollbar-payload/src/main/java/com/rollbar/payload/data/body/CrashReport.java
index 6e8c5d0..6f02533 100644
--- a/rollbar-payload/src/main/java/com/rollbar/payload/data/body/CrashReport.java
+++ b/rollbar-payload/src/main/java/com/rollbar/payload/data/body/CrashReport.java
@@ -45,4 +45,8 @@ public class CrashReport implements BodyContents, JsonSerializable {
         obj.put("raw", raw());
         return obj;
     }
+
+    public String getKeyName() {
+        return "crash_report";
+    }
 }
diff --git a/rollbar-payload/src/main/java/com/rollbar/payload/data/body/Frame.java b/rollbar-payload/src/main/java/com/rollbar/payload/data/body/Frame.java
index 3747adc..ff2d5b9 100644
--- a/rollbar-payload/src/main/java/com/rollbar/payload/data/body/Frame.java
+++ b/rollbar-payload/src/main/java/com/rollbar/payload/data/body/Frame.java
@@ -216,15 +216,34 @@ public class Frame implements JsonSerializable {
     }
 
     public Map<String, Object> asJson() {
+
         Map<String, Object> obj = new LinkedHashMap<String, Object>();
-        obj.put("filename", filename());
-        obj.put("lineno", lineNumber());
-        obj.put("colno", columnNumber());
-        obj.put("method", method());
-        obj.put("code", code());
-        obj.put("context", context());
-        obj.put("args", args());
-        obj.put("kwargs", keywordArgs());
+        String filename = filename();
+        Integer lineNumber = lineNumber();
+        Integer columnNumber = columnNumber();
+        String method = method();
+        String code = code();
+        CodeContext context = context();
+        Object[] args = args();
+        Map<String, Object> keywordArgs = keywordArgs();
+
+        if (filename != null)
+            obj.put("filename", filename);
+        if (lineNumber != null)
+            obj.put("lineno", lineNumber);
+        if (columnNumber != null)
+            obj.put("colno", columnNumber);
+        if (method != null)
+            obj.put("method", method);
+        if (code != null)
+            obj.put("code", code);
+        if (context != null)
+            obj.put("context", context);
+        if (args != null)
+            obj.put("args", args);
+        if (keywordArgs != null)
+            obj.put("kwargs", keywordArgs);
+
         return obj;
     }
 }
diff --git a/rollbar-payload/src/main/java/com/rollbar/payload/data/body/Message.java b/rollbar-payload/src/main/java/com/rollbar/payload/data/body/Message.java
index d349997..29aba9e 100644
--- a/rollbar-payload/src/main/java/com/rollbar/payload/data/body/Message.java
+++ b/rollbar-payload/src/main/java/com/rollbar/payload/data/body/Message.java
@@ -67,4 +67,8 @@ public class Message extends Extensible<Message> implements BodyContents {
     public Message body(String body) throws ArgumentNullException {
         return new Message(body, getMembers());
     }
+
+    public String getKeyName() {
+        return "message";
+    }
 }
diff --git a/rollbar-payload/src/main/java/com/rollbar/payload/data/body/Trace.java b/rollbar-payload/src/main/java/com/rollbar/payload/data/body/Trace.java
index befb994..1b3b379 100644
--- a/rollbar-payload/src/main/java/com/rollbar/payload/data/body/Trace.java
+++ b/rollbar-payload/src/main/java/com/rollbar/payload/data/body/Trace.java
@@ -93,4 +93,8 @@ public class Trace implements BodyContents, JsonSerializable {
         obj.put("exception", exception());
         return obj;
     }
+
+    public String getKeyName() {
+        return "trace";
+    }
 }
diff --git a/rollbar-payload/src/main/java/com/rollbar/payload/data/body/TraceChain.java b/rollbar-payload/src/main/java/com/rollbar/payload/data/body/TraceChain.java
index 142fa72..2824217 100644
--- a/rollbar-payload/src/main/java/com/rollbar/payload/data/body/TraceChain.java
+++ b/rollbar-payload/src/main/java/com/rollbar/payload/data/body/TraceChain.java
@@ -75,4 +75,8 @@ public class TraceChain implements BodyContents, JsonSerializable {
     public Trace[] asJson() {
         return traces();
     }
+
+    public String getKeyName() {
+        return "trace_chain";
+    }
 }


Test patch:
diff --git a/rollbar-payload/src/test/java/com/rollbar/payload/data/body/FrameTest.java b/rollbar-payload/src/test/java/com/rollbar/payload/data/body/FrameTest.java
index 2b891d0..7569507 100644
--- a/rollbar-payload/src/test/java/com/rollbar/payload/data/body/FrameTest.java
+++ b/rollbar-payload/src/test/java/com/rollbar/payload/data/body/FrameTest.java
@@ -139,4 +139,17 @@ public class FrameTest {
             }
         });
     }
+
+    @Test
+    public void testEmptyValues() {
+        Frame frame = new Frame("foo.java");
+        Map<String, Object> json = frame.asJson();
+        assertFalse(json.containsKey("lineno"));
+        assertFalse(json.containsKey("colno"));
+        assertFalse(json.containsKey("method"));
+        assertFalse(json.containsKey("code"));
+        assertFalse(json.containsKey("context"));
+        assertFalse(json.containsKey("args"));
+        assertFalse(json.containsKey("kwargs"));
+    }
 }
\ No newline at end of file
diff --git a/rollbar/src/test/java/com/rollbar/RollbarTest.java b/rollbar/src/test/java/com/rollbar/RollbarTest.java
index a766037..208ed64 100644
--- a/rollbar/src/test/java/com/rollbar/RollbarTest.java
+++ b/rollbar/src/test/java/com/rollbar/RollbarTest.java
@@ -19,4 +19,10 @@ public class RollbarTest {
         Rollbar rollbar = new Rollbar("some-access-token", "some-environment");
         rollbar.log(new Exception("some exception"));
     }
+
+    @Test
+    public void sendItemToRollbar() {
+        Rollbar rollbar = new Rollbar("e3a49f757f86465097c000cb2de9de08", "testing");
+        rollbar.error(new Exception("this is a test exception")); // should not throw an exception
+    }
 }