Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Avro] Fix MapWriteContext not correctly resolving union values #136

Merged
merged 1 commit into from
May 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}

}