Skip to content

Commit

Permalink
fix: allow all fields to be null values (#1450)
Browse files Browse the repository at this point in the history
  • Loading branch information
yirutang authored Dec 23, 2021
1 parent bb494d8 commit e47ac79
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,6 @@ private static DynamicMessage convertJsonToProtoMessageImpl(
String.format(
"JSONObject does not have the required field %s.%s.", jsonScope, missingFieldName));
}
if (topLevel && msg.getSerializedSize() == 0) {
throw new IllegalArgumentException("The created protobuf message is empty.");
}
return msg;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1035,13 +1035,9 @@ public void testEmptyProtoMessage() throws Exception {
JSONObject json = new JSONObject();
json.put("test_repeated", new JSONArray(new int[0]));

try {
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(RepeatedInt64.getDescriptor(), json);
Assert.fail("Should fail");
} catch (IllegalArgumentException e) {
assertEquals("The created protobuf message is empty.", e.getMessage());
}
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(RepeatedInt64.getDescriptor(), json);
assertEquals(protoMsg.getAllFields().size(), 0);
}

@Test
Expand Down Expand Up @@ -1123,4 +1119,15 @@ public void testJsonNullValue() throws Exception {
JsonToProtoMessage.convertJsonToProtoMessage(TestInt64.getDescriptor(), json);
assertEquals(expectedProto, protoMsg);
}

@Test
public void testJsonAllFieldsNullValue() throws Exception {
TestInt64 expectedProto = TestInt64.newBuilder().build();
JSONObject json = new JSONObject();
json.put("long", JSONObject.NULL);
json.put("int", JSONObject.NULL);
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(TestInt64.getDescriptor(), json);
assertEquals(expectedProto, protoMsg);
}
}

0 comments on commit e47ac79

Please sign in to comment.