|
10 | 10 | import org.apache.avro.JsonProperties;
|
11 | 11 | import org.apache.avro.Schema;
|
12 | 12 | import org.apache.avro.Schema.Parser;
|
| 13 | +import org.apache.avro.SchemaParseException; |
13 | 14 | import org.apache.avro.reflect.AvroAlias;
|
14 | 15 | import org.apache.avro.reflect.Stringable;
|
15 | 16 | import org.apache.avro.specific.SpecificData;
|
16 | 17 |
|
17 | 18 | import tools.jackson.core.JacksonException;
|
| 19 | +import tools.jackson.core.JsonGenerator; |
18 | 20 | import tools.jackson.core.JsonParser;
|
19 | 21 |
|
20 | 22 | import tools.jackson.databind.*;
|
21 | 23 | import tools.jackson.databind.cfg.MapperConfig;
|
| 24 | +import tools.jackson.databind.exc.InvalidDefinitionException; |
22 | 25 | import tools.jackson.databind.introspect.AnnotatedClass;
|
23 | 26 | import tools.jackson.databind.introspect.AnnotatedConstructor;
|
24 | 27 | import tools.jackson.databind.json.JsonMapper;
|
@@ -257,19 +260,26 @@ public static Schema parseJsonSchema(String json) {
|
257 | 260 | */
|
258 | 261 | public static Schema createEnumSchema(MapperConfig<?> config, JavaType enumType,
|
259 | 262 | AnnotatedClass annotations, List<String> values) {
|
260 |
| - return addAlias(Schema.createEnum( |
261 |
| - getTypeName(enumType), |
262 |
| - config.getAnnotationIntrospector().findClassDescription(config, annotations), |
263 |
| - getNamespace(enumType, annotations), values |
264 |
| - ), annotations); |
| 263 | + final Schema avroSchema; |
| 264 | + try { |
| 265 | + avroSchema = Schema.createEnum( |
| 266 | + getTypeName(enumType), |
| 267 | + config.getAnnotationIntrospector().findClassDescription(config, annotations), |
| 268 | + getNamespace(enumType, annotations), |
| 269 | + values); |
| 270 | + } catch (SchemaParseException spe) { |
| 271 | + final String msg = String.format("Problem generating Avro `Schema` for Enum type %s: %s", |
| 272 | + ClassUtil.getTypeDescription(enumType), spe.getMessage()); |
| 273 | + throw InvalidDefinitionException.from((JsonGenerator) null, msg, enumType); |
| 274 | + } |
| 275 | + |
| 276 | + return addAlias(avroSchema, annotations); |
265 | 277 | }
|
266 | 278 |
|
267 | 279 | /**
|
268 | 280 | * Helper method to enclose details of expressing best Avro Schema for
|
269 | 281 | * {@link java.util.UUID}: 16-byte fixed-length binary (alternative would
|
270 | 282 | * be basic variable length "bytes").
|
271 |
| - * |
272 |
| - * @since 2.11 |
273 | 283 | */
|
274 | 284 | public static Schema createUUIDSchema() {
|
275 | 285 | return Schema.createFixed("UUID", null, "java.util", 16);
|
|
0 commit comments