Skip to content

Commit f43adf5

Browse files
committed
#589: Failing test With "Duplicate in union:com.fasterxml.jackson.dataformat.avro.schema.RecordVisitor_schemaCreationTest.Cat"
1 parent 63e3481 commit f43adf5

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.fasterxml.jackson.dataformat.avro.schema;
2+
3+
import com.fasterxml.jackson.annotation.JsonSubTypes;
4+
import com.fasterxml.jackson.databind.JsonMappingException;
5+
import com.fasterxml.jackson.dataformat.avro.AvroMapper;
6+
import com.fasterxml.jackson.dataformat.avro.annotation.AvroNamespace;
7+
import org.apache.avro.Schema;
8+
import org.junit.jupiter.api.Test;
9+
10+
import java.io.IOException;
11+
12+
import static org.assertj.core.api.Assertions.assertThat;
13+
14+
public class RecordVisitor_schemaCreationFailingTest {
15+
16+
private static final AvroMapper MAPPER = AvroMapper.builder().build();
17+
// it is easier maintain string schema representation when namespace is constant, rather than being inferend from this class package name
18+
private static final String TEST_NAMESPACE = "test";
19+
20+
@JsonSubTypes({
21+
@JsonSubTypes.Type(value = AbstractMammal.class),
22+
@JsonSubTypes.Type(value = Cat.class),
23+
// Cat being twice in @JsonSubTypes causes Duplicate in union:com.fasterxml.jackson.dataformat.avro.schema.RecordVisitor_schemaCreationTest.Cat"
24+
@JsonSubTypes.Type(value = Cat.class),
25+
@JsonSubTypes.Type(value = Dog.class),
26+
})
27+
private interface Animal {
28+
}
29+
30+
@JsonSubTypes({
31+
@JsonSubTypes.Type(value = Cat.class),
32+
@JsonSubTypes.Type(value = Dog.class),
33+
})
34+
private static abstract class AbstractMammal implements Animal {
35+
public int legs;
36+
}
37+
38+
static class Cat extends AbstractMammal {
39+
public String color;
40+
}
41+
42+
static class Dog extends AbstractMammal {
43+
public int size;
44+
}
45+
46+
// It fails cause Cat and Dog are references twice as subTypes and this gets twice into a union
47+
// Easiest way to reproduce it is to add class twice into @JsonSubTypes
48+
// Causes Duplicate in union:com.fasterxml.jackson.dataformat.avro.schema.RecordVisitor_schemaCreationTest.Cat"
49+
@Test
50+
public void class_is_referenced_twice_in_hierarchy_test() throws JsonMappingException {
51+
// GIVEN
52+
final Schema catSchema = MAPPER.schemaFor(Cat.class).getAvroSchema();
53+
final Schema dogSchema = MAPPER.schemaFor(Dog.class).getAvroSchema();
54+
55+
// WHEN
56+
Schema actualSchema = MAPPER.schemaFor(Animal.class).getAvroSchema();
57+
58+
System.out.println("Animal schema:\n" + actualSchema.toString(true));
59+
60+
// THEN
61+
assertThat(actualSchema.getType()).isEqualTo(Schema.Type.UNION);
62+
// Because Animal is interface and Mammal is abstract, they are not expected to be among types in union
63+
assertThat(actualSchema.getTypes()).containsExactlyInAnyOrder(catSchema, dogSchema);
64+
}
65+
66+
}

0 commit comments

Comments
 (0)