Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -791,9 +791,17 @@ public Entity<?> serialize(Object obj, Map<String, Object> 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;
Expand Down Expand Up @@ -828,7 +836,7 @@ public String serializeToString(Object obj, Map<String, Object> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -870,9 +870,17 @@ public Entity<?> serialize(Object obj, Map<String, Object> 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;
Expand Down Expand Up @@ -907,7 +915,7 @@ public String serializeToString(Object obj, Map<String, Object> 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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}

}