diff --git a/converter/src/main/java/tech/allegro/schema/json2avro/converter/JsonGenericRecordReader.java b/converter/src/main/java/tech/allegro/schema/json2avro/converter/JsonGenericRecordReader.java index d1c4547..480d5bb 100644 --- a/converter/src/main/java/tech/allegro/schema/json2avro/converter/JsonGenericRecordReader.java +++ b/converter/src/main/java/tech/allegro/schema/json2avro/converter/JsonGenericRecordReader.java @@ -168,6 +168,9 @@ private Object ensureEnum(Schema schema, Object value, Deque path) { if (symbols.contains(value)) { return new GenericData.EnumSymbol(schema, value); } + else if (schema.getEnumDefault() != null) { + return new GenericData.EnumSymbol(schema, schema.getEnumDefault()); + } throw enumException(path, symbols.stream().map(String::valueOf).collect(joining(", "))); } diff --git a/converter/src/test/groovy/tech/allegro/schema/json2avro/converter/JsonAvroConverterSpec.groovy b/converter/src/test/groovy/tech/allegro/schema/json2avro/converter/JsonAvroConverterSpec.groovy index 125f00f..bb46c9f 100644 --- a/converter/src/test/groovy/tech/allegro/schema/json2avro/converter/JsonAvroConverterSpec.groovy +++ b/converter/src/test/groovy/tech/allegro/schema/json2avro/converter/JsonAvroConverterSpec.groovy @@ -848,6 +848,45 @@ class JsonAvroConverterSpec extends Specification { exception.cause.message ==~ /.*enum type and be one of A, B, C.*/ } + def 'should fallback to enum default when passing an invalid enum type'() { + given: + def schema = ''' + { + "name": "testSchema", + "type": "record", + "fields": [ + { + "name" : "field_enum", + "type" : { + "name" : "MyEnums", + "type" : "enum", + "symbols" : [ "A", "B", "C" ], + "default" : "B" + } + } + ] + } + ''' + + def json = ''' + { + "field_enum": "D" + } + ''' + + def jsonAfterParse = ''' + { + "field_enum": "B" + } + ''' + + when: + def result = converter.convertToJson(converter.convertToAvro(json.bytes, schema), schema) + + then: + toMap(jsonAfterParse) == toMap(result) + } + def 'should accept null when value can be of any nullable array/map type'() { given: def schema = '''