Skip to content

Commit

Permalink
Merge pull request #175 from cjbooms/ISSUE-174
Browse files Browse the repository at this point in the history
Add companion object on polymorphic subtypes
  • Loading branch information
averabaq authored Mar 15, 2023
2 parents f9a9a15 + faf30cb commit 5d932ad
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ class JacksonModelGenerator(
.addQuarkusReflectionAnnotation()
.addMicronautIntrospectedAnnotation()
.addMicronautReflectionAnnotation()
.addCompanionObject()
.superclass(
toModelType(packages.base, KotlinTypeInfo.from(superType.schema, superType.name))
)
Expand Down
24 changes: 24 additions & 0 deletions src/test/resources/examples/companionObject/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,41 @@ components:
schemas:
Pet:
type: object
discriminator:
propertyName: petType
required:
- id
- name
- petType
properties:
id:
type: integer
format: int64
name:
type: string
petType:
type: string
tag:
type: string
Cat:
allOf:
- $ref: '#/components/schemas/Pet'
- type: object
properties:
hunts:
type: boolean
age:
type: integer
Dog:
allOf:
- $ref: '#/components/schemas/Pet'
- type: object
properties:
bark:
type: boolean
breed:
type: string
enum: [Dingo, Husky, Retriever, Shepherd]
Pets:
type: array
maxItems: 100
Expand Down
106 changes: 94 additions & 12 deletions src/test/resources/examples/companionObject/models/Models.kt
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
}

0 comments on commit 5d932ad

Please sign in to comment.