Skip to content

Commit

Permalink
Fixing bug with constructor when multiple objects under an allOf, but…
Browse files Browse the repository at this point in the history
… only one is the super class (#40)
  • Loading branch information
cjbooms authored May 1, 2021
1 parent c6eac15 commit ff42271
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/main/kotlin/com/cjbooms/fabrikt/model/PropertyInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.cjbooms.fabrikt.model

import com.cjbooms.fabrikt.util.KaizenParserExtensions.getKeyIfDiscriminator
import com.cjbooms.fabrikt.util.KaizenParserExtensions.hasAdditionalProperties
import com.cjbooms.fabrikt.util.KaizenParserExtensions.hasNoDiscriminator
import com.cjbooms.fabrikt.util.KaizenParserExtensions.isDiscriminatorProperty
import com.cjbooms.fabrikt.util.KaizenParserExtensions.isInLinedObjectUnderAllOf
import com.cjbooms.fabrikt.util.KaizenParserExtensions.isInlineableMapDefinition
Expand Down Expand Up @@ -52,7 +53,7 @@ sealed class PropertyInfo {
}

private fun maybeMarkInherited(settings: Settings, it: Schema) =
settings.copy(markAsInherited = if (it.isInLinedObjectUnderAllOf()) settings.markAsInherited else true)
settings.copy(markAsInherited = if (it.isInLinedObjectUnderAllOf() || it.hasNoDiscriminator()) settings.markAsInherited else true)

private fun Schema.getInLinedProperties(
settings: Settings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ object KaizenParserExtensions {
path[path.lastIndex].toIntOrNull() != null && (path[path.lastIndex - 1] == "allOf")
}

fun Schema.hasNoDiscriminator(): Boolean = this.discriminator.propertyName == null

fun Schema.safeName(): String =
when {
isOneOfPolymorphicTypes() -> this.oneOfSchemas.first().allOfSchemas.first().safeName()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ class ModelGeneratorTest {

@Test
fun `sealed classes are correctly grouped into a single file`() {
val basePackage = "examples.polymorphicModels"
val basePackage = "examples.polymorphicModels.sealed"
val spec = javaClass.getResource("/examples/polymorphicModels/api.yaml").readText()
val expectedModels = javaClass.getResource("/examples/polymorphicModels/models/Models.kt").readText()
val expectedModels = javaClass.getResource("/examples/polymorphicModels/sealed/models/Models.kt").readText()

val models = JacksonModelGenerator(
Packages(basePackage),
Expand Down
10 changes: 9 additions & 1 deletion src/test/resources/examples/polymorphicModels/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,16 @@ components:
PolymorphicTypeTwo:
allOf:
- $ref: "#/components/schemas/PolymorphicSuperType"
- $ref: "#/components/schemas/AnotherObject"
- type: object
properties:
child_two_age:
type: integer
format: int32
format: int32

AnotherObject:
type: object
properties:
some_integer_propery:
type: integer
format: int32
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ data class PolymorphicTypeTwo(
@get:JsonProperty("last_name")
@get:NotNull
override val lastName: String,
@param:JsonProperty("some_integer_propery")
@get:JsonProperty("some_integer_propery")
val someIntegerPropery: Int? = null,
@param:JsonProperty("child_two_age")
@get:JsonProperty("child_two_age")
val childTwoAge: Int? = null
Expand All @@ -67,3 +70,9 @@ data class PolymorphicTypeTwo(
@get:NotNull
override val generation: String = "PolymorphicTypeTwo"
}

data class AnotherObject(
@param:JsonProperty("some_integer_propery")
@get:JsonProperty("some_integer_propery")
val someIntegerPropery: Int? = null
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package examples.polymorphicModels.sealed.models

import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.annotation.JsonSubTypes
import com.fasterxml.jackson.annotation.JsonTypeInfo
import javax.validation.constraints.NotNull
import kotlin.Int
import kotlin.String

@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.EXISTING_PROPERTY,
property = "generation",
visible = true
)
@JsonSubTypes(
JsonSubTypes.Type(
value = PolymorphicTypeOne::class,
name =
"PolymorphicTypeOne"
),
JsonSubTypes.Type(
value = PolymorphicTypeTwo::class,
name =
"PolymorphicTypeTwo"
)
)
sealed class PolymorphicSuperType(
open val firstName: String,
open val lastName: String
) {
abstract val generation: String
}

data class PolymorphicTypeOne(
@param:JsonProperty("first_name")
@get:JsonProperty("first_name")
@get:NotNull
override val firstName: String,
@param:JsonProperty("last_name")
@get:JsonProperty("last_name")
@get:NotNull
override val lastName: String,
@param:JsonProperty("child_one_name")
@get:JsonProperty("child_one_name")
val childOneName: String? = null
) : PolymorphicSuperType(firstName, lastName) {
@get:JsonProperty("generation")
@get:NotNull
override val generation: String = "PolymorphicTypeOne"
}

data class PolymorphicTypeTwo(
@param:JsonProperty("first_name")
@get:JsonProperty("first_name")
@get:NotNull
override val firstName: String,
@param:JsonProperty("last_name")
@get:JsonProperty("last_name")
@get:NotNull
override val lastName: String,
@param:JsonProperty("some_integer_propery")
@get:JsonProperty("some_integer_propery")
val someIntegerPropery: Int? = null,
@param:JsonProperty("child_two_age")
@get:JsonProperty("child_two_age")
val childTwoAge: Int? = null
) : PolymorphicSuperType(firstName, lastName) {
@get:JsonProperty("generation")
@get:NotNull
override val generation: String = "PolymorphicTypeTwo"
}

0 comments on commit ff42271

Please sign in to comment.