Skip to content

Commit d41d22f

Browse files
authored
Update generic type arguments of implemented interfaces and extended classes when transforming bean property types to ensure the property type matches the one configured in implements/extends section (#936)
1 parent f2f9f56 commit d41d22f

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1280,7 +1280,23 @@ private static TsModel transformBeanPropertyTypes(TsModel tsModel, TsType.Transf
12801280
final TsType newReturnType = TsType.transformTsType(context, method.getReturnType(), transformer);
12811281
newMethods.add(new TsMethodModel(method.getName(), method.getModifiers(), method.getTypeParameters(), newParameters, newReturnType, method.getBody(), method.getComments()));
12821282
}
1283-
newBeans.add(bean.withProperties(newProperties).withMethods(newMethods));
1283+
final List<TsType> newImplements = new ArrayList<>();
1284+
for (TsType type: bean.getImplementsList()) {
1285+
if (type instanceof TsType.GenericBasicType || type instanceof TsType.GenericReferenceType) {
1286+
newImplements.add(TsType.transformTsType(context, type, transformer));
1287+
} else {
1288+
newImplements.add(type);
1289+
}
1290+
}
1291+
final List<TsType> newExtends = new ArrayList<>();
1292+
for (TsType type: bean.getExtendsList()) {
1293+
if (type instanceof TsType.GenericBasicType || type instanceof TsType.GenericReferenceType) {
1294+
newExtends.add(TsType.transformTsType(context, type, transformer));
1295+
} else {
1296+
newExtends.add(type);
1297+
}
1298+
}
1299+
newBeans.add(bean.withProperties(newProperties).withMethods(newMethods).withImplements(newImplements).withExtends(newExtends));
12841300
}
12851301
return tsModel.withBeans(newBeans);
12861302
}

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/emitter/TsBeanModel.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,17 @@ public TsBeanModel withMethods(List<TsMethodModel> methods) {
160160
public boolean isJaxrsApplicationClientBean() {
161161
return category == TsBeanCategory.Service && isClass;
162162
}
163-
163+
164164
public boolean isDataClass() {
165165
return category == TsBeanCategory.Data && isClass;
166166
}
167-
167+
168+
public TsBeanModel withImplements(List<TsType> implementsList) {
169+
return new TsBeanModel(origin, category, isClass, decorators, name, typeParameters, parent, extendsList, implementsList, taggedUnionClasses, discriminantProperty, discriminantLiteral, taggedUnionAlias, properties, constructor, methods, comments);
170+
}
171+
172+
public TsBeanModel withExtends(List<TsType> extendsList) {
173+
return new TsBeanModel(origin, category, isClass, decorators, name, typeParameters, parent, extendsList, implementsList, taggedUnionClasses, discriminantProperty, discriminantLiteral, taggedUnionAlias, properties, constructor, methods, comments);
174+
}
175+
168176
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,27 @@ public void byteArrayAsString() {
154154
Assertions.assertTrue(output.contains("specialData: SpecialString"), output);
155155
}
156156

157+
@Test
158+
public void testGenericSuperType() {
159+
final Settings settings = TestUtils.settings();
160+
settings.mapDate = DateMapping.asString;
161+
final String output = new TypeScriptGenerator(settings).generateTypeScript(Input.from(Class3.class));
162+
Assertions.assertTrue(output.contains("Interface<DateAsString>"));
163+
Assertions.assertTrue(output.contains("interfaceValue: DateAsString;"));
164+
Assertions.assertTrue(output.contains("AbstractClass<DateAsString>"));
165+
Assertions.assertTrue(output.contains("abstractValue: DateAsString;"));
166+
}
167+
168+
private interface Interface<T> {
169+
T getInterfaceValue();
170+
}
171+
172+
private static abstract class AbstractClass<T> {
173+
public abstract T getAbstractValue();
174+
}
175+
176+
private static abstract class Class3 extends AbstractClass<Date> implements Interface<Date>{
177+
}
178+
179+
157180
}

0 commit comments

Comments
 (0)