|
4 | 4 | import com.fasterxml.jackson.annotation.JsonSubTypes; |
5 | 5 | import com.fasterxml.jackson.annotation.JsonTypeInfo; |
6 | 6 | import com.fasterxml.jackson.annotation.JsonTypeName; |
| 7 | +import com.fasterxml.jackson.databind.ObjectMapper; |
7 | 8 | import java.util.List; |
8 | 9 | import org.junit.Assert; |
9 | 10 | import org.junit.Test; |
@@ -120,6 +121,23 @@ private static interface DiamondC extends DiamondB1, DiamondB2 { |
120 | 121 | public String getC(); |
121 | 122 | } |
122 | 123 |
|
| 124 | + @JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="@class") |
| 125 | + @JsonSubTypes({ |
| 126 | + @JsonSubTypes.Type(DieselCar.class), |
| 127 | + @JsonSubTypes.Type(ElectricCar.class), |
| 128 | + }) |
| 129 | + private static class Car { |
| 130 | + public String name; |
| 131 | + } |
| 132 | + |
| 133 | + private static class DieselCar extends Car { |
| 134 | + public double fuelTankCapacityInLiters; |
| 135 | + } |
| 136 | + |
| 137 | + private static class ElectricCar extends Car { |
| 138 | + public double batteryCapacityInKWh; |
| 139 | + } |
| 140 | + |
123 | 141 | @Test |
124 | 142 | public void testTaggedUnions() { |
125 | 143 | final Settings settings = TestUtils.settings(); |
@@ -295,4 +313,39 @@ public void testTaggedUnionsWithDiamond() { |
295 | 313 | Assert.assertEquals(expected, output); |
296 | 314 | } |
297 | 315 |
|
| 316 | + @Test |
| 317 | + public void testIdClass() { |
| 318 | + final Settings settings = TestUtils.settings(); |
| 319 | + final String output = new TypeScriptGenerator(settings).generateTypeScript(Input.from(Car.class)); |
| 320 | + System.out.println(output); |
| 321 | + final String expected = ( |
| 322 | + "\n" + |
| 323 | + "interface Car {\n" + |
| 324 | + " '@class': 'cz.habarta.typescript.generator.TaggedUnionsTest$DieselCar' | 'cz.habarta.typescript.generator.TaggedUnionsTest$ElectricCar';\n" + |
| 325 | + " name: string;\n" + |
| 326 | + "}\n" + |
| 327 | + "\n" + |
| 328 | + "interface DieselCar extends Car {\n" + |
| 329 | + " '@class': 'cz.habarta.typescript.generator.TaggedUnionsTest$DieselCar';\n" + |
| 330 | + " fuelTankCapacityInLiters: number;\n" + |
| 331 | + "}\n" + |
| 332 | + "\n" + |
| 333 | + "interface ElectricCar extends Car {\n" + |
| 334 | + " '@class': 'cz.habarta.typescript.generator.TaggedUnionsTest$ElectricCar';\n" + |
| 335 | + " batteryCapacityInKWh: number;\n" + |
| 336 | + "}\n" + |
| 337 | + "\n" + |
| 338 | + "type CarUnion = DieselCar | ElectricCar;\n" + |
| 339 | + "" |
| 340 | + ).replace('\'', '"'); |
| 341 | + Assert.assertEquals(expected, output); |
| 342 | + } |
| 343 | + |
| 344 | + public static void main(String[] args) throws Exception { |
| 345 | + final ElectricCar electricCar = new ElectricCar(); |
| 346 | + electricCar.name = "Tesla"; |
| 347 | + electricCar.batteryCapacityInKWh = 75; // kWh |
| 348 | + System.out.println(new ObjectMapper().writeValueAsString(electricCar)); |
| 349 | + } |
| 350 | + |
298 | 351 | } |
0 commit comments