[Java] Skip wildcard media types when selecting request Content-Type (#24118)
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/jsononly as a last resort when every candidate is a wildcard. - Applied to the
restclient,resttemplateandwebclientApiClient.mustachetemplates; 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 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). - Rebuilt the CLI (JDK21) and regenerated all java restclient/resttemplate/webclient configs;
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, and webclient generators. Fixes #24118.
- Bug Fixes
- JSON-compatible wildcards (e.g.,
application/*,*/*) now fall back toapplication/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 addedApiClientTestin petstorerestclientandresttemplate.
- JSON-compatible wildcards (e.g.,