Skip to content

Commit 12943f7

Browse files
Do not handle generic classes as tagged unions (#383)
1 parent de44907 commit 12943f7

File tree

2 files changed

+62
-6
lines changed

2 files changed

+62
-6
lines changed

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -269,21 +269,27 @@ private <T> TsBeanModel processBean(SymbolTable symbolTable, Model model, Map<Ty
269269

270270
final List<TsPropertyModel> properties = processProperties(symbolTable, model, bean);
271271

272-
boolean isDiscriminated = false;
272+
boolean isTaggedUnion = false;
273273
if (bean.getDiscriminantProperty() != null && bean.getProperty(bean.getDiscriminantProperty()) == null) {
274-
isDiscriminated = true;
274+
isTaggedUnion = true;
275+
boolean isDisciminantProperty = true;
275276
final List<BeanModel> selfAndDescendants = getSelfAndDescendants(bean, children);
276277
final List<TsType.StringLiteralType> literals = new ArrayList<>();
277278
for (BeanModel descendant : selfAndDescendants) {
278279
if (descendant.getDiscriminantProperty() == null || descendant.getProperty(bean.getDiscriminantProperty()) != null) {
279-
// do not handle bean as discriminated if any descendant or it itself has duplicate discriminant property
280-
isDiscriminated = false;
280+
// do not handle bean as tagged union if any descendant or it itself has duplicate discriminant property
281+
isTaggedUnion = false;
282+
isDisciminantProperty = false;
283+
}
284+
if (descendant.getOrigin().getTypeParameters().length != 0) {
285+
// do not handle bean as tagged union if any descendant or it itself is a generic class
286+
isTaggedUnion = false;
281287
}
282288
if (descendant.getDiscriminantLiteral() != null) {
283289
literals.add(new TsType.StringLiteralType(descendant.getDiscriminantLiteral()));
284290
}
285291
}
286-
final TsType discriminantType = isDiscriminated && !literals.isEmpty()
292+
final TsType discriminantType = isDisciminantProperty && !literals.isEmpty()
287293
? new TsType.UnionType(literals)
288294
: TsType.String;
289295
final TsModifierFlags modifiers = TsModifierFlags.None.setReadonly(settings.declarePropertiesAsReadOnly);
@@ -303,7 +309,7 @@ private <T> TsBeanModel processBean(SymbolTable symbolTable, Model model, Map<Ty
303309
/*constructor*/ null,
304310
/*methods*/ null,
305311
bean.getComments());
306-
return isDiscriminated
312+
return isTaggedUnion
307313
? tsBean.withTaggedUnion(bean.getTaggedUnionClasses(), bean.getDiscriminantProperty(), bean.getDiscriminantLiteral())
308314
: tsBean;
309315
}

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,48 @@ private static class ElectricCar extends Car {
138138
public double batteryCapacityInKWh;
139139
}
140140

141+
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "kind")
142+
@JsonSubTypes({
143+
@JsonSubTypes.Type(ElectricEngine.class),
144+
@JsonSubTypes.Type(DieselEngine.class),
145+
})
146+
private static abstract class Engine {
147+
public double horsePower;
148+
}
149+
150+
@JsonTypeName("electric")
151+
private static class ElectricEngine extends Engine {
152+
public double consumptionInKWh;
153+
}
154+
155+
@JsonTypeName("diesel")
156+
private static class DieselEngine extends Engine {
157+
public double consumptionInLiters;
158+
}
159+
160+
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "kind")
161+
@JsonSubTypes({
162+
@JsonSubTypes.Type(Boat.class),
163+
@JsonSubTypes.Type(Plane.class),
164+
})
165+
private static abstract class Vehicule<M extends Engine> {
166+
public boolean canMove;
167+
}
168+
169+
@JsonTypeName("boat")
170+
private static class Boat<M extends Engine> extends Vehicule<M> {
171+
public boolean isFloating;
172+
}
173+
174+
@JsonTypeName("plane")
175+
private static class Plane<M extends Engine> extends Vehicule<M> {
176+
public double altitude;
177+
}
178+
179+
private static class Earth {
180+
public List<Vehicule<Engine>> vehicules;
181+
}
182+
141183
@Test
142184
public void testTaggedUnions() {
143185
final Settings settings = TestUtils.settings();
@@ -340,6 +382,14 @@ public void testIdClass() {
340382
Assert.assertEquals(expected, output);
341383
}
342384

385+
@Test
386+
public void testWithTypeParameter() {
387+
final Settings settings = TestUtils.settings();
388+
final String output = new TypeScriptGenerator(settings).generateTypeScript(Input.from(Earth.class));
389+
Assert.assertTrue(output.contains("EngineUnion"));
390+
Assert.assertTrue(!output.contains("VehiculeUnion"));
391+
}
392+
343393
public static void main(String[] args) throws Exception {
344394
final ElectricCar electricCar = new ElectricCar();
345395
electricCar.name = "Tesla";

0 commit comments

Comments
 (0)