-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Jackson2: Super class did not get type information (#497)
* Jackson2: Classes with type info annotation can be used as type if they are not abstract. * Mark test classes that should not be deserializable as abstract.
- Loading branch information
1 parent
c8b517c
commit 8e91cd7
Showing
5 changed files
with
73 additions
and
7 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
62 changes: 62 additions & 0 deletions
62
...or-core/src/test/java/cz/habarta/typescript/generator/Jackson2DeserializableRootType.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,62 @@ | ||
|
||
package cz.habarta.typescript.generator; | ||
|
||
import com.fasterxml.jackson.annotation.JsonSubTypes; | ||
import com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
import com.fasterxml.jackson.annotation.JsonTypeName; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Test that root type name in a hirarchy is included iff root type itself is not abstract | ||
*/ | ||
public class Jackson2DeserializableRootType { | ||
|
||
@Test | ||
public void testHowJacksonDeserializes() throws JsonProcessingException { | ||
NonAbstractRoot nar = new ObjectMapper() | ||
.readValue("{\"type\": \"rootType\"}", | ||
NonAbstractRoot.class); | ||
NonAbstractRoot nars = new ObjectMapper() | ||
.readValue("{\"type\": \"subType\"}", | ||
NonAbstractRoot.class); | ||
|
||
Assert.assertSame(NonAbstractRoot.class, nar.getClass()); | ||
Assert.assertSame(NonAbstractRootSub.class, nars.getClass()); | ||
} | ||
|
||
@Test | ||
public void testRootTypeIncludedIfNotAbstract() { | ||
final String output = new TypeScriptGenerator(TestUtils.settings()).generateTypeScript(Input.from(NonAbstractRoot.class)); | ||
Assert.assertTrue(output.contains("\"rootType\"")); | ||
} | ||
|
||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type") | ||
@JsonSubTypes(@JsonSubTypes.Type(NonAbstractRootSub.class)) | ||
@JsonTypeName("rootType") | ||
public static class NonAbstractRoot { | ||
} | ||
|
||
@JsonTypeName("subType") | ||
public static class NonAbstractRootSub extends NonAbstractRoot { | ||
} | ||
|
||
@Test | ||
public void testRootTypeNotIncludedIfAbstract() { | ||
final String output = new TypeScriptGenerator(TestUtils.settings()).generateTypeScript(Input.from(AbstractRoot.class)); | ||
// Root type is abstract and therefore ignored in the type list | ||
Assert.assertFalse(output.contains("\"rootType\"")); | ||
} | ||
|
||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type") | ||
@JsonSubTypes(@JsonSubTypes.Type(AbstractRootSub.class)) | ||
@JsonTypeName("rootType") | ||
public static abstract class AbstractRoot { | ||
} | ||
|
||
@JsonTypeName("subType") | ||
public static class AbstractRootSub extends AbstractRoot { | ||
} | ||
} |
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
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