From 1d8e1bc60d76ad2080741e4f5b03de9641f29926 Mon Sep 17 00:00:00 2001 From: Gennadi Kudrjavtsev Date: Sat, 26 Feb 2022 14:47:17 +0200 Subject: [PATCH] fix: Support Maps with sealed class type (#211) --- CHANGELOG.md | 2 ++ .../bkbn/kompendium/core/handler/MapHandler.kt | 4 ++-- .../resources/crazy_polymorphic_example.json | 18 ++++++++++++++++++ .../polymorphic_error_status_codes.json | 6 ++++++ .../resources/polymorphic_list_response.json | 6 ++++++ .../resources/polymorphic_map_response.json | 10 ++++++---- .../test/resources/polymorphic_response.json | 6 ++++++ .../kompendium/core/fixtures/TestModels.kt | 8 +++++--- 8 files changed, 51 insertions(+), 9 deletions(-) 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!!) } 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