Skip to content

Commit bb73f23

Browse files
committed
fix possible NPE and add test
1 parent d0b4e97 commit bb73f23

3 files changed

Lines changed: 29 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 3.4.2 (2018-11-28)
4+
5+
* Ensure session counts are thread safe
6+
[#122](https://github.com/bugsnag/bugsnag-java/pull/122)
7+
38
## 3.4.1
49

510
(Skipped, duplicate of 3.4.0)

bugsnag/src/main/java/com/bugsnag/Report.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,16 +137,16 @@ Map<String, Object> getSession() {
137137
void setSession(Session session) {
138138
if (session == null) {
139139
sessionMap = null;
140+
} else {
141+
sessionMap = new HashMap<String, Object>();
142+
sessionMap.put("id", session.getId());
143+
sessionMap.put("startedAt", session.getStartedAt());
144+
145+
Map<String, Object> handledCounts = new HashMap<String, Object>();
146+
handledCounts.put("handled", session.getHandledCount());
147+
handledCounts.put("unhandled", session.getUnhandledCount());
148+
sessionMap.put("events", handledCounts);
140149
}
141-
142-
sessionMap = new HashMap<String, Object>();
143-
sessionMap.put("id", session.getId());
144-
sessionMap.put("startedAt", session.getStartedAt());
145-
146-
Map<String, Object> handledCounts = new HashMap<String, Object>();
147-
handledCounts.put("handled", session.getHandledCount());
148-
handledCounts.put("unhandled", session.getUnhandledCount());
149-
sessionMap.put("events", handledCounts);
150150
}
151151

152152
/**

bugsnag/src/test/java/com/bugsnag/NotificationTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static org.junit.Assert.assertNotNull;
66
import static org.junit.Assert.assertNull;
77

8+
import com.fasterxml.jackson.annotation.JsonInclude;
89
import com.fasterxml.jackson.databind.JsonNode;
910
import com.fasterxml.jackson.databind.ObjectMapper;
1011
import org.junit.Before;
@@ -31,6 +32,9 @@ public void setUp() {
3132
config.appVersion = "1.2.3";
3233
config.releaseStage = "dev";
3334
report = new Report(config, new RuntimeException());
35+
36+
// Only include properties with non-null values
37+
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
3438
}
3539

3640
private JsonNode generateJson(ObjectMapper mapper,
@@ -67,6 +71,17 @@ public void testWithoutSessionSerialisation() throws Throwable {
6771
assertNull(rootNode.get("events").get("session"));
6872
}
6973

74+
@Test
75+
public void testNullSession() throws Throwable {
76+
report.setSession(null);
77+
78+
JsonNode rootNode = generateJson(mapper, config, report);
79+
validateErrorReport(rootNode);
80+
81+
JsonNode session = rootNode.get("events").get(0).get("session");
82+
assertNull(session);
83+
}
84+
7085
private void validateErrorReport(JsonNode rootNode) {
7186
assertNotNull(rootNode);
7287
assertNotNull(rootNode.get("apiKey").asText());

0 commit comments

Comments
 (0)