Skip to content

Commit

Permalink
fixes: avro schema ser for nullable enums
Browse files Browse the repository at this point in the history
  • Loading branch information
DimaVilda committed Nov 29, 2024
1 parent e7df880 commit 843622f
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,11 @@ private FieldSchema createUnionSchema(Schema schema, Map<String, FieldSchema> de
final Map<String, FieldSchema> fields = schema.getTypes().stream()
.filter(t -> !t.getType().equals(Schema.Type.NULL))
.map(f -> {
String oneOfFieldName;
if (f.getType().equals(Schema.Type.RECORD)) {
// for records using full record name
oneOfFieldName = f.getFullName();
} else {
// for primitive types - using type name
oneOfFieldName = f.getType().getName().toLowerCase();
}
String oneOfFieldName = switch (f.getType()) {
case RECORD -> f.getFullName();
case ENUM -> f.getName();
default -> f.getType().getName().toLowerCase();
};
return Tuples.of(oneOfFieldName, convertSchema(f, definitions, false));
}).collect(Collectors.toMap(
Tuple2::getT1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,48 @@ void testRecordReferences() {
convertAndCompare(expectedJsonSchema, avroSchema);
}

@Test
void testNullableUnionEnum() {
String avroSchema =
" {"
+ " \"type\": \"record\","
+ " \"name\": \"Message\","
+ " \"namespace\": \"com.provectus.kafka\","
+ " \"fields\": ["
+ " {"
+ " \"name\": \"enum_nullable_union\","
+ " \"type\": [\"null\", {"
+ " \"type\": \"enum\","
+ " \"name\": \"Suit\","
+ " \"symbols\": [\"SPADES\",\"HEARTS\",\"DIAMONDS\",\"CLUBS\"]"
+ " }]"
+ " }"
+ " ]"
+ " }";

String expectedJsonSchema =
"{\"$id\":\"http://example.com/Message\","
+ "\"$schema\":\"https://json-schema.org/draft/2020-12/schema\","
+ "\"type\":\"object\","
+ "\"properties\":{"
+ "\"enum_nullable_union\":{"
+ "\"oneOf\":["
+ "{\"type\":\"null\"},"
+ "{\"type\":\"object\","
+ "\"properties\":{"
+ "\"Suit\":{"
+ "\"type\":\"string\","
+ "\"enum\":[\"SPADES\",\"HEARTS\",\"DIAMONDS\",\"CLUBS\"]"
+ "}}}"
+ "]"
+ "}},"
+ "\"definitions\":{"
+ "\"com.provectus.kafka.Message\":{\"$ref\":\"#\"}"
+ "}}";

convertAndCompare(expectedJsonSchema, avroSchema);
}

@SneakyThrows
private void convertAndCompare(String expectedJsonSchema, String sourceAvroSchema) {
var parseAvroSchema = new Schema.Parser().parse(sourceAvroSchema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,43 @@ void unionFieldWithInnerTypesNamesClash() {

}

@Test
void unionNullableEnumField() {
var schema = createSchema(
"""
{
"type": "record",
"namespace": "com.test",
"name": "TestAvroRecord",
"fields": [
{
"name": "enum_nullable_union",
"type" : [ "null", {
"type" : "enum",
"name" : "Suit",
"symbols" : ["SPADES", "HEARTS", "DIAMONDS", "CLUBS"]
} ]
}
]
}"""
);

GenericData.Record inputRecord = new GenericData.Record(schema);
inputRecord.put("enum_nullable_union",
new GenericData.EnumSymbol(
schema.getField("enum_nullable_union").schema().getTypes().get(1), "SPADES"));
String expectedJsonWithEnum = """
{
"enum_nullable_union": { "Suit": "SPADES"}\s
}
\s""";
assertJsonsEqual(expectedJsonWithEnum, convertAvroToJson(inputRecord, schema));

GenericData.Record inputNullRecord = new GenericData.Record(schema);
inputNullRecord.put("enum_nullable_union", null);
assertJsonsEqual("{}", convertAvroToJson(inputNullRecord, schema));
}

private Schema createSchema(String schema) {
return new AvroSchema(schema).rawSchema();
}
Expand Down

0 comments on commit 843622f

Please sign in to comment.