Skip to content

Commit dcc804d

Browse files
Merge pull request #150 from glentakahashi/glen/sort-discriminant-types
Add option to sort discriminant types
2 parents 921d591 + 198d5e5 commit dcc804d

File tree

6 files changed

+104
-3
lines changed

6 files changed

+104
-3
lines changed

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/Settings.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public class Settings {
5656
public boolean restOptionsTypeIsGeneric;
5757
public TypeProcessor customTypeProcessor = null;
5858
public boolean sortDeclarations = false;
59+
public boolean sortDiscriminantTypes = false;
5960
public boolean sortTypeDeclarations = false;
6061
public boolean noFileComment = false;
6162
public List<File> javadocXmlFiles = null;

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/TsType.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,16 +166,26 @@ public String format(Settings settings) {
166166
public static class UnionType extends TsType {
167167

168168
public final List<TsType> types;
169+
public final boolean sort;
169170

170171
public UnionType(List<? extends TsType> types) {
172+
this(types, false);
173+
}
174+
175+
public UnionType(List<? extends TsType> types, boolean sort) {
171176
this.types = new ArrayList<TsType>(new LinkedHashSet<TsType>(types));
177+
this.sort = sort;
172178
}
173179

174180
@Override
175181
public String format(Settings settings) {
176-
return types.isEmpty()
182+
List<String> formattedTypes = format(this.types, settings);
183+
if (this.sort) {
184+
Collections.sort(formattedTypes);
185+
}
186+
return formattedTypes.isEmpty()
177187
? "never"
178-
: Utils.join(format(types, settings), " | ");
188+
: Utils.join(formattedTypes, " | ");
179189
}
180190

181191
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ private <T> TsBeanModel processBean(SymbolTable symbolTable, Model model, Map<Ty
150150
}
151151
final TsType discriminantType = literals.isEmpty()
152152
? TsType.String
153-
: new TsType.UnionType(literals);
153+
: new TsType.UnionType(literals, settings.sortDiscriminantTypes);
154154
properties.add(0, new TsPropertyModel(bean.getDiscriminantProperty(), discriminantType, settings.declarePropertiesAsReadOnly, null));
155155
}
156156

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
2+
package cz.habarta.typescript.generator;
3+
4+
import com.fasterxml.jackson.annotation.JsonSubTypes;
5+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
6+
import com.fasterxml.jackson.annotation.JsonTypeName;
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
10+
import java.util.List;
11+
12+
13+
public class SortedDiscriminantTypesTest {
14+
15+
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "kind")
16+
@JsonSubTypes({
17+
@JsonSubTypes.Type(Square.class),
18+
@JsonSubTypes.Type(Rectangle.class),
19+
@JsonSubTypes.Type(Circle.class),
20+
})
21+
private interface Shape {
22+
}
23+
24+
private interface Quadrilateral extends Shape {
25+
}
26+
27+
@JsonTypeName("square")
28+
private static class Square implements Quadrilateral {
29+
public double size;
30+
}
31+
32+
@JsonTypeName("rectangle")
33+
private static class Rectangle implements Quadrilateral {
34+
public double width;
35+
public double height;
36+
}
37+
38+
@JsonTypeName("circle")
39+
private static class Circle implements Shape {
40+
public double radius;
41+
}
42+
43+
@Test
44+
public void testSortedDiscriminantTypes() {
45+
final Settings settings = TestUtils.settings();
46+
settings.sortDiscriminantTypes = true;
47+
final String output = new TypeScriptGenerator(settings).generateTypeScript(Input.from(Shape.class));
48+
System.out.println(output);
49+
final String expected = (
50+
"\n" +
51+
"interface Shape {\n" +
52+
" kind: 'circle' | 'rectangle' | 'square';\n" +
53+
"}\n" +
54+
"\n" +
55+
"interface Square extends Quadrilateral {\n" +
56+
" kind: 'square';\n" +
57+
" size: number;\n" +
58+
"}\n" +
59+
"\n" +
60+
"interface Rectangle extends Quadrilateral {\n" +
61+
" kind: 'rectangle';\n" +
62+
" width: number;\n" +
63+
" height: number;\n" +
64+
"}\n" +
65+
"\n" +
66+
"interface Circle extends Shape {\n" +
67+
" kind: 'circle';\n" +
68+
" radius: number;\n" +
69+
"}\n" +
70+
"\n" +
71+
"interface Quadrilateral extends Shape {\n" +
72+
" kind: 'rectangle' | 'square';\n" +
73+
"}\n" +
74+
"\n" +
75+
"type ShapeUnion = Square | Rectangle | Circle;\n" +
76+
""
77+
).replace('\'', '"');
78+
Assert.assertEquals(expected, output);
79+
}
80+
81+
}

typescript-generator-gradle-plugin/src/main/java/cz/habarta/typescript/generator/gradle/GenerateTask.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public class GenerateTask extends DefaultTask {
5151
public String restOptionsType;
5252
public String customTypeProcessor;
5353
public boolean sortDeclarations;
54+
public boolean sortDiscriminantTypes;
5455
public boolean sortTypeDeclarations;
5556
public boolean noFileComment;
5657
public List<File> javadocXmlFiles;
@@ -123,6 +124,7 @@ public void generate() throws Exception {
123124
settings.setRestOptionsType(restOptionsType);
124125
settings.loadCustomTypeProcessor(classLoader, customTypeProcessor);
125126
settings.sortDeclarations = sortDeclarations;
127+
settings.sortDiscriminantTypes = sortDiscriminantTypes;
126128
settings.sortTypeDeclarations = sortTypeDeclarations;
127129
settings.noFileComment = noFileComment;
128130
settings.javadocXmlFiles = javadocXmlFiles;

typescript-generator-maven-plugin/src/main/java/cz/habarta/typescript/generator/maven/GenerateMojo.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,12 @@ public class GenerateMojo extends AbstractMojo {
317317
@Parameter
318318
private boolean sortDeclarations;
319319

320+
/**
321+
* If true discriminant types will be sorted alphabetically.
322+
*/
323+
@Parameter
324+
private boolean sortDiscriminantTypes;
325+
320326
/**
321327
* If true TypeScript type declarations (interfaces) will be sorted alphabetically.
322328
*/
@@ -462,6 +468,7 @@ public void execute() {
462468
settings.setRestOptionsType(restOptionsType);
463469
settings.loadCustomTypeProcessor(classLoader, customTypeProcessor);
464470
settings.sortDeclarations = sortDeclarations;
471+
settings.sortDiscriminantTypes = sortDiscriminantTypes;
465472
settings.sortTypeDeclarations = sortTypeDeclarations;
466473
settings.noFileComment = noFileComment;
467474
settings.javadocXmlFiles = javadocXmlFiles;

0 commit comments

Comments
 (0)