Skip to content

Commit 97ed96f

Browse files
committed
Merge branch '2.10'
2 parents dd54a02 + e998ecd commit 97ed96f

File tree

5 files changed

+96
-10
lines changed

5 files changed

+96
-10
lines changed

avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ public void close() throws IOException
357357

358358
@Override
359359
public final void writeStartArray() throws IOException {
360-
_tokenWriteContext = _tokenWriteContext.createChildArrayContext();
360+
_tokenWriteContext = _tokenWriteContext.createChildArrayContext(null);
361361
_complete = false;
362362
}
363363

avro/src/main/java/com/fasterxml/jackson/dataformat/avro/ser/AvroWriteContext.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,6 @@ public static AvroWriteContext nullContext() {
6868
return NullContext.instance;
6969
}
7070

71-
public final AvroWriteContext createChildArrayContext() throws JsonMappingException {
72-
return createChildArrayContext(null);
73-
}
74-
7571
public abstract AvroWriteContext createChildArrayContext(Object currValue) throws JsonMappingException;
7672

7773
public AvroWriteContext createChildObjectContext() throws JsonMappingException {
@@ -298,8 +294,7 @@ public static int resolveUnionIndex(Schema unionSchema, Object datum) {
298294
// !!! TODO:
299295
// - ByteBuffer
300296
// - Number wrappers (Integer, ...)
301-
302-
/*
297+
303298
String typeId = AvroSchemaHelper.getTypeId(datum.getClass());
304299
for (int i = 0, size = types.size(); i < size; ++i) {
305300
Schema schema = types.get(i);
@@ -308,7 +303,6 @@ public static int resolveUnionIndex(Schema unionSchema, Object datum) {
308303
return i;
309304
}
310305
}
311-
*/
312306
}
313307
//System.err.println("Missing index for: "+datum.getClass().getName()+" ("+types.size()+") ->\n"+types);
314308
return ReflectData.get().resolveUnion(unionSchema, datum);
@@ -350,7 +344,6 @@ public static Schema resolveUnionType(Schema unionSchema, Object datum) {
350344
return types.get(_resolveMapIndex(unionSchema, types, datum));
351345
}
352346

353-
/*
354347
String typeId = AvroSchemaHelper.getTypeId(datum.getClass());
355348
for (int i = 0, size = types.size(); i < size; ++i) {
356349
Schema schema = types.get(i);
@@ -359,7 +352,6 @@ public static Schema resolveUnionType(Schema unionSchema, Object datum) {
359352
return schema;
360353
}
361354
}
362-
*/
363355
}
364356
//System.err.println("Missing schema for: "+datum.getClass().getName()+" ("+types.size()+") ->\n"+types);
365357
int ix = ReflectData.get().resolveUnion(unionSchema, datum);
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.fasterxml.jackson.dataformat.avro.interop.annotations;
2+
3+
import java.io.IOException;
4+
import java.util.Collections;
5+
import java.util.HashSet;
6+
import java.util.Objects;
7+
import java.util.Set;
8+
import org.apache.avro.Schema;
9+
import org.apache.avro.SchemaBuilder;
10+
import org.apache.avro.reflect.Union;
11+
import org.junit.Test;
12+
13+
import static com.fasterxml.jackson.dataformat.avro.interop.ApacheAvroInteropUtil.jacksonDeserialize;
14+
import static com.fasterxml.jackson.dataformat.avro.interop.ApacheAvroInteropUtil.jacksonSerialize;
15+
import static org.assertj.core.api.Assertions.assertThat;
16+
17+
public class UnionResolvingTest {
18+
private Schema SCHEMA = SchemaBuilder
19+
.builder(UnionResolvingTest.class.getName() + "$")
20+
.record(Box.class.getSimpleName())
21+
.fields()
22+
.name("animal")
23+
.type()
24+
.unionOf()
25+
.record(Cat.class.getSimpleName())
26+
.fields()
27+
.name("name").type().stringType().noDefault()
28+
.endRecord()
29+
.and()
30+
.record(Dog.class.getSimpleName())
31+
.fields()
32+
.name("tricks").type().array().items().stringType().noDefault()
33+
.endRecord()
34+
.endUnion()
35+
.noDefault()
36+
.endRecord();
37+
38+
39+
@Union({ Cat.class, Dog.class })
40+
public interface Animal { }
41+
42+
static final class Cat implements Animal {
43+
public Cat() {}
44+
45+
@Override
46+
public boolean equals(Object o) {
47+
return o instanceof Cat;
48+
}
49+
}
50+
51+
static final class Dog implements Animal {
52+
// Unsupported schema generation
53+
public Set<?> tricks = new HashSet<>();
54+
55+
public Dog() {
56+
}
57+
58+
public Dog(final Set<?> tricks) {
59+
this.tricks = tricks;
60+
}
61+
62+
@Override
63+
public boolean equals(Object o) {
64+
return o instanceof Dog && tricks.equals(((Dog) o).tricks);
65+
}
66+
}
67+
68+
public static class Box {
69+
public Animal animal;
70+
71+
public Box() {
72+
}
73+
74+
public Box(Animal a) { animal = a; }
75+
76+
@Override
77+
public boolean equals(Object o) {
78+
return o instanceof Box && Objects.equals(animal, ((Box) o).animal);
79+
}
80+
}
81+
82+
@Test
83+
public void testResolveUnionUsingSchemaName() throws IOException {
84+
final Box box = new Box(new Dog(Collections.singleton("catch stick")));
85+
86+
final Box result = jacksonDeserialize(SCHEMA, Box.class, jacksonSerialize(SCHEMA, box));
87+
88+
assertThat(result).isEqualTo(box);
89+
}
90+
}

release-notes/CREDITS-2.x

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ Juliana Amorim (amorimjuliana@github)
102102
Marcos Passos (marcospassos@github)
103103
* Contributed fix for #168: (avro) `JsonMappingException` for union types with multiple Record types
104104
(2.10.0)
105+
* Contributed fix for #173: (Avro) Improve Union type serialization performance
106+
(2.10.0)
105107

106108

107109

release-notes/VERSION-2.x

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Project: jackson-datatypes-binaryModules:
1212

1313
#168: (avro) `JsonMappingException` for union types with multiple Record types
1414
(reported by Juliana A; fix contributed by Marcos P)
15+
#173: (Avro) Improve Union type serialization performance
16+
(fix contributed by Marcos P)
1517

1618
2.10.0.pr1 (19-Jul-2019)
1719

0 commit comments

Comments
 (0)