Skip to content

Commit

Permalink
Support for @JsonTypeInfo(use=Id.CLASS) (refs #189)
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtechhabarta committed Oct 25, 2017
1 parent 40e1caa commit ccd2bbe
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private BeanModel parseBean(SourceType<Class<?>> sourceClass) {
} else if (isSupported(parentJsonTypeInfo = getAnnotationRecursive(sourceClass.type, JsonTypeInfo.class))) {
// this is child class
discriminantProperty = getDiscriminantPropertyName(parentJsonTypeInfo);
discriminantLiteral = getTypeName(sourceClass.type);
discriminantLiteral = getTypeName(parentJsonTypeInfo, sourceClass.type);
} else {
// not part of explicit hierarchy
discriminantProperty = null;
Expand Down Expand Up @@ -172,7 +172,11 @@ private String getDiscriminantPropertyName(JsonTypeInfo jsonTypeInfo) {
: jsonTypeInfo.property();
}

private String getTypeName(final Class<?> cls) {
private String getTypeName(JsonTypeInfo parentJsonTypeInfo, final Class<?> cls) {
// Id.CLASS
if (parentJsonTypeInfo.use() == JsonTypeInfo.Id.CLASS) {
return cls.getName();
}
// find @JsonTypeName recursively
final JsonTypeName jsonTypeName = getAnnotationRecursive(cls, JsonTypeName.class);
if (jsonTypeName != null && !jsonTypeName.value().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
Expand Down Expand Up @@ -120,6 +121,23 @@ private static interface DiamondC extends DiamondB1, DiamondB2 {
public String getC();
}

@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="@class")
@JsonSubTypes({
@JsonSubTypes.Type(DieselCar.class),
@JsonSubTypes.Type(ElectricCar.class),
})
private static class Car {
public String name;
}

private static class DieselCar extends Car {
public double fuelTankCapacityInLiters;
}

private static class ElectricCar extends Car {
public double batteryCapacityInKWh;
}

@Test
public void testTaggedUnions() {
final Settings settings = TestUtils.settings();
Expand Down Expand Up @@ -295,4 +313,39 @@ public void testTaggedUnionsWithDiamond() {
Assert.assertEquals(expected, output);
}

@Test
public void testIdClass() {
final Settings settings = TestUtils.settings();
final String output = new TypeScriptGenerator(settings).generateTypeScript(Input.from(Car.class));
System.out.println(output);
final String expected = (
"\n" +
"interface Car {\n" +
" '@class': 'cz.habarta.typescript.generator.TaggedUnionsTest$DieselCar' | 'cz.habarta.typescript.generator.TaggedUnionsTest$ElectricCar';\n" +
" name: string;\n" +
"}\n" +
"\n" +
"interface DieselCar extends Car {\n" +
" '@class': 'cz.habarta.typescript.generator.TaggedUnionsTest$DieselCar';\n" +
" fuelTankCapacityInLiters: number;\n" +
"}\n" +
"\n" +
"interface ElectricCar extends Car {\n" +
" '@class': 'cz.habarta.typescript.generator.TaggedUnionsTest$ElectricCar';\n" +
" batteryCapacityInKWh: number;\n" +
"}\n" +
"\n" +
"type CarUnion = DieselCar | ElectricCar;\n" +
""
).replace('\'', '"');
Assert.assertEquals(expected, output);
}

public static void main(String[] args) throws Exception {
final ElectricCar electricCar = new ElectricCar();
electricCar.name = "Tesla";
electricCar.batteryCapacityInKWh = 75; // kWh
System.out.println(new ObjectMapper().writeValueAsString(electricCar));
}

}

0 comments on commit ccd2bbe

Please sign in to comment.