Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow all fields to be null values #1450

Merged
merged 3 commits into from Dec 23, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Expand Up @@ -1033,13 +1033,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 @@ -1121,4 +1117,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);
}
}