diff --git a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/schema/AvroSchemaHelper.java b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/schema/AvroSchemaHelper.java index 00f0592f6..f548de1ff 100644 --- a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/schema/AvroSchemaHelper.java +++ b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/schema/AvroSchemaHelper.java @@ -269,12 +269,17 @@ public static Schema parseJsonSchema(String json) { * @param values List of enum names * @return An {@link org.apache.avro.Schema.Type#ENUM ENUM} schema. */ - public static Schema createEnumSchema(BeanDescription bean, List values) { + public static Schema createEnumSchema(BeanDescription bean, List values, + AnnotationIntrospector intr) { final JavaType enumType = bean.getType(); + @SuppressWarnings("unchecked") + Class> rawEnumClass = (Class>) enumType.getRawClass(); + Enum defaultEnumValue = intr.findDefaultEnumValue(bean.getClassInfo(), + rawEnumClass.getEnumConstants()); return addAlias(Schema.createEnum( getName(enumType), bean.findClassDescription(), - getNamespace(enumType, bean.getClassInfo()), values + getNamespace(enumType, bean.getClassInfo()), values, defaultEnumValue != null ? defaultEnumValue.toString() : null ), bean); } diff --git a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/schema/EnumVisitor.java b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/schema/EnumVisitor.java index 20f61e163..25aa9ec66 100644 --- a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/schema/EnumVisitor.java +++ b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/schema/EnumVisitor.java @@ -43,7 +43,8 @@ public Schema builtAvroSchema() { } BeanDescription bean = _provider.getConfig().introspectClassAnnotations(_type); - Schema schema = AvroSchemaHelper.createEnumSchema(bean, new ArrayList<>(_enums)); + Schema schema = AvroSchemaHelper.createEnumSchema(bean, new ArrayList<>(_enums), + _provider.getAnnotationIntrospector()); _schemas.addSchema(_type, schema); return schema; } diff --git a/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/AvroTestBase.java b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/AvroTestBase.java index c579f3c96..7d0fad5f7 100644 --- a/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/AvroTestBase.java +++ b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/AvroTestBase.java @@ -9,6 +9,8 @@ import junit.framework.TestCase; import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import com.fasterxml.jackson.annotation.JsonEnumDefaultValue; +import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonToken; import com.fasterxml.jackson.databind.ObjectMapper; @@ -249,6 +251,19 @@ public Image(String uri, String title, int w, int h, Size s) public enum Size { SMALL, LARGE; } + public enum ABC { + A, + B, + @JsonEnumDefaultValue + C; + } + + public static class ABCDefaultClass { + public String name; + @JsonProperty(required = true) + public ABC abc; + } + /* /********************************************************** /* Recycling for commonly needed helper objects diff --git a/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/schema/AvroSchemaGenerationTest.java b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/schema/AvroSchemaGenerationTest.java index 3c70389ca..211c54c7b 100644 --- a/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/schema/AvroSchemaGenerationTest.java +++ b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/schema/AvroSchemaGenerationTest.java @@ -2,6 +2,8 @@ import java.nio.ByteBuffer; import java.util.*; +import java.util.stream.Collectors; +import java.util.stream.Stream; import com.fasterxml.jackson.annotation.JsonAlias; import com.fasterxml.jackson.annotation.JsonProperty; @@ -11,6 +13,7 @@ import com.fasterxml.jackson.dataformat.avro.*; import org.apache.avro.Schema; +import org.apache.avro.Schema.Type; public class AvroSchemaGenerationTest extends AvroTestBase { @@ -169,4 +172,38 @@ public void testSchemaForUntypedMap() throws Exception verifyException(e, "Maps with non-stringable keys are not supported (yet?)"); } } + + // Issue 388 Default value for enums with class + public void testClassEnumWithDefault() throws Exception + { + AvroSchemaGenerator gen = new AvroSchemaGenerator(); + + MAPPER.acceptJsonFormatVisitor(ABCDefaultClass.class, gen); + AvroSchema schema = gen.getGeneratedSchema(); + assertNotNull(schema); + + String json = schema.getAvroSchema().toString(true); + assertNotNull(json); + + + // And read it back too just for fun + AvroSchema s2 = MAPPER.schemaFrom(json); + assertNotNull(s2); + + Schema avroSchema = s2.getAvroSchema(); + + // String name, int value + assertEquals(Type.RECORD, avroSchema.getType()); + Schema.Field f = avroSchema.getField("abc"); + assertNotNull(f); + assertEquals("abc", f.name()); + + assertEquals(Type.ENUM, f.schema().getType()); + assertEquals(ABC.C.toString(), f.schema().getEnumDefault()); + assertEquals(Stream.of(ABC.values()) + .map(ABC::name) + .collect(Collectors.toList()), f.schema().getEnumSymbols()); + + + } }