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

Add @AvroNamespace annotation to override Avro type namespace #324

Merged
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
@@ -0,0 +1,16 @@
package com.fasterxml.jackson.dataformat.avro.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Annotation allows to override default Avro type namespace value.
* Default value is Java package name.
*/
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface AvroNamespace {
String value();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.fasterxml.jackson.dataformat.avro.schema;

import com.fasterxml.jackson.databind.util.LRUMap;
import java.io.File;
import java.math.BigDecimal;
import java.math.BigInteger;
Expand All @@ -23,6 +22,8 @@
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes;
import com.fasterxml.jackson.databind.util.ClassUtil;
import com.fasterxml.jackson.databind.util.LRUMap;
import com.fasterxml.jackson.dataformat.avro.annotations.AvroNamespace;

public abstract class AvroSchemaHelper
{
Expand Down Expand Up @@ -97,8 +98,9 @@ public static boolean isStringable(AnnotatedClass type) {
return false;
}

protected static String getNamespace(JavaType type) {
return getNamespace(type.getRawClass());
protected static String getNamespace(BeanDescription bean) {
AvroNamespace ann = bean.getClassInfo().getAnnotation(AvroNamespace.class);
return ann != null ? ann.value() : getNamespace(bean.getType().getRawClass());
}

protected static String getNamespace(Class<?> cls) {
Expand Down Expand Up @@ -244,7 +246,7 @@ public static Schema initializeRecordSchema(BeanDescription bean) {
return addAlias(Schema.createRecord(
getName(bean.getType()),
bean.findClassDescription(),
getNamespace(bean.getType()),
getNamespace(bean),
bean.getType().isTypeOrSubTypeOf(Throwable.class)
), bean);
}
Expand All @@ -268,7 +270,7 @@ public static Schema createEnumSchema(BeanDescription bean, List<String> values)
return addAlias(Schema.createEnum(
getName(bean.getType()),
bean.findClassDescription(),
getNamespace(bean.getType()), values
getNamespace(bean), values
), bean);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.fasterxml.jackson.dataformat.avro.annotations;

import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.dataformat.avro.AvroMapper;
import com.fasterxml.jackson.dataformat.avro.schema.AvroSchemaGenerator;
import org.apache.avro.Schema;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class AvroNamespaceTest {

static class ClassWithoutAvroNamespaceAnnotation {
}

@AvroNamespace("ClassWithAvroNamespaceAnnotation.namespace")
static class ClassWithAvroNamespaceAnnotation {
}

enum EnumWithoutAvroNamespaceAnnotation {FOO, BAR;}

@AvroNamespace("EnumWithAvroNamespaceAnnotation.namespace")
enum EnumWithAvroNamespaceAnnotation {FOO, BAR;}

@Test
public void class_without_AvroNamespace_test() throws JsonMappingException {
// GIVEN
AvroMapper mapper = new AvroMapper();
AvroSchemaGenerator gen = new AvroSchemaGenerator();

// WHEN
mapper.acceptJsonFormatVisitor(ClassWithoutAvroNamespaceAnnotation.class, gen);
Schema actualSchema = gen.getGeneratedSchema().getAvroSchema();

// THEN
assertThat(actualSchema.getNamespace())
.isEqualTo("com.fasterxml.jackson.dataformat.avro.annotations.AvroNamespaceTest$");
}

@Test
public void class_with_AvroNamespace_test() throws JsonMappingException {
// GIVEN
AvroMapper mapper = new AvroMapper();
AvroSchemaGenerator gen = new AvroSchemaGenerator();

// WHEN
mapper.acceptJsonFormatVisitor(ClassWithAvroNamespaceAnnotation.class, gen);
Schema actualSchema = gen.getGeneratedSchema().getAvroSchema();

// THEN
assertThat(actualSchema.getNamespace())
.isEqualTo("ClassWithAvroNamespaceAnnotation.namespace");
}

@Test
public void enum_without_AvroNamespace_test() throws JsonMappingException {
// GIVEN
AvroMapper mapper = new AvroMapper();
AvroSchemaGenerator gen = new AvroSchemaGenerator();

// WHEN
mapper.acceptJsonFormatVisitor(EnumWithoutAvroNamespaceAnnotation.class, gen);
Schema actualSchema = gen.getGeneratedSchema().getAvroSchema();

// THEN
assertThat(actualSchema.getNamespace())
.isEqualTo("com.fasterxml.jackson.dataformat.avro.annotations.AvroNamespaceTest$");
}

@Test
public void enum_with_AvroNamespace_test() throws JsonMappingException {
// GIVEN
AvroMapper mapper = new AvroMapper();
AvroSchemaGenerator gen = new AvroSchemaGenerator();

// WHEN
mapper.acceptJsonFormatVisitor(EnumWithAvroNamespaceAnnotation.class, gen);
Schema actualSchema = gen.getGeneratedSchema().getAvroSchema();

// THEN
assertThat(actualSchema.getNamespace())
.isEqualTo("EnumWithAvroNamespaceAnnotation.namespace");
}

}