Repository: cjbooms/fabrikt
Pull request #63 (2021-07-08 18:45:25)

ISSUE-60
- Fixing issue with external models missing the JsonSubType values
- Also restoring linting of generated files

cjbooms: 👍 

Patch:
diff --git a/src/main/kotlin/com/cjbooms/fabrikt/cli/CodeGen.kt b/src/main/kotlin/com/cjbooms/fabrikt/cli/CodeGen.kt
index ade74bf..78eda10 100644
--- a/src/main/kotlin/com/cjbooms/fabrikt/cli/CodeGen.kt
+++ b/src/main/kotlin/com/cjbooms/fabrikt/cli/CodeGen.kt
@@ -4,6 +4,7 @@ import com.beust.jcommander.ParameterException
 import com.cjbooms.fabrikt.configurations.Packages
 import com.cjbooms.fabrikt.generators.MutableSettings
 import com.cjbooms.fabrikt.model.SourceApi
+import com.cjbooms.fabrikt.validation.Linter
 import java.nio.file.Files
 import java.nio.file.Path
 import java.util.logging.Logger
@@ -50,5 +51,6 @@ object CodeGen {
         val sourceApi = SourceApi.create(suppliedApi, apiFragments, baseDir)
         val generator = CodeGenerator(packages, sourceApi)
         generator.generate().forEach { it.writeFileTo(outputDir.toFile()) }
+        Linter.lint(outputDir)
     }
 }
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 a95c9bb..bfc0cdb 100644
--- a/src/main/kotlin/com/cjbooms/fabrikt/generators/model/JacksonModelGenerator.kt
+++ b/src/main/kotlin/com/cjbooms/fabrikt/generators/model/JacksonModelGenerator.kt
@@ -140,7 +140,7 @@ class JacksonModelGenerator(
         .flatMap {
             val properties = it.schema.topLevelProperties(HTTP_SETTINGS, it.schema)
             if (properties.isNotEmpty() || it.typeInfo is KotlinTypeInfo.Enum) {
-                val primaryModel = buildPrimaryModel(api, it, properties)
+                val primaryModel = buildPrimaryModel(api, it, properties, schemas)
                 val inlinedModels = buildInLinedModels(properties, it.schema)
                 listOf(primaryModel) + inlinedModels
             } else emptyList()
@@ -149,14 +149,16 @@ class JacksonModelGenerator(
     private fun buildPrimaryModel(
         api: OpenApi3,
         schemaInfo: SchemaInfo,
-        properties: Collection<PropertyInfo>
+        properties: Collection<PropertyInfo>,
+        allSchemas: List<SchemaInfo>
     ): TypeSpec {
         val modelName = schemaInfo.name.toModelClassName()
         return when {
             schemaInfo.schema.isPolymorphicSuperType() -> polymorphicSuperType(
                 modelName,
                 properties,
-                schemaInfo.schema.discriminator
+                schemaInfo.schema.discriminator,
+                allSchemas
             )
             schemaInfo.schema.isPolymorphicSubType(api) -> polymorphicSubType(
                 modelName,
@@ -295,7 +297,8 @@ class JacksonModelGenerator(
     private fun polymorphicSuperType(
         modelName: String,
         properties: Collection<PropertyInfo>,
-        discriminator: Discriminator
+        discriminator: Discriminator,
+        allSchemas: List<SchemaInfo>
     ): TypeSpec {
         val classBuilder = TypeSpec.classBuilder(
             generatedType(
@@ -306,7 +309,7 @@ class JacksonModelGenerator(
             .addModifiers(KModifier.SEALED)
         classBuilder.addAnnotation(basePolymorphicType(discriminator.propertyName))
 
-        val subTypes = sourceApi.allSchemas
+        val subTypes = allSchemas
             .filter { model ->
                 model.schema.allOfSchemas.any { allOfRef ->
                     allOfRef.name == modelName && allOfRef.discriminator == discriminator


Test patch:
diff --git a/src/test/resources/examples/externalReferences/models/Models.kt b/src/test/resources/examples/externalReferences/models/Models.kt
index f14dd80..82befc1 100644
--- a/src/test/resources/examples/externalReferences/models/Models.kt
+++ b/src/test/resources/examples/externalReferences/models/Models.kt
@@ -62,7 +62,14 @@ data class ExternalObject(
     property = "discriminator",
     visible = true
 )
-@JsonSubTypes()
+@JsonSubTypes(
+    JsonSubTypes.Type(value = OneOfOne::class, name = "OneOfOne"),
+    JsonSubTypes.Type(
+        value =
+        OneOfTwo::class,
+        name = "OneOfTwo"
+    )
+)
 sealed class ParentOneOf() {
     abstract val discriminator: String
 }