Skip to content

Commit

Permalink
Merge pull request #136 from baharclerode/bah.AvroUnionMapValues
Browse files Browse the repository at this point in the history
[Avro] Fix MapWriteContext not correctly resolving union values
  • Loading branch information
cowtowncoder authored May 23, 2018
2 parents f1ddf07 + bdfb871 commit e03fa92
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -87,17 +89,21 @@ public boolean equals(Object o) {
public static class PetShop {
public List<Animal> pets;

public Map<String, Animal> specialPets;

protected PetShop() { }
public PetShop(Animal... p) {
pets = Arrays.asList(p);
specialPets = new HashMap<>();
}

@Override
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;
}
Expand Down Expand Up @@ -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);
}

}

0 comments on commit e03fa92

Please sign in to comment.