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 repeated field to have a null or missing json array #1760

Merged
merged 5 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,11 @@ private static void fillRepeatedField(
try {
jsonArray = json.getJSONArray(exactJsonKeyName);
} catch (JSONException e) {
java.lang.Object val = json.get(exactJsonKeyName);
// It is OK for repeated field to be null.
if (val == JSONObject.NULL) {
return;
}
throw new IllegalArgumentException(
"JSONObject does not have a array field at " + currentScope + ".");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1275,4 +1275,40 @@ public void testBadJsonFieldIntRepeated() throws Exception {
assertEquals(ex.getMessage(), "Text 'blah' could not be parsed at index 0");
}
}

@Test
public void testNullRepeatedField() throws Exception {
TableSchema ts =
TableSchema.newBuilder()
.addFields(
0,
TableFieldSchema.newBuilder()
.setName("test_repeated")
.setType(TableFieldSchema.Type.DATE)
.setMode(TableFieldSchema.Mode.REPEATED)
.build())
.addFields(
1,
TableFieldSchema.newBuilder()
.setName("test_non_repeated")
.setType(TableFieldSchema.Type.DATE)
.setMode(TableFieldSchema.Mode.NULLABLE)
.build())
.build();
JSONObject json = new JSONObject();
// Null repeated field.
json.put("test_repeated", JSONObject.NULL);

DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(RepeatedInt32.getDescriptor(), ts, json);
assertTrue(protoMsg.getAllFields().isEmpty());

// Missing repeated field.
json = new JSONObject();
json.put("test_non_repeated", JSONObject.NULL);

protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(RepeatedInt32.getDescriptor(), ts, json);
assertTrue(protoMsg.getAllFields().isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ message RepeatedInt64 {

message RepeatedInt32 {
repeated int32 test_repeated = 1;
optional int32 test_non_repeated = 2;
}

message RepeatedDouble {
Expand Down