From bdfb87152d076a0c8489f64598f163e87e30f2a8 Mon Sep 17 00:00:00 2001 From: Bryan Harclerode Date: Wed, 23 May 2018 09:40:15 -0500 Subject: [PATCH] [Avro] Fix MapWriteContext not correctly resolving union values --- .../dataformat/avro/ser/MapWriteContext.java | 8 ++++++++ .../avro/interop/annotations/UnionTest.java | 19 ++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/ser/MapWriteContext.java b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/ser/MapWriteContext.java index d6d884bf1..aac4596b5 100644 --- a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/ser/MapWriteContext.java +++ b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/ser/MapWriteContext.java @@ -52,6 +52,14 @@ public final AvroWriteContext createChildObjectContext() throws JsonMappingExcep return child; } + @Override + public final AvroWriteContext createChildObjectContext(Object currValue) throws JsonMappingException { + _verifyValueWrite(); + AvroWriteContext child = _createObjectContext(_schema.getValueType(), currValue); + _data.put(_currentName, child.rawValue()); + return child; + } + @Override public void writeValue(Object value) { _verifyValueWrite(); diff --git a/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/interop/annotations/UnionTest.java b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/interop/annotations/UnionTest.java index 1f1611979..adb3dfebe 100644 --- a/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/interop/annotations/UnionTest.java +++ b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/interop/annotations/UnionTest.java @@ -2,7 +2,9 @@ import java.io.IOException; import java.util.Arrays; +import java.util.HashMap; import java.util.List; +import java.util.Map; import org.apache.avro.UnresolvedUnionException; import org.apache.avro.reflect.Nullable; @@ -87,9 +89,12 @@ public boolean equals(Object o) { public static class PetShop { public List pets; + public Map specialPets; + protected PetShop() { } public PetShop(Animal... p) { pets = Arrays.asList(p); + specialPets = new HashMap<>(); } @Override @@ -97,7 +102,8 @@ public boolean equals(Object o) { if (o.getClass() == getClass()) { PetShop other = (PetShop) o; if (pets == null) return other.pets == null; - else return pets.equals(other.pets); + if (specialPets == null) return other.specialPets == null; + else return pets.equals(other.pets) && specialPets.equals(other.specialPets); } return false; } @@ -142,4 +148,15 @@ public void testListWithInterfaceUnion() throws IOException { assertThat(result).isEqualTo(shop); } + @Test + public void testMapWithInterfaceUnion() throws IOException { + PetShop shop = new PetShop(new Cat("tabby"), new Dog(4), new Dog(5), new Cat("calico")); + shop.specialPets.put("pet1", new Cat("siamese")); + shop.specialPets.put("pet2", new Dog(6)); + // + PetShop result = roundTrip(shop); + // + assertThat(result).isEqualTo(shop); + } + }