Skip to content

Commit

Permalink
Nails down record default spec for unions (#486)
Browse files Browse the repository at this point in the history
  • Loading branch information
dg-builder authored May 1, 2023
1 parent 2717123 commit 54e2c11
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import com.linkedin.avroutil1.testcommon.TestUtil;
import org.apache.avro.AvroTypeException;
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericData;
import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.generic.IndexedRecord;
import org.apache.avro.io.BinaryDecoder;
import org.apache.avro.io.Decoder;
Expand Down Expand Up @@ -98,4 +100,39 @@ public void testCompatibleJsonParsingOfWrongNumericLiterals() throws Exception {
Assert.assertEquals(deserialized2.get(schema.getField("floatField").pos()), 3.0f);
Assert.assertEquals(deserialized2.get(schema.getField("doubleField").pos()), 4.0d);
}

@Test
public void demonstrateDefaultRecordsUnionFieldMustBeFirstElement() throws Exception {
Schema writerSchema =
AvroCompatibilityHelper.parse(TestUtil.load("allavro/RecordWithDefaultUnionField_writer.avsc"));
GenericData.Record record = new GenericData.Record(writerSchema);
byte[] bytes = AvroCodecUtil.serializeBinary(record);

// uses 1st field (correct)
Schema correctReaderSchema =
AvroCompatibilityHelper.parse(TestUtil.load("allavro/RecordWithDefaultUnionFieldCorrect_reader.avsc"));
GenericRecord deserializedGenericRecord =
AvroCodecUtil.deserializeAsGeneric(bytes, writerSchema, correctReaderSchema);
Assert.assertEquals(deserializedGenericRecord.get("mainField").toString(), "{\"f1\": \"default\"}");

// tries to use 2nd field as default (object notation)
Schema incorrectReaderSchemaUsingObjectNotation = AvroCompatibilityHelper.parse(
TestUtil.load("allavro/RecordWithDefaultUnionFieldIncorrectObjectNotation_reader.avsc"));
try {
AvroCodecUtil.deserializeAsGeneric(bytes, writerSchema, incorrectReaderSchemaUsingObjectNotation);
Assert.fail("Expected exception");
} catch (Exception e) {
Assert.assertTrue(e.getClass().getName().contains("AvroTypeException"));
}

// tries to use 2nd field as default (flat notation)
Schema incorrectReaderSchemaUsingFlatNotation = AvroCompatibilityHelper.parse(
TestUtil.load("allavro/RecordWithDefaultUnionFieldIncorrectFlatNotation_reader.avsc"));
try {
AvroCodecUtil.deserializeAsGeneric(bytes, writerSchema, incorrectReaderSchemaUsingFlatNotation);
Assert.fail("Expected exception");
} catch (Exception e) {
Assert.assertTrue(e.getClass().getName().contains("AvroTypeException"));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"type": "record",
"name": "SchemaWithRecordDefault",
"fields": [
{
"name": "mainField",
"type": {
"type": "record",
"name": "InnerRecord",
"fields": [
{
"name": "f1",
"type": [
"string",
"int",
"null"
]
}
]
},
"default": {
"f1": "default"
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"type": "record",
"name": "SchemaWithRecordDefault",
"fields": [
{
"name": "mainField",
"type": {
"type": "record",
"name": "InnerRecord",
"fields": [
{
"name": "f1",
"type": [
"string",
"int",
"null"
]
}
]
},
"default": {
"f1": null
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"type": "record",
"name": "SchemaWithRecordDefault",
"fields": [
{
"name": "mainField",
"type": {
"type": "record",
"name": "InnerRecord",
"fields": [
{
"name": "f1",
"type": [
"string",
"int",
"null"
]
}
]
},
"default": {
"f1": {
"int": 1
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "record",
"name": "SchemaWithRecordDefault",
"fields": [
]
}

0 comments on commit 54e2c11

Please sign in to comment.