Skip to content

Commit 843622f

Browse files
committed
fixes: avro schema ser for nullable enums
1 parent e7df880 commit 843622f

File tree

3 files changed

+84
-8
lines changed

3 files changed

+84
-8
lines changed

api/src/main/java/io/kafbat/ui/util/jsonschema/AvroJsonSchemaConverter.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,11 @@ private FieldSchema createUnionSchema(Schema schema, Map<String, FieldSchema> de
8080
final Map<String, FieldSchema> fields = schema.getTypes().stream()
8181
.filter(t -> !t.getType().equals(Schema.Type.NULL))
8282
.map(f -> {
83-
String oneOfFieldName;
84-
if (f.getType().equals(Schema.Type.RECORD)) {
85-
// for records using full record name
86-
oneOfFieldName = f.getFullName();
87-
} else {
88-
// for primitive types - using type name
89-
oneOfFieldName = f.getType().getName().toLowerCase();
90-
}
83+
String oneOfFieldName = switch (f.getType()) {
84+
case RECORD -> f.getFullName();
85+
case ENUM -> f.getName();
86+
default -> f.getType().getName().toLowerCase();
87+
};
9188
return Tuples.of(oneOfFieldName, convertSchema(f, definitions, false));
9289
}).collect(Collectors.toMap(
9390
Tuple2::getT1,

api/src/test/java/io/kafbat/ui/util/jsonschema/AvroJsonSchemaConverterTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,48 @@ void testRecordReferences() {
244244
convertAndCompare(expectedJsonSchema, avroSchema);
245245
}
246246

247+
@Test
248+
void testNullableUnionEnum() {
249+
String avroSchema =
250+
" {"
251+
+ " \"type\": \"record\","
252+
+ " \"name\": \"Message\","
253+
+ " \"namespace\": \"com.provectus.kafka\","
254+
+ " \"fields\": ["
255+
+ " {"
256+
+ " \"name\": \"enum_nullable_union\","
257+
+ " \"type\": [\"null\", {"
258+
+ " \"type\": \"enum\","
259+
+ " \"name\": \"Suit\","
260+
+ " \"symbols\": [\"SPADES\",\"HEARTS\",\"DIAMONDS\",\"CLUBS\"]"
261+
+ " }]"
262+
+ " }"
263+
+ " ]"
264+
+ " }";
265+
266+
String expectedJsonSchema =
267+
"{\"$id\":\"http://example.com/Message\","
268+
+ "\"$schema\":\"https://json-schema.org/draft/2020-12/schema\","
269+
+ "\"type\":\"object\","
270+
+ "\"properties\":{"
271+
+ "\"enum_nullable_union\":{"
272+
+ "\"oneOf\":["
273+
+ "{\"type\":\"null\"},"
274+
+ "{\"type\":\"object\","
275+
+ "\"properties\":{"
276+
+ "\"Suit\":{"
277+
+ "\"type\":\"string\","
278+
+ "\"enum\":[\"SPADES\",\"HEARTS\",\"DIAMONDS\",\"CLUBS\"]"
279+
+ "}}}"
280+
+ "]"
281+
+ "}},"
282+
+ "\"definitions\":{"
283+
+ "\"com.provectus.kafka.Message\":{\"$ref\":\"#\"}"
284+
+ "}}";
285+
286+
convertAndCompare(expectedJsonSchema, avroSchema);
287+
}
288+
247289
@SneakyThrows
248290
private void convertAndCompare(String expectedJsonSchema, String sourceAvroSchema) {
249291
var parseAvroSchema = new Schema.Parser().parse(sourceAvroSchema);

api/src/test/java/io/kafbat/ui/util/jsonschema/JsonAvroConversionTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,43 @@ void unionFieldWithInnerTypesNamesClash() {
699699

700700
}
701701

702+
@Test
703+
void unionNullableEnumField() {
704+
var schema = createSchema(
705+
"""
706+
{
707+
"type": "record",
708+
"namespace": "com.test",
709+
"name": "TestAvroRecord",
710+
"fields": [
711+
{
712+
"name": "enum_nullable_union",
713+
"type" : [ "null", {
714+
"type" : "enum",
715+
"name" : "Suit",
716+
"symbols" : ["SPADES", "HEARTS", "DIAMONDS", "CLUBS"]
717+
} ]
718+
}
719+
]
720+
}"""
721+
);
722+
723+
GenericData.Record inputRecord = new GenericData.Record(schema);
724+
inputRecord.put("enum_nullable_union",
725+
new GenericData.EnumSymbol(
726+
schema.getField("enum_nullable_union").schema().getTypes().get(1), "SPADES"));
727+
String expectedJsonWithEnum = """
728+
{
729+
"enum_nullable_union": { "Suit": "SPADES"}\s
730+
}
731+
\s""";
732+
assertJsonsEqual(expectedJsonWithEnum, convertAvroToJson(inputRecord, schema));
733+
734+
GenericData.Record inputNullRecord = new GenericData.Record(schema);
735+
inputNullRecord.put("enum_nullable_union", null);
736+
assertJsonsEqual("{}", convertAvroToJson(inputNullRecord, schema));
737+
}
738+
702739
private Schema createSchema(String schema) {
703740
return new AvroSchema(schema).rawSchema();
704741
}

0 commit comments

Comments
 (0)