Skip to content

Commit 99311ae

Browse files
committed
Add subType schema into this union, unless it is already there.
1 parent 46cbe12 commit 99311ae

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

avro/src/test/java/com/fasterxml/jackson/dataformat/avro/schema/PolymorphicTypeAnnotationsTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,46 @@ public void subclasses_of_subclasses_test() throws IOException {
153153
boatSchema, submarineSchema);
154154
}
155155

156+
// Helium is twice in subtypes hierarchy, once as ElementInterface subtype and second time as subtype
157+
// of AbstractGas subtype. This situation may result in
158+
// "Failed to generate `AvroSchema` for ...., problem: (AvroRuntimeException) Duplicate in union:com.fasterxml...PolymorphicTypeAnnotationsTest.Helium"
159+
// error.
160+
@JsonSubTypes({
161+
@JsonSubTypes.Type(value = AbstractGas.class),
162+
@JsonSubTypes.Type(value = Helium.class),
163+
})
164+
private interface ElementInterface {
165+
}
166+
167+
@JsonSubTypes({
168+
@JsonSubTypes.Type(value = Helium.class),
169+
@JsonSubTypes.Type(value = Oxygen.class),
170+
})
171+
static abstract class AbstractGas implements ElementInterface {
172+
public int atomicMass;
173+
}
174+
175+
private static class Helium extends AbstractGas {
176+
}
177+
178+
private static class Oxygen extends AbstractGas {
179+
}
180+
181+
@Test
182+
public void class_is_referenced_twice_in_hierarchy_test() throws JsonMappingException {
183+
// GIVEN
184+
final Schema heliumSchema = MAPPER.schemaFor(Helium.class).getAvroSchema();
185+
final Schema oxygenSchema = MAPPER.schemaFor(Oxygen.class).getAvroSchema();
186+
187+
// WHEN
188+
Schema actualSchema = MAPPER.schemaFor(ElementInterface.class).getAvroSchema();
189+
190+
System.out.println("ElementInterface schema:\n" + actualSchema.toString(true));
191+
192+
// THEN
193+
assertThat(actualSchema.getType()).isEqualTo(Schema.Type.UNION);
194+
// ElementInterface and AbstractGas are not concrete classes they are not expected to be among types in union
195+
assertThat(actualSchema.getTypes()).containsExactlyInAnyOrder(heliumSchema, oxygenSchema);
196+
}
197+
156198
}

0 commit comments

Comments
 (0)