[Java][jersey2] Fix serializeToString (#6956)

* fix empty get body in serializeToString, add tests

* add new file

* fix serialize to better handle null string

* update test comments
This commit is contained in:
William Cheng
2020-07-23 10:39:04 +08:00
committed by GitHub
parent 6e21ca5930
commit 9899315aab
4 changed files with 97 additions and 9 deletions

View File

@@ -791,9 +791,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;
@@ -828,7 +836,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) {