[Java] Skip wildcard media types when selecting request Content-Type (#24118)#24127
Merged
wing328 merged 1 commit intoJun 25, 2026
Conversation
…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.
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #24118
Root cause
ApiClient.selectHeaderContentType(String[])could hand back a wildcard media type.isJsonMime()treats wildcards such asapplication/*and*/*as JSON-compatible, so the JSON branch returned the wildcard unchanged; and when no JSON type was present the method returnedcontentTypes[0]verbatim. When that value is applied as a requestContent-Typeheader, Spring throwsIllegalArgumentException: Content-Type cannot contain wildcard type '*', so any spec declaring a wildcard request body (e.g.application/*) produced clients that fail at runtime.Change
application/json.application/jsononly as a last resort when every candidate is a wildcard.restclient,resttemplateandwebclientApiClient.mustachetemplates; all affected Java samples regenerated.Tests
Added
ApiClientTestto the restclient and resttemplate petstore samples.selectHeaderContentTypeFallsBackToJsonForJsonCompatibleWildcardfails 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
gh pr listby symbolselectHeaderContentType/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).git diffconfirms every changed sample is exactly theselectHeaderContentTypechange 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, andwebclientgenerators. Fixes #24118.application/*,*/*) now fall back toapplication/json.application/json.modules/openapi-generator/src/main/resources/Java/libraries/{restclient,resttemplate,webclient}/ApiClient.mustache; regenerated affected samples and addedApiClientTestin petstorerestclientandresttemplate.Written for commit 472db60. Summary will update on new commits.