Repository navigation
fix(kotlin-client): use escaped propertyBaseName for JsonClassDiscriminator - #24725
Conversation
…inator The kotlinx_serialization and multiplatform templates emitted discriminator.propertyName, which DefaultCodegen sets via toVarName() (DefaultCodegen.java:3770). A discriminator named $type was emitted as dollarType, so the annotation disagreed with oneof_class.mustache, which already writes and reads the discriminator using propertyBaseName. Switch to propertyBaseName and wrap it in lambda.escapeDollar, matching the ~40 existing uses of that lambda in the same template, so the raw name is valid inside a Kotlin string literal. Adds a sample spec with a $type discriminator covering both templates. fixes OpenAPITools#22523
There was a problem hiding this comment.
All reported issues were addressed across 77 files
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
|
Re: the cubic review — a note on scope. Six of the seven findings are in generated sample output (ApiClient.kt, ApiKeyAuth.kt, BirdApi.md), byte-identical to what every existing Kotlin sample contains. They appear here only because the new sample directories make that code show up as new files. The markdown table issue was real but predated this PR — root cause was api_doc.mustache emitting the header inside {{#-last}}. Fixed separately in #24742. The remaining finding (escapeDollar on data_class.mustache) is addressed in the thread above. Resolving these to keep the diff readable. |
|
tested the new samples locally and the tests passed will add these to the CI after merging this PR |
Fixes #22523
Problem
The
kotlinx_serializationandmultiplatformtemplates inkotlin-client/data_class.mustacheemitteddiscriminator.propertyName.DefaultCodegensets that field throughtoVarName()(DefaultCodegen.java:3770), so it is a sanitized Kotlin identifier rather than the wire-format name — a discriminator declared as$typewas emitted asdollarType.This left the generated client internally inconsistent.
oneof_class.mustachealready writes and reads the discriminator usingpropertyBaseName(lines 142, 154), andtypeInfoAnnotation.mustacheusespropertyBaseNameinkotlin-client,kotlin-server, andkotlin-spring. These two annotation lines were the only remaining uses ofpropertyName.Fix
Switch both lines to
propertyBaseName, wrapped inlambda.escapeDollar— the same lambda already used ~40 times in this file forbaseName— because the raw name lands inside a Kotlin string literal, where an unescaped$begins an interpolation.Verification
Same spec, same command, only the template differs:
discriminator = "dollarType"propertyBaseNamealonediscriminator = "$type"— correct name, does not compilepropertyBaseName+escapeDollardiscriminator = "\$type"— correct and compiles./gradlew compileKotlinpasses on the generatedkotlin-allOf-special-discriminator-kotlinx-serializationsample.Regenerating all
bin/configs/kotlin-*.yamlproduces no diff in existing samples — every current spec uses a discriminator that is already a valid identifier, sotoVarName()was a no-op there.New samples
Adds
polymorphism-special-discriminator.yaml(a copy of the existingpolymorphism.yamlwith a$typediscriminator) plus two configs covering both affected templates, giving CI a regression case.Relation to #23633
That draft addresses Kotlin escaping broadly but is currently blocked on build failures. This is a narrow, independent fix for the discriminator case only.
cc @karismann @Zomzog @andrewemery @4brunu @yutaka0m @stefankoppier @e5l @dennisameling
PR checklist
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*.IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
Summary by cubic
Fixes Kotlin client generation for polymorphic discriminators when using
kotlinx-serializationand the multiplatform library. Previously,@JsonClassDiscriminatorused a transformed name (e.g., $type -> dollarType), which disagreed withoneof_class.mustacheand broke serialization; now it uses the raw discriminator name with escaped dollars. Fixes #22523.modules/openapi-generator/src/main/resources/kotlin-client/data_class.mustacheto usediscriminator.propertyBaseNamewrapped withlambda.escapeDollarin bothkotlinx_serializationandmultiplatformpaths.$typediscriminator and generate samples forkotlinx-serializationand multiplatform to validate the fix.Written for commit 54a99bb. Summary will update on new commits.