-
Notifications
You must be signed in to change notification settings - Fork 47
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 #175 from cjbooms/ISSUE-174
Add companion object on polymorphic subtypes
- Loading branch information
Showing
3 changed files
with
119 additions
and
12 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
106 changes: 94 additions & 12 deletions
106
src/test/resources/examples/companionObject/models/Models.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 |
---|---|---|
@@ -1,36 +1,118 @@ | ||
package examples.companionObject.models | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty | ||
import com.fasterxml.jackson.annotation.JsonSubTypes | ||
import com.fasterxml.jackson.annotation.JsonTypeInfo | ||
import com.fasterxml.jackson.annotation.JsonValue | ||
import javax.validation.constraints.NotNull | ||
import kotlin.Boolean | ||
import kotlin.Int | ||
import kotlin.Long | ||
import kotlin.String | ||
import kotlin.collections.Map | ||
|
||
data class Error( | ||
@param:JsonProperty("code") | ||
@get:JsonProperty("code") | ||
data class Cat( | ||
@param:JsonProperty("id") | ||
@get:JsonProperty("id") | ||
@get:NotNull | ||
val code: Int, | ||
@param:JsonProperty("message") | ||
@get:JsonProperty("message") | ||
override val id: Long, | ||
@param:JsonProperty("name") | ||
@get:JsonProperty("name") | ||
@get:NotNull | ||
val message: String | ||
) { | ||
override val name: String, | ||
@param:JsonProperty("tag") | ||
@get:JsonProperty("tag") | ||
override val tag: String? = null, | ||
@param:JsonProperty("hunts") | ||
@get:JsonProperty("hunts") | ||
val hunts: Boolean? = null, | ||
@param:JsonProperty("age") | ||
@get:JsonProperty("age") | ||
val age: Int? = null | ||
) : Pet(id, name, tag) { | ||
@get:JsonProperty("petType") | ||
@get:NotNull | ||
override val petType: String = "Cat" | ||
|
||
companion object | ||
} | ||
|
||
data class Pet( | ||
data class Dog( | ||
@param:JsonProperty("id") | ||
@get:JsonProperty("id") | ||
@get:NotNull | ||
val id: Long, | ||
override val id: Long, | ||
@param:JsonProperty("name") | ||
@get:JsonProperty("name") | ||
@get:NotNull | ||
val name: String, | ||
override val name: String, | ||
@param:JsonProperty("tag") | ||
@get:JsonProperty("tag") | ||
val tag: String? = null | ||
override val tag: String? = null, | ||
@param:JsonProperty("bark") | ||
@get:JsonProperty("bark") | ||
val bark: Boolean? = null, | ||
@param:JsonProperty("breed") | ||
@get:JsonProperty("breed") | ||
val breed: DogBreed? = null | ||
) : Pet(id, name, tag) { | ||
@get:JsonProperty("petType") | ||
@get:NotNull | ||
override val petType: String = "Dog" | ||
|
||
companion object | ||
} | ||
|
||
enum class DogBreed( | ||
@JsonValue | ||
val value: String | ||
) { | ||
DINGO("Dingo"), | ||
|
||
HUSKY("Husky"), | ||
|
||
RETRIEVER("Retriever"), | ||
|
||
SHEPHERD("Shepherd"); | ||
|
||
companion object { | ||
private val mapping: Map<String, DogBreed> = values().associateBy(DogBreed::value) | ||
|
||
fun fromValue(value: String): DogBreed? = mapping[value] | ||
} | ||
} | ||
|
||
data class Error( | ||
@param:JsonProperty("code") | ||
@get:JsonProperty("code") | ||
@get:NotNull | ||
val code: Int, | ||
@param:JsonProperty("message") | ||
@get:JsonProperty("message") | ||
@get:NotNull | ||
val message: String | ||
) { | ||
companion object | ||
} | ||
|
||
@JsonTypeInfo( | ||
use = JsonTypeInfo.Id.NAME, | ||
include = JsonTypeInfo.As.EXISTING_PROPERTY, | ||
property = "petType", | ||
visible = true | ||
) | ||
@JsonSubTypes( | ||
JsonSubTypes.Type(value = Cat::class, name = "Cat"), | ||
JsonSubTypes.Type( | ||
value = | ||
Dog::class, | ||
name = "Dog" | ||
) | ||
) | ||
sealed class Pet( | ||
open val id: Long, | ||
open val name: String, | ||
open val tag: String? = null | ||
) { | ||
abstract val petType: String | ||
} |