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

Allow alias annotation on fields #174

Closed
wants to merge 1 commit into from
Closed
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 @@ -28,7 +28,7 @@
* named by the alias.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Target({ElementType.TYPE, ElementType.FIELD})
public @interface AvroAlias {
String NULL = "NOT A VALID NAMESPACE";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,11 @@ protected Schema createSchema(Type type, Map<String,Schema> names) {
if (f.name().equals(fieldName))
throw new AvroTypeException("double field entry: "+ fieldName);
}

if (field.isAnnotationPresent(AvroAlias.class)) {
recordField.addAlias(field.getAnnotation(AvroAlias.class).alias());
}

fields.add(recordField);
}
if (error) // add Throwable message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.apache.avro.AvroTypeException;
import org.apache.avro.Protocol;
import org.apache.avro.Schema;
import org.apache.avro.SchemaBuilder;
import org.apache.avro.Schema.Field;
import org.apache.avro.generic.GenericData;
import org.apache.avro.io.Decoder;
Expand Down Expand Up @@ -1029,12 +1030,27 @@ private static class AliasB { }
private static class AliasC { }

@Test
public void testAvroAlias() {
public void testAvroAliasOnClass() {
check(AliasA.class, "{\"type\":\"record\",\"name\":\"AliasA\",\"namespace\":\"org.apache.avro.reflect.TestReflect$\",\"fields\":[],\"aliases\":[\"b.a\"]}");
check(AliasB.class, "{\"type\":\"record\",\"name\":\"AliasB\",\"namespace\":\"org.apache.avro.reflect.TestReflect$\",\"fields\":[],\"aliases\":[\"a\"]}");
check(AliasC.class, "{\"type\":\"record\",\"name\":\"AliasC\",\"namespace\":\"org.apache.avro.reflect.TestReflect$\",\"fields\":[],\"aliases\":[\"a\"]}");
}

private static class ClassWithAliasOnField {
@AvroAlias(alias = "aliasName")
int primitiveField;
}

@Test
public void testAvroAliasOnField() {

Schema expectedSchema = SchemaBuilder.record(ClassWithAliasOnField.class.getSimpleName())
.namespace("org.apache.avro.reflect.TestReflect$").fields().name("primitiveField").aliases("aliasName")
.type(Schema.create(org.apache.avro.Schema.Type.INT)).noDefault().endRecord();

check(ClassWithAliasOnField.class, expectedSchema.toString());
}

private static class DefaultTest {
@AvroDefault("1")
int foo;
Expand Down