Skip to content

Commit 3f27907

Browse files
psainicsvikasrathee-cs
authored andcommitted
Remove duplicate path when writing nested JSON array
1 parent a76b973 commit 3f27907

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed

src/main/java/io/cdap/plugin/gcp/bigquery/sink/BigQueryRecordToJson.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,10 @@ private static void writeArray(JsonWriter writer,
250250
}
251251
if (element instanceof StructuredRecord) {
252252
StructuredRecord record = (StructuredRecord) element;
253-
path.add(name);
254253
processRecord(writer, record, Objects.requireNonNull(record.getSchema().getFields()),
255254
path, jsonStringFieldsPaths);
256-
path.remove(path.size() - 1);
257255
} else {
258-
path.add(name);
259256
write(writer, name, true, element, componentSchema, path, jsonStringFieldsPaths);
260-
path.remove(path.size() - 1);
261257
}
262258
}
263259
}

src/test/java/io/cdap/plugin/gcp/bigquery/BigQueryRecordToJsonTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,58 @@ public void testJsonStringWithEmptyArray() throws IOException {
422422
}
423423
}
424424

425+
@Test
426+
public void testJsonStringWithStringArray() throws IOException {
427+
Schema recordSchema = Schema.recordOf("record",
428+
Schema.Field.of("arrayOfString", Schema.arrayOf(Schema.of(Schema.Type.STRING))));
429+
List<String> jsonStringList = ImmutableList.of("{\"arrayKey1\": \"arrayValue1\"}",
430+
"{\"arrayKey2\": \"arrayValue2\"}");
431+
StructuredRecord record = StructuredRecord.builder(recordSchema).set("arrayOfString", jsonStringList).build();
432+
Set<String> jsonStringFieldsPaths = ImmutableSet.of("arrayOfString");
433+
try (JsonTreeWriter writer = new JsonTreeWriter()) {
434+
writer.beginObject();
435+
for (Schema.Field recordField : Objects.requireNonNull(record.getSchema().getFields())) {
436+
if (recordSchema.getField(recordField.getName()) != null) {
437+
BigQueryRecordToJson.write(writer, recordField.getName(), record.get(recordField.getName()),
438+
recordField.getSchema(), jsonStringFieldsPaths);
439+
}
440+
}
441+
writer.endObject();
442+
JsonObject actual = writer.get().getAsJsonObject();
443+
String actualJsonString = actual.get("arrayOfString").getAsJsonArray().toString();
444+
String expectedJsonString = "[{\"arrayKey1\":\"arrayValue1\"},{\"arrayKey2\":\"arrayValue2\"}]";
445+
Assert.assertEquals(expectedJsonString, actualJsonString);
446+
}
447+
}
448+
449+
@Test
450+
public void testJsonStringWithArrayAndNestedRecord() throws IOException {
451+
Schema nestedRecordSchema = Schema.recordOf("nestedRecord",
452+
Schema.Field.of("nestedJsonString", Schema.of(Schema.Type.STRING)));
453+
StructuredRecord nestedRecord = StructuredRecord.builder(nestedRecordSchema)
454+
.set("nestedJsonString", "{\"nestedKey1\":\"nestedValue1\"}").build();
455+
Schema recordSchema = Schema.recordOf("record",
456+
Schema.Field.of("arrayOfNestedRecord", Schema.arrayOf(nestedRecordSchema)));
457+
List<StructuredRecord> nestedRecordList = ImmutableList.of(nestedRecord);
458+
StructuredRecord record = StructuredRecord.builder(recordSchema).set("arrayOfNestedRecord", nestedRecordList)
459+
.build();
460+
461+
Set<String> jsonStringFieldsPaths = ImmutableSet.of("arrayOfNestedRecord.nestedJsonString");
462+
try (JsonTreeWriter writer = new JsonTreeWriter()) {
463+
writer.beginObject();
464+
for (Schema.Field recordField : Objects.requireNonNull(record.getSchema().getFields())) {
465+
if (recordSchema.getField(recordField.getName()) != null) {
466+
BigQueryRecordToJson.write(writer, recordField.getName(), record.get(recordField.getName()),
467+
recordField.getSchema(), jsonStringFieldsPaths);
468+
}
469+
}
470+
writer.endObject();
471+
JsonObject actual = writer.get().getAsJsonObject();
472+
String actualJsonString = actual.get("arrayOfNestedRecord").toString();
473+
String expectedJsonString = "[{\"nestedJsonString\":{\"nestedKey1\":\"nestedValue1\"}}]";
474+
Assert.assertEquals(expectedJsonString, actualJsonString);
475+
}
476+
}
425477

426478
/**
427479
* Empty JSON string is not a valid JSON string and should throw an exception.

0 commit comments

Comments
 (0)