fix(kotlin-server): use valid Kotlin String conversions for ktor delegate header params

Merged
SE
5mo ago
+83-1

Fixes #24214

Root cause

The ktor delegatePattern header-extraction template (kotlin-server/libraries/ktor/_extract_param_value.mustache, added in #23756) converts a raw header String with it.to{{{dataType}}}(), interpolating the fully-qualified Kotlin type. For a non-string/enum header this produces uncompilable Kotlin, e.g. a boolean header generates it.tokotlin.Boolean() and a number header generates it.tojava.math.BigDecimal(). The endpoint's generated code therefore does not compile. Query/path params are unaffected because they go through ktor typed routing.

Change

Kotlin's String conversion functions use the simple type name, not the FQN. The template now maps the primitive type flags to the correct stdlib functions:

header typebefore (broken)after
booleanit.tokotlin.Boolean()it.toBoolean()
integer (int32)it.tokotlin.Int()it.toInt()
integer (int64)it.tokotlin.Long()it.toLong()
number/doubleit.tokotlin.Double()it.toDouble()
number (BigDecimal)it.tojava.math.BigDecimal()it.toBigDecimal()

string/enum headers are unchanged. Types outside this set (e.g. UUID, date-time) keep the previous fallback, so the change is scoped to the reported primitive/number regression. The int32/isShort flag is deliberately not mapped to toShort() because isShort is set for format: int32 (which maps to kotlin.Int), and no schema type maps to kotlin.Short.

Tests

Added KotlinServerCodegenTest#delegatePattern_headerParamPrimitiveConversion, which generates a spec with boolean/int32/int64/double/number header params and asserts the generated conversions are it.toBoolean()/it.toInt()/it.toLong()/it.toDouble()/it.toBigDecimal(), and that the old it.tokotlin.Boolean()/it.tojava.math.BigDecimal() forms are gone. Fails before the fix, passes after.

Verification done

  • Reproduced on latest master via the CLI (-g kotlin-server --library ktor --additional-properties delegatePattern=true): boolean/numeric header params emitted uncompilable it.to<FQN>(); confirmed corrected output after the change.
  • ./mvnw -pl modules/openapi-generator test -Dtest=KotlinServerCodegenTest — all 26 tests pass (JDK 21).
  • Confirmed the change produces no diff in the committed kotlin-server-ktor-delegate-pattern sample (petstore has no boolean/numeric header params), so no sample regeneration is required.

Summary by cubic

Fixes invalid header param conversions in the kotlin-server generator when using ktor delegate pattern. Generated code now uses Kotlin String extension functions so header parsing compiles correctly.

  • Bug Fixes
    • Map header conversions to valid Kotlin functions: boolean → toBoolean(), int32 → toInt(), int64 → toLong(), double → toDouble(), unformatted number (BigDecimal) → toBigDecimal().
    • Leave string/enum headers and non-primitive types unchanged; fallback remains for others.
    • Added a regression test to verify correct conversions and remove it.to<fully.qualified.DataType>() usages. Fixes #24214.