Skip to content

Commit

Permalink
Discriminant properties preserved in inherited beans (refs #149)
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtechhabarta committed Jun 28, 2017
1 parent d92fc86 commit 22b2859
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private <T> TsBeanModel processBean(SymbolTable symbolTable, Model model, Map<Ty
final TsType discriminantType = literals.isEmpty()
? TsType.String
: new TsType.UnionType(literals);
properties.add(0, new TsPropertyModel(bean.getDiscriminantProperty(), discriminantType, settings.declarePropertiesAsReadOnly, null));
properties.add(0, new TsPropertyModel(bean.getDiscriminantProperty(), discriminantType, settings.declarePropertiesAsReadOnly, /*ownProperty*/ true, null));
}

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

private TsEnumModel<?> processEnum(SymbolTable symbolTable, EnumModel<?> enumModel) {
Expand Down Expand Up @@ -238,7 +238,7 @@ private TsModel removeInheritedProperties(SymbolTable symbolTable, TsModel tsMod
final Map<String, TsType> inheritedPropertyTypes = getInheritedProperties(symbolTable, tsModel, bean.getParentAndInterfaces());
final List<TsPropertyModel> properties = new ArrayList<>();
for (TsPropertyModel property : bean.getProperties()) {
if (!Objects.equals(property.getTsType(), inheritedPropertyTypes.get(property.getName()))) {
if (property.isOwnProperty() || !Objects.equals(property.getTsType(), inheritedPropertyTypes.get(property.getName()))) {
properties.add(property);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,26 @@
public class TsPropertyModel extends TsProperty implements Comparable<TsProperty> {

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

public TsPropertyModel(String name, TsType tsType, boolean readonly, List<String> comments) {
public TsPropertyModel(String name, TsType tsType, boolean readonly, boolean ownProperty, List<String> comments) {
super(name, tsType);
this.readonly = readonly;
this.comments = comments;
this.ownProperty = ownProperty;
}

public boolean isOwnProperty() {
return ownProperty;
}

public List<String> getComments() {
return comments;
}

public TsPropertyModel setTsType(TsType type) {
return new TsPropertyModel(getName(), type, readonly, getComments());
return new TsPropertyModel(getName(), type, readonly, ownProperty, getComments());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public void testImplements() {
final String nl = settings.newline;
final String expected =
"export interface IA extends IB<string> {" + nl +
" type: string;" + nl +
"}" + nl +
"" + nl +
"export interface IB<T> {" + nl +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,14 @@ public void testTaggedUnionsWithInterfaces() {
public void testTaggedUnionsWithOverlappingInterfaces() {
final Settings settings = TestUtils.settings();
final String output = new TypeScriptGenerator(settings).generateTypeScript(Input.from(IShape3.class));
System.out.println(output);
final String expected = (
"\n" +
"interface IShape3 {\n" +
" kind: 'circle' | 'rectangle';\n" +
"}\n" +
"\n" +
"interface IRectangle3 extends INamedQuadrilateral3 {\n" +
" kind: 'rectangle';\n" +
"}\n" +
"\n" +
"interface ICircle3 extends INamedShape3 {\n" +
Expand All @@ -215,6 +215,7 @@ public void testTaggedUnionsWithOverlappingInterfaces() {
"}\n" +
"\n" +
"interface INamedShape3 extends IShape3 {\n" +
" kind: 'circle' | 'rectangle';\n" +
" name: string;\n" +
"}\n" +
"\n" +
Expand Down Expand Up @@ -266,7 +267,6 @@ public void testTaggedUnionsDisabled() {
public void testTaggedUnionsWithDiamond() {
final Settings settings = TestUtils.settings();
final String output = new TypeScriptGenerator(settings).generateTypeScript(Input.from(DiamondA.class));
System.out.println(output);
final String expected = (
"\n" +
"interface DiamondA {\n" +
Expand Down

0 comments on commit 22b2859

Please sign in to comment.