Skip to content

Commit e03fa92

Browse files
authored
Merge pull request #136 from baharclerode/bah.AvroUnionMapValues
[Avro] Fix MapWriteContext not correctly resolving union values
2 parents f1ddf07 + bdfb871 commit e03fa92

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

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

+8
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ public final AvroWriteContext createChildObjectContext() throws JsonMappingExcep
5252
return child;
5353
}
5454

55+
@Override
56+
public final AvroWriteContext createChildObjectContext(Object currValue) throws JsonMappingException {
57+
_verifyValueWrite();
58+
AvroWriteContext child = _createObjectContext(_schema.getValueType(), currValue);
59+
_data.put(_currentName, child.rawValue());
60+
return child;
61+
}
62+
5563
@Override
5664
public void writeValue(Object value) {
5765
_verifyValueWrite();

avro/src/test/java/com/fasterxml/jackson/dataformat/avro/interop/annotations/UnionTest.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import java.io.IOException;
44
import java.util.Arrays;
5+
import java.util.HashMap;
56
import java.util.List;
7+
import java.util.Map;
68

79
import org.apache.avro.UnresolvedUnionException;
810
import org.apache.avro.reflect.Nullable;
@@ -87,17 +89,21 @@ public boolean equals(Object o) {
8789
public static class PetShop {
8890
public List<Animal> pets;
8991

92+
public Map<String, Animal> specialPets;
93+
9094
protected PetShop() { }
9195
public PetShop(Animal... p) {
9296
pets = Arrays.asList(p);
97+
specialPets = new HashMap<>();
9398
}
9499

95100
@Override
96101
public boolean equals(Object o) {
97102
if (o.getClass() == getClass()) {
98103
PetShop other = (PetShop) o;
99104
if (pets == null) return other.pets == null;
100-
else return pets.equals(other.pets);
105+
if (specialPets == null) return other.specialPets == null;
106+
else return pets.equals(other.pets) && specialPets.equals(other.specialPets);
101107
}
102108
return false;
103109
}
@@ -142,4 +148,15 @@ public void testListWithInterfaceUnion() throws IOException {
142148
assertThat(result).isEqualTo(shop);
143149
}
144150

151+
@Test
152+
public void testMapWithInterfaceUnion() throws IOException {
153+
PetShop shop = new PetShop(new Cat("tabby"), new Dog(4), new Dog(5), new Cat("calico"));
154+
shop.specialPets.put("pet1", new Cat("siamese"));
155+
shop.specialPets.put("pet2", new Dog(6));
156+
//
157+
PetShop result = roundTrip(shop);
158+
//
159+
assertThat(result).isEqualTo(shop);
160+
}
161+
145162
}

0 commit comments

Comments
 (0)