fix: Support Maps with sealed class type (#211)

This commit is contained in:
Gennadi Kudrjavtsev
2022-02-26 14:47:17 +02:00
committed by GitHub
parent 89ac0e19dd
commit 1d8e1bc60d
8 changed files with 51 additions and 9 deletions

View File

@ -1,6 +1,8 @@
# Changelog # Changelog
## Unreleased ## Unreleased
- Fix to support sealed class typed Maps
### Added ### Added
### Changed ### Changed

View File

@ -28,8 +28,7 @@ object MapHandler : SchemaHandler {
if (keyType?.classifier != String::class) { if (keyType?.classifier != String::class) {
error("Invalid Map $type: OpenAPI dictionaries must have keys of type String") 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 valClassName = valClass.simpleName
val referenceName = genericNameAdapter(type, clazz) val referenceName = genericNameAdapter(type, clazz)
val valueReference = when (valClass.isSealed) { val valueReference = when (valClass.isSealed) {
@ -43,6 +42,7 @@ object MapHandler : SchemaHandler {
}) })
} }
false -> { false -> {
generateKTypeKontent(valType, cache)
val schema = cache[valClassName] ?: error("$valClassName not found") val schema = cache[valClassName] ?: error("$valClassName not found")
postProcessSchema(schema, valClassName!!) postProcessSchema(schema, valClassName!!)
} }

View File

@ -130,6 +130,9 @@
"properties": { "properties": {
"a": { "a": {
"type": "string" "type": "string"
},
"z": {
"type": "string"
} }
}, },
"required": [ "required": [
@ -145,6 +148,9 @@
"c": { "c": {
"format": "int32", "format": "int32",
"type": "integer" "type": "integer"
},
"z": {
"type": "string"
} }
}, },
"required": [ "required": [
@ -165,6 +171,9 @@
"properties": { "properties": {
"a": { "a": {
"type": "string" "type": "string"
},
"z": {
"type": "string"
} }
}, },
"required": [ "required": [
@ -180,6 +189,9 @@
"c": { "c": {
"format": "int32", "format": "int32",
"type": "integer" "type": "integer"
},
"z": {
"type": "string"
} }
}, },
"required": [ "required": [
@ -199,6 +211,9 @@
"properties": { "properties": {
"a": { "a": {
"type": "string" "type": "string"
},
"z": {
"type": "string"
} }
}, },
"required": [ "required": [
@ -214,6 +229,9 @@
"c": { "c": {
"format": "int32", "format": "int32",
"type": "integer" "type": "integer"
},
"z": {
"type": "string"
} }
}, },
"required": [ "required": [

View File

@ -102,6 +102,9 @@
"properties": { "properties": {
"a": { "a": {
"type": "string" "type": "string"
},
"z": {
"type": "string"
} }
}, },
"required": [ "required": [
@ -117,6 +120,9 @@
"c": { "c": {
"format": "int32", "format": "int32",
"type": "integer" "type": "integer"
},
"z": {
"type": "string"
} }
}, },
"required": [ "required": [

View File

@ -64,6 +64,9 @@
"properties": { "properties": {
"a": { "a": {
"type": "string" "type": "string"
},
"z": {
"type": "string"
} }
}, },
"required": [ "required": [
@ -79,6 +82,9 @@
"c": { "c": {
"format": "int32", "format": "int32",
"type": "integer" "type": "integer"
},
"z": {
"type": "string"
} }
}, },
"required": [ "required": [

View File

@ -60,14 +60,13 @@
}, },
"components": { "components": {
"schemas": { "schemas": {
"FlibbityGibbit": {
"properties": {},
"type": "object"
},
"SimpleGibbit": { "SimpleGibbit": {
"properties": { "properties": {
"a": { "a": {
"type": "string" "type": "string"
},
"z": {
"type": "string"
} }
}, },
"required": [ "required": [
@ -83,6 +82,9 @@
"c": { "c": {
"format": "int32", "format": "int32",
"type": "integer" "type": "integer"
},
"z": {
"type": "string"
} }
}, },
"required": [ "required": [

View File

@ -61,6 +61,9 @@
"properties": { "properties": {
"a": { "a": {
"type": "string" "type": "string"
},
"z": {
"type": "string"
} }
}, },
"required": [ "required": [
@ -76,6 +79,9 @@
"c": { "c": {
"format": "int32", "format": "int32",
"type": "integer" "type": "integer"
},
"z": {
"type": "string"
} }
}, },
"required": [ "required": [

View File

@ -202,10 +202,12 @@ data class OptionalParams(
@Param(ParamType.QUERY) val notRequired: String? @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 SimpleGibbit(val a: String, override val z: String = "z") : FlibbityGibbit()
data class ComplexGibbit(val b: String, val c: Int) : FlibbityGibbit() data class ComplexGibbit(val b: String, val c: Int, override val z: String = "z") : FlibbityGibbit()
sealed interface SlammaJamma sealed interface SlammaJamma