Skip to content
This repository was archived by the owner on Feb 24, 2026. It is now read-only.

Commit ef24825

Browse files
fix: allow repeated field to have a null or missing json array (#1760)
* 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>
1 parent ac3f0b8 commit ef24825

3 files changed

Lines changed: 42 additions & 0 deletions

File tree

google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/JsonToProtoMessage.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,11 @@ private static void fillRepeatedField(
502502
try {
503503
jsonArray = json.getJSONArray(exactJsonKeyName);
504504
} catch (JSONException e) {
505+
java.lang.Object val = json.get(exactJsonKeyName);
506+
// It is OK for repeated field to be null.
507+
if (val == JSONObject.NULL) {
508+
return;
509+
}
505510
throw new IllegalArgumentException(
506511
"JSONObject does not have a array field at " + currentScope + ".");
507512
}

google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/JsonToProtoMessageTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,4 +1275,40 @@ public void testBadJsonFieldIntRepeated() throws Exception {
12751275
assertEquals(ex.getMessage(), "Text 'blah' could not be parsed at index 0");
12761276
}
12771277
}
1278+
1279+
@Test
1280+
public void testNullRepeatedField() throws Exception {
1281+
TableSchema ts =
1282+
TableSchema.newBuilder()
1283+
.addFields(
1284+
0,
1285+
TableFieldSchema.newBuilder()
1286+
.setName("test_repeated")
1287+
.setType(TableFieldSchema.Type.DATE)
1288+
.setMode(TableFieldSchema.Mode.REPEATED)
1289+
.build())
1290+
.addFields(
1291+
1,
1292+
TableFieldSchema.newBuilder()
1293+
.setName("test_non_repeated")
1294+
.setType(TableFieldSchema.Type.DATE)
1295+
.setMode(TableFieldSchema.Mode.NULLABLE)
1296+
.build())
1297+
.build();
1298+
JSONObject json = new JSONObject();
1299+
// Null repeated field.
1300+
json.put("test_repeated", JSONObject.NULL);
1301+
1302+
DynamicMessage protoMsg =
1303+
JsonToProtoMessage.convertJsonToProtoMessage(RepeatedInt32.getDescriptor(), ts, json);
1304+
assertTrue(protoMsg.getAllFields().isEmpty());
1305+
1306+
// Missing repeated field.
1307+
json = new JSONObject();
1308+
json.put("test_non_repeated", JSONObject.NULL);
1309+
1310+
protoMsg =
1311+
JsonToProtoMessage.convertJsonToProtoMessage(RepeatedInt32.getDescriptor(), ts, json);
1312+
assertTrue(protoMsg.getAllFields().isEmpty());
1313+
}
12781314
}

google-cloud-bigquerystorage/src/test/proto/jsonTest.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ message RepeatedInt64 {
8989

9090
message RepeatedInt32 {
9191
repeated int32 test_repeated = 1;
92+
optional int32 test_non_repeated = 2;
9293
}
9394

9495
message RepeatedDouble {

0 commit comments

Comments
 (0)