Skip to content

Commit

Permalink
Avoid deserializing null nodes in reflection free Jackson serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
mariofusco committed Nov 5, 2024
1 parent 6ca44ed commit 33066b8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@
* Map.Entry entry = (Map.iterator) var3.next();
* String field = (String) entry.getKey();
* JsonNode jsonNode = (JsonNode) entry.getValue();
* if (jsonNode.isNull()) {
* continue;
* }
* switch (field) {
* case "content":
* dataItem.setContent(context.readTreeAsValue(jsonNode, this.valueTypes[0]));
Expand Down Expand Up @@ -240,10 +243,13 @@ private boolean deserializeObject(ClassInfo classInfo, ResultHandle objHandle, C
.invokeInterfaceMethod(ofMethod(Map.Entry.class, "getKey", Object.class), mapEntry);
ResultHandle fieldValue = loopCreator.checkCast(loopCreator
.invokeInterfaceMethod(ofMethod(Map.Entry.class, "getValue", Object.class), mapEntry), JsonNode.class);
Switch.StringSwitch strSwitch = loopCreator.stringSwitch(fieldName);

loopCreator.ifTrue(loopCreator.invokeVirtualMethod(ofMethod(JsonNode.class, "isNull", boolean.class), fieldValue))
.trueBranch().continueScope(loopCreator);

Set<String> deserializedFields = new HashSet<>();
ResultHandle deserializationContext = deserialize.getMethodParam(1);
Switch.StringSwitch strSwitch = loopCreator.stringSwitch(fieldName);
return deserializeFields(classCreator, classInfo, deserializationContext, objHandle, fieldValue, deserializedFields,
strSwitch, parseTypeParameters(classInfo, classCreator));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,24 @@ public void testEcho() {
.body("veterinarian.title", Matchers.nullValue());
}

@Test
public void testEchoWithNullString() {
RestAssured
.with()
.body("{\"publicName\":null,\"veterinarian\":{\"name\":\"Dolittle\"},\"age\":5,\"vaccinated\":true}")
.contentType("application/json; charset=utf-8")
.post("/simple/dog-echo")
.then()
.statusCode(200)
.contentType("application/json")
.body("publicName", Matchers.nullValue())
.body("privateName", Matchers.nullValue())
.body("age", Matchers.is(5))
.body("vaccinated", Matchers.is(true))
.body("veterinarian.name", Matchers.is("Dolittle"))
.body("veterinarian.title", Matchers.nullValue());
}

@Test
public void testEchoWithMissingPrimitive() {
RestAssured
Expand Down

0 comments on commit 33066b8

Please sign in to comment.