Skip to content

Commit ee0c73c

Browse files
committed
Failing test With "Duplicate in union:com.fasterxml.jackson.dataformat.avro.schema.RecordVisitor_schemaCreationTest.Cat"
1 parent 984a7e9 commit ee0c73c

File tree

1 file changed

+63
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)