Skip to content

Commit b1c0bda

Browse files
committed
Merge branch '2.19'
2 parents babb8da + da74291 commit b1c0bda

File tree

5 files changed

+85
-12
lines changed

5 files changed

+85
-12
lines changed

avro/src/main/java/tools/jackson/dataformat/avro/schema/AvroSchemaHelper.java

+17-7
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,18 @@
1010
import org.apache.avro.JsonProperties;
1111
import org.apache.avro.Schema;
1212
import org.apache.avro.Schema.Parser;
13+
import org.apache.avro.SchemaParseException;
1314
import org.apache.avro.reflect.AvroAlias;
1415
import org.apache.avro.reflect.Stringable;
1516
import org.apache.avro.specific.SpecificData;
1617

1718
import tools.jackson.core.JacksonException;
19+
import tools.jackson.core.JsonGenerator;
1820
import tools.jackson.core.JsonParser;
1921

2022
import tools.jackson.databind.*;
2123
import tools.jackson.databind.cfg.MapperConfig;
24+
import tools.jackson.databind.exc.InvalidDefinitionException;
2225
import tools.jackson.databind.introspect.AnnotatedClass;
2326
import tools.jackson.databind.introspect.AnnotatedConstructor;
2427
import tools.jackson.databind.json.JsonMapper;
@@ -257,19 +260,26 @@ public static Schema parseJsonSchema(String json) {
257260
*/
258261
public static Schema createEnumSchema(MapperConfig<?> config, JavaType enumType,
259262
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);
265277
}
266278

267279
/**
268280
* Helper method to enclose details of expressing best Avro Schema for
269281
* {@link java.util.UUID}: 16-byte fixed-length binary (alternative would
270282
* be basic variable length "bytes").
271-
*
272-
* @since 2.11
273283
*/
274284
public static Schema createUUIDSchema() {
275285
return Schema.createFixed("UUID", null, "java.util", 16);

avro/src/main/java/tools/jackson/dataformat/avro/schema/EnumVisitor.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package tools.jackson.dataformat.avro.schema;
22

3+
import java.util.ArrayList;
4+
import java.util.Set;
5+
36
import tools.jackson.databind.*;
47
import tools.jackson.databind.introspect.AnnotatedClass;
58
import tools.jackson.databind.jsonFormatVisitors.JsonStringFormatVisitor;
69

710
import org.apache.avro.Schema;
811

9-
import java.util.ArrayList;
10-
import java.util.Set;
11-
1212
/**
1313
* Specific visitor for Java Enum types that are to be exposed as
1414
* Avro Enums. Used unless Java Enums are to be mapped to Avro Strings.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package tools.jackson.dataformat.avro.schema;
2+
3+
import org.junit.Test;
4+
5+
import com.fasterxml.jackson.annotation.JsonValue;
6+
7+
import tools.jackson.databind.exc.InvalidDefinitionException;
8+
import tools.jackson.dataformat.avro.AvroMapper;
9+
import tools.jackson.dataformat.avro.AvroTestBase;
10+
11+
// For [dataformats-binary#422]
12+
public class EnumSchema422Test extends AvroTestBase
13+
{
14+
enum EnumType422 {
15+
CARD_S("CARD-S");
16+
17+
private final String value;
18+
19+
EnumType422(String value) {
20+
this.value = value;
21+
}
22+
23+
@JsonValue
24+
public String value() {
25+
return this.value;
26+
}
27+
}
28+
29+
static class Wrapper422 {
30+
public EnumType422 contract;
31+
}
32+
33+
private final AvroMapper MAPPER = newMapper();
34+
35+
// For [dataformats-binary#422]
36+
@Test
37+
public void testEnumSchemaGeneration422() throws Exception
38+
{
39+
// First, failure due to invalid enum value (when generating as Enum)
40+
AvroSchemaGenerator gen = new AvroSchemaGenerator()
41+
.enableLogicalTypes();
42+
try {
43+
MAPPER.acceptJsonFormatVisitor(Wrapper422.class, gen);
44+
fail("Expected failure");
45+
} catch (InvalidDefinitionException e) { // in 2.x
46+
verifyException(e, "Problem generating Avro `Schema` for Enum type");
47+
verifyException(e, "Illegal character in");
48+
}
49+
50+
// But then success when configuring to produce Strings for Enum types
51+
52+
gen = new AvroSchemaGenerator()
53+
.enableLogicalTypes()
54+
.enableWriteEnumAsString();
55+
MAPPER.acceptJsonFormatVisitor(Wrapper422.class, gen);
56+
57+
org.apache.avro.Schema avroSchema = gen.getGeneratedSchema().getAvroSchema();
58+
String avroSchemaInJSON = avroSchema.toString(true);
59+
assertNotNull(avroSchemaInJSON);
60+
}
61+
}

avro/src/test/java/tools/jackson/dataformat/avro/schema/Enum_schemaCreationTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
import static org.assertj.core.api.Assertions.assertThat;
1212

13-
public class Enum_schemaCreationTest extends AvroTestBase {
14-
13+
public class Enum_schemaCreationTest extends AvroTestBase
14+
{
1515
static enum NumbersEnum {
1616
ONE, TWO, THREE
1717
}

release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Active maintainers:
1919
#308: (avro) Incorrect serialization for `LogicalType.Decimal` (Java `BigDecimal`)
2020
(reported by Idan S)
2121
(fix contributed by Michal F)
22+
#422: Avro generation failed with enums containing values with special characters
23+
(reported by @pfr-enedis)
2224
#535: (avro) AvroSchemaGenerator: logicalType(s) never set for non-date classes
2325
(reported by Cormac R)
2426
(fix contributed by Michal F)

0 commit comments

Comments
 (0)