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 support for @AvroAlias annotation #59

Merged
merged 1 commit into from
Mar 20, 2017
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
Expand Up @@ -9,6 +9,7 @@

import org.apache.avro.Schema;
import org.apache.avro.Schema.Parser;
import org.apache.avro.reflect.AvroAlias;
import org.apache.avro.reflect.Stringable;
import org.apache.avro.specific.SpecificData;

Expand Down Expand Up @@ -197,12 +198,12 @@ protected static <T> T throwUnsupported() {
* needs to have fields added to it.
*/
public static Schema initializeRecordSchema(BeanDescription bean) {
return Schema.createRecord(
return addAlias(Schema.createRecord(
getName(bean.getType()),
bean.findClassDescription(),
getNamespace(bean.getType()),
bean.getType().isTypeOrSubTypeOf(Throwable.class)
);
), bean);
}

/**
Expand All @@ -221,7 +222,25 @@ public static Schema parseJsonSchema(String json) {
* @return An {@link org.apache.avro.Schema.Type#ENUM ENUM} schema.
*/
public static Schema createEnumSchema(BeanDescription bean, List<String> values) {
return Schema.createEnum(getName(bean.getType()), bean.findClassDescription(), getNamespace(bean.getType()), values);
return addAlias(Schema.createEnum(
getName(bean.getType()),
bean.findClassDescription(),
getNamespace(bean.getType()), values
), bean);
}

/**
* Looks for {@link AvroAlias @AvroAlias} on {@code bean} and adds it to {@code schema} if it exists
* @param schema Schema to which the alias should be added
* @param bean Bean to inspect for type aliases
* @return {@code schema}, possibly with an alias added
*/
public static Schema addAlias(Schema schema, BeanDescription bean) {
AvroAlias ann = bean.getClassInfo().getAnnotation(AvroAlias.class);
if (ann != null) {
schema.addAlias(ann.alias(), ann.space().equals(AvroAlias.NULL) ? null : ann.space());
}
return schema;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import junit.framework.TestCase;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.avro.AvroSchema;

public abstract class AvroTestBase extends TestCase
{
Expand Down Expand Up @@ -61,7 +61,7 @@ public abstract class AvroTestBase extends TestCase
/**********************************************************
*/

protected static class Employee
public static class Employee
{
public Employee() { }

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

import java.io.IOException;

import org.apache.avro.Schema;
import org.apache.avro.SchemaCompatibility;
import org.apache.avro.reflect.AvroAlias;
import org.apache.avro.reflect.Nullable;
import org.junit.Test;

import com.fasterxml.jackson.dataformat.avro.AvroTestBase;
import com.fasterxml.jackson.dataformat.avro.interop.InteropTestBase;

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

public class AvroAliasTest extends InteropTestBase {

@AvroAlias(alias = "Employee", space = "com.fasterxml.jackson.dataformat.avro.AvroTestBase$")
public static class NewEmployee {

public String name;

public int age;

public String[] emails;

public NewEmployee boss;
}

@AvroAlias(alias = "NewEmployee")
public static class AliasedNameEmployee {

public String name;

public int age;

public String[] emails;

@Nullable
public AliasedNameEmployee boss;
}

@AvroAlias(alias = "Size", space = "com.fasterxml.jackson.dataformat.avro.AvroTestBase$")
public static enum NewSize {
SMALL,
LARGE;
}

@AvroAlias(alias = "NewestSize")
public static enum NewerSize {
SMALL,
LARGE;
}

@AvroAlias(alias = "NewerSize")
public static enum NewestSize {
SMALL,
LARGE;
}

@Test
public void testAliasedRecordForwardsCompatible() throws IOException {
Schema oldSchema = schemaFunctor.apply(AvroTestBase.Employee.class);
Schema newSchema = schemaFunctor.apply(NewEmployee.class);
//
SchemaCompatibility.SchemaPairCompatibility compatibility =
SchemaCompatibility.checkReaderWriterCompatibility(newSchema, oldSchema);
//
assertThat(compatibility.getType()).isEqualTo(SchemaCompatibility.SchemaCompatibilityType.COMPATIBLE);
}

@Test
public void testAliasedRecordBackwardsCompatible() throws IOException {
Schema oldSchema = schemaFunctor.apply(AvroTestBase.Employee.class);
Schema newSchema = schemaFunctor.apply(NewEmployee.class);
//
SchemaCompatibility.SchemaPairCompatibility compatibility =
SchemaCompatibility.checkReaderWriterCompatibility(oldSchema, newSchema);
//
assertThat(compatibility.getType()).isEqualTo(SchemaCompatibility.SchemaCompatibilityType.INCOMPATIBLE);
}

@Test
public void testAliasedRecordForwardsCompatibleSameNamespace() throws IOException {
Schema oldSchema = schemaFunctor.apply(NewEmployee.class);
Schema newSchema = schemaFunctor.apply(AliasedNameEmployee.class);
//
SchemaCompatibility.SchemaPairCompatibility compatibility =
SchemaCompatibility.checkReaderWriterCompatibility(newSchema, oldSchema);
//
assertThat(compatibility.getType()).isEqualTo(SchemaCompatibility.SchemaCompatibilityType.COMPATIBLE);
}

@Test
public void testAliasedRecordBackwardsCompatibleSameNamespace() throws IOException {
Schema oldSchema = schemaFunctor.apply(NewEmployee.class);
Schema newSchema = schemaFunctor.apply(AliasedNameEmployee.class);
//
SchemaCompatibility.SchemaPairCompatibility compatibility =
SchemaCompatibility.checkReaderWriterCompatibility(oldSchema, newSchema);
//
assertThat(compatibility.getType()).isEqualTo(SchemaCompatibility.SchemaCompatibilityType.INCOMPATIBLE);
}

@Test
public void testAliasedEnumForwardsCompatible() throws IOException {
Schema oldSchema = schemaFunctor.apply(AvroTestBase.Size.class);
Schema newSchema = schemaFunctor.apply(NewSize.class);
//
SchemaCompatibility.SchemaPairCompatibility compatibility =
SchemaCompatibility.checkReaderWriterCompatibility(newSchema, oldSchema);
//
assertThat(compatibility.getType()).isEqualTo(SchemaCompatibility.SchemaCompatibilityType.COMPATIBLE);
}

@Test
public void testAliasedEnumBackwardsCompatible() throws IOException {
Schema oldSchema = schemaFunctor.apply(AvroTestBase.Size.class);
Schema newSchema = schemaFunctor.apply(NewSize.class);
//
SchemaCompatibility.SchemaPairCompatibility compatibility =
SchemaCompatibility.checkReaderWriterCompatibility(oldSchema, newSchema);
//
assertThat(compatibility.getType()).isEqualTo(SchemaCompatibility.SchemaCompatibilityType.INCOMPATIBLE);
}

@Test
public void testAliasedEnumForwardsAndBackwardsCompatible() throws IOException {
Schema oldSchema = schemaFunctor.apply(NewerSize.class);
Schema newSchema = schemaFunctor.apply(NewestSize.class);
//
SchemaCompatibility.SchemaPairCompatibility backwardsCompatibility =
SchemaCompatibility.checkReaderWriterCompatibility(oldSchema, newSchema);
SchemaCompatibility.SchemaPairCompatibility forwardsCompatibility =
SchemaCompatibility.checkReaderWriterCompatibility(newSchema, oldSchema);
//
assertThat(backwardsCompatibility.getType()).isEqualTo(SchemaCompatibility.SchemaCompatibilityType.COMPATIBLE);
assertThat(forwardsCompatibility.getType()).isEqualTo(SchemaCompatibility.SchemaCompatibilityType.COMPATIBLE);
}

}