Skip to content

fix(kotlin-restclient): only emit first consumes type as Content-Type - #24502

Open
bugs84 wants to merge 1 commit into
OpenAPITools:masterfrom
bugs84:fix/kotlin-restclient-multi-consumes-content-type
Open

fix(kotlin-restclient): only emit first consumes type as Content-Type#24502
bugs84 wants to merge 1 commit into
OpenAPITools:masterfrom
bugs84:fix/kotlin-restclient-multi-consumes-content-type

Conversation

@bugs84

@bugs84 bugs84 commented Jul 27, 2026

Copy link
Copy Markdown

Fixes #24500.

For operations with no  formData  parameters but multiple declared  consumes  media types (e.g. an endpoint that accepts both a JSON body and a separate multipart/form-data variant), the  jvm-spring-restclient   api.mustache  template emitted one  localVariableHeaders["Content-Type"] = ...  assignment per  consumes  entry. Since every assignment targets the same map key, the last declared media type always won at runtime — regardless of which one actually matched the body being serialized.

This mirrors the sibling  hasFormParams  branch a few lines above in the same template, which already guards against this by using  consumes.0  instead of iterating over all  consumes  entries. This PR applies the same fix to the  hasFormParams=false  branch.

Verified with a minimal reproduction spec (attached to #24500) and by regenerating all  jvm-spring-restclient  Petstore samples: only  samples/client/petstore/kotlin-jvm-spring-3-restclient/.../PetApi.kt  changes, removing the previously-bogus second  Content-Type  assignment on  addPet / updatePet  (which declare  consumes: [application/json, application/xml] ) — confirming the same latent bug was already present in the committed sample and is fixed by this change.

Note: this fix makes content-type selection deterministic by always using the first  consumes  entry (matching the  native  Java library's convention), but is still order-dependent, unlike Java's  restclient / resttemplate / webclient  libraries which use a runtime  selectHeaderContentType  helper that prefers JSON regardless of position. See comment on #24500 for a possible follow-up.


Summary by cubic

Set Content-Type to the first declared consumes media type in the Kotlin jvm-spring-restclient generator when no form params are present. This prevents duplicate header assignments where the last consumes value wins (fixes #24500).

  • Bug Fixes
    • In api.mustache, use consumes.0 for Content-Type when hasFormParams=false instead of iterating all consumes.
    • Aligns with the hasFormParams branch and makes selection deterministic.
    • Updated Petstore sample to remove the extra Content-Type assignment.

Written for commit 763dafb. Summary will update on new commits.

Review in cubic

For operations with no formData parameters but multiple declared consumes
media types (e.g. a body param whose endpoint also supports a separate
multipart variant), the jvm-spring-restclient api.mustache template emitted
one localVariableHeaders["Content-Type"] = ... assignment per consumes
entry. Since all assignments target the same map key, the last declared
media type always won, regardless of which one actually matches the
serialized body.

Mirror the sibling hasFormParams branch just above (which already uses
consumes.0) by only emitting the first consumes entry here too.

Fixes OpenAPITools#24500

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

Re-trigger cubic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][KOTLIN][RESTCLIENT] Multiple consumes media types overwrite Content-Type header, last one always wins

1 participant