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 a10302b183df..48c9d986bb53 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 @@ -874,9 +874,17 @@ public class ApiClient { } else { // We let jersey handle the serialization if (isBodyNullable) { // payload is nullable - entity = Entity.entity(obj == null ? "null" : obj, contentType); + if (obj instanceof String) { + entity = Entity.entity(obj == null ? "null" : "\"" + ((String)obj).replaceAll("\"", Matcher.quoteReplacement("\\\"")) + "\"", contentType); + } else { + entity = Entity.entity(obj == null ? "null" : obj, contentType); + } } else { - entity = Entity.entity(obj == null ? "" : obj, contentType); + if (obj instanceof String) { + entity = Entity.entity(obj == null ? "" : "\"" + ((String)obj).replaceAll("\"", Matcher.quoteReplacement("\\\"")) + "\"", contentType); + } else { + entity = Entity.entity(obj == null ? "" : obj, contentType); + } } } return entity; @@ -911,7 +919,7 @@ public class ApiClient { if (isBodyNullable) { return obj == null ? "null" : json.getMapper().writeValueAsString(obj); } else { - return json.getMapper().writeValueAsString(obj); + return obj == null ? "" : json.getMapper().writeValueAsString(obj); } } } catch (Exception ex) { 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 a8ce193dc362..a0529ef23392 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 @@ -791,9 +791,17 @@ public Entity serialize(Object obj, Map formParams, String co } else { // We let jersey handle the serialization if (isBodyNullable) { // payload is nullable - entity = Entity.entity(obj == null ? "null" : obj, contentType); + if (obj instanceof String) { + entity = Entity.entity(obj == null ? "null" : "\"" + ((String)obj).replaceAll("\"", Matcher.quoteReplacement("\\\"")) + "\"", contentType); + } else { + entity = Entity.entity(obj == null ? "null" : obj, contentType); + } } else { - entity = Entity.entity(obj == null ? "" : obj, contentType); + if (obj instanceof String) { + entity = Entity.entity(obj == null ? "" : "\"" + ((String)obj).replaceAll("\"", Matcher.quoteReplacement("\\\"")) + "\"", contentType); + } else { + entity = Entity.entity(obj == null ? "" : obj, contentType); + } } } return entity; @@ -828,7 +836,7 @@ public String serializeToString(Object obj, Map formParams, Stri if (isBodyNullable) { return obj == null ? "null" : json.getMapper().writeValueAsString(obj); } else { - return json.getMapper().writeValueAsString(obj); + return obj == null ? "" : json.getMapper().writeValueAsString(obj); } } } catch (Exception ex) { 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 2e37397e8ab7..af327d787786 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 @@ -870,9 +870,17 @@ public Entity serialize(Object obj, Map formParams, String co } else { // We let jersey handle the serialization if (isBodyNullable) { // payload is nullable - entity = Entity.entity(obj == null ? "null" : obj, contentType); + if (obj instanceof String) { + entity = Entity.entity(obj == null ? "null" : "\"" + ((String)obj).replaceAll("\"", Matcher.quoteReplacement("\\\"")) + "\"", contentType); + } else { + entity = Entity.entity(obj == null ? "null" : obj, contentType); + } } else { - entity = Entity.entity(obj == null ? "" : obj, contentType); + if (obj instanceof String) { + entity = Entity.entity(obj == null ? "" : "\"" + ((String)obj).replaceAll("\"", Matcher.quoteReplacement("\\\"")) + "\"", contentType); + } else { + entity = Entity.entity(obj == null ? "" : obj, contentType); + } } } return entity; @@ -907,7 +915,7 @@ public String serializeToString(Object obj, Map formParams, Stri if (isBodyNullable) { return obj == null ? "null" : json.getMapper().writeValueAsString(obj); } else { - return json.getMapper().writeValueAsString(obj); + return obj == null ? "" : json.getMapper().writeValueAsString(obj); } } } catch (Exception ex) { diff --git a/samples/openapi3/client/petstore/java/jersey2-java8/src/test/java/org/openapitools/client/ApiClientTest.java b/samples/openapi3/client/petstore/java/jersey2-java8/src/test/java/org/openapitools/client/ApiClientTest.java new file mode 100644 index 000000000000..fcccad7efba3 --- /dev/null +++ b/samples/openapi3/client/petstore/java/jersey2-java8/src/test/java/org/openapitools/client/ApiClientTest.java @@ -0,0 +1,64 @@ +package org.openapitools.client; + +import org.openapitools.client.model.*; +import org.openapitools.client.ApiClient; + +import java.lang.Exception; +import java.util.Arrays; +import java.util.Date; +import java.util.TimeZone; +import java.text.SimpleDateFormat; +import java.time.OffsetDateTime; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; +import org.junit.*; +import static org.junit.Assert.*; + +public class ApiClientTest { + ApiClient apiClient = null; + Pet pet = null; + + @Before + public void setup() { + apiClient = new ApiClient(); + pet = new Pet(); + } + + @Test + public void testSerializeToString() throws Exception { + Long petId = 4321L; + pet.setId(petId); + pet.setName("jersey2 java8 pet"); + Category category = new Category(); + category.setId(petId); + category.setName("jersey2 java8 category"); + pet.setCategory(category); + pet.setStatus(Pet.StatusEnum.AVAILABLE); + pet.setPhotoUrls(Arrays.asList("A", "B", "C")); + Tag tag = new Tag(); + tag.setId(petId); + tag.setName("jersey2 java8 tag"); + pet.setTags(Arrays.asList(tag)); + + String result = "{\"id\":4321,\"category\":{\"id\":4321,\"name\":\"jersey2 java8 category\"},\"name\":\"jersey2 java8 pet\",\"photoUrls\":[\"A\",\"B\",\"C\"],\"tags\":[{\"id\":4321,\"name\":\"jersey2 java8 tag\"}],\"status\":\"available\"}"; + assertEquals(result, apiClient.serializeToString(pet, null, "application/json", false)); + // nulllable and there should be no diffencne as the payload is not null + assertEquals(result, apiClient.serializeToString(pet, null, "application/json", true)); + + // non-nullable null object should be converted to "" (empty body) + assertEquals("", apiClient.serializeToString(null, null, "application/json", false)); + // nullable null object should be converted to "null" + assertEquals("null", apiClient.serializeToString(null, null, "application/json", true)); + + // non-nullable empty string should be converted to "\"\"" (empty json string) + assertEquals("\"\"", apiClient.serializeToString("", null, "application/json", false)); + // nullable empty string should be converted to "\"\"" (empty json string) + assertEquals("\"\"", apiClient.serializeToString("", null, "application/json", true)); + + // non-nullable string "null" should be converted to "\"null\"" + assertEquals("\"null\"", apiClient.serializeToString("null", null, "application/json", false)); + // nullable string "null" should be converted to "\"null\"" + assertEquals("\"null\"", apiClient.serializeToString("null", null, "application/json", true)); + } + +}