-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Subclasses of sealed classes are now discovered automatically,
making it possible to omit the @JsonSubTypes annotation.
- Loading branch information
Showing
2 changed files
with
95 additions
and
5 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
73 changes: 73 additions & 0 deletions
73
src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/Github239.kt
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,73 @@ | ||
package com.fasterxml.jackson.module.kotlin.test | ||
|
||
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 com.fasterxml.jackson.module.kotlin.KotlinModule | ||
import com.fasterxml.jackson.module.kotlin.readValue | ||
import org.junit.Test | ||
import kotlin.test.assertEquals | ||
|
||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME) | ||
sealed class Github239Either { | ||
|
||
@JsonTypeName("a") | ||
data class A(var field: String = "") : Github239Either() | ||
|
||
@JsonTypeName("b") | ||
data class B(var otherField: String = "") : Github239Either() | ||
|
||
} | ||
|
||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME) | ||
@JsonSubTypes( | ||
JsonSubTypes.Type(Github239EitherCustomized.A::class, name = "a"), | ||
JsonSubTypes.Type(Github239EitherCustomized.B::class, name = "b") | ||
) | ||
sealed class Github239EitherCustomized { | ||
|
||
data class A(var field: String = "") : Github239EitherCustomized() | ||
|
||
data class B(var otherField: String = "") : Github239EitherCustomized() | ||
|
||
} | ||
|
||
class TestGithub239 { | ||
val json = """[ | ||
{ | ||
"@type": "a", | ||
"field": "value" | ||
}, | ||
{ | ||
"@type": "b", | ||
"otherField": "1234" | ||
} | ||
]""" | ||
|
||
val mapper = ObjectMapper() | ||
.registerModule(KotlinModule()) | ||
|
||
@Test | ||
fun test_implicit_subclasses() { | ||
|
||
val array = mapper.readValue<Array<Github239Either>>(json) | ||
|
||
assertEquals(2, array.size) | ||
assertEquals(Github239Either.A("value"), array[0]) | ||
assertEquals(Github239Either.B("1234"), array[1]) | ||
|
||
} | ||
|
||
@Test | ||
fun test_explicit_subclasses() { | ||
|
||
val array = mapper.readValue<Array<Github239EitherCustomized>>(json) | ||
|
||
assertEquals(2, array.size) | ||
assertEquals(Github239EitherCustomized.A("value"), array[0]) | ||
assertEquals(Github239EitherCustomized.B("1234"), array[1]) | ||
|
||
} | ||
|
||
} |