Skip to content

Commit d3812bd

Browse files
Merge branch 'bug-181' of https://github.com/Yona-Appletree/typescript-generator into Yona-Appletree-bug-181
2 parents 1e2aa46 + fe8c39b commit d3812bd

File tree

2 files changed

+70
-7
lines changed

2 files changed

+70
-7
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: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11

22
package cz.habarta.typescript.generator;
33

4-
import java.lang.reflect.*;
5-
import java.util.*;
6-
import org.junit.*;
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;
9+
import org.hamcrest.CoreMatchers;
10+
import org.junit.Assert;
11+
import org.junit.Test;
712

13+
import java.lang.reflect.Type;
14+
import java.util.Arrays;
15+
import java.util.Date;
16+
import java.util.List;
17+
import java.util.Map;
818

919
public class ModelCompilerTest {
1020

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

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

0 commit comments

Comments
 (0)