From d4573cce5bec7e9507e56d2ee472650fe1b0583a Mon Sep 17 00:00:00 2001 From: Simon Vergauwen Date: Wed, 3 Jul 2024 14:54:38 +0200 Subject: [PATCH] Improve deserialization of Model.OpenEnum --- .../kotlin/io/github/nomisrev/openapi/Models.kt | 3 +-- .../kotlin/io/github/nomisrev/openapi/Predef.kt | 17 +++++++++++------ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/generation/src/main/kotlin/io/github/nomisrev/openapi/Models.kt b/generation/src/main/kotlin/io/github/nomisrev/openapi/Models.kt index 3f1c486..e0b5b91 100644 --- a/generation/src/main/kotlin/io/github/nomisrev/openapi/Models.kt +++ b/generation/src/main/kotlin/io/github/nomisrev/openapi/Models.kt @@ -380,7 +380,7 @@ private fun Model.Enum.Open.toTypeSpec(): TypeSpec { .addCode( CodeBlock.builder() .addStatement("val value = decoder.decodeString()") - .addStatement("return attemptDeserialize(value,") + .addStatement("return deserializeOpenEnum(value, { OpenCase(value) },") .withIndent { rawToName.forEach { (_, name) -> val nested = NamingContext.Nested(Named(name), context) @@ -389,7 +389,6 @@ private fun Model.Enum.Open.toTypeSpec(): TypeSpec { toClassName(nested) ) } - addStatement("Pair(OpenCase::class) { OpenCase(value) }") } .addStatement(")") .build() diff --git a/generation/src/main/kotlin/io/github/nomisrev/openapi/Predef.kt b/generation/src/main/kotlin/io/github/nomisrev/openapi/Predef.kt index e0fed2d..5a88183 100644 --- a/generation/src/main/kotlin/io/github/nomisrev/openapi/Predef.kt +++ b/generation/src/main/kotlin/io/github/nomisrev/openapi/Predef.kt @@ -243,19 +243,25 @@ fun predef(): FileSpec = .build() ) .addFunction( - FunSpec.builder("attemptDeserialize") + FunSpec.builder("deserializeOpenEnum") .addTypeVariable(TypeVariableName("A")) .returns(TypeVariableName("A")) .addParameter("value", String::class) + .addParameter( + "open", + LambdaTypeName.get( + parameters = listOf(ParameterSpec.unnamed(String::class)), + returnType = TypeVariableName("A") + ) + ) .addParameter( "block", ClassName("kotlin", "Pair") .parameterizedBy( ClassName("kotlin.reflect", "KClass").parameterizedBy(TypeVariableName("*")), LambdaTypeName.get( - receiver = null, - returnType = TypeVariableName("A").nullable(), - parameters = listOf(ParameterSpec.unnamed(String::class)) + parameters = listOf(ParameterSpec.unnamed(String::class)), + returnType = TypeVariableName("A").nullable() ) ), KModifier.VARARG @@ -270,8 +276,7 @@ fun predef(): FileSpec = errors[kclass] = e } } - // TODO Improve this error message - throw RuntimeException("BOOM! Improve this error message") + return open(value) """ .trimIndent() )