Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
jurgispods committed Jan 24, 2024
1 parent 00e84e0 commit acfe0e5
Showing 1 changed file with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1997,6 +1997,49 @@ public void testLongWithConnectDefault() {
assertEquals(42L, schema.field("f").schema().defaultValue());
}

@Test
public void testNestedRecordWithNullDefault() {
final String fullSchema = "{"
+ " \"name\": \"RecordWithObjectDefault\","
+ " \"type\": \"record\","
+ " \"fields\": [{"
+ " \"name\": \"obj\","
+ " \"default\": {\"nullableString\": null},"
+ " \"type\": {"
+ " \"name\": \"Object\","
+ " \"type\": \"record\","
+ " \"fields\": [{"
+ " \"name\": \"nullableString\","
+ " \"type\": [\"null\",\"string\"]}"
+ " ]}"
+ " }]"
+ "}";

org.apache.avro.Schema avroSchema = new org.apache.avro.Schema.Parser().parse(fullSchema);

org.apache.avro.Schema innerSchema = new org.apache.avro.Schema.Parser().parse("{"
+ " \"name\": \"Object\","
+ " \"type\": \"record\","
+ " \"fields\": [{"
+ " \"name\": \"nullableString\","
+ " \"type\": [\"null\",\"string\"]}"
+ " ]}");

AvroData avroData = new AvroData(0);

// test record:
// {"obj": {"nullableString": null}}
GenericRecord nestedRecord = new GenericRecordBuilder(avroSchema)
.set("obj", new GenericRecordBuilder(innerSchema).set("nullableString", null).build())
.build();

SchemaAndValue schemaAndValue = avroData.toConnectData(avroSchema, nestedRecord);
Struct value = (Struct)schemaAndValue.value();
assertNotNull(value.get("obj"));
Struct objFieldValue = (Struct)value.get("obj");
assertNull(objFieldValue.get("nullableString"));
}

@Test
public void testArrayOfRecordWithNullNamespace() {
org.apache.avro.Schema avroSchema = org.apache.avro.SchemaBuilder.array().items()
Expand Down

0 comments on commit acfe0e5

Please sign in to comment.