Skip to content

Commit f5807a5

Browse files
change file
1 parent 3eea7c5 commit f5807a5

2 files changed

Lines changed: 4 additions & 52 deletions

File tree

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

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

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

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -422,58 +422,6 @@ 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-
}
477425

478426
/**
479427
* Empty JSON string is not a valid JSON string and should throw an exception.

0 commit comments

Comments
 (0)