Skip to content

Commit

Permalink
Fix generic tagged unions with non-generic constituents (#428)
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtechhabarta committed Oct 23, 2019
1 parent 4a5a4d2 commit d02b649
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ private TsModel createAndUseTaggedUnions(final SymbolTable symbolTable, TsModel
final List<TsType> unionTypes = new ArrayList<>();
for (Class<?> cls : bean.getTaggedUnionClasses()) {
final TsType type;
if (isGeneric) {
if (isGeneric && cls.getTypeParameters().length != 0) {
final List<String> mappedGenericVariables = GenericsResolver.mapGenericVariablesToBase(cls, bean.getOrigin());
type = new TsType.GenericReferenceType(
symbolTable.getSymbol(cls),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,4 +460,36 @@ public void testBaseWithGenerics() {
Assert.assertTrue(output.contains("type BaseUnion<A, B> = FlippedGenericParameters<B, A>"));
}

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY)
@JsonSubTypes({
@JsonSubTypes.Type(value = Foo.class, name = "Foo"),
@JsonSubTypes.Type(value = Bar.class, name = "Bar")
})
public static abstract class Entity<T> {
public T id;
}

public static class Foo extends Entity<String> {
}

public static class Bar extends Entity<Integer> {
}

public class EntityCollection {
public List<Entity<?>> entities;
}

@Test
public void testGenericBaseWithNonGenericSubType() {
final Settings settings = TestUtils.settings();
settings.outputFileType = TypeScriptFileType.implementationFile;
settings.outputKind = TypeScriptOutputKind.module;
settings.mapClasses = ClassMapping.asClasses;
settings.mapEnum = EnumMapping.asEnum;
settings.nonConstEnums = true;
settings.mapPackagesToNamespaces = true;
final String output = new TypeScriptGenerator(settings).generateTypeScript(Input.from(EntityCollection.class));
Assert.assertTrue(output.contains("type EntityUnion<T> = cz.habarta.typescript.generator.TaggedUnionsTest.Foo | cz.habarta.typescript.generator.TaggedUnionsTest.Bar"));
}

}

0 comments on commit d02b649

Please sign in to comment.