-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #149 from glentakahashi/glen/allow-immutables
Allow JsonSubTypes to be interfaces/abstract classes
- Loading branch information
Showing
8 changed files
with
105 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
typescript-generator-core/src/test/java/cz/habarta/typescript/generator/Circle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package cz.habarta.typescript.generator; | ||
|
||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import com.fasterxml.jackson.databind.annotation.JsonSerialize; | ||
import org.immutables.value.Value; | ||
|
||
@Value.Immutable | ||
@JsonSerialize(as = ImmutableCircle.class) | ||
@JsonDeserialize(as = ImmutableCircle.class) | ||
public interface Circle extends Shape { | ||
double radius(); | ||
|
||
final class Builder extends ImmutableCircle.Builder {} | ||
} |
41 changes: 41 additions & 0 deletions
41
typescript-generator-core/src/test/java/cz/habarta/typescript/generator/ImmutablesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
package cz.habarta.typescript.generator; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class ImmutablesTest { | ||
|
||
@Test | ||
public void testImmutables() { | ||
final Settings settings = TestUtils.settings(); | ||
final String output = new TypeScriptGenerator(settings).generateTypeScript(Input.from(Shape.class)); | ||
final String expected = ( | ||
"\n" + | ||
"interface Shape {\n" + | ||
" kind: 'square' | 'rectangle' | 'circle';\n" + | ||
"}\n" + | ||
"\n" + | ||
"interface Square extends Shape {\n" + | ||
" kind: 'square';\n" + | ||
" size: number;\n" + | ||
"}\n" + | ||
"\n" + | ||
"interface Rectangle extends Shape {\n" + | ||
" kind: 'rectangle';\n" + | ||
" width: number;\n" + | ||
" height: number;\n" + | ||
"}\n" + | ||
"\n" + | ||
"interface Circle extends Shape {\n" + | ||
" kind: 'circle';\n" + | ||
" radius: number;\n" + | ||
"}\n" + | ||
"\n" + | ||
"type ShapeUnion = Square | Rectangle | Circle;\n" + | ||
"" | ||
).replace('\'', '"'); | ||
Assert.assertEquals(expected, output); | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
typescript-generator-core/src/test/java/cz/habarta/typescript/generator/Rectangle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package cz.habarta.typescript.generator; | ||
|
||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import com.fasterxml.jackson.databind.annotation.JsonSerialize; | ||
import org.immutables.value.Value; | ||
|
||
@Value.Immutable | ||
@JsonSerialize(as = ImmutableRectangle.class) | ||
@JsonDeserialize(as = ImmutableRectangle.class) | ||
public abstract class Rectangle implements Shape { | ||
public abstract double width(); | ||
public abstract double height(); | ||
|
||
public static Rectangle.Builder builder() { | ||
return new Rectangle.Builder(); | ||
} | ||
|
||
public static final class Builder extends ImmutableRectangle.Builder {} | ||
} |
13 changes: 13 additions & 0 deletions
13
typescript-generator-core/src/test/java/cz/habarta/typescript/generator/Shape.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package cz.habarta.typescript.generator; | ||
|
||
import com.fasterxml.jackson.annotation.JsonSubTypes; | ||
import com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
|
||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "kind") | ||
@JsonSubTypes({ | ||
@JsonSubTypes.Type(value = Square.class, name = "square"), | ||
@JsonSubTypes.Type(value = Rectangle.class, name = "rectangle"), | ||
@JsonSubTypes.Type(value = Circle.class, name = "circle"), | ||
}) | ||
public interface Shape { | ||
} |
5 changes: 5 additions & 0 deletions
5
typescript-generator-core/src/test/java/cz/habarta/typescript/generator/Square.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package cz.habarta.typescript.generator; | ||
|
||
public class Square implements Shape { | ||
public double size; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters