Skip to content

Commit 1306810

Browse files
authored
Merge pull request #175 from marcospassos/union-fix-2.10
Fix union serialization
2 parents a4cfc3e + d85fae9 commit 1306810

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
@@ -317,8 +317,7 @@ public static int resolveUnionIndex(Schema unionSchema, Object datum) {
317317
// !!! TODO:
318318
// - ByteBuffer
319319
// - Number wrappers (Integer, ...)
320-
321-
/*
320+
322321
String typeId = AvroSchemaHelper.getTypeId(datum.getClass());
323322
for (int i = 0, size = types.size(); i < size; ++i) {
324323
Schema schema = types.get(i);
@@ -327,7 +326,6 @@ public static int resolveUnionIndex(Schema unionSchema, Object datum) {
327326
return i;
328327
}
329328
}
330-
*/
331329
}
332330
//System.err.println("Missing index for: "+datum.getClass().getName()+" ("+types.size()+") ->\n"+types);
333331
return ReflectData.get().resolveUnion(unionSchema, datum);
@@ -369,7 +367,6 @@ public static Schema resolveUnionType(Schema unionSchema, Object datum) {
369367
return types.get(_resolveMapIndex(unionSchema, types, datum));
370368
}
371369

372-
/*
373370
String typeId = AvroSchemaHelper.getTypeId(datum.getClass());
374371
for (int i = 0, size = types.size(); i < size; ++i) {
375372
Schema schema = types.get(i);
@@ -378,7 +375,6 @@ public static Schema resolveUnionType(Schema unionSchema, Object datum) {
378375
return schema;
379376
}
380377
}
381-
*/
382378
}
383379
//System.err.println("Missing schema for: "+datum.getClass().getName()+" ("+types.size()+") ->\n"+types);
384380
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)