Skip to content

Commit d02b649

Browse files
Fix generic tagged unions with non-generic constituents (#428)
1 parent 4a5a4d2 commit d02b649

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/compiler/ModelCompiler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,7 @@ private TsModel createAndUseTaggedUnions(final SymbolTable symbolTable, TsModel
902902
final List<TsType> unionTypes = new ArrayList<>();
903903
for (Class<?> cls : bean.getTaggedUnionClasses()) {
904904
final TsType type;
905-
if (isGeneric) {
905+
if (isGeneric && cls.getTypeParameters().length != 0) {
906906
final List<String> mappedGenericVariables = GenericsResolver.mapGenericVariablesToBase(cls, bean.getOrigin());
907907
type = new TsType.GenericReferenceType(
908908
symbolTable.getSymbol(cls),

typescript-generator-core/src/test/java/cz/habarta/typescript/generator/TaggedUnionsTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,4 +460,36 @@ public void testBaseWithGenerics() {
460460
Assert.assertTrue(output.contains("type BaseUnion<A, B> = FlippedGenericParameters<B, A>"));
461461
}
462462

463+
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY)
464+
@JsonSubTypes({
465+
@JsonSubTypes.Type(value = Foo.class, name = "Foo"),
466+
@JsonSubTypes.Type(value = Bar.class, name = "Bar")
467+
})
468+
public static abstract class Entity<T> {
469+
public T id;
470+
}
471+
472+
public static class Foo extends Entity<String> {
473+
}
474+
475+
public static class Bar extends Entity<Integer> {
476+
}
477+
478+
public class EntityCollection {
479+
public List<Entity<?>> entities;
480+
}
481+
482+
@Test
483+
public void testGenericBaseWithNonGenericSubType() {
484+
final Settings settings = TestUtils.settings();
485+
settings.outputFileType = TypeScriptFileType.implementationFile;
486+
settings.outputKind = TypeScriptOutputKind.module;
487+
settings.mapClasses = ClassMapping.asClasses;
488+
settings.mapEnum = EnumMapping.asEnum;
489+
settings.nonConstEnums = true;
490+
settings.mapPackagesToNamespaces = true;
491+
final String output = new TypeScriptGenerator(settings).generateTypeScript(Input.from(EntityCollection.class));
492+
Assert.assertTrue(output.contains("type EntityUnion<T> = cz.habarta.typescript.generator.TaggedUnionsTest.Foo | cz.habarta.typescript.generator.TaggedUnionsTest.Bar"));
493+
}
494+
463495
}

0 commit comments

Comments
 (0)