Skip to content

Commit c024680

Browse files
Merge pull request #182
2 parents 1e2aa46 + d3812bd commit c024680

File tree

2 files changed

+63
-4
lines changed

2 files changed

+63
-4
lines changed

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,12 @@ private Map<Type, List<BeanModel>> createChildrenMap(Model model) {
119119
final Map<Type, List<BeanModel>> children = new LinkedHashMap<>();
120120
for (BeanModel bean : model.getBeans()) {
121121
for (Type ancestor : bean.getParentAndInterfaces()) {
122-
if (!children.containsKey(ancestor)) {
123-
children.put(ancestor, new ArrayList<BeanModel>());
122+
Type processedAncestor = processTypeForDescendantLookup(ancestor);
123+
124+
if (!children.containsKey(processedAncestor)) {
125+
children.put(processedAncestor, new ArrayList<BeanModel>());
124126
}
125-
children.get(ancestor).add(bean);
127+
children.get(processedAncestor).add(bean);
126128
}
127129
}
128130
return children;
@@ -186,10 +188,21 @@ private List<TsPropertyModel> processProperties(SymbolTable symbolTable, Model m
186188
return properties;
187189
}
188190

191+
/**
192+
* Given a type, returns the type that should be used for the purpose of looking up implementations of that type.
193+
*/
194+
private static Type processTypeForDescendantLookup(Type type) {
195+
if (type instanceof ParameterizedType) {
196+
return ((ParameterizedType) type).getRawType();
197+
} else {
198+
return type;
199+
}
200+
}
201+
189202
private static List<BeanModel> getSelfAndDescendants(BeanModel bean, Map<Type, List<BeanModel>> children) {
190203
final List<BeanModel> descendants = new ArrayList<>();
191204
descendants.add(bean);
192-
final List<BeanModel> directDescendants = children.get(bean.getOrigin());
205+
final List<BeanModel> directDescendants = children.get(processTypeForDescendantLookup(bean.getOrigin()));
193206
if (directDescendants != null) {
194207
for (BeanModel descendant : directDescendants) {
195208
descendants.addAll(getSelfAndDescendants(descendant, children));

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11

22
package cz.habarta.typescript.generator;
33

4+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
5+
import cz.habarta.typescript.generator.compiler.ModelCompiler;
6+
import cz.habarta.typescript.generator.emitter.TsModel;
7+
import cz.habarta.typescript.generator.parser.Jackson2Parser;
8+
import cz.habarta.typescript.generator.parser.Model;
49
import java.lang.reflect.*;
510
import java.util.*;
11+
import org.hamcrest.CoreMatchers;
612
import org.junit.*;
713

814

@@ -37,6 +43,46 @@ public void testExclusionPattern() throws Exception {
3743
Assert.assertEquals("{ [index: string]: any }[]", TestUtils.compileType(settings, javaType).toString());
3844
}
3945

46+
@Test
47+
public void testIntermediateInterfacesWithoutTypeParams() throws Exception {
48+
final Settings settings = TestUtils.settings();
49+
50+
final Jackson2Parser jacksonParser = new Jackson2Parser(settings, new DefaultTypeProcessor());
51+
final Model model = jacksonParser.parseModel(Implementation.class);
52+
final ModelCompiler modelCompiler = new TypeScriptGenerator(settings).getModelCompiler();
53+
54+
final TsModel result = modelCompiler.javaToTypeScript(model);
55+
56+
Assert.assertThat(
57+
result.getBean(WithoutTypeParam.class).getProperties().get(0).tsType,
58+
CoreMatchers.instanceOf(TsType.UnionType.class)
59+
);
60+
}
61+
62+
@Test
63+
public void testIntermediateInterfacesWithTypeParams() throws Exception {
64+
final Settings settings = TestUtils.settings();
65+
66+
final Jackson2Parser jacksonParser = new Jackson2Parser(settings, new DefaultTypeProcessor());
67+
final Model model = jacksonParser.parseModel(Implementation.class);
68+
final ModelCompiler modelCompiler = new TypeScriptGenerator(settings).getModelCompiler();
69+
70+
final TsModel result = modelCompiler.javaToTypeScript(model);
71+
72+
Assert.assertThat(
73+
result.getBean(WithTypeParam.class).getProperties().get(0).tsType,
74+
CoreMatchers.instanceOf(TsType.UnionType.class)
75+
);
76+
}
77+
78+
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY)
79+
private static interface WithoutTypeParam {}
80+
81+
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY)
82+
private static interface WithTypeParam<T> {}
83+
84+
private static class Implementation implements WithTypeParam<Integer>, WithoutTypeParam {}
85+
4086
private static Settings getTestSettings(String... excludedClassNames) {
4187
final Settings settings = TestUtils.settings();
4288
settings.mapDate = DateMapping.asString;

0 commit comments

Comments
 (0)