Skip to content

Commit

Permalink
Merge pull request #175 from marcospassos/union-fix-2.10
Browse files Browse the repository at this point in the history
Fix union serialization
  • Loading branch information
cowtowncoder authored Aug 15, 2019
2 parents a4cfc3e + d85fae9 commit 1306810
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,7 @@ public static int resolveUnionIndex(Schema unionSchema, Object datum) {
// !!! TODO:
// - ByteBuffer
// - Number wrappers (Integer, ...)

/*

String typeId = AvroSchemaHelper.getTypeId(datum.getClass());
for (int i = 0, size = types.size(); i < size; ++i) {
Schema schema = types.get(i);
Expand All @@ -327,7 +326,6 @@ public static int resolveUnionIndex(Schema unionSchema, Object datum) {
return i;
}
}
*/
}
//System.err.println("Missing index for: "+datum.getClass().getName()+" ("+types.size()+") ->\n"+types);
return ReflectData.get().resolveUnion(unionSchema, datum);
Expand Down Expand Up @@ -369,7 +367,6 @@ public static Schema resolveUnionType(Schema unionSchema, Object datum) {
return types.get(_resolveMapIndex(unionSchema, types, datum));
}

/*
String typeId = AvroSchemaHelper.getTypeId(datum.getClass());
for (int i = 0, size = types.size(); i < size; ++i) {
Schema schema = types.get(i);
Expand All @@ -378,7 +375,6 @@ public static Schema resolveUnionType(Schema unionSchema, Object datum) {
return schema;
}
}
*/
}
//System.err.println("Missing schema for: "+datum.getClass().getName()+" ("+types.size()+") ->\n"+types);
int ix = ReflectData.get().resolveUnion(unionSchema, datum);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.fasterxml.jackson.dataformat.avro.interop.annotations;

import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import org.apache.avro.Schema;
import org.apache.avro.SchemaBuilder;
import org.apache.avro.reflect.Union;
import org.junit.Test;

import static com.fasterxml.jackson.dataformat.avro.interop.ApacheAvroInteropUtil.jacksonDeserialize;
import static com.fasterxml.jackson.dataformat.avro.interop.ApacheAvroInteropUtil.jacksonSerialize;
import static org.assertj.core.api.Assertions.assertThat;

public class UnionResolvingTest {
private Schema SCHEMA = SchemaBuilder
.builder(UnionResolvingTest.class.getName() + "$")
.record(Box.class.getSimpleName())
.fields()
.name("animal")
.type()
.unionOf()
.record(Cat.class.getSimpleName())
.fields()
.name("name").type().stringType().noDefault()
.endRecord()
.and()
.record(Dog.class.getSimpleName())
.fields()
.name("tricks").type().array().items().stringType().noDefault()
.endRecord()
.endUnion()
.noDefault()
.endRecord();


@Union({ Cat.class, Dog.class })
public interface Animal { }

static final class Cat implements Animal {
public Cat() {}

@Override
public boolean equals(Object o) {
return o instanceof Cat;
}
}

static final class Dog implements Animal {
// Unsupported schema generation
public Set<?> tricks = new HashSet<>();

public Dog() {
}

public Dog(final Set<?> tricks) {
this.tricks = tricks;
}

@Override
public boolean equals(Object o) {
return o instanceof Dog && tricks.equals(((Dog) o).tricks);
}
}

public static class Box {
public Animal animal;

public Box() {
}

public Box(Animal a) { animal = a; }

@Override
public boolean equals(Object o) {
return o instanceof Box && Objects.equals(animal, ((Box) o).animal);
}
}

@Test
public void testResolveUnionUsingSchemaName() throws IOException {
final Box box = new Box(new Dog(Collections.singleton("catch stick")));

final Box result = jacksonDeserialize(SCHEMA, Box.class, jacksonSerialize(SCHEMA, box));

assertThat(result).isEqualTo(box);
}
}

0 comments on commit 1306810

Please sign in to comment.