From 575d3cfd6cdb8607264e19b22abd17003959d90a Mon Sep 17 00:00:00 2001 From: Conor Gallagher Date: Tue, 13 Jun 2023 15:59:38 +0100 Subject: [PATCH] Move discriminated field to constructor. (#217) * Move discriminated field to constructor. This is necessary even when there is only one value to have it used as part of the equals function * fix tests * move override value to make it a little clearer * improve readability * simplify * remove debug statement --- .../fabrikt/generators/PropertyUtils.kt | 46 +++++++++++-------- .../generators/model/JacksonModelGenerator.kt | 7 +++ .../examples/companionObject/models/Models.kt | 10 ++-- .../models/Models.kt | 15 +++--- .../externalReferences/models/Models.kt | 10 ++-- .../javaSerializableModels/models/Models.kt | 15 +++--- .../models/Models.kt | 15 +++--- .../models/Models.kt | 15 +++--- .../nestedPolymorphicModels/models/Models.kt | 35 ++++++-------- .../examples/okHttpClient/models/Models.kt | 15 +++--- .../models/Models.kt | 15 +++--- .../oneOfPolymorphicModels/models/Models.kt | 30 +++++------- .../polymorphicModels/models/Models.kt | 25 ++++------ .../polymorphicModels/sealed/models/Models.kt | 25 ++++------ .../quarkusReflectionModels/models/Models.kt | 15 +++--- 15 files changed, 130 insertions(+), 163 deletions(-) diff --git a/src/main/kotlin/com/cjbooms/fabrikt/generators/PropertyUtils.kt b/src/main/kotlin/com/cjbooms/fabrikt/generators/PropertyUtils.kt index 6057f66d..fd6b038e 100644 --- a/src/main/kotlin/com/cjbooms/fabrikt/generators/PropertyUtils.kt +++ b/src/main/kotlin/com/cjbooms/fabrikt/generators/PropertyUtils.kt @@ -86,18 +86,6 @@ object PropertyUtils { ClassSettings.PolymorphyType.SUB -> { if (this is PropertyInfo.Field && isPolymorphicDiscriminator) { property.addModifiers(KModifier.OVERRIDE) - val discriminators = maybeDiscriminator.getDiscriminatorMappings(schemaName) - if (discriminators.size == 1) { - when (val discriminator = discriminators.first()) { - is PropertyInfo.DiscriminatorKey.EnumKey -> - property.initializer("%T.%L", wrappedType, discriminator.enumKey) - - is PropertyInfo.DiscriminatorKey.StringKey -> - property.initializer("%S", discriminator.stringValue) - } - } else { - property.addAnnotation(JacksonMetadata.jacksonParameterAnnotation(oasKey)) - } } else { if (isInherited) { property.addModifiers(KModifier.OVERRIDE) @@ -116,21 +104,33 @@ object PropertyUtils { } } - if (this !is PropertyInfo.Field || - !isPolymorphicDiscriminator || - isSubTypeDiscriminatorWithNoValue(classSettings) || - isSubTypeDiscriminatorWithMultipleValues(classSettings, schemaName) - ) { + if (isDiscriminatorFieldWithSingleKnownValue(classSettings, schemaName)) { + this as PropertyInfo.Field + if (classSettings.polymorphyType == ClassSettings.PolymorphyType.SUB) { + property.initializer(name) + val constructorParameter: ParameterSpec.Builder = ParameterSpec.builder(name, wrappedType) + val discriminators = maybeDiscriminator.getDiscriminatorMappings(schemaName) + when (val discriminator = discriminators.first()) { + is PropertyInfo.DiscriminatorKey.EnumKey -> + constructorParameter.defaultValue("%T.%L", wrappedType, discriminator.enumKey) + + is PropertyInfo.DiscriminatorKey.StringKey -> + constructorParameter.defaultValue("%S", discriminator.stringValue) + } + constructorBuilder.addParameter(constructorParameter.build()) + } + } else { property.initializer(name) val constructorParameter: ParameterSpec.Builder = ParameterSpec.builder(name, wrappedType) val oasDefault = getDefaultValue(this, parameterizedType) if (!isRequired) { if (oasDefault != null) { val wrappedDefault = - if (classSettings.isMergePatchPattern) + if (classSettings.isMergePatchPattern) { OasDefault.JsonNullableValue(oasDefault) - else + } else { oasDefault + } constructorParameter.defaultValue(wrappedDefault.getDefault()) } else { val undefinedDefault = if (classSettings.isMergePatchPattern) { @@ -148,6 +148,14 @@ object PropertyUtils { classBuilder.addProperty(property.build()) } + private fun PropertyInfo.isDiscriminatorFieldWithSingleKnownValue( + classSettings: ClassSettings, + schemaName: String, + ) = this is PropertyInfo.Field && + isPolymorphicDiscriminator && + !isSubTypeDiscriminatorWithNoValue(classSettings) && + !isSubTypeDiscriminatorWithMultipleValues(classSettings, schemaName) + private fun Map?.getDiscriminatorMappings( schemaName: String, ): List = diff --git a/src/main/kotlin/com/cjbooms/fabrikt/generators/model/JacksonModelGenerator.kt b/src/main/kotlin/com/cjbooms/fabrikt/generators/model/JacksonModelGenerator.kt index ba977c31..880a6795 100644 --- a/src/main/kotlin/com/cjbooms/fabrikt/generators/model/JacksonModelGenerator.kt +++ b/src/main/kotlin/com/cjbooms/fabrikt/generators/model/JacksonModelGenerator.kt @@ -726,9 +726,16 @@ class JacksonModelGenerator( KModifier.DATA, ) } + sortConstructorParameters(constructorBuilder, classType) return classBuilder.primaryConstructor(constructorBuilder.build()) } + private fun sortConstructorParameters(constructorBuilder: FunSpec.Builder, classType: ClassSettings) { + if (classType.polymorphyType != ClassSettings.PolymorphyType.NONE) { + constructorBuilder.parameters.sortBy { it.defaultValue?.toString() != "null" && it.defaultValue != null } + } + } + private fun Discriminator.getDiscriminatorMappings(schemaInfo: SchemaInfo): Map = mappingKeys(schemaInfo.schema) .filter { it.value == schemaInfo.schema.name || it.key == schemaInfo.schema.name } diff --git a/src/test/resources/examples/companionObject/models/Models.kt b/src/test/resources/examples/companionObject/models/Models.kt index c3f3de23..9bf6cb87 100644 --- a/src/test/resources/examples/companionObject/models/Models.kt +++ b/src/test/resources/examples/companionObject/models/Models.kt @@ -28,12 +28,11 @@ data class Cat( val hunts: Boolean? = null, @param:JsonProperty("age") @get:JsonProperty("age") - val age: Int? = null -) : Pet(id, name, tag) { + val age: Int? = null, @get:JsonProperty("petType") @get:NotNull override val petType: String = "Cat" - +) : Pet(id, name, tag) { companion object } @@ -54,12 +53,11 @@ data class Dog( val bark: Boolean? = null, @param:JsonProperty("breed") @get:JsonProperty("breed") - val breed: DogBreed? = null -) : Pet(id, name, tag) { + val breed: DogBreed? = null, @get:JsonProperty("petType") @get:NotNull override val petType: String = "Dog" - +) : Pet(id, name, tag) { companion object } diff --git a/src/test/resources/examples/enumPolymorphicDiscriminator/models/Models.kt b/src/test/resources/examples/enumPolymorphicDiscriminator/models/Models.kt index 5c53a4f1..4ef0d73e 100644 --- a/src/test/resources/examples/enumPolymorphicDiscriminator/models/Models.kt +++ b/src/test/resources/examples/enumPolymorphicDiscriminator/models/Models.kt @@ -12,31 +12,28 @@ import kotlin.collections.Map data class ConcreteImplOne( @param:JsonProperty("some_prop") @get:JsonProperty("some_prop") - val someProp: String? = null -) : PolymorphicEnumDiscriminator() { + val someProp: String? = null, @get:JsonProperty("some_enum") @get:NotNull override val someEnum: EnumDiscriminator = EnumDiscriminator.OBJ_ONE_ONLY -} +) : PolymorphicEnumDiscriminator() data class ConcreteImplOneRef( @param:JsonProperty("some_prop") @get:JsonProperty("some_prop") - val someProp: String? = null -) : PolymorphicEnumDiscriminator() { + val someProp: String? = null, @get:JsonProperty("some_enum") @get:NotNull override val someEnum: EnumDiscriminator = EnumDiscriminator.OBJ_ONE_ONLY -} +) : PolymorphicEnumDiscriminator() -class ConcreteImplThree() : PolymorphicEnumDiscriminator() { +data class ConcreteImplThree( @get:JsonProperty("some_enum") @get:NotNull override val someEnum: EnumDiscriminator = EnumDiscriminator.OBJ_THREE -} +) : PolymorphicEnumDiscriminator() data class ConcreteImplTwo( - @param:JsonProperty("some_enum") @get:JsonProperty("some_enum") @get:NotNull override val someEnum: EnumDiscriminator, diff --git a/src/test/resources/examples/externalReferences/models/Models.kt b/src/test/resources/examples/externalReferences/models/Models.kt index 2d7c44d1..5bc7fd11 100644 --- a/src/test/resources/examples/externalReferences/models/Models.kt +++ b/src/test/resources/examples/externalReferences/models/Models.kt @@ -87,22 +87,20 @@ data class ExternalObjectTwo( data class OneOfOne( @param:JsonProperty("oneOfOne") @get:JsonProperty("oneOfOne") - val oneOfOne: String? = null -) : ParentOneOf() { + val oneOfOne: String? = null, @get:JsonProperty("discriminator") @get:NotNull override val discriminator: String = "OneOfOne" -} +) : ParentOneOf() data class OneOfTwo( @param:JsonProperty("oneOfTwo") @get:JsonProperty("oneOfTwo") - val oneOfTwo: String? = null -) : ParentOneOf() { + val oneOfTwo: String? = null, @get:JsonProperty("discriminator") @get:NotNull override val discriminator: String = "OneOfTwo" -} +) : ParentOneOf() @JsonTypeInfo( use = JsonTypeInfo.Id.NAME, diff --git a/src/test/resources/examples/javaSerializableModels/models/Models.kt b/src/test/resources/examples/javaSerializableModels/models/Models.kt index ef91f9c6..2bd175db 100644 --- a/src/test/resources/examples/javaSerializableModels/models/Models.kt +++ b/src/test/resources/examples/javaSerializableModels/models/Models.kt @@ -96,12 +96,11 @@ data class FirstModel( override val etag: String? = null, @param:JsonProperty("extra_first_attr") @get:JsonProperty("extra_first_attr") - val extraFirstAttr: List? = null -) : Content(id, firstAttr, secondAttr, thirdAttr, etag), Serializable { + val extraFirstAttr: List? = null, @get:JsonProperty("model_type") @get:NotNull override val modelType: ContentModelType = ContentModelType.FIRST_MODEL -} +) : Content(id, firstAttr, secondAttr, thirdAttr, etag), Serializable data class QueryResult( @param:JsonProperty("items") @@ -133,12 +132,11 @@ data class SecondModel( val extraFirstAttr: String? = null, @param:JsonProperty("extra_second_attr") @get:JsonProperty("extra_second_attr") - val extraSecondAttr: Boolean? = null -) : Content(id, firstAttr, secondAttr, thirdAttr, etag), Serializable { + val extraSecondAttr: Boolean? = null, @get:JsonProperty("model_type") @get:NotNull override val modelType: ContentModelType = ContentModelType.SECOND_MODEL -} +) : Content(id, firstAttr, secondAttr, thirdAttr, etag), Serializable data class ThirdModel( @param:JsonProperty("id") @@ -161,9 +159,8 @@ data class ThirdModel( val extraFirstAttr: OffsetDateTime? = null, @param:JsonProperty("extra_second_attr") @get:JsonProperty("extra_second_attr") - val extraSecondAttr: Int? = null -) : Content(id, firstAttr, secondAttr, thirdAttr, etag), Serializable { + val extraSecondAttr: Int? = null, @get:JsonProperty("model_type") @get:NotNull override val modelType: ContentModelType = ContentModelType.THIRD_MODEL -} +) : Content(id, firstAttr, secondAttr, thirdAttr, etag), Serializable diff --git a/src/test/resources/examples/micronautIntrospectedModels/models/Models.kt b/src/test/resources/examples/micronautIntrospectedModels/models/Models.kt index ebd663b8..b2aac872 100644 --- a/src/test/resources/examples/micronautIntrospectedModels/models/Models.kt +++ b/src/test/resources/examples/micronautIntrospectedModels/models/Models.kt @@ -100,12 +100,11 @@ data class FirstModel( override val etag: String? = null, @param:JsonProperty("extra_first_attr") @get:JsonProperty("extra_first_attr") - val extraFirstAttr: List? = null -) : Content(id, firstAttr, secondAttr, thirdAttr, etag) { + val extraFirstAttr: List? = null, @get:JsonProperty("model_type") @get:NotNull override val modelType: ContentModelType = ContentModelType.FIRST_MODEL -} +) : Content(id, firstAttr, secondAttr, thirdAttr, etag) @Introspected data class QueryResult( @@ -139,12 +138,11 @@ data class SecondModel( val extraFirstAttr: String? = null, @param:JsonProperty("extra_second_attr") @get:JsonProperty("extra_second_attr") - val extraSecondAttr: Boolean? = null -) : Content(id, firstAttr, secondAttr, thirdAttr, etag) { + val extraSecondAttr: Boolean? = null, @get:JsonProperty("model_type") @get:NotNull override val modelType: ContentModelType = ContentModelType.SECOND_MODEL -} +) : Content(id, firstAttr, secondAttr, thirdAttr, etag) @Introspected data class ThirdModel( @@ -168,9 +166,8 @@ data class ThirdModel( val extraFirstAttr: OffsetDateTime? = null, @param:JsonProperty("extra_second_attr") @get:JsonProperty("extra_second_attr") - val extraSecondAttr: Int? = null -) : Content(id, firstAttr, secondAttr, thirdAttr, etag) { + val extraSecondAttr: Int? = null, @get:JsonProperty("model_type") @get:NotNull override val modelType: ContentModelType = ContentModelType.THIRD_MODEL -} +) : Content(id, firstAttr, secondAttr, thirdAttr, etag) diff --git a/src/test/resources/examples/micronautReflectionModels/models/Models.kt b/src/test/resources/examples/micronautReflectionModels/models/Models.kt index eca9ede5..4036e2a4 100644 --- a/src/test/resources/examples/micronautReflectionModels/models/Models.kt +++ b/src/test/resources/examples/micronautReflectionModels/models/Models.kt @@ -100,12 +100,11 @@ data class FirstModel( override val etag: String? = null, @param:JsonProperty("extra_first_attr") @get:JsonProperty("extra_first_attr") - val extraFirstAttr: List? = null -) : Content(id, firstAttr, secondAttr, thirdAttr, etag) { + val extraFirstAttr: List? = null, @get:JsonProperty("model_type") @get:NotNull override val modelType: ContentModelType = ContentModelType.FIRST_MODEL -} +) : Content(id, firstAttr, secondAttr, thirdAttr, etag) @ReflectiveAccess data class QueryResult( @@ -139,12 +138,11 @@ data class SecondModel( val extraFirstAttr: String? = null, @param:JsonProperty("extra_second_attr") @get:JsonProperty("extra_second_attr") - val extraSecondAttr: Boolean? = null -) : Content(id, firstAttr, secondAttr, thirdAttr, etag) { + val extraSecondAttr: Boolean? = null, @get:JsonProperty("model_type") @get:NotNull override val modelType: ContentModelType = ContentModelType.SECOND_MODEL -} +) : Content(id, firstAttr, secondAttr, thirdAttr, etag) @ReflectiveAccess data class ThirdModel( @@ -168,9 +166,8 @@ data class ThirdModel( val extraFirstAttr: OffsetDateTime? = null, @param:JsonProperty("extra_second_attr") @get:JsonProperty("extra_second_attr") - val extraSecondAttr: Int? = null -) : Content(id, firstAttr, secondAttr, thirdAttr, etag) { + val extraSecondAttr: Int? = null, @get:JsonProperty("model_type") @get:NotNull override val modelType: ContentModelType = ContentModelType.THIRD_MODEL -} +) : Content(id, firstAttr, secondAttr, thirdAttr, etag) diff --git a/src/test/resources/examples/nestedPolymorphicModels/models/Models.kt b/src/test/resources/examples/nestedPolymorphicModels/models/Models.kt index 4329349d..9ac74ac2 100644 --- a/src/test/resources/examples/nestedPolymorphicModels/models/Models.kt +++ b/src/test/resources/examples/nestedPolymorphicModels/models/Models.kt @@ -49,12 +49,11 @@ sealed class FirstLevelChild( @get:JsonProperty("rootField2") override val rootField2: Boolean? = null, open val firstLevelField1: String, - open val firstLevelField2: Int? = null -) : RootType(rootField1, rootField2) { + open val firstLevelField2: Int? = null, @get:JsonProperty("rootDiscriminator") @get:NotNull override val rootDiscriminator: RootDiscriminator = RootDiscriminator.FIRST_LEVEL_CHILD - +) : RootType(rootField1, rootField2) { abstract val firstLevelDiscriminator: FirstLevelDiscriminator } @@ -135,13 +134,12 @@ sealed class SecondLevelChild1( @param:JsonProperty("firstLevelField2") @get:JsonProperty("firstLevelField2") override val firstLevelField2: Int? = null, - open val metadata: SecondLevelMetadata -) : FirstLevelChild(rootField1, rootField2, firstLevelField1, firstLevelField2) { + open val metadata: SecondLevelMetadata, @get:JsonProperty("firstLevelDiscriminator") @get:NotNull override val firstLevelDiscriminator: FirstLevelDiscriminator = FirstLevelDiscriminator.SECOND_LEVEL_CHILD1 - +) : FirstLevelChild(rootField1, rootField2, firstLevelField1, firstLevelField2) { abstract val secondLevelDiscriminator: SecondLevelDiscriminator } @@ -178,13 +176,12 @@ sealed class SecondLevelChild2( @param:JsonProperty("firstLevelField2") @get:JsonProperty("firstLevelField2") override val firstLevelField2: Int? = null, - open val metadata: SecondLevelMetadata -) : FirstLevelChild(rootField1, rootField2, firstLevelField1, firstLevelField2) { + open val metadata: SecondLevelMetadata, @get:JsonProperty("firstLevelDiscriminator") @get:NotNull override val firstLevelDiscriminator: FirstLevelDiscriminator = FirstLevelDiscriminator.SECOND_LEVEL_CHILD2 - +) : FirstLevelChild(rootField1, rootField2, firstLevelField1, firstLevelField2) { abstract val secondLevelDiscriminator: SecondLevelDiscriminator } @@ -235,13 +232,12 @@ data class ThirdLevelChild11( @param:JsonProperty("creationDate") @get:JsonProperty("creationDate") @get:NotNull - val creationDate: Int -) : SecondLevelChild1(rootField1, rootField2, firstLevelField1, firstLevelField2, metadata) { + val creationDate: Int, @get:JsonProperty("secondLevelDiscriminator") @get:NotNull override val secondLevelDiscriminator: SecondLevelDiscriminator = SecondLevelDiscriminator.THIRD_LEVEL_CHILD1 -} +) : SecondLevelChild1(rootField1, rootField2, firstLevelField1, firstLevelField2, metadata) data class ThirdLevelChild12( @param:JsonProperty("rootField1") @@ -266,13 +262,12 @@ data class ThirdLevelChild12( @param:JsonProperty("isDeleted") @get:JsonProperty("isDeleted") @get:NotNull - val isDeleted: Boolean -) : SecondLevelChild1(rootField1, rootField2, firstLevelField1, firstLevelField2, metadata) { + val isDeleted: Boolean, @get:JsonProperty("secondLevelDiscriminator") @get:NotNull override val secondLevelDiscriminator: SecondLevelDiscriminator = SecondLevelDiscriminator.THIRD_LEVEL_CHILD2 -} +) : SecondLevelChild1(rootField1, rootField2, firstLevelField1, firstLevelField2, metadata) data class ThirdLevelChild21( @param:JsonProperty("rootField1") @@ -297,13 +292,12 @@ data class ThirdLevelChild21( @param:JsonProperty("creationDate") @get:JsonProperty("creationDate") @get:NotNull - val creationDate: Int -) : SecondLevelChild2(rootField1, rootField2, firstLevelField1, firstLevelField2, metadata) { + val creationDate: Int, @get:JsonProperty("secondLevelDiscriminator") @get:NotNull override val secondLevelDiscriminator: SecondLevelDiscriminator = SecondLevelDiscriminator.THIRD_LEVEL_CHILD1 -} +) : SecondLevelChild2(rootField1, rootField2, firstLevelField1, firstLevelField2, metadata) data class ThirdLevelChild22( @param:JsonProperty("rootField1") @@ -328,10 +322,9 @@ data class ThirdLevelChild22( @param:JsonProperty("isDeleted") @get:JsonProperty("isDeleted") @get:NotNull - val isDeleted: Boolean -) : SecondLevelChild2(rootField1, rootField2, firstLevelField1, firstLevelField2, metadata) { + val isDeleted: Boolean, @get:JsonProperty("secondLevelDiscriminator") @get:NotNull override val secondLevelDiscriminator: SecondLevelDiscriminator = SecondLevelDiscriminator.THIRD_LEVEL_CHILD2 -} +) : SecondLevelChild2(rootField1, rootField2, firstLevelField1, firstLevelField2, metadata) diff --git a/src/test/resources/examples/okHttpClient/models/Models.kt b/src/test/resources/examples/okHttpClient/models/Models.kt index 1a217d7e..3b4843cc 100644 --- a/src/test/resources/examples/okHttpClient/models/Models.kt +++ b/src/test/resources/examples/okHttpClient/models/Models.kt @@ -95,12 +95,11 @@ data class FirstModel( override val etag: String? = null, @param:JsonProperty("extra_first_attr") @get:JsonProperty("extra_first_attr") - val extraFirstAttr: List? = null -) : Content(id, firstAttr, secondAttr, thirdAttr, etag) { + val extraFirstAttr: List? = null, @get:JsonProperty("model_type") @get:NotNull override val modelType: ContentModelType = ContentModelType.FIRST_MODEL -} +) : Content(id, firstAttr, secondAttr, thirdAttr, etag) data class QueryResult( @param:JsonProperty("items") @@ -132,12 +131,11 @@ data class SecondModel( val extraFirstAttr: String? = null, @param:JsonProperty("extra_second_attr") @get:JsonProperty("extra_second_attr") - val extraSecondAttr: Boolean? = null -) : Content(id, firstAttr, secondAttr, thirdAttr, etag) { + val extraSecondAttr: Boolean? = null, @get:JsonProperty("model_type") @get:NotNull override val modelType: ContentModelType = ContentModelType.SECOND_MODEL -} +) : Content(id, firstAttr, secondAttr, thirdAttr, etag) data class ThirdModel( @param:JsonProperty("id") @@ -160,9 +158,8 @@ data class ThirdModel( val extraFirstAttr: OffsetDateTime? = null, @param:JsonProperty("extra_second_attr") @get:JsonProperty("extra_second_attr") - val extraSecondAttr: Int? = null -) : Content(id, firstAttr, secondAttr, thirdAttr, etag) { + val extraSecondAttr: Int? = null, @get:JsonProperty("model_type") @get:NotNull override val modelType: ContentModelType = ContentModelType.THIRD_MODEL -} +) : Content(id, firstAttr, secondAttr, thirdAttr, etag) diff --git a/src/test/resources/examples/okHttpClientMultiMediaType/models/Models.kt b/src/test/resources/examples/okHttpClientMultiMediaType/models/Models.kt index 3b3709b5..668dc7f2 100644 --- a/src/test/resources/examples/okHttpClientMultiMediaType/models/Models.kt +++ b/src/test/resources/examples/okHttpClientMultiMediaType/models/Models.kt @@ -119,12 +119,11 @@ data class FirstModel( override val etag: String? = null, @param:JsonProperty("extra_first_attr") @get:JsonProperty("extra_first_attr") - val extraFirstAttr: List? = null -) : Content(id, firstAttr, secondAttr, thirdAttr, etag) { + val extraFirstAttr: List? = null, @get:JsonProperty("model_type") @get:NotNull override val modelType: ContentModelType = ContentModelType.FIRST_MODEL -} +) : Content(id, firstAttr, secondAttr, thirdAttr, etag) data class OtherQueryResult( @param:JsonProperty("items") @@ -165,12 +164,11 @@ data class SecondModel( val extraFirstAttr: String? = null, @param:JsonProperty("extra_second_attr") @get:JsonProperty("extra_second_attr") - val extraSecondAttr: Boolean? = null -) : Content(id, firstAttr, secondAttr, thirdAttr, etag) { + val extraSecondAttr: Boolean? = null, @get:JsonProperty("model_type") @get:NotNull override val modelType: ContentModelType = ContentModelType.SECOND_MODEL -} +) : Content(id, firstAttr, secondAttr, thirdAttr, etag) data class ThirdModel( @param:JsonProperty("id") @@ -193,9 +191,8 @@ data class ThirdModel( val extraFirstAttr: OffsetDateTime? = null, @param:JsonProperty("extra_second_attr") @get:JsonProperty("extra_second_attr") - val extraSecondAttr: Int? = null -) : Content(id, firstAttr, secondAttr, thirdAttr, etag) { + val extraSecondAttr: Int? = null, @get:JsonProperty("model_type") @get:NotNull override val modelType: ContentModelType = ContentModelType.THIRD_MODEL -} +) : Content(id, firstAttr, secondAttr, thirdAttr, etag) diff --git a/src/test/resources/examples/oneOfPolymorphicModels/models/Models.kt b/src/test/resources/examples/oneOfPolymorphicModels/models/Models.kt index bd5a8220..7911a4e8 100644 --- a/src/test/resources/examples/oneOfPolymorphicModels/models/Models.kt +++ b/src/test/resources/examples/oneOfPolymorphicModels/models/Models.kt @@ -15,23 +15,21 @@ data class ChildTypeA( @param:JsonProperty("some_string") @get:JsonProperty("some_string") @get:NotNull - val someString: String -) : ParentSpec() { + val someString: String, @get:JsonProperty("type") @get:NotNull override val type: ParentType = ParentType.CHILD_TYPE_A -} +) : ParentSpec() data class ChildTypeB( @param:JsonProperty("some_int") @get:JsonProperty("some_int") @get:NotNull - val someInt: Int -) : ParentSpec() { + val someInt: Int, @get:JsonProperty("type") @get:NotNull override val type: ParentType = ParentType.CHILD_TYPE_B -} +) : ParentSpec() data class ContainsOneOfPolymorphicTypes( @param:JsonProperty("one_one_of") @@ -124,39 +122,35 @@ sealed class PolymorphicSuperTypeTwo() { data class PolymorphicTypeOneA( @param:JsonProperty("whateverA") @get:JsonProperty("whateverA") - val whateverA: String? = null -) : PolymorphicSuperTypeOne() { + val whateverA: String? = null, @get:JsonProperty("shared") @get:NotNull override val shared: String = "PolymorphicTypeOneA" -} +) : PolymorphicSuperTypeOne() data class PolymorphicTypeOneB( @param:JsonProperty("whateverB") @get:JsonProperty("whateverB") - val whateverB: Int? = null -) : PolymorphicSuperTypeOne() { + val whateverB: Int? = null, @get:JsonProperty("shared") @get:NotNull override val shared: String = "PolymorphicTypeOneB" -} +) : PolymorphicSuperTypeOne() data class PolymorphicTypeTwoA( @param:JsonProperty("whateverC") @get:JsonProperty("whateverC") - val whateverC: String? = null -) : PolymorphicSuperTypeTwo() { + val whateverC: String? = null, @get:JsonProperty("shared") @get:NotNull override val shared: String = "PolymorphicTypeTwoA" -} +) : PolymorphicSuperTypeTwo() data class PolymorphicTypeTwoB( @param:JsonProperty("whateverD") @get:JsonProperty("whateverD") - val whateverD: Int? = null -) : PolymorphicSuperTypeTwo() { + val whateverD: Int? = null, @get:JsonProperty("shared") @get:NotNull override val shared: String = "PolymorphicTypeTwoB" -} +) : PolymorphicSuperTypeTwo() diff --git a/src/test/resources/examples/polymorphicModels/models/Models.kt b/src/test/resources/examples/polymorphicModels/models/Models.kt index 64af0394..f7a03b28 100644 --- a/src/test/resources/examples/polymorphicModels/models/Models.kt +++ b/src/test/resources/examples/polymorphicModels/models/Models.kt @@ -49,12 +49,11 @@ data class PolymorphicTypeOne( override val lastName: String, @param:JsonProperty("child_one_name") @get:JsonProperty("child_one_name") - val childOneName: String? = null -) : PolymorphicSuperType(firstName, lastName) { + val childOneName: String? = null, @get:JsonProperty("generation") @get:NotNull override val generation: String = "PolymorphicTypeOne" -} +) : PolymorphicSuperType(firstName, lastName) data class PolymorphicTypeOneAnotherRef( @param:JsonProperty("first_name") @@ -67,12 +66,11 @@ data class PolymorphicTypeOneAnotherRef( override val lastName: String, @param:JsonProperty("child_one_name") @get:JsonProperty("child_one_name") - val childOneName: String? = null -) : PolymorphicSuperType(firstName, lastName) { + val childOneName: String? = null, @get:JsonProperty("generation") @get:NotNull override val generation: String = "PolymorphicTypeOne" -} +) : PolymorphicSuperType(firstName, lastName) data class PolymorphicTypeOneRef( @param:JsonProperty("first_name") @@ -85,12 +83,11 @@ data class PolymorphicTypeOneRef( override val lastName: String, @param:JsonProperty("child_one_name") @get:JsonProperty("child_one_name") - val childOneName: String? = null -) : PolymorphicSuperType(firstName, lastName) { + val childOneName: String? = null, @get:JsonProperty("generation") @get:NotNull override val generation: String = "PolymorphicTypeOne" -} +) : PolymorphicSuperType(firstName, lastName) data class PolymorphicTypeTwo( @param:JsonProperty("first_name") @@ -106,12 +103,11 @@ data class PolymorphicTypeTwo( val someIntegerPropery: Int? = null, @param:JsonProperty("child_two_age") @get:JsonProperty("child_two_age") - val childTwoAge: Int? = null -) : PolymorphicSuperType(firstName, lastName) { + val childTwoAge: Int? = null, @get:JsonProperty("generation") @get:NotNull override val generation: String = "polymorphic_type_two" -} +) : PolymorphicSuperType(firstName, lastName) data class PolymorphicTypeTwoRef( @param:JsonProperty("first_name") @@ -127,9 +123,8 @@ data class PolymorphicTypeTwoRef( val someIntegerPropery: Int? = null, @param:JsonProperty("child_two_age") @get:JsonProperty("child_two_age") - val childTwoAge: Int? = null -) : PolymorphicSuperType(firstName, lastName) { + val childTwoAge: Int? = null, @get:JsonProperty("generation") @get:NotNull override val generation: String = "polymorphic_type_two" -} +) : PolymorphicSuperType(firstName, lastName) diff --git a/src/test/resources/examples/polymorphicModels/sealed/models/Models.kt b/src/test/resources/examples/polymorphicModels/sealed/models/Models.kt index b2236d98..3fd04997 100644 --- a/src/test/resources/examples/polymorphicModels/sealed/models/Models.kt +++ b/src/test/resources/examples/polymorphicModels/sealed/models/Models.kt @@ -43,12 +43,11 @@ data class PolymorphicTypeOneRef( override val lastName: String, @param:JsonProperty("child_one_name") @get:JsonProperty("child_one_name") - val childOneName: String? = null -) : PolymorphicSuperType(firstName, lastName) { + val childOneName: String? = null, @get:JsonProperty("generation") @get:NotNull override val generation: String = "PolymorphicTypeOne" -} +) : PolymorphicSuperType(firstName, lastName) data class PolymorphicTypeOneAnotherRef( @param:JsonProperty("first_name") @@ -61,12 +60,11 @@ data class PolymorphicTypeOneAnotherRef( override val lastName: String, @param:JsonProperty("child_one_name") @get:JsonProperty("child_one_name") - val childOneName: String? = null -) : PolymorphicSuperType(firstName, lastName) { + val childOneName: String? = null, @get:JsonProperty("generation") @get:NotNull override val generation: String = "PolymorphicTypeOne" -} +) : PolymorphicSuperType(firstName, lastName) data class PolymorphicTypeTwoRef( @param:JsonProperty("first_name") @@ -82,12 +80,11 @@ data class PolymorphicTypeTwoRef( val someIntegerPropery: Int? = null, @param:JsonProperty("child_two_age") @get:JsonProperty("child_two_age") - val childTwoAge: Int? = null -) : PolymorphicSuperType(firstName, lastName) { + val childTwoAge: Int? = null, @get:JsonProperty("generation") @get:NotNull override val generation: String = "polymorphic_type_two" -} +) : PolymorphicSuperType(firstName, lastName) data class PolymorphicTypeOne( @param:JsonProperty("first_name") @@ -100,12 +97,11 @@ data class PolymorphicTypeOne( override val lastName: String, @param:JsonProperty("child_one_name") @get:JsonProperty("child_one_name") - val childOneName: String? = null -) : PolymorphicSuperType(firstName, lastName) { + val childOneName: String? = null, @get:JsonProperty("generation") @get:NotNull override val generation: String = "PolymorphicTypeOne" -} +) : PolymorphicSuperType(firstName, lastName) data class PolymorphicTypeTwo( @param:JsonProperty("first_name") @@ -121,9 +117,8 @@ data class PolymorphicTypeTwo( val someIntegerPropery: Int? = null, @param:JsonProperty("child_two_age") @get:JsonProperty("child_two_age") - val childTwoAge: Int? = null -) : PolymorphicSuperType(firstName, lastName) { + val childTwoAge: Int? = null, @get:JsonProperty("generation") @get:NotNull override val generation: String = "polymorphic_type_two" -} +) : PolymorphicSuperType(firstName, lastName) diff --git a/src/test/resources/examples/quarkusReflectionModels/models/Models.kt b/src/test/resources/examples/quarkusReflectionModels/models/Models.kt index 6fc5bfb0..6853cbf2 100644 --- a/src/test/resources/examples/quarkusReflectionModels/models/Models.kt +++ b/src/test/resources/examples/quarkusReflectionModels/models/Models.kt @@ -100,12 +100,11 @@ data class FirstModel( override val etag: String? = null, @param:JsonProperty("extra_first_attr") @get:JsonProperty("extra_first_attr") - val extraFirstAttr: List? = null -) : Content(id, firstAttr, secondAttr, thirdAttr, etag) { + val extraFirstAttr: List? = null, @get:JsonProperty("model_type") @get:NotNull override val modelType: ContentModelType = ContentModelType.FIRST_MODEL -} +) : Content(id, firstAttr, secondAttr, thirdAttr, etag) @RegisterForReflection data class QueryResult( @@ -139,12 +138,11 @@ data class SecondModel( val extraFirstAttr: String? = null, @param:JsonProperty("extra_second_attr") @get:JsonProperty("extra_second_attr") - val extraSecondAttr: Boolean? = null -) : Content(id, firstAttr, secondAttr, thirdAttr, etag) { + val extraSecondAttr: Boolean? = null, @get:JsonProperty("model_type") @get:NotNull override val modelType: ContentModelType = ContentModelType.SECOND_MODEL -} +) : Content(id, firstAttr, secondAttr, thirdAttr, etag) @RegisterForReflection data class ThirdModel( @@ -168,9 +166,8 @@ data class ThirdModel( val extraFirstAttr: OffsetDateTime? = null, @param:JsonProperty("extra_second_attr") @get:JsonProperty("extra_second_attr") - val extraSecondAttr: Int? = null -) : Content(id, firstAttr, secondAttr, thirdAttr, etag) { + val extraSecondAttr: Int? = null, @get:JsonProperty("model_type") @get:NotNull override val modelType: ContentModelType = ContentModelType.THIRD_MODEL -} +) : Content(id, firstAttr, secondAttr, thirdAttr, etag)