Skip to content

Commit

Permalink
Added handling of union type in AvscParser to handles cases like Map …
Browse files Browse the repository at this point in the history
…of union (#547)
  • Loading branch information
li-ukumar authored Feb 13, 2024
1 parent 2de30d1 commit 5249a34
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,16 @@ mapSchema, locationOf(context.getUri(), literalNode), map
return new LiteralOrIssue(new AvroRecordLiteral(
recordSchema, locationOf(context.getUri(), literalNode), map
));
case UNION:
AvroUnionSchema unionSchema = (AvroUnionSchema) schema;
LiteralOrIssue branchValueOrIssue =
parseLiteral(literalNode, unionSchema.getTypes().get(0).getSchema(), fieldName, context);
if (branchValueOrIssue.getIssue() == null) {
return branchValueOrIssue;
}
return new LiteralOrIssue(
AvscIssues.badFieldDefaultValue(locationOf(context.getUri(), literalNode), literalNode.toString(),
avroType, fieldName));
default:
throw new UnsupportedOperationException("dont know how to parse a " + avroType + " at " + literalNode.getStartLocation()
+ " out of a " + literalNode.getValueType() + " (" + literalNode + ")");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,21 @@ public void testRecursiveReferenceUnionWithDefault() throws Exception {
Assert.assertEquals(recordParseResult.getIssues().size(), 0);
}

@Test
public void testMapOfUnionDefault() throws Exception {
String recordAvsc = TestUtil.load("schemas/RecordWithMapOfUnion.avsc");
AvscParser avscParser = new AvscParser();

AvscParseResult recordParseResult = avscParser.parse(recordAvsc);
AvroRecordSchema topLevelSchema = (AvroRecordSchema) recordParseResult.getTopLevelSchema();

// Tests we don't have unparsed default values in for of AvscUnparsedLiteral.
new AvscSchemaWriter().writeSingle(topLevelSchema);

Assert.assertFalse(hasUnparsedDefault(topLevelSchema));
Assert.assertEquals(recordParseResult.getIssues().size(), 0);
}

/**
* Recursively goes through all fields and their schemas to check if any field has an unparsed default value
* @param schema
Expand Down
24 changes: 24 additions & 0 deletions parser/src/test/resources/schemas/RecordWithMapOfUnion.avsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"type": "record",
"name": "RecordWithMapOfUnion",
"fields": [
{
"name": "myField2",
"type": {
"type": "map",
"values": [
"string",
{
"type": "array",
"items": "string"
},
{
"type": "map",
"values": "string"
}
]
},
"default": {"key1": "value1"}
}
]
}

0 comments on commit 5249a34

Please sign in to comment.