Skip to content

Commit e8380e8

Browse files
committed
[Avro] Encapsulate schema parsing and record schema initialization
1 parent d507813 commit e8380e8

File tree

2 files changed

+40
-21
lines changed

2 files changed

+40
-21
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
import java.util.*;
99

1010
import org.apache.avro.Schema;
11+
import org.apache.avro.Schema.Parser;
1112
import org.apache.avro.reflect.Stringable;
1213
import org.apache.avro.specific.SpecificData;
1314

1415
import com.fasterxml.jackson.core.JsonParser;
16+
import com.fasterxml.jackson.databind.BeanDescription;
1517
import com.fasterxml.jackson.databind.JavaType;
1618
import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
1719
import com.fasterxml.jackson.databind.introspect.AnnotatedConstructor;
@@ -188,6 +190,27 @@ protected static <T> T throwUnsupported() {
188190
throw new UnsupportedOperationException("Format variation not supported");
189191
}
190192

193+
/**
194+
* Initializes a record schema with metadata from the given class; this schema is returned in a non-finalized state, and still
195+
* needs to have fields added to it.
196+
*/
197+
public static Schema initializeRecordSchema(BeanDescription bean) {
198+
return Schema.createRecord(
199+
getName(bean.getType()),
200+
bean.findClassDescription(),
201+
getNamespace(bean.getType()),
202+
bean.getType().isTypeOrSubTypeOf(Throwable.class)
203+
);
204+
}
205+
206+
/**
207+
* Parses a JSON-formatted representation of a schema
208+
*/
209+
public static Schema parseJsonSchema(String json) {
210+
Schema.Parser parser = new Parser();
211+
return parser.parse(json);
212+
}
213+
191214
/**
192215
* Returns the Avro type ID for a given type
193216
*/

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

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,16 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6-
import com.fasterxml.jackson.databind.BeanProperty;
7-
import com.fasterxml.jackson.databind.JavaType;
8-
import com.fasterxml.jackson.databind.JsonMappingException;
9-
import com.fasterxml.jackson.databind.JsonSerializer;
10-
import com.fasterxml.jackson.databind.SerializerProvider;
11-
import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
6+
import org.apache.avro.Schema;
7+
import org.apache.avro.reflect.AvroMeta;
8+
import org.apache.avro.reflect.AvroSchema;
9+
10+
import com.fasterxml.jackson.databind.*;
1211
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitable;
1312
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonObjectFormatVisitor;
1413
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
1514
import com.fasterxml.jackson.dataformat.avro.AvroFixedSize;
1615

17-
import org.apache.avro.Schema;
18-
import org.apache.avro.reflect.AvroMeta;
19-
import org.apache.avro.reflect.AvroSchema;
20-
2116
public class RecordVisitor
2217
extends JsonObjectFormatVisitor.Base
2318
implements SchemaBuilder
@@ -26,9 +21,13 @@ public class RecordVisitor
2621

2722
protected final DefinedSchemas _schemas;
2823

29-
protected Schema _avroSchema;
24+
/**
25+
* Tracks if the schema for this record has been overridden (by an annotation or other means), and calls to the {@code property} and
26+
* {@code optionalProperty} methods should be ignored.
27+
*/
28+
protected final boolean _overridden;
3029

31-
protected boolean _overridden;
30+
protected Schema _avroSchema;
3231

3332
protected List<Schema.Field> _fields = new ArrayList<Schema.Field>();
3433

@@ -38,17 +37,15 @@ public RecordVisitor(SerializerProvider p, JavaType type, DefinedSchemas schemas
3837
_type = type;
3938
_schemas = schemas;
4039
// Check if the schema for this record is overridden
41-
AnnotatedClass ac = getProvider().getConfig().introspectDirectClassAnnotations(_type).getClassInfo();
42-
AvroSchema ann = ac.getAnnotation(AvroSchema.class);
40+
BeanDescription bean = getProvider().getConfig().introspectDirectClassAnnotations(_type);
41+
AvroSchema ann = bean.getClassInfo().getAnnotation(AvroSchema.class);
4342
if (ann != null) {
44-
Schema.Parser parser = new Schema.Parser();
45-
_avroSchema = parser.parse(ann.value());
43+
_avroSchema = AvroSchemaHelper.parseJsonSchema(ann.value());
4644
_overridden = true;
4745
} else {
48-
String description = getProvider().getAnnotationIntrospector().findClassDescription(ac);
49-
_avroSchema = Schema.createRecord(AvroSchemaHelper.getName(type), description, AvroSchemaHelper.getNamespace(type), false);
46+
_avroSchema = AvroSchemaHelper.initializeRecordSchema(bean);
5047
_overridden = false;
51-
AvroMeta meta = ac.getAnnotation(AvroMeta.class);
48+
AvroMeta meta = bean.getClassInfo().getAnnotation(AvroMeta.class);
5249
if (meta != null) {
5350
_avroSchema.addProp(meta.key(), meta.value());
5451
}
@@ -163,8 +160,7 @@ protected Schema.Field schemaFieldForWriter(BeanProperty prop, boolean optional)
163160
writerSchema = AvroSchemaHelper.unionWithNull(writerSchema);
164161
}
165162
}
166-
String description = getProvider().getAnnotationIntrospector().findPropertyDescription(prop.getMember());
167-
Schema.Field field = new Schema.Field(prop.getName(), writerSchema, description, null);
163+
Schema.Field field = new Schema.Field(prop.getName(), writerSchema, prop.getMetadata().getDescription(), null);
168164

169165
AvroMeta meta = prop.getAnnotation(AvroMeta.class);
170166
if (meta != null) {

0 commit comments

Comments
 (0)