Skip to content

[Java] Skip wildcard media types when selecting request Content-Type (#24118)#24127

Merged
wing328 merged 1 commit into
OpenAPITools:masterfrom
seonwooj0810:fix/issue-24118-wildcard-content-type
Jun 25, 2026
Merged

[Java] Skip wildcard media types when selecting request Content-Type (#24118)#24127
wing328 merged 1 commit into
OpenAPITools:masterfrom
seonwooj0810:fix/issue-24118-wildcard-content-type

Conversation

@seonwooj0810

@seonwooj0810 seonwooj0810 commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Fixes #24118

Root cause

ApiClient.selectHeaderContentType(String[]) could hand back a wildcard media type. isJsonMime() treats wildcards such as application/* and */* as JSON-compatible, so the JSON branch returned the wildcard unchanged; and when no JSON type was present the method returned contentTypes[0] verbatim. When that value is applied as a request Content-Type header, Spring throws IllegalArgumentException: Content-Type cannot contain wildcard type '*', so any spec declaring a wildcard request body (e.g. application/*) produced clients that fail at runtime.

Change

  • JSON-compatible wildcards now fall back to the concrete application/json.
  • The no-JSON path returns the first non-wildcard media type, and application/json only as a last resort when every candidate is a wildcard.
  • Applied to the restclient, resttemplate and webclient ApiClient.mustache templates; all affected Java samples regenerated.

Tests

Added ApiClientTest to the restclient and resttemplate petstore samples. selectHeaderContentTypeFallsBackToJsonForJsonCompatibleWildcard fails before this change (the old code returned the wildcard) and passes after; companion cases cover JSON preference, first-concrete selection, and skipping non-JSON wildcards. Both run green (Tests run: 4, Failures: 0).

Verification done

  • Checks 1-5: no in-flight/own PR for this root cause (gh pr list by symbol selectHeaderContentType/wildcard content-type + cross-check of my open PRs); no claim in the thread; fix is code (.mustache + generated .java); confirmed the old pattern still present on the current head; issue is standalone (no parent epic).
  • Rebuilt the CLI (JDK21) and regenerated all java restclient/resttemplate/webclient configs; git diff confirms every changed sample is exactly the selectHeaderContentType change and matches the templates, so the samples-up-to-date CI check will pass.

Summary by cubic

Skip wildcard media types when selecting request Content-Type in Java clients to avoid Spring errors. Ensures a concrete media type is always set and updates restclient, resttemplate, and webclient generators. Fixes #24118.

  • Bug Fixes
    • JSON-compatible wildcards (e.g., application/*, */*) now fall back to application/json.
    • If no JSON is present, pick the first non-wildcard type; if all are wildcards, use application/json.
    • Applied in modules/openapi-generator/src/main/resources/Java/libraries/{restclient,resttemplate,webclient}/ApiClient.mustache; regenerated affected samples and added ApiClientTest in petstore restclient and resttemplate.

Written for commit 472db60. Summary will update on new commits.

Review in cubic

…penAPITools#24118)

selectHeaderContentType() could return a wildcard media type (e.g.
"application/*" or "*/*"). isJsonMime() reports such wildcards as
JSON-compatible, so the JSON branch returned the wildcard unchanged, and
the no-JSON fallback returned contentTypes[0] verbatim. Spring's
MediaType then throws IllegalArgumentException("Content-Type cannot
contain wildcard type '*'") when the value is used as a request header.

Now JSON-compatible wildcards fall back to concrete application/json, and
the no-JSON path returns the first non-wildcard media type (or
application/json if every candidate is a wildcard). Fixes restclient,
resttemplate and webclient ApiClient templates plus regenerated samples;
adds ApiClientTest covering wildcard handling.

@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 38 files

Re-trigger cubic

@wing328

wing328 commented Jun 25, 2026

Copy link
Copy Markdown
Member

thanks for the pr

cc @bbdouglas (2017/07) @sreeshas (2017/08) @jfiala (2017/08) @lukoyanov (2017/09) @cbornet (2017/09) @jeff9finger (2018/01) @karismann (2019/03) @Zomzog (2019/04) @lwlee2608 (2019/10) @martin-mfg (2023/08)

@wing328 wing328 added this to the 7.24.0 milestone Jun 25, 2026
@wing328 wing328 merged commit 6622b5e into OpenAPITools:master Jun 25, 2026
137 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] [JAVA] Wildcard Content-Types used in request without validation

2 participants