Skip to content

Commit

Permalink
fix: allow repeated field to have a null or missing json array (#1760)
Browse files Browse the repository at this point in the history
* feat: Throw explicit exception in the case of Json input has more fields than table schema of the table

* .

* fix: allow repeated field to have a null or missing json array

* .

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
yirutang and gcf-owl-bot[bot] committed Aug 24, 2022
1 parent ac3f0b8 commit ef24825
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
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
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());
}
}
1 change: 1 addition & 0 deletions google-cloud-bigquerystorage/src/test/proto/jsonTest.proto
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

0 comments on commit ef24825

Please sign in to comment.