|
1 | 1 | package com.fasterxml.jackson.dataformat.avro;
|
2 | 2 |
|
| 3 | +import java.util.ArrayList; |
3 | 4 | import java.util.Collections;
|
4 | 5 | import java.util.List;
|
5 | 6 |
|
6 | 7 | import org.apache.avro.reflect.*;
|
7 | 8 |
|
8 | 9 | import com.fasterxml.jackson.annotation.JsonCreator;
|
| 10 | +import com.fasterxml.jackson.annotation.JsonTypeInfo; |
9 | 11 | import com.fasterxml.jackson.core.Version;
|
10 | 12 | import com.fasterxml.jackson.databind.AnnotationIntrospector;
|
| 13 | +import com.fasterxml.jackson.databind.JavaType; |
11 | 14 | import com.fasterxml.jackson.databind.PropertyName;
|
12 | 15 | import com.fasterxml.jackson.databind.cfg.MapperConfig;
|
13 | 16 | import com.fasterxml.jackson.databind.introspect.Annotated;
|
14 | 17 | import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
|
15 | 18 | import com.fasterxml.jackson.databind.introspect.AnnotatedConstructor;
|
16 | 19 | import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
|
| 20 | +import com.fasterxml.jackson.databind.jsontype.NamedType; |
| 21 | +import com.fasterxml.jackson.databind.jsontype.TypeResolverBuilder; |
17 | 22 | import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
| 23 | +import com.fasterxml.jackson.dataformat.avro.schema.AvroSchemaHelper; |
| 24 | + |
18 | 25 | /**
|
19 | 26 | * Adds support for the following annotations from the Apache Avro implementation:
|
20 | 27 | * <ul>
|
|
26 | 33 | * <li>{@link Nullable @Nullable} - Alias for <code>JsonProperty(required = false)</code></li>
|
27 | 34 | * <li>{@link Stringable @Stringable} - Alias for <code>JsonCreator</code> on the constructor and <code>JsonValue</code> on
|
28 | 35 | * the {@link #toString()} method. </li>
|
| 36 | + * <li>{@link Union @Union} - Alias for <code>JsonSubTypes</code></li> |
29 | 37 | * </ul>
|
30 | 38 | *
|
31 | 39 | * @since 2.9
|
@@ -70,7 +78,7 @@ public List<PropertyName> findPropertyAliases(Annotated m) {
|
70 | 78 | }
|
71 | 79 |
|
72 | 80 | protected PropertyName _findName(Annotated a)
|
73 |
| - { |
| 81 | + { |
74 | 82 | AvroName ann = _findAnnotation(a, AvroName.class);
|
75 | 83 | return (ann == null) ? null : PropertyName.construct(ann.value());
|
76 | 84 | }
|
@@ -107,4 +115,41 @@ public Object findSerializer(Annotated a) {
|
107 | 115 | }
|
108 | 116 | return null;
|
109 | 117 | }
|
| 118 | + |
| 119 | + @Override |
| 120 | + public List<NamedType> findSubtypes(Annotated a) { |
| 121 | + Union union = _findAnnotation(a, Union.class); |
| 122 | + if (union == null) { |
| 123 | + return null; |
| 124 | + } |
| 125 | + ArrayList<NamedType> names = new ArrayList<>(union.value().length); |
| 126 | + for (Class<?> subtype : union.value()) { |
| 127 | + names.add(new NamedType(subtype, AvroSchemaHelper.getTypeId(subtype))); |
| 128 | + } |
| 129 | + return names; |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public TypeResolverBuilder<?> findTypeResolver(MapperConfig<?> config, AnnotatedClass ac, JavaType baseType) { |
| 134 | + return _findTypeResolver(config, ac, baseType); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public TypeResolverBuilder<?> findPropertyTypeResolver(MapperConfig<?> config, AnnotatedMember am, JavaType baseType) { |
| 139 | + return _findTypeResolver(config, am, baseType); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public TypeResolverBuilder<?> findPropertyContentTypeResolver(MapperConfig<?> config, AnnotatedMember am, JavaType containerType) { |
| 144 | + return _findTypeResolver(config, am, containerType); |
| 145 | + } |
| 146 | + |
| 147 | + protected TypeResolverBuilder<?> _findTypeResolver(MapperConfig<?> config, Annotated ann, JavaType baseType) { |
| 148 | + TypeResolverBuilder<?> resolver = new AvroTypeResolverBuilder(); |
| 149 | + JsonTypeInfo typeInfo = ann.getAnnotation(JsonTypeInfo.class); |
| 150 | + if (typeInfo != null && typeInfo.defaultImpl() != JsonTypeInfo.class) { |
| 151 | + resolver = resolver.defaultImpl(typeInfo.defaultImpl()); |
| 152 | + } |
| 153 | + return resolver; |
| 154 | + } |
110 | 155 | }
|
0 commit comments