From 472db60cbcfec60a321d6571dc5d8017fa110546 Mon Sep 17 00:00:00 2001 From: seonwoo_jung <79202163+seonwooj0810@users.noreply.github.com> Date: Thu, 25 Jun 2026 15:07:42 +0900 Subject: [PATCH] [Java] Skip wildcard media types when selecting request Content-Type (#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. --- .../libraries/restclient/ApiClient.mustache | 16 +++++-- .../libraries/resttemplate/ApiClient.mustache | 16 +++++-- .../libraries/webclient/ApiClient.mustache | 16 +++++-- .../org/openapitools/client/ApiClient.java | 16 +++++-- .../org/openapitools/client/ApiClient.java | 16 +++++-- .../org/openapitools/client/ApiClient.java | 16 +++++-- .../org/openapitools/client/ApiClient.java | 16 +++++-- .../org/openapitools/client/ApiClient.java | 16 +++++-- .../org/openapitools/client/ApiClient.java | 16 +++++-- .../org/openapitools/client/ApiClient.java | 16 +++++-- .../org/openapitools/client/ApiClient.java | 16 +++++-- .../org/openapitools/client/ApiClient.java | 16 +++++-- .../org/openapitools/client/ApiClient.java | 16 +++++-- .../org/openapitools/client/ApiClient.java | 16 +++++-- .../org/openapitools/client/ApiClient.java | 16 +++++-- .../org/openapitools/client/ApiClient.java | 16 +++++-- .../org/openapitools/client/ApiClient.java | 16 +++++-- .../org/openapitools/client/ApiClient.java | 16 +++++-- .../org/openapitools/client/ApiClient.java | 16 +++++-- .../org/openapitools/client/ApiClient.java | 16 +++++-- .../org/openapitools/client/ApiClient.java | 16 +++++-- .../openapitools/client/ApiClientTest.java | 47 +++++++++++++++++++ .../org/openapitools/client/ApiClient.java | 16 +++++-- .../org/openapitools/client/ApiClient.java | 16 +++++-- .../org/openapitools/client/ApiClient.java | 16 +++++-- .../org/openapitools/client/ApiClient.java | 16 +++++-- .../org/openapitools/client/ApiClient.java | 16 +++++-- .../org/openapitools/client/ApiClient.java | 16 +++++-- .../org/openapitools/client/ApiClient.java | 16 +++++-- .../org/openapitools/client/ApiClient.java | 16 +++++-- .../openapitools/client/ApiClientTest.java | 47 +++++++++++++++++++ .../org/openapitools/client/ApiClient.java | 16 +++++-- .../org/openapitools/client/ApiClient.java | 16 +++++-- .../org/openapitools/client/ApiClient.java | 16 +++++-- .../org/openapitools/client/ApiClient.java | 16 +++++-- .../org/openapitools/client/ApiClient.java | 16 +++++-- .../org/openapitools/client/ApiClient.java | 16 +++++-- .../org/openapitools/client/ApiClient.java | 16 +++++-- 38 files changed, 562 insertions(+), 108 deletions(-) create mode 100644 samples/client/petstore/java/restclient/src/test/java/org/openapitools/client/ApiClientTest.java create mode 100644 samples/client/petstore/java/resttemplate/src/test/java/org/openapitools/client/ApiClientTest.java diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/restclient/ApiClient.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/restclient/ApiClient.mustache index 82aa2f9c1b01..e4c10c425c18 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/restclient/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/restclient/ApiClient.mustache @@ -741,10 +741,10 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { /** * Select the Content-Type header's value from the given array: * if JSON exists in the given array, use it; - * otherwise use the first one of the array. + * otherwise use the first non-wildcard one of the array. * * @param contentTypes The Content-Type array to select from - * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned. + * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned; if it only contains wildcard media types, JSON will be used. */ public MediaType selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { @@ -753,10 +753,20 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { for (String contentType : contentTypes) { MediaType mediaType = MediaType.parseMediaType(contentType); if (isJsonMime(mediaType)) { + // A wildcard media type (e.g. "*/*" or "application/*") is treated as + // JSON-compatible but cannot be used as a request Content-Type header, + // so fall back to concrete JSON in that case. + return mediaType.isWildcardType() || mediaType.isWildcardSubtype() ? MediaType.APPLICATION_JSON : mediaType; + } + } + // No JSON type found; use the first concrete (non-wildcard) media type instead. + for (String contentType : contentTypes) { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (!mediaType.isWildcardType() && !mediaType.isWildcardSubtype()) { return mediaType; } } - return MediaType.parseMediaType(contentTypes[0]); + return MediaType.APPLICATION_JSON; } /** diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/ApiClient.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/ApiClient.mustache index 5a6cca9ad49f..07c1c9eda5ca 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/ApiClient.mustache @@ -643,10 +643,10 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { /** * Select the Content-Type header's value from the given array: * if JSON exists in the given array, use it; - * otherwise use the first one of the array. + * otherwise use the first non-wildcard one of the array. * * @param contentTypes The Content-Type array to select from - * @return MediaType The Content-Type header to use. If the given array is empty, JSON will be used. + * @return MediaType The Content-Type header to use. If the given array is empty, or only contains wildcard media types, JSON will be used. */ public MediaType selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { @@ -655,10 +655,20 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { for (String contentType : contentTypes) { MediaType mediaType = MediaType.parseMediaType(contentType); if (isJsonMime(mediaType)) { + // A wildcard media type (e.g. "*/*" or "application/*") is treated as + // JSON-compatible but cannot be used as a request Content-Type header, + // so fall back to concrete JSON in that case. + return mediaType.isWildcardType() || mediaType.isWildcardSubtype() ? MediaType.APPLICATION_JSON : mediaType; + } + } + // No JSON type found; use the first concrete (non-wildcard) media type instead. + for (String contentType : contentTypes) { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (!mediaType.isWildcardType() && !mediaType.isWildcardSubtype()) { return mediaType; } } - return MediaType.parseMediaType(contentTypes[0]); + return MediaType.APPLICATION_JSON; } /** diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/webclient/ApiClient.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/webclient/ApiClient.mustache index 1aae23cc945e..5a2cde83a729 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/webclient/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/webclient/ApiClient.mustache @@ -642,10 +642,10 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { /** * Select the Content-Type header's value from the given array: * if JSON exists in the given array, use it; - * otherwise use the first one of the array. + * otherwise use the first non-wildcard one of the array. * * @param contentTypes The Content-Type array to select from - * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned. + * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned; if it only contains wildcard media types, JSON will be used. */ public MediaType selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { @@ -654,10 +654,20 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { for (String contentType : contentTypes) { MediaType mediaType = MediaType.parseMediaType(contentType); if (isJsonMime(mediaType)) { + // A wildcard media type (e.g. "*/*" or "application/*") is treated as + // JSON-compatible but cannot be used as a request Content-Type header, + // so fall back to concrete JSON in that case. + return mediaType.isWildcardType() || mediaType.isWildcardSubtype() ? MediaType.APPLICATION_JSON : mediaType; + } + } + // No JSON type found; use the first concrete (non-wildcard) media type instead. + for (String contentType : contentTypes) { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (!mediaType.isWildcardType() && !mediaType.isWildcardSubtype()) { return mediaType; } } - return MediaType.parseMediaType(contentTypes[0]); + return MediaType.APPLICATION_JSON; } /** diff --git a/samples/client/echo_api/java/restclient/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/echo_api/java/restclient/src/main/java/org/openapitools/client/ApiClient.java index 6a8cd8ea705d..66d1d8c8926d 100644 --- a/samples/client/echo_api/java/restclient/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/echo_api/java/restclient/src/main/java/org/openapitools/client/ApiClient.java @@ -591,10 +591,10 @@ public List selectHeaderAccept(String[] accepts) { /** * Select the Content-Type header's value from the given array: * if JSON exists in the given array, use it; - * otherwise use the first one of the array. + * otherwise use the first non-wildcard one of the array. * * @param contentTypes The Content-Type array to select from - * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned. + * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned; if it only contains wildcard media types, JSON will be used. */ public MediaType selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { @@ -603,10 +603,20 @@ public MediaType selectHeaderContentType(String[] contentTypes) { for (String contentType : contentTypes) { MediaType mediaType = MediaType.parseMediaType(contentType); if (isJsonMime(mediaType)) { + // A wildcard media type (e.g. "*/*" or "application/*") is treated as + // JSON-compatible but cannot be used as a request Content-Type header, + // so fall back to concrete JSON in that case. + return mediaType.isWildcardType() || mediaType.isWildcardSubtype() ? MediaType.APPLICATION_JSON : mediaType; + } + } + // No JSON type found; use the first concrete (non-wildcard) media type instead. + for (String contentType : contentTypes) { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (!mediaType.isWildcardType() && !mediaType.isWildcardSubtype()) { return mediaType; } } - return MediaType.parseMediaType(contentTypes[0]); + return MediaType.APPLICATION_JSON; } /** diff --git a/samples/client/echo_api/java/resttemplate/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/echo_api/java/resttemplate/src/main/java/org/openapitools/client/ApiClient.java index 8897915ac045..7cc90b8dbff9 100644 --- a/samples/client/echo_api/java/resttemplate/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/echo_api/java/resttemplate/src/main/java/org/openapitools/client/ApiClient.java @@ -555,10 +555,10 @@ public List selectHeaderAccept(String[] accepts) { /** * Select the Content-Type header's value from the given array: * if JSON exists in the given array, use it; - * otherwise use the first one of the array. + * otherwise use the first non-wildcard one of the array. * * @param contentTypes The Content-Type array to select from - * @return MediaType The Content-Type header to use. If the given array is empty, JSON will be used. + * @return MediaType The Content-Type header to use. If the given array is empty, or only contains wildcard media types, JSON will be used. */ public MediaType selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { @@ -567,10 +567,20 @@ public MediaType selectHeaderContentType(String[] contentTypes) { for (String contentType : contentTypes) { MediaType mediaType = MediaType.parseMediaType(contentType); if (isJsonMime(mediaType)) { + // A wildcard media type (e.g. "*/*" or "application/*") is treated as + // JSON-compatible but cannot be used as a request Content-Type header, + // so fall back to concrete JSON in that case. + return mediaType.isWildcardType() || mediaType.isWildcardSubtype() ? MediaType.APPLICATION_JSON : mediaType; + } + } + // No JSON type found; use the first concrete (non-wildcard) media type instead. + for (String contentType : contentTypes) { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (!mediaType.isWildcardType() && !mediaType.isWildcardSubtype()) { return mediaType; } } - return MediaType.parseMediaType(contentTypes[0]); + return MediaType.APPLICATION_JSON; } /** diff --git a/samples/client/others/java/restclient-enum-in-multipart/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/others/java/restclient-enum-in-multipart/src/main/java/org/openapitools/client/ApiClient.java index 2850a819fc2c..ee6a99bf6886 100644 --- a/samples/client/others/java/restclient-enum-in-multipart/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/others/java/restclient-enum-in-multipart/src/main/java/org/openapitools/client/ApiClient.java @@ -590,10 +590,10 @@ public List selectHeaderAccept(String[] accepts) { /** * Select the Content-Type header's value from the given array: * if JSON exists in the given array, use it; - * otherwise use the first one of the array. + * otherwise use the first non-wildcard one of the array. * * @param contentTypes The Content-Type array to select from - * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned. + * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned; if it only contains wildcard media types, JSON will be used. */ public MediaType selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { @@ -602,10 +602,20 @@ public MediaType selectHeaderContentType(String[] contentTypes) { for (String contentType : contentTypes) { MediaType mediaType = MediaType.parseMediaType(contentType); if (isJsonMime(mediaType)) { + // A wildcard media type (e.g. "*/*" or "application/*") is treated as + // JSON-compatible but cannot be used as a request Content-Type header, + // so fall back to concrete JSON in that case. + return mediaType.isWildcardType() || mediaType.isWildcardSubtype() ? MediaType.APPLICATION_JSON : mediaType; + } + } + // No JSON type found; use the first concrete (non-wildcard) media type instead. + for (String contentType : contentTypes) { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (!mediaType.isWildcardType() && !mediaType.isWildcardSubtype()) { return mediaType; } } - return MediaType.parseMediaType(contentTypes[0]); + return MediaType.APPLICATION_JSON; } /** diff --git a/samples/client/others/java/restclient-sealedInterface/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/others/java/restclient-sealedInterface/src/main/java/org/openapitools/client/ApiClient.java index 918efb7c0b95..23f3e3e7351f 100644 --- a/samples/client/others/java/restclient-sealedInterface/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/others/java/restclient-sealedInterface/src/main/java/org/openapitools/client/ApiClient.java @@ -589,10 +589,10 @@ public List selectHeaderAccept(String[] accepts) { /** * Select the Content-Type header's value from the given array: * if JSON exists in the given array, use it; - * otherwise use the first one of the array. + * otherwise use the first non-wildcard one of the array. * * @param contentTypes The Content-Type array to select from - * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned. + * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned; if it only contains wildcard media types, JSON will be used. */ public MediaType selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { @@ -601,10 +601,20 @@ public MediaType selectHeaderContentType(String[] contentTypes) { for (String contentType : contentTypes) { MediaType mediaType = MediaType.parseMediaType(contentType); if (isJsonMime(mediaType)) { + // A wildcard media type (e.g. "*/*" or "application/*") is treated as + // JSON-compatible but cannot be used as a request Content-Type header, + // so fall back to concrete JSON in that case. + return mediaType.isWildcardType() || mediaType.isWildcardSubtype() ? MediaType.APPLICATION_JSON : mediaType; + } + } + // No JSON type found; use the first concrete (non-wildcard) media type instead. + for (String contentType : contentTypes) { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (!mediaType.isWildcardType() && !mediaType.isWildcardSubtype()) { return mediaType; } } - return MediaType.parseMediaType(contentTypes[0]); + return MediaType.APPLICATION_JSON; } /** diff --git a/samples/client/others/java/restclient-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/others/java/restclient-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java index 50264f009dd9..76e3f9e9a13e 100644 --- a/samples/client/others/java/restclient-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/others/java/restclient-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java @@ -589,10 +589,10 @@ public List selectHeaderAccept(String[] accepts) { /** * Select the Content-Type header's value from the given array: * if JSON exists in the given array, use it; - * otherwise use the first one of the array. + * otherwise use the first non-wildcard one of the array. * * @param contentTypes The Content-Type array to select from - * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned. + * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned; if it only contains wildcard media types, JSON will be used. */ public MediaType selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { @@ -601,10 +601,20 @@ public MediaType selectHeaderContentType(String[] contentTypes) { for (String contentType : contentTypes) { MediaType mediaType = MediaType.parseMediaType(contentType); if (isJsonMime(mediaType)) { + // A wildcard media type (e.g. "*/*" or "application/*") is treated as + // JSON-compatible but cannot be used as a request Content-Type header, + // so fall back to concrete JSON in that case. + return mediaType.isWildcardType() || mediaType.isWildcardSubtype() ? MediaType.APPLICATION_JSON : mediaType; + } + } + // No JSON type found; use the first concrete (non-wildcard) media type instead. + for (String contentType : contentTypes) { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (!mediaType.isWildcardType() && !mediaType.isWildcardSubtype()) { return mediaType; } } - return MediaType.parseMediaType(contentTypes[0]); + return MediaType.APPLICATION_JSON; } /** diff --git a/samples/client/others/java/resttemplate-list-schema-validation/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/others/java/resttemplate-list-schema-validation/src/main/java/org/openapitools/client/ApiClient.java index 09d277f3d66c..a3cb15dd5c1c 100644 --- a/samples/client/others/java/resttemplate-list-schema-validation/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/others/java/resttemplate-list-schema-validation/src/main/java/org/openapitools/client/ApiClient.java @@ -498,10 +498,10 @@ public List selectHeaderAccept(String[] accepts) { /** * Select the Content-Type header's value from the given array: * if JSON exists in the given array, use it; - * otherwise use the first one of the array. + * otherwise use the first non-wildcard one of the array. * * @param contentTypes The Content-Type array to select from - * @return MediaType The Content-Type header to use. If the given array is empty, JSON will be used. + * @return MediaType The Content-Type header to use. If the given array is empty, or only contains wildcard media types, JSON will be used. */ public MediaType selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { @@ -510,10 +510,20 @@ public MediaType selectHeaderContentType(String[] contentTypes) { for (String contentType : contentTypes) { MediaType mediaType = MediaType.parseMediaType(contentType); if (isJsonMime(mediaType)) { + // A wildcard media type (e.g. "*/*" or "application/*") is treated as + // JSON-compatible but cannot be used as a request Content-Type header, + // so fall back to concrete JSON in that case. + return mediaType.isWildcardType() || mediaType.isWildcardSubtype() ? MediaType.APPLICATION_JSON : mediaType; + } + } + // No JSON type found; use the first concrete (non-wildcard) media type instead. + for (String contentType : contentTypes) { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (!mediaType.isWildcardType() && !mediaType.isWildcardSubtype()) { return mediaType; } } - return MediaType.parseMediaType(contentTypes[0]); + return MediaType.APPLICATION_JSON; } /** diff --git a/samples/client/others/java/resttemplate-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/others/java/resttemplate-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java index a8a83049ad26..8c796b7aadf9 100644 --- a/samples/client/others/java/resttemplate-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/others/java/resttemplate-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java @@ -498,10 +498,10 @@ public List selectHeaderAccept(String[] accepts) { /** * Select the Content-Type header's value from the given array: * if JSON exists in the given array, use it; - * otherwise use the first one of the array. + * otherwise use the first non-wildcard one of the array. * * @param contentTypes The Content-Type array to select from - * @return MediaType The Content-Type header to use. If the given array is empty, JSON will be used. + * @return MediaType The Content-Type header to use. If the given array is empty, or only contains wildcard media types, JSON will be used. */ public MediaType selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { @@ -510,10 +510,20 @@ public MediaType selectHeaderContentType(String[] contentTypes) { for (String contentType : contentTypes) { MediaType mediaType = MediaType.parseMediaType(contentType); if (isJsonMime(mediaType)) { + // A wildcard media type (e.g. "*/*" or "application/*") is treated as + // JSON-compatible but cannot be used as a request Content-Type header, + // so fall back to concrete JSON in that case. + return mediaType.isWildcardType() || mediaType.isWildcardSubtype() ? MediaType.APPLICATION_JSON : mediaType; + } + } + // No JSON type found; use the first concrete (non-wildcard) media type instead. + for (String contentType : contentTypes) { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (!mediaType.isWildcardType() && !mediaType.isWildcardSubtype()) { return mediaType; } } - return MediaType.parseMediaType(contentTypes[0]); + return MediaType.APPLICATION_JSON; } /** diff --git a/samples/client/others/java/webclient-sealedInterface/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/others/java/webclient-sealedInterface/src/main/java/org/openapitools/client/ApiClient.java index 2226fab221ff..0eaacdc515cb 100644 --- a/samples/client/others/java/webclient-sealedInterface/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/others/java/webclient-sealedInterface/src/main/java/org/openapitools/client/ApiClient.java @@ -564,10 +564,10 @@ public List selectHeaderAccept(String[] accepts) { /** * Select the Content-Type header's value from the given array: * if JSON exists in the given array, use it; - * otherwise use the first one of the array. + * otherwise use the first non-wildcard one of the array. * * @param contentTypes The Content-Type array to select from - * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned. + * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned; if it only contains wildcard media types, JSON will be used. */ public MediaType selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { @@ -576,10 +576,20 @@ public MediaType selectHeaderContentType(String[] contentTypes) { for (String contentType : contentTypes) { MediaType mediaType = MediaType.parseMediaType(contentType); if (isJsonMime(mediaType)) { + // A wildcard media type (e.g. "*/*" or "application/*") is treated as + // JSON-compatible but cannot be used as a request Content-Type header, + // so fall back to concrete JSON in that case. + return mediaType.isWildcardType() || mediaType.isWildcardSubtype() ? MediaType.APPLICATION_JSON : mediaType; + } + } + // No JSON type found; use the first concrete (non-wildcard) media type instead. + for (String contentType : contentTypes) { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (!mediaType.isWildcardType() && !mediaType.isWildcardSubtype()) { return mediaType; } } - return MediaType.parseMediaType(contentTypes[0]); + return MediaType.APPLICATION_JSON; } /** diff --git a/samples/client/others/java/webclient-sealedInterface_3_1/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/others/java/webclient-sealedInterface_3_1/src/main/java/org/openapitools/client/ApiClient.java index 2226fab221ff..0eaacdc515cb 100644 --- a/samples/client/others/java/webclient-sealedInterface_3_1/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/others/java/webclient-sealedInterface_3_1/src/main/java/org/openapitools/client/ApiClient.java @@ -564,10 +564,10 @@ public List selectHeaderAccept(String[] accepts) { /** * Select the Content-Type header's value from the given array: * if JSON exists in the given array, use it; - * otherwise use the first one of the array. + * otherwise use the first non-wildcard one of the array. * * @param contentTypes The Content-Type array to select from - * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned. + * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned; if it only contains wildcard media types, JSON will be used. */ public MediaType selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { @@ -576,10 +576,20 @@ public MediaType selectHeaderContentType(String[] contentTypes) { for (String contentType : contentTypes) { MediaType mediaType = MediaType.parseMediaType(contentType); if (isJsonMime(mediaType)) { + // A wildcard media type (e.g. "*/*" or "application/*") is treated as + // JSON-compatible but cannot be used as a request Content-Type header, + // so fall back to concrete JSON in that case. + return mediaType.isWildcardType() || mediaType.isWildcardSubtype() ? MediaType.APPLICATION_JSON : mediaType; + } + } + // No JSON type found; use the first concrete (non-wildcard) media type instead. + for (String contentType : contentTypes) { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (!mediaType.isWildcardType() && !mediaType.isWildcardSubtype()) { return mediaType; } } - return MediaType.parseMediaType(contentTypes[0]); + return MediaType.APPLICATION_JSON; } /** diff --git a/samples/client/others/java/webclient-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/others/java/webclient-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java index c73926320972..7d0feceb6909 100644 --- a/samples/client/others/java/webclient-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/others/java/webclient-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java @@ -564,10 +564,10 @@ public List selectHeaderAccept(String[] accepts) { /** * Select the Content-Type header's value from the given array: * if JSON exists in the given array, use it; - * otherwise use the first one of the array. + * otherwise use the first non-wildcard one of the array. * * @param contentTypes The Content-Type array to select from - * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned. + * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned; if it only contains wildcard media types, JSON will be used. */ public MediaType selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { @@ -576,10 +576,20 @@ public MediaType selectHeaderContentType(String[] contentTypes) { for (String contentType : contentTypes) { MediaType mediaType = MediaType.parseMediaType(contentType); if (isJsonMime(mediaType)) { + // A wildcard media type (e.g. "*/*" or "application/*") is treated as + // JSON-compatible but cannot be used as a request Content-Type header, + // so fall back to concrete JSON in that case. + return mediaType.isWildcardType() || mediaType.isWildcardSubtype() ? MediaType.APPLICATION_JSON : mediaType; + } + } + // No JSON type found; use the first concrete (non-wildcard) media type instead. + for (String contentType : contentTypes) { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (!mediaType.isWildcardType() && !mediaType.isWildcardSubtype()) { return mediaType; } } - return MediaType.parseMediaType(contentTypes[0]); + return MediaType.APPLICATION_JSON; } /** diff --git a/samples/client/petstore/java/restclient-nullable-arrays/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/restclient-nullable-arrays/src/main/java/org/openapitools/client/ApiClient.java index 9d3522ddd555..7c09445a28f2 100644 --- a/samples/client/petstore/java/restclient-nullable-arrays/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/restclient-nullable-arrays/src/main/java/org/openapitools/client/ApiClient.java @@ -589,10 +589,10 @@ public List selectHeaderAccept(String[] accepts) { /** * Select the Content-Type header's value from the given array: * if JSON exists in the given array, use it; - * otherwise use the first one of the array. + * otherwise use the first non-wildcard one of the array. * * @param contentTypes The Content-Type array to select from - * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned. + * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned; if it only contains wildcard media types, JSON will be used. */ public MediaType selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { @@ -601,10 +601,20 @@ public MediaType selectHeaderContentType(String[] contentTypes) { for (String contentType : contentTypes) { MediaType mediaType = MediaType.parseMediaType(contentType); if (isJsonMime(mediaType)) { + // A wildcard media type (e.g. "*/*" or "application/*") is treated as + // JSON-compatible but cannot be used as a request Content-Type header, + // so fall back to concrete JSON in that case. + return mediaType.isWildcardType() || mediaType.isWildcardSubtype() ? MediaType.APPLICATION_JSON : mediaType; + } + } + // No JSON type found; use the first concrete (non-wildcard) media type instead. + for (String contentType : contentTypes) { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (!mediaType.isWildcardType() && !mediaType.isWildcardSubtype()) { return mediaType; } } - return MediaType.parseMediaType(contentTypes[0]); + return MediaType.APPLICATION_JSON; } /** diff --git a/samples/client/petstore/java/restclient-springBoot4-jackson2/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/restclient-springBoot4-jackson2/src/main/java/org/openapitools/client/ApiClient.java index 18ea3292185c..592c90e841a0 100644 --- a/samples/client/petstore/java/restclient-springBoot4-jackson2/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/restclient-springBoot4-jackson2/src/main/java/org/openapitools/client/ApiClient.java @@ -615,10 +615,10 @@ public List selectHeaderAccept(String[] accepts) { /** * Select the Content-Type header's value from the given array: * if JSON exists in the given array, use it; - * otherwise use the first one of the array. + * otherwise use the first non-wildcard one of the array. * * @param contentTypes The Content-Type array to select from - * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned. + * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned; if it only contains wildcard media types, JSON will be used. */ public MediaType selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { @@ -627,10 +627,20 @@ public MediaType selectHeaderContentType(String[] contentTypes) { for (String contentType : contentTypes) { MediaType mediaType = MediaType.parseMediaType(contentType); if (isJsonMime(mediaType)) { + // A wildcard media type (e.g. "*/*" or "application/*") is treated as + // JSON-compatible but cannot be used as a request Content-Type header, + // so fall back to concrete JSON in that case. + return mediaType.isWildcardType() || mediaType.isWildcardSubtype() ? MediaType.APPLICATION_JSON : mediaType; + } + } + // No JSON type found; use the first concrete (non-wildcard) media type instead. + for (String contentType : contentTypes) { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (!mediaType.isWildcardType() && !mediaType.isWildcardSubtype()) { return mediaType; } } - return MediaType.parseMediaType(contentTypes[0]); + return MediaType.APPLICATION_JSON; } /** diff --git a/samples/client/petstore/java/restclient-springBoot4-jackson3-jspecify/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/restclient-springBoot4-jackson3-jspecify/src/main/java/org/openapitools/client/ApiClient.java index df2af3b63b9d..c43d9b329b65 100644 --- a/samples/client/petstore/java/restclient-springBoot4-jackson3-jspecify/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/restclient-springBoot4-jackson3-jspecify/src/main/java/org/openapitools/client/ApiClient.java @@ -582,10 +582,10 @@ public List selectHeaderAccept(String[] accepts) { /** * Select the Content-Type header's value from the given array: * if JSON exists in the given array, use it; - * otherwise use the first one of the array. + * otherwise use the first non-wildcard one of the array. * * @param contentTypes The Content-Type array to select from - * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned. + * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned; if it only contains wildcard media types, JSON will be used. */ public MediaType selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { @@ -594,10 +594,20 @@ public MediaType selectHeaderContentType(String[] contentTypes) { for (String contentType : contentTypes) { MediaType mediaType = MediaType.parseMediaType(contentType); if (isJsonMime(mediaType)) { + // A wildcard media type (e.g. "*/*" or "application/*") is treated as + // JSON-compatible but cannot be used as a request Content-Type header, + // so fall back to concrete JSON in that case. + return mediaType.isWildcardType() || mediaType.isWildcardSubtype() ? MediaType.APPLICATION_JSON : mediaType; + } + } + // No JSON type found; use the first concrete (non-wildcard) media type instead. + for (String contentType : contentTypes) { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (!mediaType.isWildcardType() && !mediaType.isWildcardSubtype()) { return mediaType; } } - return MediaType.parseMediaType(contentTypes[0]); + return MediaType.APPLICATION_JSON; } /** diff --git a/samples/client/petstore/java/restclient-springBoot4-jackson3/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/restclient-springBoot4-jackson3/src/main/java/org/openapitools/client/ApiClient.java index bbd001e0985f..40f672de2ff7 100644 --- a/samples/client/petstore/java/restclient-springBoot4-jackson3/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/restclient-springBoot4-jackson3/src/main/java/org/openapitools/client/ApiClient.java @@ -608,10 +608,10 @@ public List selectHeaderAccept(String[] accepts) { /** * Select the Content-Type header's value from the given array: * if JSON exists in the given array, use it; - * otherwise use the first one of the array. + * otherwise use the first non-wildcard one of the array. * * @param contentTypes The Content-Type array to select from - * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned. + * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned; if it only contains wildcard media types, JSON will be used. */ public MediaType selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { @@ -620,10 +620,20 @@ public MediaType selectHeaderContentType(String[] contentTypes) { for (String contentType : contentTypes) { MediaType mediaType = MediaType.parseMediaType(contentType); if (isJsonMime(mediaType)) { + // A wildcard media type (e.g. "*/*" or "application/*") is treated as + // JSON-compatible but cannot be used as a request Content-Type header, + // so fall back to concrete JSON in that case. + return mediaType.isWildcardType() || mediaType.isWildcardSubtype() ? MediaType.APPLICATION_JSON : mediaType; + } + } + // No JSON type found; use the first concrete (non-wildcard) media type instead. + for (String contentType : contentTypes) { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (!mediaType.isWildcardType() && !mediaType.isWildcardSubtype()) { return mediaType; } } - return MediaType.parseMediaType(contentTypes[0]); + return MediaType.APPLICATION_JSON; } /** diff --git a/samples/client/petstore/java/restclient-swagger2/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/restclient-swagger2/src/main/java/org/openapitools/client/ApiClient.java index 7fec017174ec..d6990308fd86 100644 --- a/samples/client/petstore/java/restclient-swagger2/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/restclient-swagger2/src/main/java/org/openapitools/client/ApiClient.java @@ -662,10 +662,10 @@ public List selectHeaderAccept(String[] accepts) { /** * Select the Content-Type header's value from the given array: * if JSON exists in the given array, use it; - * otherwise use the first one of the array. + * otherwise use the first non-wildcard one of the array. * * @param contentTypes The Content-Type array to select from - * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned. + * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned; if it only contains wildcard media types, JSON will be used. */ public MediaType selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { @@ -674,10 +674,20 @@ public MediaType selectHeaderContentType(String[] contentTypes) { for (String contentType : contentTypes) { MediaType mediaType = MediaType.parseMediaType(contentType); if (isJsonMime(mediaType)) { + // A wildcard media type (e.g. "*/*" or "application/*") is treated as + // JSON-compatible but cannot be used as a request Content-Type header, + // so fall back to concrete JSON in that case. + return mediaType.isWildcardType() || mediaType.isWildcardSubtype() ? MediaType.APPLICATION_JSON : mediaType; + } + } + // No JSON type found; use the first concrete (non-wildcard) media type instead. + for (String contentType : contentTypes) { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (!mediaType.isWildcardType() && !mediaType.isWildcardSubtype()) { return mediaType; } } - return MediaType.parseMediaType(contentTypes[0]); + return MediaType.APPLICATION_JSON; } /** diff --git a/samples/client/petstore/java/restclient-useSingleRequestParameter-static/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/restclient-useSingleRequestParameter-static/src/main/java/org/openapitools/client/ApiClient.java index 7fec017174ec..d6990308fd86 100644 --- a/samples/client/petstore/java/restclient-useSingleRequestParameter-static/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/restclient-useSingleRequestParameter-static/src/main/java/org/openapitools/client/ApiClient.java @@ -662,10 +662,10 @@ public List selectHeaderAccept(String[] accepts) { /** * Select the Content-Type header's value from the given array: * if JSON exists in the given array, use it; - * otherwise use the first one of the array. + * otherwise use the first non-wildcard one of the array. * * @param contentTypes The Content-Type array to select from - * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned. + * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned; if it only contains wildcard media types, JSON will be used. */ public MediaType selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { @@ -674,10 +674,20 @@ public MediaType selectHeaderContentType(String[] contentTypes) { for (String contentType : contentTypes) { MediaType mediaType = MediaType.parseMediaType(contentType); if (isJsonMime(mediaType)) { + // A wildcard media type (e.g. "*/*" or "application/*") is treated as + // JSON-compatible but cannot be used as a request Content-Type header, + // so fall back to concrete JSON in that case. + return mediaType.isWildcardType() || mediaType.isWildcardSubtype() ? MediaType.APPLICATION_JSON : mediaType; + } + } + // No JSON type found; use the first concrete (non-wildcard) media type instead. + for (String contentType : contentTypes) { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (!mediaType.isWildcardType() && !mediaType.isWildcardSubtype()) { return mediaType; } } - return MediaType.parseMediaType(contentTypes[0]); + return MediaType.APPLICATION_JSON; } /** diff --git a/samples/client/petstore/java/restclient-useSingleRequestParameter/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/restclient-useSingleRequestParameter/src/main/java/org/openapitools/client/ApiClient.java index 7fec017174ec..d6990308fd86 100644 --- a/samples/client/petstore/java/restclient-useSingleRequestParameter/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/restclient-useSingleRequestParameter/src/main/java/org/openapitools/client/ApiClient.java @@ -662,10 +662,10 @@ public List selectHeaderAccept(String[] accepts) { /** * Select the Content-Type header's value from the given array: * if JSON exists in the given array, use it; - * otherwise use the first one of the array. + * otherwise use the first non-wildcard one of the array. * * @param contentTypes The Content-Type array to select from - * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned. + * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned; if it only contains wildcard media types, JSON will be used. */ public MediaType selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { @@ -674,10 +674,20 @@ public MediaType selectHeaderContentType(String[] contentTypes) { for (String contentType : contentTypes) { MediaType mediaType = MediaType.parseMediaType(contentType); if (isJsonMime(mediaType)) { + // A wildcard media type (e.g. "*/*" or "application/*") is treated as + // JSON-compatible but cannot be used as a request Content-Type header, + // so fall back to concrete JSON in that case. + return mediaType.isWildcardType() || mediaType.isWildcardSubtype() ? MediaType.APPLICATION_JSON : mediaType; + } + } + // No JSON type found; use the first concrete (non-wildcard) media type instead. + for (String contentType : contentTypes) { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (!mediaType.isWildcardType() && !mediaType.isWildcardSubtype()) { return mediaType; } } - return MediaType.parseMediaType(contentTypes[0]); + return MediaType.APPLICATION_JSON; } /** diff --git a/samples/client/petstore/java/restclient/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/restclient/src/main/java/org/openapitools/client/ApiClient.java index 7fec017174ec..d6990308fd86 100644 --- a/samples/client/petstore/java/restclient/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/restclient/src/main/java/org/openapitools/client/ApiClient.java @@ -662,10 +662,10 @@ public List selectHeaderAccept(String[] accepts) { /** * Select the Content-Type header's value from the given array: * if JSON exists in the given array, use it; - * otherwise use the first one of the array. + * otherwise use the first non-wildcard one of the array. * * @param contentTypes The Content-Type array to select from - * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned. + * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned; if it only contains wildcard media types, JSON will be used. */ public MediaType selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { @@ -674,10 +674,20 @@ public MediaType selectHeaderContentType(String[] contentTypes) { for (String contentType : contentTypes) { MediaType mediaType = MediaType.parseMediaType(contentType); if (isJsonMime(mediaType)) { + // A wildcard media type (e.g. "*/*" or "application/*") is treated as + // JSON-compatible but cannot be used as a request Content-Type header, + // so fall back to concrete JSON in that case. + return mediaType.isWildcardType() || mediaType.isWildcardSubtype() ? MediaType.APPLICATION_JSON : mediaType; + } + } + // No JSON type found; use the first concrete (non-wildcard) media type instead. + for (String contentType : contentTypes) { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (!mediaType.isWildcardType() && !mediaType.isWildcardSubtype()) { return mediaType; } } - return MediaType.parseMediaType(contentTypes[0]); + return MediaType.APPLICATION_JSON; } /** diff --git a/samples/client/petstore/java/restclient/src/test/java/org/openapitools/client/ApiClientTest.java b/samples/client/petstore/java/restclient/src/test/java/org/openapitools/client/ApiClientTest.java new file mode 100644 index 000000000000..512d9cb86b70 --- /dev/null +++ b/samples/client/petstore/java/restclient/src/test/java/org/openapitools/client/ApiClientTest.java @@ -0,0 +1,47 @@ +package org.openapitools.client; + +import org.junit.jupiter.api.Test; +import org.springframework.http.MediaType; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; + +class ApiClientTest { + + private final ApiClient apiClient = new ApiClient(); + + @Test + void selectHeaderContentTypePrefersJson() { + assertEquals(MediaType.APPLICATION_JSON, + apiClient.selectHeaderContentType(new String[]{"application/xml", "application/json"})); + } + + @Test + void selectHeaderContentTypeReturnsFirstConcreteTypeWhenNoJson() { + assertEquals(MediaType.IMAGE_PNG, + apiClient.selectHeaderContentType(new String[]{"image/png", "application/xml"})); + } + + // https://github.com/OpenAPITools/openapi-generator/issues/24118 + // A wildcard media type must never be returned: it cannot be used as a request + // Content-Type header (Spring throws "Content-Type cannot contain wildcard type '*'"). + + @Test + void selectHeaderContentTypeFallsBackToJsonForJsonCompatibleWildcard() { + // "application/*" / "*/*" are reported JSON-compatible by isJsonMime, but are wildcards. + for (String wildcard : new String[]{"application/*", "*/*"}) { + MediaType selected = apiClient.selectHeaderContentType(new String[]{wildcard}); + assertEquals(MediaType.APPLICATION_JSON, selected); + assertFalse(selected.isWildcardType()); + assertFalse(selected.isWildcardSubtype()); + } + } + + @Test + void selectHeaderContentTypeSkipsNonJsonWildcardForConcreteType() { + MediaType selected = apiClient.selectHeaderContentType(new String[]{"image/*", "text/plain"}); + assertEquals(MediaType.TEXT_PLAIN, selected); + assertFalse(selected.isWildcardType()); + assertFalse(selected.isWildcardSubtype()); + } +} diff --git a/samples/client/petstore/java/resttemplate-jakarta/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/resttemplate-jakarta/src/main/java/org/openapitools/client/ApiClient.java index 55f2ee438bf1..84abcec6258b 100644 --- a/samples/client/petstore/java/resttemplate-jakarta/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/resttemplate-jakarta/src/main/java/org/openapitools/client/ApiClient.java @@ -556,10 +556,10 @@ public List selectHeaderAccept(String[] accepts) { /** * Select the Content-Type header's value from the given array: * if JSON exists in the given array, use it; - * otherwise use the first one of the array. + * otherwise use the first non-wildcard one of the array. * * @param contentTypes The Content-Type array to select from - * @return MediaType The Content-Type header to use. If the given array is empty, JSON will be used. + * @return MediaType The Content-Type header to use. If the given array is empty, or only contains wildcard media types, JSON will be used. */ public MediaType selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { @@ -568,10 +568,20 @@ public MediaType selectHeaderContentType(String[] contentTypes) { for (String contentType : contentTypes) { MediaType mediaType = MediaType.parseMediaType(contentType); if (isJsonMime(mediaType)) { + // A wildcard media type (e.g. "*/*" or "application/*") is treated as + // JSON-compatible but cannot be used as a request Content-Type header, + // so fall back to concrete JSON in that case. + return mediaType.isWildcardType() || mediaType.isWildcardSubtype() ? MediaType.APPLICATION_JSON : mediaType; + } + } + // No JSON type found; use the first concrete (non-wildcard) media type instead. + for (String contentType : contentTypes) { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (!mediaType.isWildcardType() && !mediaType.isWildcardSubtype()) { return mediaType; } } - return MediaType.parseMediaType(contentTypes[0]); + return MediaType.APPLICATION_JSON; } /** diff --git a/samples/client/petstore/java/resttemplate-springBoot4-jackson2/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/resttemplate-springBoot4-jackson2/src/main/java/org/openapitools/client/ApiClient.java index c0422b15eaa9..64642c0f460a 100644 --- a/samples/client/petstore/java/resttemplate-springBoot4-jackson2/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/resttemplate-springBoot4-jackson2/src/main/java/org/openapitools/client/ApiClient.java @@ -524,10 +524,10 @@ public List selectHeaderAccept(String[] accepts) { /** * Select the Content-Type header's value from the given array: * if JSON exists in the given array, use it; - * otherwise use the first one of the array. + * otherwise use the first non-wildcard one of the array. * * @param contentTypes The Content-Type array to select from - * @return MediaType The Content-Type header to use. If the given array is empty, JSON will be used. + * @return MediaType The Content-Type header to use. If the given array is empty, or only contains wildcard media types, JSON will be used. */ public MediaType selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { @@ -536,10 +536,20 @@ public MediaType selectHeaderContentType(String[] contentTypes) { for (String contentType : contentTypes) { MediaType mediaType = MediaType.parseMediaType(contentType); if (isJsonMime(mediaType)) { + // A wildcard media type (e.g. "*/*" or "application/*") is treated as + // JSON-compatible but cannot be used as a request Content-Type header, + // so fall back to concrete JSON in that case. + return mediaType.isWildcardType() || mediaType.isWildcardSubtype() ? MediaType.APPLICATION_JSON : mediaType; + } + } + // No JSON type found; use the first concrete (non-wildcard) media type instead. + for (String contentType : contentTypes) { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (!mediaType.isWildcardType() && !mediaType.isWildcardSubtype()) { return mediaType; } } - return MediaType.parseMediaType(contentTypes[0]); + return MediaType.APPLICATION_JSON; } /** diff --git a/samples/client/petstore/java/resttemplate-springBoot4-jackson3-jspecify/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/resttemplate-springBoot4-jackson3-jspecify/src/main/java/org/openapitools/client/ApiClient.java index 21fc9847741d..d6bfae085050 100644 --- a/samples/client/petstore/java/resttemplate-springBoot4-jackson3-jspecify/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/resttemplate-springBoot4-jackson3-jspecify/src/main/java/org/openapitools/client/ApiClient.java @@ -497,10 +497,10 @@ public List selectHeaderAccept(String[] accepts) { /** * Select the Content-Type header's value from the given array: * if JSON exists in the given array, use it; - * otherwise use the first one of the array. + * otherwise use the first non-wildcard one of the array. * * @param contentTypes The Content-Type array to select from - * @return MediaType The Content-Type header to use. If the given array is empty, JSON will be used. + * @return MediaType The Content-Type header to use. If the given array is empty, or only contains wildcard media types, JSON will be used. */ public MediaType selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { @@ -509,10 +509,20 @@ public MediaType selectHeaderContentType(String[] contentTypes) { for (String contentType : contentTypes) { MediaType mediaType = MediaType.parseMediaType(contentType); if (isJsonMime(mediaType)) { + // A wildcard media type (e.g. "*/*" or "application/*") is treated as + // JSON-compatible but cannot be used as a request Content-Type header, + // so fall back to concrete JSON in that case. + return mediaType.isWildcardType() || mediaType.isWildcardSubtype() ? MediaType.APPLICATION_JSON : mediaType; + } + } + // No JSON type found; use the first concrete (non-wildcard) media type instead. + for (String contentType : contentTypes) { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (!mediaType.isWildcardType() && !mediaType.isWildcardSubtype()) { return mediaType; } } - return MediaType.parseMediaType(contentTypes[0]); + return MediaType.APPLICATION_JSON; } /** diff --git a/samples/client/petstore/java/resttemplate-springBoot4-jackson3/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/resttemplate-springBoot4-jackson3/src/main/java/org/openapitools/client/ApiClient.java index c066d2873994..ddde42f46d6b 100644 --- a/samples/client/petstore/java/resttemplate-springBoot4-jackson3/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/resttemplate-springBoot4-jackson3/src/main/java/org/openapitools/client/ApiClient.java @@ -523,10 +523,10 @@ public List selectHeaderAccept(String[] accepts) { /** * Select the Content-Type header's value from the given array: * if JSON exists in the given array, use it; - * otherwise use the first one of the array. + * otherwise use the first non-wildcard one of the array. * * @param contentTypes The Content-Type array to select from - * @return MediaType The Content-Type header to use. If the given array is empty, JSON will be used. + * @return MediaType The Content-Type header to use. If the given array is empty, or only contains wildcard media types, JSON will be used. */ public MediaType selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { @@ -535,10 +535,20 @@ public MediaType selectHeaderContentType(String[] contentTypes) { for (String contentType : contentTypes) { MediaType mediaType = MediaType.parseMediaType(contentType); if (isJsonMime(mediaType)) { + // A wildcard media type (e.g. "*/*" or "application/*") is treated as + // JSON-compatible but cannot be used as a request Content-Type header, + // so fall back to concrete JSON in that case. + return mediaType.isWildcardType() || mediaType.isWildcardSubtype() ? MediaType.APPLICATION_JSON : mediaType; + } + } + // No JSON type found; use the first concrete (non-wildcard) media type instead. + for (String contentType : contentTypes) { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (!mediaType.isWildcardType() && !mediaType.isWildcardSubtype()) { return mediaType; } } - return MediaType.parseMediaType(contentTypes[0]); + return MediaType.APPLICATION_JSON; } /** diff --git a/samples/client/petstore/java/resttemplate-swagger1/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/resttemplate-swagger1/src/main/java/org/openapitools/client/ApiClient.java index 8974341dccfa..1930446c988b 100644 --- a/samples/client/petstore/java/resttemplate-swagger1/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/resttemplate-swagger1/src/main/java/org/openapitools/client/ApiClient.java @@ -556,10 +556,10 @@ public List selectHeaderAccept(String[] accepts) { /** * Select the Content-Type header's value from the given array: * if JSON exists in the given array, use it; - * otherwise use the first one of the array. + * otherwise use the first non-wildcard one of the array. * * @param contentTypes The Content-Type array to select from - * @return MediaType The Content-Type header to use. If the given array is empty, JSON will be used. + * @return MediaType The Content-Type header to use. If the given array is empty, or only contains wildcard media types, JSON will be used. */ public MediaType selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { @@ -568,10 +568,20 @@ public MediaType selectHeaderContentType(String[] contentTypes) { for (String contentType : contentTypes) { MediaType mediaType = MediaType.parseMediaType(contentType); if (isJsonMime(mediaType)) { + // A wildcard media type (e.g. "*/*" or "application/*") is treated as + // JSON-compatible but cannot be used as a request Content-Type header, + // so fall back to concrete JSON in that case. + return mediaType.isWildcardType() || mediaType.isWildcardSubtype() ? MediaType.APPLICATION_JSON : mediaType; + } + } + // No JSON type found; use the first concrete (non-wildcard) media type instead. + for (String contentType : contentTypes) { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (!mediaType.isWildcardType() && !mediaType.isWildcardSubtype()) { return mediaType; } } - return MediaType.parseMediaType(contentTypes[0]); + return MediaType.APPLICATION_JSON; } /** diff --git a/samples/client/petstore/java/resttemplate-swagger2/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/resttemplate-swagger2/src/main/java/org/openapitools/client/ApiClient.java index 8974341dccfa..1930446c988b 100644 --- a/samples/client/petstore/java/resttemplate-swagger2/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/resttemplate-swagger2/src/main/java/org/openapitools/client/ApiClient.java @@ -556,10 +556,10 @@ public List selectHeaderAccept(String[] accepts) { /** * Select the Content-Type header's value from the given array: * if JSON exists in the given array, use it; - * otherwise use the first one of the array. + * otherwise use the first non-wildcard one of the array. * * @param contentTypes The Content-Type array to select from - * @return MediaType The Content-Type header to use. If the given array is empty, JSON will be used. + * @return MediaType The Content-Type header to use. If the given array is empty, or only contains wildcard media types, JSON will be used. */ public MediaType selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { @@ -568,10 +568,20 @@ public MediaType selectHeaderContentType(String[] contentTypes) { for (String contentType : contentTypes) { MediaType mediaType = MediaType.parseMediaType(contentType); if (isJsonMime(mediaType)) { + // A wildcard media type (e.g. "*/*" or "application/*") is treated as + // JSON-compatible but cannot be used as a request Content-Type header, + // so fall back to concrete JSON in that case. + return mediaType.isWildcardType() || mediaType.isWildcardSubtype() ? MediaType.APPLICATION_JSON : mediaType; + } + } + // No JSON type found; use the first concrete (non-wildcard) media type instead. + for (String contentType : contentTypes) { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (!mediaType.isWildcardType() && !mediaType.isWildcardSubtype()) { return mediaType; } } - return MediaType.parseMediaType(contentTypes[0]); + return MediaType.APPLICATION_JSON; } /** diff --git a/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/ApiClient.java index 2998e66ed378..807106243caa 100644 --- a/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/ApiClient.java @@ -619,10 +619,10 @@ public List selectHeaderAccept(String[] accepts) { /** * Select the Content-Type header's value from the given array: * if JSON exists in the given array, use it; - * otherwise use the first one of the array. + * otherwise use the first non-wildcard one of the array. * * @param contentTypes The Content-Type array to select from - * @return MediaType The Content-Type header to use. If the given array is empty, JSON will be used. + * @return MediaType The Content-Type header to use. If the given array is empty, or only contains wildcard media types, JSON will be used. */ public MediaType selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { @@ -631,10 +631,20 @@ public MediaType selectHeaderContentType(String[] contentTypes) { for (String contentType : contentTypes) { MediaType mediaType = MediaType.parseMediaType(contentType); if (isJsonMime(mediaType)) { + // A wildcard media type (e.g. "*/*" or "application/*") is treated as + // JSON-compatible but cannot be used as a request Content-Type header, + // so fall back to concrete JSON in that case. + return mediaType.isWildcardType() || mediaType.isWildcardSubtype() ? MediaType.APPLICATION_JSON : mediaType; + } + } + // No JSON type found; use the first concrete (non-wildcard) media type instead. + for (String contentType : contentTypes) { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (!mediaType.isWildcardType() && !mediaType.isWildcardSubtype()) { return mediaType; } } - return MediaType.parseMediaType(contentTypes[0]); + return MediaType.APPLICATION_JSON; } /** diff --git a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/ApiClient.java index 56358a4f83b0..4867f4ff5213 100644 --- a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/ApiClient.java @@ -614,10 +614,10 @@ public List selectHeaderAccept(String[] accepts) { /** * Select the Content-Type header's value from the given array: * if JSON exists in the given array, use it; - * otherwise use the first one of the array. + * otherwise use the first non-wildcard one of the array. * * @param contentTypes The Content-Type array to select from - * @return MediaType The Content-Type header to use. If the given array is empty, JSON will be used. + * @return MediaType The Content-Type header to use. If the given array is empty, or only contains wildcard media types, JSON will be used. */ public MediaType selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { @@ -626,10 +626,20 @@ public MediaType selectHeaderContentType(String[] contentTypes) { for (String contentType : contentTypes) { MediaType mediaType = MediaType.parseMediaType(contentType); if (isJsonMime(mediaType)) { + // A wildcard media type (e.g. "*/*" or "application/*") is treated as + // JSON-compatible but cannot be used as a request Content-Type header, + // so fall back to concrete JSON in that case. + return mediaType.isWildcardType() || mediaType.isWildcardSubtype() ? MediaType.APPLICATION_JSON : mediaType; + } + } + // No JSON type found; use the first concrete (non-wildcard) media type instead. + for (String contentType : contentTypes) { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (!mediaType.isWildcardType() && !mediaType.isWildcardSubtype()) { return mediaType; } } - return MediaType.parseMediaType(contentTypes[0]); + return MediaType.APPLICATION_JSON; } /** diff --git a/samples/client/petstore/java/resttemplate/src/test/java/org/openapitools/client/ApiClientTest.java b/samples/client/petstore/java/resttemplate/src/test/java/org/openapitools/client/ApiClientTest.java new file mode 100644 index 000000000000..512d9cb86b70 --- /dev/null +++ b/samples/client/petstore/java/resttemplate/src/test/java/org/openapitools/client/ApiClientTest.java @@ -0,0 +1,47 @@ +package org.openapitools.client; + +import org.junit.jupiter.api.Test; +import org.springframework.http.MediaType; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; + +class ApiClientTest { + + private final ApiClient apiClient = new ApiClient(); + + @Test + void selectHeaderContentTypePrefersJson() { + assertEquals(MediaType.APPLICATION_JSON, + apiClient.selectHeaderContentType(new String[]{"application/xml", "application/json"})); + } + + @Test + void selectHeaderContentTypeReturnsFirstConcreteTypeWhenNoJson() { + assertEquals(MediaType.IMAGE_PNG, + apiClient.selectHeaderContentType(new String[]{"image/png", "application/xml"})); + } + + // https://github.com/OpenAPITools/openapi-generator/issues/24118 + // A wildcard media type must never be returned: it cannot be used as a request + // Content-Type header (Spring throws "Content-Type cannot contain wildcard type '*'"). + + @Test + void selectHeaderContentTypeFallsBackToJsonForJsonCompatibleWildcard() { + // "application/*" / "*/*" are reported JSON-compatible by isJsonMime, but are wildcards. + for (String wildcard : new String[]{"application/*", "*/*"}) { + MediaType selected = apiClient.selectHeaderContentType(new String[]{wildcard}); + assertEquals(MediaType.APPLICATION_JSON, selected); + assertFalse(selected.isWildcardType()); + assertFalse(selected.isWildcardSubtype()); + } + } + + @Test + void selectHeaderContentTypeSkipsNonJsonWildcardForConcreteType() { + MediaType selected = apiClient.selectHeaderContentType(new String[]{"image/*", "text/plain"}); + assertEquals(MediaType.TEXT_PLAIN, selected); + assertFalse(selected.isWildcardType()); + assertFalse(selected.isWildcardSubtype()); + } +} diff --git a/samples/client/petstore/java/webclient-jakarta/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/webclient-jakarta/src/main/java/org/openapitools/client/ApiClient.java index 5b0ba5df93f2..498c11297e4f 100644 --- a/samples/client/petstore/java/webclient-jakarta/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/webclient-jakarta/src/main/java/org/openapitools/client/ApiClient.java @@ -584,10 +584,10 @@ public List selectHeaderAccept(String[] accepts) { /** * Select the Content-Type header's value from the given array: * if JSON exists in the given array, use it; - * otherwise use the first one of the array. + * otherwise use the first non-wildcard one of the array. * * @param contentTypes The Content-Type array to select from - * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned. + * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned; if it only contains wildcard media types, JSON will be used. */ public MediaType selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { @@ -596,10 +596,20 @@ public MediaType selectHeaderContentType(String[] contentTypes) { for (String contentType : contentTypes) { MediaType mediaType = MediaType.parseMediaType(contentType); if (isJsonMime(mediaType)) { + // A wildcard media type (e.g. "*/*" or "application/*") is treated as + // JSON-compatible but cannot be used as a request Content-Type header, + // so fall back to concrete JSON in that case. + return mediaType.isWildcardType() || mediaType.isWildcardSubtype() ? MediaType.APPLICATION_JSON : mediaType; + } + } + // No JSON type found; use the first concrete (non-wildcard) media type instead. + for (String contentType : contentTypes) { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (!mediaType.isWildcardType() && !mediaType.isWildcardSubtype()) { return mediaType; } } - return MediaType.parseMediaType(contentTypes[0]); + return MediaType.APPLICATION_JSON; } /** diff --git a/samples/client/petstore/java/webclient-nullable-arrays/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/webclient-nullable-arrays/src/main/java/org/openapitools/client/ApiClient.java index e498ef59c468..7df339025926 100644 --- a/samples/client/petstore/java/webclient-nullable-arrays/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/webclient-nullable-arrays/src/main/java/org/openapitools/client/ApiClient.java @@ -564,10 +564,10 @@ public List selectHeaderAccept(String[] accepts) { /** * Select the Content-Type header's value from the given array: * if JSON exists in the given array, use it; - * otherwise use the first one of the array. + * otherwise use the first non-wildcard one of the array. * * @param contentTypes The Content-Type array to select from - * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned. + * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned; if it only contains wildcard media types, JSON will be used. */ public MediaType selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { @@ -576,10 +576,20 @@ public MediaType selectHeaderContentType(String[] contentTypes) { for (String contentType : contentTypes) { MediaType mediaType = MediaType.parseMediaType(contentType); if (isJsonMime(mediaType)) { + // A wildcard media type (e.g. "*/*" or "application/*") is treated as + // JSON-compatible but cannot be used as a request Content-Type header, + // so fall back to concrete JSON in that case. + return mediaType.isWildcardType() || mediaType.isWildcardSubtype() ? MediaType.APPLICATION_JSON : mediaType; + } + } + // No JSON type found; use the first concrete (non-wildcard) media type instead. + for (String contentType : contentTypes) { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (!mediaType.isWildcardType() && !mediaType.isWildcardSubtype()) { return mediaType; } } - return MediaType.parseMediaType(contentTypes[0]); + return MediaType.APPLICATION_JSON; } /** diff --git a/samples/client/petstore/java/webclient-springBoot4-jackson3-jspecify/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/webclient-springBoot4-jackson3-jspecify/src/main/java/org/openapitools/client/ApiClient.java index 8100838231f2..ce8325a910bd 100644 --- a/samples/client/petstore/java/webclient-springBoot4-jackson3-jspecify/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/webclient-springBoot4-jackson3-jspecify/src/main/java/org/openapitools/client/ApiClient.java @@ -556,10 +556,10 @@ public List selectHeaderAccept(String[] accepts) { /** * Select the Content-Type header's value from the given array: * if JSON exists in the given array, use it; - * otherwise use the first one of the array. + * otherwise use the first non-wildcard one of the array. * * @param contentTypes The Content-Type array to select from - * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned. + * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned; if it only contains wildcard media types, JSON will be used. */ public MediaType selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { @@ -568,10 +568,20 @@ public MediaType selectHeaderContentType(String[] contentTypes) { for (String contentType : contentTypes) { MediaType mediaType = MediaType.parseMediaType(contentType); if (isJsonMime(mediaType)) { + // A wildcard media type (e.g. "*/*" or "application/*") is treated as + // JSON-compatible but cannot be used as a request Content-Type header, + // so fall back to concrete JSON in that case. + return mediaType.isWildcardType() || mediaType.isWildcardSubtype() ? MediaType.APPLICATION_JSON : mediaType; + } + } + // No JSON type found; use the first concrete (non-wildcard) media type instead. + for (String contentType : contentTypes) { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (!mediaType.isWildcardType() && !mediaType.isWildcardSubtype()) { return mediaType; } } - return MediaType.parseMediaType(contentTypes[0]); + return MediaType.APPLICATION_JSON; } /** diff --git a/samples/client/petstore/java/webclient-springBoot4-jackson3/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/webclient-springBoot4-jackson3/src/main/java/org/openapitools/client/ApiClient.java index 6e238763296a..90d83a69d33c 100644 --- a/samples/client/petstore/java/webclient-springBoot4-jackson3/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/webclient-springBoot4-jackson3/src/main/java/org/openapitools/client/ApiClient.java @@ -572,10 +572,10 @@ public List selectHeaderAccept(String[] accepts) { /** * Select the Content-Type header's value from the given array: * if JSON exists in the given array, use it; - * otherwise use the first one of the array. + * otherwise use the first non-wildcard one of the array. * * @param contentTypes The Content-Type array to select from - * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned. + * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned; if it only contains wildcard media types, JSON will be used. */ public MediaType selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { @@ -584,10 +584,20 @@ public MediaType selectHeaderContentType(String[] contentTypes) { for (String contentType : contentTypes) { MediaType mediaType = MediaType.parseMediaType(contentType); if (isJsonMime(mediaType)) { + // A wildcard media type (e.g. "*/*" or "application/*") is treated as + // JSON-compatible but cannot be used as a request Content-Type header, + // so fall back to concrete JSON in that case. + return mediaType.isWildcardType() || mediaType.isWildcardSubtype() ? MediaType.APPLICATION_JSON : mediaType; + } + } + // No JSON type found; use the first concrete (non-wildcard) media type instead. + for (String contentType : contentTypes) { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (!mediaType.isWildcardType() && !mediaType.isWildcardSubtype()) { return mediaType; } } - return MediaType.parseMediaType(contentTypes[0]); + return MediaType.APPLICATION_JSON; } /** diff --git a/samples/client/petstore/java/webclient-swagger2/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/webclient-swagger2/src/main/java/org/openapitools/client/ApiClient.java index 3e6fc228b089..6a1a0c57c27a 100644 --- a/samples/client/petstore/java/webclient-swagger2/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/webclient-swagger2/src/main/java/org/openapitools/client/ApiClient.java @@ -584,10 +584,10 @@ public List selectHeaderAccept(String[] accepts) { /** * Select the Content-Type header's value from the given array: * if JSON exists in the given array, use it; - * otherwise use the first one of the array. + * otherwise use the first non-wildcard one of the array. * * @param contentTypes The Content-Type array to select from - * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned. + * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned; if it only contains wildcard media types, JSON will be used. */ public MediaType selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { @@ -596,10 +596,20 @@ public MediaType selectHeaderContentType(String[] contentTypes) { for (String contentType : contentTypes) { MediaType mediaType = MediaType.parseMediaType(contentType); if (isJsonMime(mediaType)) { + // A wildcard media type (e.g. "*/*" or "application/*") is treated as + // JSON-compatible but cannot be used as a request Content-Type header, + // so fall back to concrete JSON in that case. + return mediaType.isWildcardType() || mediaType.isWildcardSubtype() ? MediaType.APPLICATION_JSON : mediaType; + } + } + // No JSON type found; use the first concrete (non-wildcard) media type instead. + for (String contentType : contentTypes) { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (!mediaType.isWildcardType() && !mediaType.isWildcardSubtype()) { return mediaType; } } - return MediaType.parseMediaType(contentTypes[0]); + return MediaType.APPLICATION_JSON; } /** diff --git a/samples/client/petstore/java/webclient-useSingleRequestParameter/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/webclient-useSingleRequestParameter/src/main/java/org/openapitools/client/ApiClient.java index 3e6fc228b089..6a1a0c57c27a 100644 --- a/samples/client/petstore/java/webclient-useSingleRequestParameter/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/webclient-useSingleRequestParameter/src/main/java/org/openapitools/client/ApiClient.java @@ -584,10 +584,10 @@ public List selectHeaderAccept(String[] accepts) { /** * Select the Content-Type header's value from the given array: * if JSON exists in the given array, use it; - * otherwise use the first one of the array. + * otherwise use the first non-wildcard one of the array. * * @param contentTypes The Content-Type array to select from - * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned. + * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned; if it only contains wildcard media types, JSON will be used. */ public MediaType selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { @@ -596,10 +596,20 @@ public MediaType selectHeaderContentType(String[] contentTypes) { for (String contentType : contentTypes) { MediaType mediaType = MediaType.parseMediaType(contentType); if (isJsonMime(mediaType)) { + // A wildcard media type (e.g. "*/*" or "application/*") is treated as + // JSON-compatible but cannot be used as a request Content-Type header, + // so fall back to concrete JSON in that case. + return mediaType.isWildcardType() || mediaType.isWildcardSubtype() ? MediaType.APPLICATION_JSON : mediaType; + } + } + // No JSON type found; use the first concrete (non-wildcard) media type instead. + for (String contentType : contentTypes) { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (!mediaType.isWildcardType() && !mediaType.isWildcardSubtype()) { return mediaType; } } - return MediaType.parseMediaType(contentTypes[0]); + return MediaType.APPLICATION_JSON; } /** diff --git a/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/ApiClient.java index 3e6fc228b089..6a1a0c57c27a 100644 --- a/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/ApiClient.java @@ -584,10 +584,10 @@ public List selectHeaderAccept(String[] accepts) { /** * Select the Content-Type header's value from the given array: * if JSON exists in the given array, use it; - * otherwise use the first one of the array. + * otherwise use the first non-wildcard one of the array. * * @param contentTypes The Content-Type array to select from - * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned. + * @return MediaType The Content-Type header to use. If the given array is empty, null will be returned; if it only contains wildcard media types, JSON will be used. */ public MediaType selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { @@ -596,10 +596,20 @@ public MediaType selectHeaderContentType(String[] contentTypes) { for (String contentType : contentTypes) { MediaType mediaType = MediaType.parseMediaType(contentType); if (isJsonMime(mediaType)) { + // A wildcard media type (e.g. "*/*" or "application/*") is treated as + // JSON-compatible but cannot be used as a request Content-Type header, + // so fall back to concrete JSON in that case. + return mediaType.isWildcardType() || mediaType.isWildcardSubtype() ? MediaType.APPLICATION_JSON : mediaType; + } + } + // No JSON type found; use the first concrete (non-wildcard) media type instead. + for (String contentType : contentTypes) { + MediaType mediaType = MediaType.parseMediaType(contentType); + if (!mediaType.isWildcardType() && !mediaType.isWildcardSubtype()) { return mediaType; } } - return MediaType.parseMediaType(contentTypes[0]); + return MediaType.APPLICATION_JSON; } /**