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

Support enum avro serialization with default value #391

Open
wants to merge 4 commits into
base: 2.16
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 @@ -17,8 +17,10 @@
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
import com.fasterxml.jackson.databind.introspect.AnnotatedConstructor;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes;
import com.fasterxml.jackson.databind.util.ClassUtil;
Expand Down Expand Up @@ -267,12 +269,14 @@ 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<String> values) {
public static Schema createEnumSchema(BeanDescription bean, List<String> values,
AnnotationIntrospector intr) {
final JavaType enumType = bean.getType();
Enum<?> defaultEnumValue = intr.findDefaultEnumValue((Class<Enum<?>>)(Class<?>) enumType.getRawClass());
return addAlias(Schema.createEnum(
getName(enumType),
bean.findClassDescription(),
getNamespace(enumType, bean.getClassInfo()), values
getNamespace(enumType, bean.getClassInfo()), values, defaultEnumValue != null ? defaultEnumValue.toString() : null
), bean);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public Schema builtAvroSchema() {
}
BeanDescription bean = _provider.getConfig().introspectClassAnnotations(_type);
if (_enums != null) {
Schema s = AvroSchemaHelper.createEnumSchema(bean, new ArrayList<>(_enums));
Schema s = AvroSchemaHelper.createEnumSchema(bean, new ArrayList<>(_enums),
_provider.getAnnotationIntrospector());
_schemas.addSchema(_type, s);
return s;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -11,6 +13,7 @@
import com.fasterxml.jackson.dataformat.avro.*;

import org.apache.avro.Schema;
import org.apache.avro.Schema.Type;

public class SchemaGenerationTest extends AvroTestBase
{
Expand Down Expand Up @@ -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());


}
}
Loading