From f5807a544cd7b92a84c9efd359e160ef81cdf08f Mon Sep 17 00:00:00 2001 From: priyabhatnagar25 <112169140+priyabhatnagar25@users.noreply.github.com> Date: Tue, 16 Jan 2024 16:19:22 +0530 Subject: [PATCH] change file --- .../bigquery/sink/BigQueryRecordToJson.java | 4 ++ .../bigquery/BigQueryRecordToJsonTest.java | 52 ------------------- 2 files changed, 4 insertions(+), 52 deletions(-) diff --git a/src/main/java/io/cdap/plugin/gcp/bigquery/sink/BigQueryRecordToJson.java b/src/main/java/io/cdap/plugin/gcp/bigquery/sink/BigQueryRecordToJson.java index c397e52c74..ec2c70edb7 100644 --- a/src/main/java/io/cdap/plugin/gcp/bigquery/sink/BigQueryRecordToJson.java +++ b/src/main/java/io/cdap/plugin/gcp/bigquery/sink/BigQueryRecordToJson.java @@ -250,10 +250,14 @@ private static void writeArray(JsonWriter writer, } if (element instanceof StructuredRecord) { StructuredRecord record = (StructuredRecord) element; + path.add(name); processRecord(writer, record, Objects.requireNonNull(record.getSchema().getFields()), path, jsonStringFieldsPaths); + path.remove(path.size() - 1); } else { + path.add(name); write(writer, name, true, element, componentSchema, path, jsonStringFieldsPaths); + path.remove(path.size() - 1); } } } diff --git a/src/test/java/io/cdap/plugin/gcp/bigquery/BigQueryRecordToJsonTest.java b/src/test/java/io/cdap/plugin/gcp/bigquery/BigQueryRecordToJsonTest.java index e7c1f82aee..962a44fd28 100644 --- a/src/test/java/io/cdap/plugin/gcp/bigquery/BigQueryRecordToJsonTest.java +++ b/src/test/java/io/cdap/plugin/gcp/bigquery/BigQueryRecordToJsonTest.java @@ -422,58 +422,6 @@ public void testJsonStringWithEmptyArray() throws IOException { } } - @Test - public void testJsonStringWithStringArray() throws IOException { - Schema recordSchema = Schema.recordOf("record", - Schema.Field.of("arrayOfString", Schema.arrayOf(Schema.of(Schema.Type.STRING)))); - List jsonStringList = ImmutableList.of("{\"arrayKey1\": \"arrayValue1\"}", - "{\"arrayKey2\": \"arrayValue2\"}"); - StructuredRecord record = StructuredRecord.builder(recordSchema).set("arrayOfString", jsonStringList).build(); - Set jsonStringFieldsPaths = ImmutableSet.of("arrayOfString"); - try (JsonTreeWriter writer = new JsonTreeWriter()) { - writer.beginObject(); - for (Schema.Field recordField : Objects.requireNonNull(record.getSchema().getFields())) { - if (recordSchema.getField(recordField.getName()) != null) { - BigQueryRecordToJson.write(writer, recordField.getName(), record.get(recordField.getName()), - recordField.getSchema(), jsonStringFieldsPaths); - } - } - writer.endObject(); - JsonObject actual = writer.get().getAsJsonObject(); - String actualJsonString = actual.get("arrayOfString").getAsJsonArray().toString(); - String expectedJsonString = "[{\"arrayKey1\":\"arrayValue1\"},{\"arrayKey2\":\"arrayValue2\"}]"; - Assert.assertEquals(expectedJsonString, actualJsonString); - } - } - - @Test - public void testJsonStringWithArrayAndNestedRecord() throws IOException { - Schema nestedRecordSchema = Schema.recordOf("nestedRecord", - Schema.Field.of("nestedJsonString", Schema.of(Schema.Type.STRING))); - StructuredRecord nestedRecord = StructuredRecord.builder(nestedRecordSchema) - .set("nestedJsonString", "{\"nestedKey1\":\"nestedValue1\"}").build(); - Schema recordSchema = Schema.recordOf("record", - Schema.Field.of("arrayOfNestedRecord", Schema.arrayOf(nestedRecordSchema))); - List nestedRecordList = ImmutableList.of(nestedRecord); - StructuredRecord record = StructuredRecord.builder(recordSchema).set("arrayOfNestedRecord", nestedRecordList) - .build(); - - Set jsonStringFieldsPaths = ImmutableSet.of("arrayOfNestedRecord.nestedJsonString"); - try (JsonTreeWriter writer = new JsonTreeWriter()) { - writer.beginObject(); - for (Schema.Field recordField : Objects.requireNonNull(record.getSchema().getFields())) { - if (recordSchema.getField(recordField.getName()) != null) { - BigQueryRecordToJson.write(writer, recordField.getName(), record.get(recordField.getName()), - recordField.getSchema(), jsonStringFieldsPaths); - } - } - writer.endObject(); - JsonObject actual = writer.get().getAsJsonObject(); - String actualJsonString = actual.get("arrayOfNestedRecord").toString(); - String expectedJsonString = "[{\"nestedJsonString\":{\"nestedKey1\":\"nestedValue1\"}}]"; - Assert.assertEquals(expectedJsonString, actualJsonString); - } - } /** * Empty JSON string is not a valid JSON string and should throw an exception.