Skip to content

Commit

Permalink
Added new integration test for #875
Browse files Browse the repository at this point in the history
  • Loading branch information
altro3 committed Dec 4, 2024
1 parent ec3f067 commit f8b815a
Show file tree
Hide file tree
Showing 12 changed files with 652 additions and 18 deletions.
1 change: 1 addition & 0 deletions doc-examples/example-kotlin-ksp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ dependencies {
runtimeOnly(mnLogging.logback.classic)

testImplementation(mnTest.micronaut.test.junit5)
testImplementation(mnTest.junit.jupiter.params)
}

java {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import io.micronaut.serde.annotation.SerdeImport
fun main(args: Array<String>) {
build()
.args(*args)
.packages("com.example")
.packages("example")
.start()
}

@SerdeImport(
value = Product::class,
mixin = ProductMixin::class) // <1>
class Serdes {}
class Serdes {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package example.openapi.test.api

import io.micronaut.http.annotation.Body
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Put
import example.openapi.test.model.Animal

@Controller
interface RequestBodyApi {

/**
* A method to send a model with discriminator in body
*
* @param animal (required)
* @return Animal
*/
@Put("/sendModelWithDiscriminator")
fun sendModelWithDiscriminator(
@Body animal: Animal,
): Animal
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package example.openapi.test.api

import io.micronaut.http.annotation.Controller
import example.openapi.test.model.Animal

@Controller
class RequestBodyApiImpl : RequestBodyApi {

override fun sendModelWithDiscriminator(animal: Animal): Animal =
animal
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package example.openapi.test.model

import com.fasterxml.jackson.annotation.*
import io.micronaut.core.annotation.Nullable
import io.micronaut.serde.annotation.Serdeable
import jakarta.annotation.Generated
import java.util.*

/**
* Animal
*/
@Serdeable
@JsonPropertyOrder(
Animal.JSON_PROPERTY_PROPERTY_CLASS,
Animal.JSON_PROPERTY_COLOR,
)
@Generated("io.micronaut.openapi.generator.KotlinMicronautServerCodegen")
@JsonIgnoreProperties(
value = ["class"], // ignore manually set class, it will be automatically generated by Jackson during serialization
allowSetters = true, // allows the class to be set during deserialization
)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "class", visible = true)
@JsonSubTypes(
JsonSubTypes.Type(value = Bird::class, name = "ave"),
JsonSubTypes.Type(value = Mammal::class, name = "mammalia"),
JsonSubTypes.Type(value = Reptile::class, name = "reptilia"),
)
open class Animal(

@field:Nullable
@field:JsonProperty(JSON_PROPERTY_PROPERTY_CLASS)
@field:JsonInclude(JsonInclude.Include.USE_DEFAULTS)
open var propertyClass: String? = null,

@field:Nullable
@field:JsonProperty(JSON_PROPERTY_COLOR)
@field:JsonInclude(JsonInclude.Include.USE_DEFAULTS)
open var color: ColorEnum? = null,
) {

override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
if (javaClass != other?.javaClass) {
return false
}
other as Animal
return propertyClass == other.propertyClass
&& color == other.color
}

override fun hashCode(): Int =
Objects.hash(propertyClass, color)

override fun toString(): String =
"Animal(propertyClass='$propertyClass', color='$color')"

companion object {

const val JSON_PROPERTY_PROPERTY_CLASS = "class"
const val JSON_PROPERTY_COLOR = "color"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package example.openapi.test.model

import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.annotation.JsonPropertyOrder
import io.micronaut.core.annotation.Nullable
import io.micronaut.serde.annotation.Serdeable
import jakarta.annotation.Generated
import java.math.BigDecimal
import java.util.*

/**
* Bird
*/
@Serdeable
@JsonPropertyOrder(
Bird.JSON_PROPERTY_NUM_WINGS,
Bird.JSON_PROPERTY_BEAK_LENGTH,
Bird.JSON_PROPERTY_FEATHER_DESCRIPTION,
)
@Generated("io.micronaut.openapi.generator.KotlinMicronautServerCodegen")
class Bird(

@field:Nullable
@field:JsonProperty(JSON_PROPERTY_NUM_WINGS)
@field:JsonInclude(JsonInclude.Include.USE_DEFAULTS)
var numWings: Int? = null,

@field:Nullable
@field:JsonProperty(JSON_PROPERTY_BEAK_LENGTH)
@field:JsonInclude(JsonInclude.Include.USE_DEFAULTS)
var beakLength: BigDecimal? = null,

@field:Nullable
@field:JsonProperty(JSON_PROPERTY_FEATHER_DESCRIPTION)
@field:JsonInclude(JsonInclude.Include.USE_DEFAULTS)
var featherDescription: String? = null,

@Nullable
@JsonProperty(JSON_PROPERTY_PROPERTY_CLASS)
@JsonInclude(JsonInclude.Include.USE_DEFAULTS)
propertyClass: String? = null,

@Nullable
@JsonProperty(JSON_PROPERTY_COLOR)
@JsonInclude(JsonInclude.Include.USE_DEFAULTS)
color: ColorEnum? = null,
) : Animal(propertyClass, color) {

override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
if (javaClass != other?.javaClass) {
return false
}
other as Bird
return numWings == other.numWings
&& beakLength == other.beakLength
&& featherDescription == other.featherDescription
&& super.equals(other)
}

override fun hashCode(): Int =
Objects.hash(numWings, beakLength, featherDescription, super.hashCode())

override fun toString(): String =
"Bird(numWings='$numWings', beakLength='$beakLength', featherDescription='$featherDescription', propertyClass='$propertyClass', color='$color')"

companion object {

const val JSON_PROPERTY_NUM_WINGS = "numWings"
const val JSON_PROPERTY_BEAK_LENGTH = "beakLength"
const val JSON_PROPERTY_FEATHER_DESCRIPTION = "featherDescription"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package example.openapi.test.model

import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.annotation.JsonValue
import io.micronaut.serde.annotation.Serdeable
import jakarta.annotation.Generated

/**
* Gets or Sets ColorEnum
*
* @param value The value represented by this enum
*/
@Serdeable
@Generated("io.micronaut.openapi.generator.KotlinMicronautServerCodegen")
enum class ColorEnum(
@get:JsonValue val value: String,
) {

@JsonProperty("red")
RED("red"),

@JsonProperty("blue")
BLUE("blue"),

@JsonProperty("green")
GREEN("green"),

@JsonProperty("light-blue")
LIGHT_BLUE("light-blue"),

@JsonProperty("dark-green")
DARK_GREEN("dark-green"),
;

override fun toString(): String = value

companion object {

@JvmField
val VALUE_MAPPING = entries.associateBy { it.value.lowercase() }

/**
* Create this enum from a value.
*
* @param value The value
*
* @return The enum
*/
@JsonCreator
@JvmStatic
fun fromValue(value: String): ColorEnum {
val key = value.lowercase()
require(VALUE_MAPPING.containsKey(key)) { "Unexpected value '$key'" }
return VALUE_MAPPING[key]!!
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package example.openapi.test.model

import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.annotation.JsonPropertyOrder
import io.micronaut.core.annotation.Nullable
import io.micronaut.serde.annotation.Serdeable
import jakarta.annotation.Generated

/**
* An object for describing errors
*/
@Serdeable
@JsonPropertyOrder(
Error.JSON_PROPERTY_MESSAGE,
)
@Generated("io.micronaut.openapi.generator.KotlinMicronautServerCodegen")
data class Error(

/**
* The error message
*/
@field:Nullable
@field:JsonProperty(JSON_PROPERTY_MESSAGE)
@field:JsonInclude(JsonInclude.Include.USE_DEFAULTS)
var message: String? = null,
) {

companion object {

const val JSON_PROPERTY_MESSAGE = "message"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package example.openapi.test.model

import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.annotation.JsonPropertyOrder
import io.micronaut.core.annotation.Nullable
import io.micronaut.serde.annotation.Serdeable
import jakarta.annotation.Generated
import java.util.*

/**
* Mammal
*/
@Serdeable
@JsonPropertyOrder(
Mammal.JSON_PROPERTY_WEIGHT,
Mammal.JSON_PROPERTY_DESCRIPTION,
)
@Generated("io.micronaut.openapi.generator.KotlinMicronautServerCodegen")
class Mammal(

@field:JsonProperty(JSON_PROPERTY_WEIGHT)
var weight: Float,

@field:JsonProperty(JSON_PROPERTY_DESCRIPTION)
var description: String,

@Nullable
@JsonProperty(JSON_PROPERTY_PROPERTY_CLASS)
@JsonInclude(JsonInclude.Include.USE_DEFAULTS)
propertyClass: String? = null,

@Nullable
@JsonProperty(JSON_PROPERTY_COLOR)
@JsonInclude(JsonInclude.Include.USE_DEFAULTS)
color: ColorEnum? = null,
) : Animal(propertyClass, color) {

override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
if (javaClass != other?.javaClass) {
return false
}
other as Mammal
return weight == other.weight
&& description == other.description
&& super.equals(other)
}

override fun hashCode(): Int =
Objects.hash(weight, description, super.hashCode())

override fun toString(): String =
"Mammal(weight='$weight', description='$description', propertyClass='$propertyClass', color='$color')"

companion object {

const val JSON_PROPERTY_WEIGHT = "weight"
const val JSON_PROPERTY_DESCRIPTION = "description"
}
}
Loading

0 comments on commit f8b815a

Please sign in to comment.