Skip to content

Commit

Permalink
FasterXML#310 New annotation @AvroNamespace to override Avro schema f…
Browse files Browse the repository at this point in the history
…ield namespace. Current namespace value is Java package name. This annotation allows to override its name.
  • Loading branch information
MichalFoksa committed May 9, 2022
1 parent 7d1ed0e commit 323edd9
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 5 deletions.
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)
@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");
}

}

0 comments on commit 323edd9

Please sign in to comment.