Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BE: fixes: avro schema ser for nullable enums #685

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please keep the comment here that this if for primitive types? Thanks

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

potentially you can have 2 unums with same names but with different packages. This is why we getting full name in RECORD

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Haarolean comment restored!

};
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 @@ -700,6 +700,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
Loading