Skip to content

Commit 351a936

Browse files
Merge pull request #149 from glentakahashi/glen/allow-immutables
Allow JsonSubTypes to be interfaces/abstract classes
2 parents dbd3437 + e3f53c1 commit 351a936

File tree

8 files changed

+105
-3
lines changed

8 files changed

+105
-3
lines changed

typescript-generator-core/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@
5050
<version>4.11</version>
5151
<scope>test</scope>
5252
</dependency>
53+
<dependency>
54+
<groupId>org.immutables</groupId>
55+
<artifactId>value</artifactId>
56+
<version>2.5.3</version>
57+
<scope>test</scope>
58+
</dependency>
5359
<dependency>
5460
<groupId>com.google.code.findbugs</groupId>
5561
<artifactId>annotations</artifactId>

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/parser/Jackson2Parser.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ private BeanModel parseBean(SourceType<Class<?>> sourceClass) {
113113
// this is parent
114114
discriminantProperty = getDiscriminantPropertyName(jsonTypeInfo);
115115
discriminantLiteral = null;
116-
} else if (!sourceClass.type.isInterface() && !Modifier.isAbstract(sourceClass.type.getModifiers()) && isSupported(parentJsonTypeInfo = getAnnotationRecursive(sourceClass.type, JsonTypeInfo.class))) {
116+
} else if (isSupported(parentJsonTypeInfo = getAnnotationRecursive(sourceClass.type, JsonTypeInfo.class))) {
117117
// this is child class
118118
discriminantProperty = getDiscriminantPropertyName(parentJsonTypeInfo);
119119
discriminantLiteral = getTypeName(sourceClass.type);
@@ -186,8 +186,11 @@ public boolean test(JsonSubTypes types) {
186186
return jsonSubType.name();
187187
}
188188
}
189-
// use simplified class name
190-
return cls.getName().substring(cls.getName().lastIndexOf(".") + 1);
189+
// use simplified class name if it's not an interface or abstract
190+
if(!cls.isInterface() && !Modifier.isAbstract(cls.getModifiers())) {
191+
return cls.getName().substring(cls.getName().lastIndexOf(".") + 1);
192+
}
193+
return null;
191194
}
192195

193196
private static JsonSubTypes.Type getJsonSubTypeForClass(JsonSubTypes types, Class<?> cls) {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package cz.habarta.typescript.generator;
2+
3+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
4+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
5+
import org.immutables.value.Value;
6+
7+
@Value.Immutable
8+
@JsonSerialize(as = ImmutableCircle.class)
9+
@JsonDeserialize(as = ImmutableCircle.class)
10+
public interface Circle extends Shape {
11+
double radius();
12+
13+
final class Builder extends ImmutableCircle.Builder {}
14+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
package cz.habarta.typescript.generator;
3+
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
7+
public class ImmutablesTest {
8+
9+
@Test
10+
public void testImmutables() {
11+
final Settings settings = TestUtils.settings();
12+
final String output = new TypeScriptGenerator(settings).generateTypeScript(Input.from(Shape.class));
13+
final String expected = (
14+
"\n" +
15+
"interface Shape {\n" +
16+
" kind: 'square' | 'rectangle' | 'circle';\n" +
17+
"}\n" +
18+
"\n" +
19+
"interface Square extends Shape {\n" +
20+
" kind: 'square';\n" +
21+
" size: number;\n" +
22+
"}\n" +
23+
"\n" +
24+
"interface Rectangle extends Shape {\n" +
25+
" kind: 'rectangle';\n" +
26+
" width: number;\n" +
27+
" height: number;\n" +
28+
"}\n" +
29+
"\n" +
30+
"interface Circle extends Shape {\n" +
31+
" kind: 'circle';\n" +
32+
" radius: number;\n" +
33+
"}\n" +
34+
"\n" +
35+
"type ShapeUnion = Square | Rectangle | Circle;\n" +
36+
""
37+
).replace('\'', '"');
38+
Assert.assertEquals(expected, output);
39+
}
40+
41+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cz.habarta.typescript.generator;
2+
3+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
4+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
5+
import org.immutables.value.Value;
6+
7+
@Value.Immutable
8+
@JsonSerialize(as = ImmutableRectangle.class)
9+
@JsonDeserialize(as = ImmutableRectangle.class)
10+
public abstract class Rectangle implements Shape {
11+
public abstract double width();
12+
public abstract double height();
13+
14+
public static Rectangle.Builder builder() {
15+
return new Rectangle.Builder();
16+
}
17+
18+
public static final class Builder extends ImmutableRectangle.Builder {}
19+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cz.habarta.typescript.generator;
2+
3+
import com.fasterxml.jackson.annotation.JsonSubTypes;
4+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
5+
6+
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "kind")
7+
@JsonSubTypes({
8+
@JsonSubTypes.Type(value = Square.class, name = "square"),
9+
@JsonSubTypes.Type(value = Rectangle.class, name = "rectangle"),
10+
@JsonSubTypes.Type(value = Circle.class, name = "circle"),
11+
})
12+
public interface Shape {
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package cz.habarta.typescript.generator;
2+
3+
public class Square implements Shape {
4+
public double size;
5+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ public void testTaggedUnionsWithInterfaces() {
132132
"}\n" +
133133
"\n" +
134134
"interface IQuadrilateral2 extends IShape2 {\n" +
135+
" kind: 'square' | 'rectangle';\n" +
135136
"}\n" +
136137
"\n" +
137138
"type IShape2Union = CSquare2 | CRectangle2 | CCircle2;\n" +

0 commit comments

Comments
 (0)