-
-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#310 New annotation @AvroNamespace to override Avro schema field name…
…space. Current namespace value is Java package name. This annotation allows to override its name.
- Loading branch information
1 parent
4827c45
commit 4567e4a
Showing
3 changed files
with
107 additions
and
4 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
avro/src/main/java/com/fasterxml/jackson/dataformat/avro/annotations/AvroNamespace.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
avro/src/test/java/com/fasterxml/jackson/dataformat/avro/annotations/AvroNamespaceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_annotation_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_annotation_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_annotation_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_annotation_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"); | ||
} | ||
|
||
} |