Skip to content

Commit 69eb2fc

Browse files
committed
Json parsing: fail on null for primitives
1 parent a8bb23f commit 69eb2fc

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/main/java/de/rwth/idsg/steve/ocpp/ws/JsonObjectMapper.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import de.rwth.idsg.steve.ocpp.ws.ocpp15.Ocpp15JacksonModule;
1010
import de.rwth.idsg.steve.ocpp.ws.ocpp16.Ocpp16JacksonModule;
1111

12+
import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES;
13+
1214
/**
1315
* Because ObjectMapper can and should be reused, if config does not change after init.
1416
*
@@ -24,6 +26,15 @@ public enum JsonObjectMapper {
2426
mapper = new ObjectMapper();
2527
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
2628

29+
// OCPP messages contain some mandatory primitive fields (like transactionId), that are not allowed
30+
// to be null. any misinterpretation/mapping of these fields like "null -> 0" is a mistake.
31+
//
32+
// true story: while testing with abusive-charge-point, it sends stopTransactions where transactionId=null
33+
// in communication flows, where a startTransaction before causes an Exception and we cannot send a regular
34+
// response with a transactionId, but an error message. if we do not fail early, it will fail at the database
35+
// level which we want to prevent.
36+
mapper.configure(FAIL_ON_NULL_FOR_PRIMITIVES, true);
37+
2738
mapper.registerModule(new Ocpp12JacksonModule());
2839
mapper.registerModule(new Ocpp15JacksonModule());
2940
mapper.registerModule(new Ocpp16JacksonModule());

0 commit comments

Comments
 (0)