Skip to content

Commit 761ca8e

Browse files
committed
Merge branch '2.19'
2 parents a03cd63 + c3da1d8 commit 761ca8e

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,17 +254,23 @@ public static Schema parseJsonSchema(String json) {
254254
* Constructs a new enum schema
255255
*
256256
* @param values List of enum names
257+
*
257258
* @return An {@link org.apache.avro.Schema.Type#ENUM ENUM} schema.
258259
*/
259260
public static Schema createEnumSchema(MapperConfig<?> config, JavaType enumType,
260261
AnnotatedClass annotations, List<String> values) {
262+
final AnnotationIntrospector intr = config.getAnnotationIntrospector();
263+
Class<Enum<?>> rawEnumClass = (Class<Enum<?>>) enumType.getRawClass();
264+
final Enum<?> defaultEnumValue = intr.findDefaultEnumValue(config, annotations,
265+
rawEnumClass.getEnumConstants());
261266
final Schema avroSchema;
262267
try {
263268
avroSchema = Schema.createEnum(
264269
getTypeName(enumType),
265270
config.getAnnotationIntrospector().findClassDescription(config, annotations),
266271
getNamespace(enumType, annotations),
267-
values);
272+
values,
273+
defaultEnumValue != null ? defaultEnumValue.toString() : null);
268274
} catch (SchemaParseException spe) {
269275
final String msg = String.format("Problem generating Avro `Schema` for Enum type %s: %s",
270276
ClassUtil.getTypeDescription(enumType), spe.getMessage());

avro/src/test/java/tools/jackson/dataformat/avro/AvroTestBase.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ public Image(String uri, String title, int w, int h, Size s)
246246
public void setSize(Size s) { _size = s; }
247247
}
248248

249+
// public because needed by "AvroAliasTest"
249250
public enum Size { SMALL, LARGE; }
250251

251252
/*

avro/src/test/java/tools/jackson/dataformat/avro/EnumTest.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
package tools.jackson.dataformat.avro;
22

3+
import java.util.stream.Collectors;
4+
import java.util.stream.Stream;
5+
36
import org.junit.jupiter.api.Test;
47

8+
import org.apache.avro.Schema;
9+
import org.apache.avro.Schema.Type;
10+
11+
import com.fasterxml.jackson.annotation.JsonEnumDefaultValue;
12+
import com.fasterxml.jackson.annotation.JsonProperty;
13+
14+
import tools.jackson.dataformat.avro.schema.AvroSchemaGenerator;
15+
516
import static org.junit.jupiter.api.Assertions.assertEquals;
617
import static org.junit.jupiter.api.Assertions.assertNotNull;
718

@@ -35,6 +46,19 @@ protected static class EmployeeStr {
3546
public String gender;
3647
}
3748

49+
protected enum ABC {
50+
A,
51+
B,
52+
@JsonEnumDefaultValue
53+
C;
54+
}
55+
56+
protected static class ABCDefaultClass {
57+
public String name;
58+
@JsonProperty(required = true)
59+
public ABC abc;
60+
}
61+
3862
private final AvroMapper MAPPER = newMapper();
3963

4064
@Test
@@ -127,4 +151,35 @@ public void test_avroSchemaWithString_fromStringValueToEnumValue() throws Except
127151
assertEquals(Gender.F, output.gender);
128152
}
129153

154+
// [dataformats-binary#388]: Default value for enums with class
155+
@Test
156+
public void testClassEnumWithDefault() throws Exception
157+
{
158+
AvroSchemaGenerator gen = new AvroSchemaGenerator();
159+
160+
MAPPER.acceptJsonFormatVisitor(ABCDefaultClass.class, gen);
161+
AvroSchema schema = gen.getGeneratedSchema();
162+
assertNotNull(schema);
163+
164+
String json = schema.getAvroSchema().toString(true);
165+
assertNotNull(json);
166+
167+
// And read it back too just for fun
168+
AvroSchema s2 = MAPPER.schemaFrom(json);
169+
assertNotNull(s2);
170+
171+
Schema avroSchema = s2.getAvroSchema();
172+
173+
// String name, int value
174+
assertEquals(Type.RECORD, avroSchema.getType());
175+
Schema.Field f = avroSchema.getField("abc");
176+
assertNotNull(f);
177+
assertEquals("abc", f.name());
178+
179+
assertEquals(Type.ENUM, f.schema().getType());
180+
assertEquals(ABC.C.toString(), f.schema().getEnumDefault());
181+
assertEquals(Stream.of(ABC.values())
182+
.map(ABC::name)
183+
.collect(Collectors.toList()), f.schema().getEnumSymbols());
184+
}
130185
}

release-notes/VERSION-2.x

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ Active maintainers:
2121
#308: (avro) Incorrect serialization for `LogicalType.Decimal` (Java `BigDecimal`)
2222
(reported by Idan S)
2323
(fix contributed by Michal F)
24+
#388: (avro) `@JsonEnumDefaultValue` not supported when using AvroMapper
25+
to generate schema from Java class
26+
(requested by @Sonic-Rage)
2427
#422: Avro generation failed with enums containing values with special characters
2528
(reported by @pfr-enedis)
2629
#535: (avro) AvroSchemaGenerator: logicalType(s) never set for non-date classes

0 commit comments

Comments
 (0)