Skip to content

Commit d85fae9

Browse files
committed
Fix union serialization
1 parent 2a5faf6 commit d85fae9

File tree

2 files changed

+91
-5
lines changed

2 files changed

+91
-5
lines changed

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,7 @@ public static int resolveUnionIndex(Schema unionSchema, Object datum) {
276276
// !!! TODO:
277277
// - ByteBuffer
278278
// - Number wrappers (Integer, ...)
279-
280-
/*
279+
281280
String typeId = AvroSchemaHelper.getTypeId(datum.getClass());
282281
for (int i = 0, size = types.size(); i < size; ++i) {
283282
Schema schema = types.get(i);
@@ -286,7 +285,6 @@ public static int resolveUnionIndex(Schema unionSchema, Object datum) {
286285
return i;
287286
}
288287
}
289-
*/
290288
}
291289
//System.err.println("Missing index for: "+datum.getClass().getName()+" ("+types.size()+") ->\n"+types);
292290
return ReflectData.get().resolveUnion(unionSchema, datum);
@@ -328,7 +326,6 @@ public static Schema resolveUnionType(Schema unionSchema, Object datum) {
328326
return types.get(_resolveMapIndex(unionSchema, types, datum));
329327
}
330328

331-
/*
332329
String typeId = AvroSchemaHelper.getTypeId(datum.getClass());
333330
for (int i = 0, size = types.size(); i < size; ++i) {
334331
Schema schema = types.get(i);
@@ -337,7 +334,6 @@ public static Schema resolveUnionType(Schema unionSchema, Object datum) {
337334
return schema;
338335
}
339336
}
340-
*/
341337
}
342338
//System.err.println("Missing schema for: "+datum.getClass().getName()+" ("+types.size()+") ->\n"+types);
343339
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+
}

0 commit comments

Comments
 (0)