diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/ApiClient.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/ApiClient.mustache index 286838dcff45..cef315b6c5e9 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/ApiClient.mustache @@ -915,17 +915,23 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { * * @param contentTypes The Content-Type array to select from * @return The Content-Type header to use. If the given array is empty, - * JSON will be used. + * returns null. If it matches "any", JSON will be used. */ public String selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { + return null; + } + + if (contentTypes[0].equals("*/*")) { return "application/json"; } + for (String contentType : contentTypes) { if (isJsonMime(contentType)) { return contentType; } } + return contentTypes[0]; } @@ -955,7 +961,9 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { */ public Entity serialize(Object obj, Map formParams, String contentType, boolean isBodyNullable) throws ApiException { Entity entity; - if (contentType.startsWith("multipart/form-data")) { + if (contentType == null) { + return null; + } else if (contentType.startsWith("multipart/form-data")) { MultiPart multiPart = new MultiPart(); for (Entry param: formParams.entrySet()) { if (param.getValue() instanceof File) { @@ -1000,12 +1008,15 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { * * @param obj Object * @param formParams Form parameters - * @param contentType Context type + * @param contentType Content type * @param isBodyNullable True if the body is nullable * @return String * @throws ApiException API exception */ public String serializeToString(Object obj, Map formParams, String contentType, boolean isBodyNullable) throws ApiException { + if (contentType == null) { + return null; + } try { if (contentType.startsWith("multipart/form-data")) { throw new ApiException("multipart/form-data not yet supported for serializeToString (http signature authentication)"); diff --git a/samples/client/petstore/java/jersey2-java8-localdatetime/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/jersey2-java8-localdatetime/src/main/java/org/openapitools/client/ApiClient.java index 5f4cb48efe22..2867c1c5ac69 100644 --- a/samples/client/petstore/java/jersey2-java8-localdatetime/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/jersey2-java8-localdatetime/src/main/java/org/openapitools/client/ApiClient.java @@ -831,17 +831,23 @@ public String selectHeaderAccept(String[] accepts) { * * @param contentTypes The Content-Type array to select from * @return The Content-Type header to use. If the given array is empty, - * JSON will be used. + * returns null. If it matches "any", JSON will be used. */ public String selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { + return null; + } + + if (contentTypes[0].equals("*/*")) { return "application/json"; } + for (String contentType : contentTypes) { if (isJsonMime(contentType)) { return contentType; } } + return contentTypes[0]; } @@ -871,7 +877,9 @@ public String escapeString(String str) { */ public Entity serialize(Object obj, Map formParams, String contentType, boolean isBodyNullable) throws ApiException { Entity entity; - if (contentType.startsWith("multipart/form-data")) { + if (contentType == null) { + return null; + } else if (contentType.startsWith("multipart/form-data")) { MultiPart multiPart = new MultiPart(); for (Entry param: formParams.entrySet()) { if (param.getValue() instanceof File) { diff --git a/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java index 5f4cb48efe22..2c0d233e303c 100644 --- a/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java @@ -831,17 +831,23 @@ public String selectHeaderAccept(String[] accepts) { * * @param contentTypes The Content-Type array to select from * @return The Content-Type header to use. If the given array is empty, - * JSON will be used. + * returns null. If it matches "any", JSON will be used. */ public String selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { + return null; + } + + if (contentTypes[0].equals("*/*")) { return "application/json"; } + for (String contentType : contentTypes) { if (isJsonMime(contentType)) { return contentType; } } + return contentTypes[0]; } @@ -871,7 +877,9 @@ public String escapeString(String str) { */ public Entity serialize(Object obj, Map formParams, String contentType, boolean isBodyNullable) throws ApiException { Entity entity; - if (contentType.startsWith("multipart/form-data")) { + if (contentType == null) { + return null; + } else if (contentType.startsWith("multipart/form-data")) { MultiPart multiPart = new MultiPart(); for (Entry param: formParams.entrySet()) { if (param.getValue() instanceof File) { @@ -916,12 +924,15 @@ public Entity serialize(Object obj, Map formParams, String co * * @param obj Object * @param formParams Form parameters - * @param contentType Context type + * @param contentType Content type * @param isBodyNullable True if the body is nullable * @return String * @throws ApiException API exception */ public String serializeToString(Object obj, Map formParams, String contentType, boolean isBodyNullable) throws ApiException { + if (contentType == null) { + return null; + } try { if (contentType.startsWith("multipart/form-data")) { throw new ApiException("multipart/form-data not yet supported for serializeToString (http signature authentication)"); diff --git a/samples/client/petstore/java/jersey2-java8/src/test/java/org/openapitools/client/ApiClientTest.java b/samples/client/petstore/java/jersey2-java8/src/test/java/org/openapitools/client/ApiClientTest.java index 90a3758eba24..20d01ee9be3b 100644 --- a/samples/client/petstore/java/jersey2-java8/src/test/java/org/openapitools/client/ApiClientTest.java +++ b/samples/client/petstore/java/jersey2-java8/src/test/java/org/openapitools/client/ApiClientTest.java @@ -95,7 +95,7 @@ public void testSelectHeaderContentType() { assertEquals("text/plain", apiClient.selectHeaderContentType(contentTypes)); contentTypes = new String[]{}; - assertEquals("application/json", apiClient.selectHeaderContentType(contentTypes)); + assertNull(apiClient.selectHeaderContentType(contentTypes)); } @Test diff --git a/samples/openapi3/client/extensions/x-auth-id-alias/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java b/samples/openapi3/client/extensions/x-auth-id-alias/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java index 7f844074052a..e1ac74328ea0 100644 --- a/samples/openapi3/client/extensions/x-auth-id-alias/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/openapi3/client/extensions/x-auth-id-alias/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java @@ -761,17 +761,23 @@ public String selectHeaderAccept(String[] accepts) { * * @param contentTypes The Content-Type array to select from * @return The Content-Type header to use. If the given array is empty, - * JSON will be used. + * returns null. If it matches "any", JSON will be used. */ public String selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { + return null; + } + + if (contentTypes[0].equals("*/*")) { return "application/json"; } + for (String contentType : contentTypes) { if (isJsonMime(contentType)) { return contentType; } } + return contentTypes[0]; } @@ -801,7 +807,9 @@ public String escapeString(String str) { */ public Entity serialize(Object obj, Map formParams, String contentType, boolean isBodyNullable) throws ApiException { Entity entity; - if (contentType.startsWith("multipart/form-data")) { + if (contentType == null) { + return null; + } else if (contentType.startsWith("multipart/form-data")) { MultiPart multiPart = new MultiPart(); for (Entry param: formParams.entrySet()) { if (param.getValue() instanceof File) { diff --git a/samples/openapi3/client/petstore/java/jersey2-java8-special-characters/src/main/java/org/openapitools/client/ApiClient.java b/samples/openapi3/client/petstore/java/jersey2-java8-special-characters/src/main/java/org/openapitools/client/ApiClient.java index 66eb9ca7f90a..936bed24e080 100644 --- a/samples/openapi3/client/petstore/java/jersey2-java8-special-characters/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/openapi3/client/petstore/java/jersey2-java8-special-characters/src/main/java/org/openapitools/client/ApiClient.java @@ -706,17 +706,23 @@ public String selectHeaderAccept(String[] accepts) { * * @param contentTypes The Content-Type array to select from * @return The Content-Type header to use. If the given array is empty, - * JSON will be used. + * returns null. If it matches "any", JSON will be used. */ public String selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { + return null; + } + + if (contentTypes[0].equals("*/*")) { return "application/json"; } + for (String contentType : contentTypes) { if (isJsonMime(contentType)) { return contentType; } } + return contentTypes[0]; } @@ -746,7 +752,9 @@ public String escapeString(String str) { */ public Entity serialize(Object obj, Map formParams, String contentType, boolean isBodyNullable) throws ApiException { Entity entity; - if (contentType.startsWith("multipart/form-data")) { + if (contentType == null) { + return null; + } else if (contentType.startsWith("multipart/form-data")) { MultiPart multiPart = new MultiPart(); for (Entry param: formParams.entrySet()) { if (param.getValue() instanceof File) { diff --git a/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java b/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java index 328fec6af920..1e6fbf590636 100644 --- a/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java @@ -915,17 +915,23 @@ public String selectHeaderAccept(String[] accepts) { * * @param contentTypes The Content-Type array to select from * @return The Content-Type header to use. If the given array is empty, - * JSON will be used. + * returns null. If it matches "any", JSON will be used. */ public String selectHeaderContentType(String[] contentTypes) { if (contentTypes.length == 0) { + return null; + } + + if (contentTypes[0].equals("*/*")) { return "application/json"; } + for (String contentType : contentTypes) { if (isJsonMime(contentType)) { return contentType; } } + return contentTypes[0]; } @@ -955,7 +961,9 @@ public String escapeString(String str) { */ public Entity serialize(Object obj, Map formParams, String contentType, boolean isBodyNullable) throws ApiException { Entity entity; - if (contentType.startsWith("multipart/form-data")) { + if (contentType == null) { + return null; + } else if (contentType.startsWith("multipart/form-data")) { MultiPart multiPart = new MultiPart(); for (Entry param: formParams.entrySet()) { if (param.getValue() instanceof File) {