Skip to content

Commit 22b2859

Browse files
Discriminant properties preserved in inherited beans (refs #149)
1 parent d92fc86 commit 22b2859

File tree

4 files changed

+14
-7
lines changed

4 files changed

+14
-7
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ private <T> TsBeanModel processBean(SymbolTable symbolTable, Model model, Map<Ty
151151
final TsType discriminantType = literals.isEmpty()
152152
? TsType.String
153153
: new TsType.UnionType(literals);
154-
properties.add(0, new TsPropertyModel(bean.getDiscriminantProperty(), discriminantType, settings.declarePropertiesAsReadOnly, null));
154+
properties.add(0, new TsPropertyModel(bean.getDiscriminantProperty(), discriminantType, settings.declarePropertiesAsReadOnly, /*ownProperty*/ true, null));
155155
}
156156

157157
return new TsBeanModel(bean.getOrigin(), TsBeanCategory.Data, isClass, beanIdentifier, typeParameters, parentType, bean.getTaggedUnionClasses(), interfaces, properties, null, null, bean.getComments());
@@ -202,7 +202,7 @@ private static boolean containsProperty(List<TsPropertyModel> properties, String
202202
private TsPropertyModel processProperty(SymbolTable symbolTable, BeanModel bean, PropertyModel property, String prefix, String suffix) {
203203
final TsType type = typeFromJava(symbolTable, property.getType(), property.getName(), bean.getOrigin());
204204
final TsType tsType = property.isOptional() ? type.optional() : type;
205-
return new TsPropertyModel(prefix + property.getName() + suffix, tsType, settings.declarePropertiesAsReadOnly, property.getComments());
205+
return new TsPropertyModel(prefix + property.getName() + suffix, tsType, settings.declarePropertiesAsReadOnly, false, property.getComments());
206206
}
207207

208208
private TsEnumModel<?> processEnum(SymbolTable symbolTable, EnumModel<?> enumModel) {
@@ -238,7 +238,7 @@ private TsModel removeInheritedProperties(SymbolTable symbolTable, TsModel tsMod
238238
final Map<String, TsType> inheritedPropertyTypes = getInheritedProperties(symbolTable, tsModel, bean.getParentAndInterfaces());
239239
final List<TsPropertyModel> properties = new ArrayList<>();
240240
for (TsPropertyModel property : bean.getProperties()) {
241-
if (!Objects.equals(property.getTsType(), inheritedPropertyTypes.get(property.getName()))) {
241+
if (property.isOwnProperty() || !Objects.equals(property.getTsType(), inheritedPropertyTypes.get(property.getName()))) {
242242
properties.add(property);
243243
}
244244
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,26 @@
99
public class TsPropertyModel extends TsProperty implements Comparable<TsProperty> {
1010

1111
public final boolean readonly;
12+
public final boolean ownProperty; // property exists directly on the bean, should not be inherited
1213
public final List<String> comments;
1314

14-
public TsPropertyModel(String name, TsType tsType, boolean readonly, List<String> comments) {
15+
public TsPropertyModel(String name, TsType tsType, boolean readonly, boolean ownProperty, List<String> comments) {
1516
super(name, tsType);
1617
this.readonly = readonly;
1718
this.comments = comments;
19+
this.ownProperty = ownProperty;
20+
}
21+
22+
public boolean isOwnProperty() {
23+
return ownProperty;
1824
}
1925

2026
public List<String> getComments() {
2127
return comments;
2228
}
2329

2430
public TsPropertyModel setTsType(TsType type) {
25-
return new TsPropertyModel(getName(), type, readonly, getComments());
31+
return new TsPropertyModel(getName(), type, readonly, ownProperty, getComments());
2632
}
2733

2834
@Override

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public void testImplements() {
8484
final String nl = settings.newline;
8585
final String expected =
8686
"export interface IA extends IB<string> {" + nl +
87+
" type: string;" + nl +
8788
"}" + nl +
8889
"" + nl +
8990
"export interface IB<T> {" + nl +

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,14 @@ public void testTaggedUnionsWithInterfaces() {
196196
public void testTaggedUnionsWithOverlappingInterfaces() {
197197
final Settings settings = TestUtils.settings();
198198
final String output = new TypeScriptGenerator(settings).generateTypeScript(Input.from(IShape3.class));
199-
System.out.println(output);
200199
final String expected = (
201200
"\n" +
202201
"interface IShape3 {\n" +
203202
" kind: 'circle' | 'rectangle';\n" +
204203
"}\n" +
205204
"\n" +
206205
"interface IRectangle3 extends INamedQuadrilateral3 {\n" +
206+
" kind: 'rectangle';\n" +
207207
"}\n" +
208208
"\n" +
209209
"interface ICircle3 extends INamedShape3 {\n" +
@@ -215,6 +215,7 @@ public void testTaggedUnionsWithOverlappingInterfaces() {
215215
"}\n" +
216216
"\n" +
217217
"interface INamedShape3 extends IShape3 {\n" +
218+
" kind: 'circle' | 'rectangle';\n" +
218219
" name: string;\n" +
219220
"}\n" +
220221
"\n" +
@@ -266,7 +267,6 @@ public void testTaggedUnionsDisabled() {
266267
public void testTaggedUnionsWithDiamond() {
267268
final Settings settings = TestUtils.settings();
268269
final String output = new TypeScriptGenerator(settings).generateTypeScript(Input.from(DiamondA.class));
269-
System.out.println(output);
270270
final String expected = (
271271
"\n" +
272272
"interface DiamondA {\n" +

0 commit comments

Comments
 (0)