Repository: bkbnio/kompendium
Pull request #211 (2022-02-26 07:47:54)

Support Maps with sealed class type
fixes https://github.com/bkbnio/kompendium/issues/210

unredundant: Nice! 

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index adb4977b2..0ca6db66c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,6 +1,8 @@
 # Changelog
 
 ## Unreleased
+- Fix to support sealed class typed Maps
+
 ### Added
 
 ### Changed
diff --git a/kompendium-core/src/main/kotlin/io/bkbn/kompendium/core/handler/MapHandler.kt b/kompendium-core/src/main/kotlin/io/bkbn/kompendium/core/handler/MapHandler.kt
index f2351804f..ecb2ea4ec 100644
--- a/kompendium-core/src/main/kotlin/io/bkbn/kompendium/core/handler/MapHandler.kt
+++ b/kompendium-core/src/main/kotlin/io/bkbn/kompendium/core/handler/MapHandler.kt
@@ -28,8 +28,7 @@ object MapHandler : SchemaHandler {
     if (keyType?.classifier != String::class) {
       error("Invalid Map $type: OpenAPI dictionaries must have keys of type String")
     }
-    generateKTypeKontent(valType!!, cache)
-    val valClass = valType.classifier as KClass<*>
+    val valClass = valType!!.classifier as KClass<*>
     val valClassName = valClass.simpleName
     val referenceName = genericNameAdapter(type, clazz)
     val valueReference = when (valClass.isSealed) {
@@ -43,6 +42,7 @@ object MapHandler : SchemaHandler {
         })
       }
       false -> {
+        generateKTypeKontent(valType, cache)
         val schema = cache[valClassName] ?: error("$valClassName not found")
         postProcessSchema(schema, valClassName!!)
       }


Test patch:
diff --git a/kompendium-core/src/test/resources/crazy_polymorphic_example.json b/kompendium-core/src/test/resources/crazy_polymorphic_example.json
index 3e071bd90..20cd409a1 100644
--- a/kompendium-core/src/test/resources/crazy_polymorphic_example.json
+++ b/kompendium-core/src/test/resources/crazy_polymorphic_example.json
@@ -130,6 +130,9 @@
                 "properties": {
                   "a": {
                     "type": "string"
+                  },
+                  "z": {
+                    "type": "string"
                   }
                 },
                 "required": [
@@ -145,6 +148,9 @@
                   "c": {
                     "format": "int32",
                     "type": "integer"
+                  },
+                  "z": {
+                    "type": "string"
                   }
                 },
                 "required": [
@@ -165,6 +171,9 @@
         "properties": {
           "a": {
             "type": "string"
+          },
+          "z": {
+            "type": "string"
           }
         },
         "required": [
@@ -180,6 +189,9 @@
           "c": {
             "format": "int32",
             "type": "integer"
+          },
+          "z": {
+            "type": "string"
           }
         },
         "required": [
@@ -199,6 +211,9 @@
                 "properties": {
                   "a": {
                     "type": "string"
+                  },
+                  "z": {
+                    "type": "string"
                   }
                 },
                 "required": [
@@ -214,6 +229,9 @@
                   "c": {
                     "format": "int32",
                     "type": "integer"
+                  },
+                  "z": {
+                    "type": "string"
                   }
                 },
                 "required": [
diff --git a/kompendium-core/src/test/resources/polymorphic_error_status_codes.json b/kompendium-core/src/test/resources/polymorphic_error_status_codes.json
index 7e168a8a1..bbd8574d1 100644
--- a/kompendium-core/src/test/resources/polymorphic_error_status_codes.json
+++ b/kompendium-core/src/test/resources/polymorphic_error_status_codes.json
@@ -102,6 +102,9 @@
         "properties": {
           "a": {
             "type": "string"
+          },
+          "z": {
+            "type": "string"
           }
         },
         "required": [
@@ -117,6 +120,9 @@
           "c": {
             "format": "int32",
             "type": "integer"
+          },
+          "z": {
+            "type": "string"
           }
         },
         "required": [
diff --git a/kompendium-core/src/test/resources/polymorphic_list_response.json b/kompendium-core/src/test/resources/polymorphic_list_response.json
index 2898dba9c..05d9aeedd 100644
--- a/kompendium-core/src/test/resources/polymorphic_list_response.json
+++ b/kompendium-core/src/test/resources/polymorphic_list_response.json
@@ -64,6 +64,9 @@
         "properties": {
           "a": {
             "type": "string"
+          },
+          "z": {
+            "type": "string"
           }
         },
         "required": [
@@ -79,6 +82,9 @@
           "c": {
             "format": "int32",
             "type": "integer"
+          },
+          "z": {
+            "type": "string"
           }
         },
         "required": [
diff --git a/kompendium-core/src/test/resources/polymorphic_map_response.json b/kompendium-core/src/test/resources/polymorphic_map_response.json
index d3e12afdd..268266592 100644
--- a/kompendium-core/src/test/resources/polymorphic_map_response.json
+++ b/kompendium-core/src/test/resources/polymorphic_map_response.json
@@ -60,14 +60,13 @@
   },
   "components": {
     "schemas": {
-      "FlibbityGibbit": {
-        "properties": {},
-        "type": "object"
-      },
       "SimpleGibbit": {
         "properties": {
           "a": {
             "type": "string"
+          },
+          "z": {
+            "type": "string"
           }
         },
         "required": [
@@ -83,6 +82,9 @@
           "c": {
             "format": "int32",
             "type": "integer"
+          },
+          "z": {
+            "type": "string"
           }
         },
         "required": [
diff --git a/kompendium-core/src/test/resources/polymorphic_response.json b/kompendium-core/src/test/resources/polymorphic_response.json
index a427970c5..75fff2e55 100644
--- a/kompendium-core/src/test/resources/polymorphic_response.json
+++ b/kompendium-core/src/test/resources/polymorphic_response.json
@@ -61,6 +61,9 @@
         "properties": {
           "a": {
             "type": "string"
+          },
+          "z": {
+            "type": "string"
           }
         },
         "required": [
@@ -76,6 +79,9 @@
           "c": {
             "format": "int32",
             "type": "integer"
+          },
+          "z": {
+            "type": "string"
           }
         },
         "required": [
diff --git a/kompendium-core/src/testFixtures/kotlin/io/bkbn/kompendium/core/fixtures/TestModels.kt b/kompendium-core/src/testFixtures/kotlin/io/bkbn/kompendium/core/fixtures/TestModels.kt
index 4edd3a3ca..09f4ed8c0 100644
--- a/kompendium-core/src/testFixtures/kotlin/io/bkbn/kompendium/core/fixtures/TestModels.kt
+++ b/kompendium-core/src/testFixtures/kotlin/io/bkbn/kompendium/core/fixtures/TestModels.kt
@@ -202,10 +202,12 @@ data class OptionalParams(
   @Param(ParamType.QUERY) val notRequired: String?
 )
 
-sealed class FlibbityGibbit
+sealed class FlibbityGibbit {
+  abstract val z: String
+}
 
-data class SimpleGibbit(val a: String) : FlibbityGibbit()
-data class ComplexGibbit(val b: String, val c: Int) : FlibbityGibbit()
+data class SimpleGibbit(val a: String, override val z: String = "z") : FlibbityGibbit()
+data class ComplexGibbit(val b: String, val c: Int, override val z: String = "z") : FlibbityGibbit()
 
 sealed interface SlammaJamma