Skip to content
Permalink
Browse files
fix: allow all fields to be null values (#1450)
  • Loading branch information
yirutang committed Dec 23, 2021
1 parent bb494d8 commit e47ac791765486f02f027d3c440645ef2a379ba9
@@ -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;
}

@@ -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
@@ -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.