Repository: cjbooms/fabrikt
Pull request #40 (2021-05-01 18:37:58)

Fixing bug with constructor when multiple objects under an allOf
Fixing bug with constructor when multiple objects under an allOf, but only one is the super class

cjbooms: 👍 

Patch:
diff --git a/README.md b/README.md
index 80e0d18..7f530a6 100644
--- a/README.md
+++ b/README.md
@@ -14,8 +14,7 @@ Generates Kotlin code from OpenApi3 specifications, ensuring code matches API co
 The library currently has support for generating:
 
 * **Jackson annotated data classes**
-* **Spring MVC annotated controllers**
-* **Service interfaces**
+* **Spring MVC annotated controller interfaces**
 * **OkHttp Client** - with the option for a resilience4j fault-tolerance wrapper
 
 ## Examples
@@ -25,7 +24,7 @@ The test directory forms a living documentation full of [code examples](src/test
 
 ## Configuration
 
-This section documents the available CLI parameters for controlling what gets generated.
+This section documents the available CLI parameters for controlling what gets generated. This documentation is generated using: `./gradlew printCodeGenUsage`
 
  | Parameter              | Description
  | ---------------------- | ----------------------
@@ -48,6 +47,7 @@ This section documents the available CLI parameters for controlling what gets ge
  |                        |   `CLIENT` - Simple http rest client.
  |                        |   `QUARKUS_REFLECTION_CONFIG` - This options generates the reflection-config.json file for quarkus integration projects
 
+
 ## Examples
 
 ### Gradle Task
diff --git a/src/main/kotlin/com/cjbooms/fabrikt/model/PropertyInfo.kt b/src/main/kotlin/com/cjbooms/fabrikt/model/PropertyInfo.kt
index 943799b..a13ebea 100644
--- a/src/main/kotlin/com/cjbooms/fabrikt/model/PropertyInfo.kt
+++ b/src/main/kotlin/com/cjbooms/fabrikt/model/PropertyInfo.kt
@@ -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
@@ -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,
diff --git a/src/main/kotlin/com/cjbooms/fabrikt/util/KaizenParserExtensions.kt b/src/main/kotlin/com/cjbooms/fabrikt/util/KaizenParserExtensions.kt
index 6ba1a7e..2b8a148 100644
--- a/src/main/kotlin/com/cjbooms/fabrikt/util/KaizenParserExtensions.kt
+++ b/src/main/kotlin/com/cjbooms/fabrikt/util/KaizenParserExtensions.kt
@@ -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()


Test patch:
diff --git a/src/test/kotlin/com/cjbooms/fabrikt/generators/ModelGeneratorTest.kt b/src/test/kotlin/com/cjbooms/fabrikt/generators/ModelGeneratorTest.kt
index d5d0602..9d220e0 100644
--- a/src/test/kotlin/com/cjbooms/fabrikt/generators/ModelGeneratorTest.kt
+++ b/src/test/kotlin/com/cjbooms/fabrikt/generators/ModelGeneratorTest.kt
@@ -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),
diff --git a/src/test/resources/examples/polymorphicModels/api.yaml b/src/test/resources/examples/polymorphicModels/api.yaml
index 8eaa3f8..b1fa056 100644
--- a/src/test/resources/examples/polymorphicModels/api.yaml
+++ b/src/test/resources/examples/polymorphicModels/api.yaml
@@ -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
\ No newline at end of file
+              format: int32
+              
+    AnotherObject:
+      type: object
+      properties:
+        some_integer_propery:
+          type: integer
+          format: int32
\ No newline at end of file
diff --git a/src/test/resources/examples/polymorphicModels/models/Models.kt b/src/test/resources/examples/polymorphicModels/models/Models.kt
index 645e8bf..23141c4 100644
--- a/src/test/resources/examples/polymorphicModels/models/Models.kt
+++ b/src/test/resources/examples/polymorphicModels/models/Models.kt
@@ -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
@@ -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
+)
diff --git a/src/test/resources/examples/polymorphicModels/sealed/models/Models.kt b/src/test/resources/examples/polymorphicModels/sealed/models/Models.kt
new file mode 100644
index 0000000..c6272d1
--- /dev/null
+++ b/src/test/resources/examples/polymorphicModels/sealed/models/Models.kt
@@ -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"
+}