Skip to content

Commit d507813

Browse files
committed
[Avro] Add support for @AvroMeta annotation
1 parent 4ed2bf0 commit d507813

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed

avro/src/main/java/com/fasterxml/jackson/dataformat/avro/schema/RecordVisitor.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.fasterxml.jackson.dataformat.avro.AvroFixedSize;
1616

1717
import org.apache.avro.Schema;
18+
import org.apache.avro.reflect.AvroMeta;
1819
import org.apache.avro.reflect.AvroSchema;
1920

2021
public class RecordVisitor
@@ -47,6 +48,10 @@ public RecordVisitor(SerializerProvider p, JavaType type, DefinedSchemas schemas
4748
String description = getProvider().getAnnotationIntrospector().findClassDescription(ac);
4849
_avroSchema = Schema.createRecord(AvroSchemaHelper.getName(type), description, AvroSchemaHelper.getNamespace(type), false);
4950
_overridden = false;
51+
AvroMeta meta = ac.getAnnotation(AvroMeta.class);
52+
if (meta != null) {
53+
_avroSchema.addProp(meta.key(), meta.value());
54+
}
5055
}
5156
schemas.addSchema(type, _avroSchema);
5257
}
@@ -159,6 +164,13 @@ protected Schema.Field schemaFieldForWriter(BeanProperty prop, boolean optional)
159164
}
160165
}
161166
String description = getProvider().getAnnotationIntrospector().findPropertyDescription(prop.getMember());
162-
return new Schema.Field(prop.getName(), writerSchema, description, null);
167+
Schema.Field field = new Schema.Field(prop.getName(), writerSchema, description, null);
168+
169+
AvroMeta meta = prop.getAnnotation(AvroMeta.class);
170+
if (meta != null) {
171+
field.addProp(meta.key(), meta.value());
172+
}
173+
174+
return field;
163175
}
164176
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.fasterxml.jackson.dataformat.avro.interop.annotations;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import com.fasterxml.jackson.dataformat.avro.interop.InteropTestBase;
5+
6+
import lombok.Data;
7+
import org.apache.avro.AvroRuntimeException;
8+
import org.apache.avro.Schema;
9+
import org.apache.avro.reflect.AvroMeta;
10+
import org.apache.avro.reflect.AvroSchema;
11+
import org.junit.Test;
12+
13+
import static org.assertj.core.api.Assertions.assertThat;
14+
15+
public class AvroMetaTest extends InteropTestBase {
16+
17+
@Data
18+
@AvroMeta(key = "class-meta", value = "class value")
19+
static class MetaTest {
20+
21+
@AvroMeta(key = "custom Property", value = "Some Value")
22+
private String someProperty;
23+
24+
@AvroMeta(key = "required custom Property", value = "Some required Value")
25+
@JsonProperty(required = true)
26+
private String someRequiredProperty;
27+
}
28+
29+
@Data
30+
static class BadMetaTest {
31+
32+
@AvroMeta(key = "name", value = "colliding property")
33+
private String types;
34+
}
35+
36+
@Data
37+
@AvroSchema("{\"type\":\"string\"}")
38+
@AvroMeta(key="overridden", value = "true")
39+
static class OverriddenClassSchema {
40+
41+
private int field;
42+
}
43+
44+
@Test
45+
public void testAnnotationPrecedence() {
46+
Schema schema = schemaFunctor.apply(OverriddenClassSchema.class);
47+
//
48+
assertThat(schema.getProp("overridden")).isNull();
49+
}
50+
51+
@Test
52+
public void testOptionalFieldMetaProperty() {
53+
Schema schema = schemaFunctor.apply(MetaTest.class);
54+
//
55+
assertThat(schema.getField("someProperty").getProp("custom Property")).isEqualTo("Some Value");
56+
}
57+
58+
@Test
59+
public void testRequiredFieldMetaProperty() {
60+
Schema schema = schemaFunctor.apply(MetaTest.class);
61+
//
62+
assertThat(schema.getField("someRequiredProperty").getProp("required custom Property")).isEqualTo("Some required Value");
63+
}
64+
65+
@Test
66+
public void testClassMetaProperty() {
67+
Schema schema = schemaFunctor.apply(MetaTest.class);
68+
//
69+
assertThat(schema.getProp("class-meta")).isEqualTo("class value");
70+
}
71+
72+
@Test(expected = AvroRuntimeException.class)
73+
public void testCollidingMeta() {
74+
schemaFunctor.apply(BadMetaTest.class);
75+
}
76+
77+
}

0 commit comments

Comments
 (0)