Skip to content

Commit 736999c

Browse files
committed
Support enum avro serialization with default value
1 parent d8a51cb commit 736999c

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

avro/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ abstractions.
4747
<dependency>
4848
<groupId>org.apache.avro</groupId>
4949
<artifactId>avro</artifactId>
50-
<version>1.8.2</version>
50+
<version>1.9.2</version>
5151
</dependency>
5252

5353
<!-- and for testing we need logback -->

avro/src/main/java/com/fasterxml/jackson/dataformat/avro/schema/AvroSchemaHelper.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.fasterxml.jackson.databind.*;
2020
import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
2121
import com.fasterxml.jackson.databind.introspect.AnnotatedConstructor;
22+
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
2223
import com.fasterxml.jackson.databind.json.JsonMapper;
2324
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes;
2425
import com.fasterxml.jackson.databind.util.ClassUtil;
@@ -71,6 +72,14 @@ public abstract class AvroSchemaHelper
7172
String.class
7273
));
7374

75+
/**
76+
*
77+
* Jackson annotation introspector for verifying enum default annotation for Avro Schema generation
78+
*
79+
* @since 2.16
80+
*/
81+
private static final JacksonAnnotationIntrospector JACKSON_ANNOTATION_INTROSPECTOR = new JacksonAnnotationIntrospector();
82+
7483
/**
7584
* Checks if a given type is "Stringable", that is one of the default
7685
* {@code STRINGABLE_CLASSES}, is an {@code Enum},
@@ -269,10 +278,11 @@ public static Schema parseJsonSchema(String json) {
269278
*/
270279
public static Schema createEnumSchema(BeanDescription bean, List<String> values) {
271280
final JavaType enumType = bean.getType();
281+
Enum<?> defaultEnumValue = JACKSON_ANNOTATION_INTROSPECTOR.findDefaultEnumValue((Class<Enum<?>>)(Class<?>) enumType.getRawClass());
272282
return addAlias(Schema.createEnum(
273283
getName(enumType),
274284
bean.findClassDescription(),
275-
getNamespace(enumType, bean.getClassInfo()), values
285+
getNamespace(enumType, bean.getClassInfo()), values, defaultEnumValue != null ? defaultEnumValue.toString() : null
276286
), bean);
277287
}
278288

avro/src/test/java/com/fasterxml/jackson/dataformat/avro/AvroTestBase.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import junit.framework.TestCase;
1010

1111
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
12+
import com.fasterxml.jackson.annotation.JsonEnumDefaultValue;
13+
import com.fasterxml.jackson.annotation.JsonProperty;
1214
import com.fasterxml.jackson.core.JsonParser;
1315
import com.fasterxml.jackson.core.JsonToken;
1416
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -249,6 +251,19 @@ public Image(String uri, String title, int w, int h, Size s)
249251

250252
public enum Size { SMALL, LARGE; }
251253

254+
public enum ABC {
255+
A,
256+
B,
257+
@JsonEnumDefaultValue
258+
C;
259+
}
260+
261+
public static class ABCDefaultClass {
262+
public String name;
263+
@JsonProperty(required = true)
264+
public ABC abc;
265+
}
266+
252267
/*
253268
/**********************************************************
254269
/* Recycling for commonly needed helper objects

avro/src/test/java/com/fasterxml/jackson/dataformat/avro/schema/SchemaGenerationTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.fasterxml.jackson.dataformat.avro.*;
1212

1313
import org.apache.avro.Schema;
14+
import org.apache.avro.Schema.Type;
1415

1516
public class SchemaGenerationTest extends AvroTestBase
1617
{
@@ -169,4 +170,38 @@ public void testSchemaForUntypedMap() throws Exception
169170
verifyException(e, "Maps with non-stringable keys are not supported (yet?)");
170171
}
171172
}
173+
174+
// Issue 388 Default value for enums with class
175+
public void testClassEnumWithDefault() throws Exception
176+
{
177+
AvroSchemaGenerator gen = new AvroSchemaGenerator();
178+
179+
MAPPER.acceptJsonFormatVisitor(ABCDefaultClass.class, gen);
180+
AvroSchema schema = gen.getGeneratedSchema();
181+
assertNotNull(schema);
182+
183+
String json = schema.getAvroSchema().toString(true);
184+
assertNotNull(json);
185+
186+
187+
// And read it back too just for fun
188+
AvroSchema s2 = MAPPER.schemaFrom(json);
189+
assertNotNull(s2);
190+
191+
Schema avroSchema = s2.getAvroSchema();
192+
193+
// String name, int value
194+
assertEquals(Type.RECORD, avroSchema.getType());
195+
Schema.Field f = avroSchema.getField("abc");
196+
assertNotNull(f);
197+
assertEquals("abc", f.name());
198+
199+
assertEquals(Type.ENUM, f.schema().getType());
200+
assertEquals(ABC.C.toString(), f.schema().getEnumDefault());
201+
assertEquals(Stream.of(ABC.values())
202+
.map(ABC::name)
203+
.collect(Collectors.toList()), f.schema().getEnumSymbols());
204+
205+
206+
}
172207
}

0 commit comments

Comments
 (0)