Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java][jersey2] Differentiate request with no body vs request that contains the null value #6634

Closed
Closed
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 @@ -832,7 +832,7 @@ public class ApiClient {
* @return Entity
* @throws ApiException API exception
*/
public Entity<?> serialize(Object obj, Map<String, Object> formParams, String contentType) throws ApiException {
public Entity<?> serialize(BodyHolder obj, Map<String, Object> formParams, String contentType) throws ApiException {
Entity<?> entity;
if (contentType.startsWith("multipart/form-data")) {
MultiPart multiPart = new MultiPart();
Expand All @@ -856,7 +856,7 @@ public class ApiClient {
entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE);
} else {
// We let jersey handle the serialization
entity = Entity.entity(obj == null ? Entity.text("") : obj, contentType);
entity = Entity.entity(obj == null ? Entity.text("") : obj.body, contentType);
}
return entity;
}
Expand All @@ -870,7 +870,7 @@ public class ApiClient {
* @return String
* @throws ApiException API exception
*/
public String serializeToString(Object obj, Map<String, Object> formParams, String contentType) throws ApiException {
public String serializeToString(BodyHolder obj, Map<String, Object> formParams, String contentType) throws ApiException {
try {
if (contentType.startsWith("multipart/form-data")) {
throw new ApiException("multipart/form-data not yet supported for serializeToString (http signature authentication)");
Expand All @@ -886,7 +886,8 @@ public class ApiClient {
return formString.substring(0, formString.length() - 1);
}
} else {
return json.getMapper().writeValueAsString(obj);
// The BodyHolder obj is null if the request has an empty body.
return obj == null ? "" : json.getMapper().writeValueAsString(obj.body);
}
} catch (Exception ex) {
throw new ApiException("Failed to perform serializeToString: " + ex.toString());
Expand Down Expand Up @@ -984,6 +985,19 @@ public class ApiClient {
return File.createTempFile(prefix, suffix, new File(tempFolderPath));
}

/**
* A wrapper class for the request body.
*
* This is needed to differentiate between a request without body (e.g. HTTP GET)
* versus a request with the 'null' value in the body.
*/
public static class BodyHolder {
private Object body;
public BodyHolder(Object body) {
this.body = body;
}
}

/**
* Invoke API by sending HTTP request with the given options.
*
Expand All @@ -1008,7 +1022,7 @@ public class ApiClient {
String path,
String method,
List<Pair> queryParams,
Object body,
BodyHolder body,
Map<String, String> headerParams,
Map<String, String> cookieParams,
Map<String, Object> formParams,
Expand Down Expand Up @@ -1163,7 +1177,7 @@ public class ApiClient {
* @deprecated Add qualified name of the operation as a first parameter.
*/
@Deprecated
public <T> ApiResponse<T> invokeAPI(String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, Map<String, String> cookieParams, Map<String, Object> formParams, String accept, String contentType, String[] authNames, GenericType<T> returnType) throws ApiException {
public <T> ApiResponse<T> invokeAPI(String path, String method, List<Pair> queryParams, BodyHolder body, Map<String, String> headerParams, Map<String, String> cookieParams, Map<String, Object> formParams, String accept, String contentType, String[] authNames, GenericType<T> returnType) throws ApiException {
return invokeAPI(null, path, method, queryParams, body, headerParams, cookieParams, formParams, accept, contentType, authNames, returnType);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ public class {{classname}} {
@Deprecated
{{/isDeprecated}}
public{{/vendorExtensions.x-group-parameters}}{{#vendorExtensions.x-group-parameters}}private{{/vendorExtensions.x-group-parameters}} ApiResponse<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {{operationId}}WithHttpInfo({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) throws ApiException {
Object localVarPostBody = {{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}};
// BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests).
// When the body is set (e.g HTTP POST request), the object maybe have a null value which needs
// to be serialized as the 'null' value.
ApiClient.BodyHolder localVarPostBody = {{#bodyParam}}new ApiClient.BodyHolder({{paramName}}){{/bodyParam}}{{^bodyParam}}null{{/bodyParam}};
{{#allParams}}{{#required}}
// verify the required parameter '{{paramName}}' is set
if ({{paramName}} == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ public String escapeString(String str) {
* @return Entity
* @throws ApiException API exception
*/
public Entity<?> serialize(Object obj, Map<String, Object> formParams, String contentType) throws ApiException {
public Entity<?> serialize(BodyHolder obj, Map<String, Object> formParams, String contentType) throws ApiException {
Entity<?> entity;
if (contentType.startsWith("multipart/form-data")) {
MultiPart multiPart = new MultiPart();
Expand All @@ -773,7 +773,7 @@ public Entity<?> serialize(Object obj, Map<String, Object> formParams, String co
entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE);
} else {
// We let jersey handle the serialization
entity = Entity.entity(obj == null ? Entity.text("") : obj, contentType);
entity = Entity.entity(obj == null ? Entity.text("") : obj.body, contentType);
}
return entity;
}
Expand All @@ -787,7 +787,7 @@ public Entity<?> serialize(Object obj, Map<String, Object> formParams, String co
* @return String
* @throws ApiException API exception
*/
public String serializeToString(Object obj, Map<String, Object> formParams, String contentType) throws ApiException {
public String serializeToString(BodyHolder obj, Map<String, Object> formParams, String contentType) throws ApiException {
Copy link
Contributor Author

@sebastien-rosset sebastien-rosset Jun 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wing328 , I'm a bit confused why updateParamsForAuth invokes serializeToString, but the request body is serialized using a different function. That means an authorization scheme that processes the message body may provide incorrect authorization data.
For example, the calculate message digest may be different because the body is serialized using two different functions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree ideally they should be using the same function to come up with the request body.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks. I propose that we tackle this in a follow-up PR, what do you think? I see some discrepancies between serializeToString and serialize.

try {
if (contentType.startsWith("multipart/form-data")) {
throw new ApiException("multipart/form-data not yet supported for serializeToString (http signature authentication)");
Expand All @@ -803,7 +803,8 @@ public String serializeToString(Object obj, Map<String, Object> formParams, Stri
return formString.substring(0, formString.length() - 1);
}
} else {
return json.getMapper().writeValueAsString(obj);
// The BodyHolder obj is null if the request has an empty body.
return obj == null ? "" : json.getMapper().writeValueAsString(obj.body);
}
} catch (Exception ex) {
throw new ApiException("Failed to perform serializeToString: " + ex.toString());
Expand Down Expand Up @@ -895,6 +896,19 @@ public File prepareDownloadFile(Response response) throws IOException {
return File.createTempFile(prefix, suffix, new File(tempFolderPath));
}

/**
* A wrapper class for the request body.
*
* This is needed to differentiate between a request without body (e.g. HTTP GET)
* versus a request with the 'null' value in the body.
*/
public static class BodyHolder {
private Object body;
public BodyHolder(Object body) {
this.body = body;
}
}

/**
* Invoke API by sending HTTP request with the given options.
*
Expand All @@ -919,7 +933,7 @@ public <T> ApiResponse<T> invokeAPI(
String path,
String method,
List<Pair> queryParams,
Object body,
BodyHolder body,
Map<String, String> headerParams,
Map<String, String> cookieParams,
Map<String, Object> formParams,
Expand Down Expand Up @@ -1072,7 +1086,7 @@ private Response sendRequest(String method, Invocation.Builder invocationBuilder
* @deprecated Add qualified name of the operation as a first parameter.
*/
@Deprecated
public <T> ApiResponse<T> invokeAPI(String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, Map<String, String> cookieParams, Map<String, Object> formParams, String accept, String contentType, String[] authNames, GenericType<T> returnType) throws ApiException {
public <T> ApiResponse<T> invokeAPI(String path, String method, List<Pair> queryParams, BodyHolder body, Map<String, String> headerParams, Map<String, String> cookieParams, Map<String, Object> formParams, String accept, String contentType, String[] authNames, GenericType<T> returnType) throws ApiException {
return invokeAPI(null, path, method, queryParams, body, headerParams, cookieParams, formParams, accept, contentType, authNames, returnType);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ public Client call123testSpecialTags(Client body) throws ApiException {
</table>
*/
public ApiResponse<Client> call123testSpecialTagsWithHttpInfo(Client body) throws ApiException {
Object localVarPostBody = body;
// BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests).
// When the body is set (e.g HTTP POST request), the object maybe have a null value which needs
// to be serialized as the 'null' value.
ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(body);

// verify the required parameter 'body' is set
if (body == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ public void createXmlItem(XmlItem xmlItem) throws ApiException {
</table>
*/
public ApiResponse<Void> createXmlItemWithHttpInfo(XmlItem xmlItem) throws ApiException {
Object localVarPostBody = xmlItem;
// BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests).
// When the body is set (e.g HTTP POST request), the object maybe have a null value which needs
// to be serialized as the 'null' value.
ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(xmlItem);

// verify the required parameter 'xmlItem' is set
if (xmlItem == null) {
Expand Down Expand Up @@ -146,7 +149,10 @@ public Boolean fakeOuterBooleanSerialize(Boolean body) throws ApiException {
</table>
*/
public ApiResponse<Boolean> fakeOuterBooleanSerializeWithHttpInfo(Boolean body) throws ApiException {
Object localVarPostBody = body;
// BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests).
// When the body is set (e.g HTTP POST request), the object maybe have a null value which needs
// to be serialized as the 'null' value.
ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(body);

// create path and map variables
String localVarPath = "/fake/outer/boolean";
Expand Down Expand Up @@ -208,7 +214,10 @@ public OuterComposite fakeOuterCompositeSerialize(OuterComposite body) throws Ap
</table>
*/
public ApiResponse<OuterComposite> fakeOuterCompositeSerializeWithHttpInfo(OuterComposite body) throws ApiException {
Object localVarPostBody = body;
// BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests).
// When the body is set (e.g HTTP POST request), the object maybe have a null value which needs
// to be serialized as the 'null' value.
ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(body);

// create path and map variables
String localVarPath = "/fake/outer/composite";
Expand Down Expand Up @@ -270,7 +279,10 @@ public BigDecimal fakeOuterNumberSerialize(BigDecimal body) throws ApiException
</table>
*/
public ApiResponse<BigDecimal> fakeOuterNumberSerializeWithHttpInfo(BigDecimal body) throws ApiException {
Object localVarPostBody = body;
// BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests).
// When the body is set (e.g HTTP POST request), the object maybe have a null value which needs
// to be serialized as the 'null' value.
ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(body);

// create path and map variables
String localVarPath = "/fake/outer/number";
Expand Down Expand Up @@ -332,7 +344,10 @@ public String fakeOuterStringSerialize(String body) throws ApiException {
</table>
*/
public ApiResponse<String> fakeOuterStringSerializeWithHttpInfo(String body) throws ApiException {
Object localVarPostBody = body;
// BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests).
// When the body is set (e.g HTTP POST request), the object maybe have a null value which needs
// to be serialized as the 'null' value.
ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(body);

// create path and map variables
String localVarPath = "/fake/outer/string";
Expand Down Expand Up @@ -393,7 +408,10 @@ public void testBodyWithFileSchema(FileSchemaTestClass body) throws ApiException
</table>
*/
public ApiResponse<Void> testBodyWithFileSchemaWithHttpInfo(FileSchemaTestClass body) throws ApiException {
Object localVarPostBody = body;
// BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests).
// When the body is set (e.g HTTP POST request), the object maybe have a null value which needs
// to be serialized as the 'null' value.
ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(body);

// verify the required parameter 'body' is set
if (body == null) {
Expand Down Expand Up @@ -459,7 +477,10 @@ public void testBodyWithQueryParams(String query, User body) throws ApiException
</table>
*/
public ApiResponse<Void> testBodyWithQueryParamsWithHttpInfo(String query, User body) throws ApiException {
Object localVarPostBody = body;
// BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests).
// When the body is set (e.g HTTP POST request), the object maybe have a null value which needs
// to be serialized as the 'null' value.
ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(body);

// verify the required parameter 'query' is set
if (query == null) {
Expand Down Expand Up @@ -530,7 +551,10 @@ public Client testClientModel(Client body) throws ApiException {
</table>
*/
public ApiResponse<Client> testClientModelWithHttpInfo(Client body) throws ApiException {
Object localVarPostBody = body;
// BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests).
// When the body is set (e.g HTTP POST request), the object maybe have a null value which needs
// to be serialized as the 'null' value.
ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(body);

// verify the required parameter 'body' is set
if (body == null) {
Expand Down Expand Up @@ -624,7 +648,10 @@ public void testEndpointParameters(BigDecimal number, Double _double, String pat
</table>
*/
public ApiResponse<Void> testEndpointParametersWithHttpInfo(BigDecimal number, Double _double, String patternWithoutDelimiter, byte[] _byte, Integer integer, Integer int32, Long int64, Float _float, String string, File binary, LocalDate date, OffsetDateTime dateTime, String password, String paramCallback) throws ApiException {
Object localVarPostBody = null;
// BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests).
// When the body is set (e.g HTTP POST request), the object maybe have a null value which needs
// to be serialized as the 'null' value.
ApiClient.BodyHolder localVarPostBody = null;

// verify the required parameter 'number' is set
if (number == null) {
Expand Down Expand Up @@ -747,7 +774,10 @@ public void testEnumParameters(List<String> enumHeaderStringArray, String enumHe
</table>
*/
public ApiResponse<Void> testEnumParametersWithHttpInfo(List<String> enumHeaderStringArray, String enumHeaderString, List<String> enumQueryStringArray, String enumQueryString, Integer enumQueryInteger, Double enumQueryDouble, List<String> enumFormStringArray, String enumFormString) throws ApiException {
Object localVarPostBody = null;
// BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests).
// When the body is set (e.g HTTP POST request), the object maybe have a null value which needs
// to be serialized as the 'null' value.
ApiClient.BodyHolder localVarPostBody = null;

// create path and map variables
String localVarPath = "/fake";
Expand Down Expand Up @@ -792,7 +822,10 @@ public ApiResponse<Void> testEnumParametersWithHttpInfo(List<String> enumHeaderS
}

private ApiResponse<Void> testGroupParametersWithHttpInfo(Integer requiredStringGroup, Boolean requiredBooleanGroup, Long requiredInt64Group, Integer stringGroup, Boolean booleanGroup, Long int64Group) throws ApiException {
Object localVarPostBody = null;
// BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests).
// When the body is set (e.g HTTP POST request), the object maybe have a null value which needs
// to be serialized as the 'null' value.
ApiClient.BodyHolder localVarPostBody = null;

// verify the required parameter 'requiredStringGroup' is set
if (requiredStringGroup == null) {
Expand Down Expand Up @@ -989,7 +1022,10 @@ public void testInlineAdditionalProperties(Map<String, String> param) throws Api
</table>
*/
public ApiResponse<Void> testInlineAdditionalPropertiesWithHttpInfo(Map<String, String> param) throws ApiException {
Object localVarPostBody = param;
// BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests).
// When the body is set (e.g HTTP POST request), the object maybe have a null value which needs
// to be serialized as the 'null' value.
ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(param);

// verify the required parameter 'param' is set
if (param == null) {
Expand Down Expand Up @@ -1055,7 +1091,10 @@ public void testJsonFormData(String param, String param2) throws ApiException {
</table>
*/
public ApiResponse<Void> testJsonFormDataWithHttpInfo(String param, String param2) throws ApiException {
Object localVarPostBody = null;
// BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests).
// When the body is set (e.g HTTP POST request), the object maybe have a null value which needs
// to be serialized as the 'null' value.
ApiClient.BodyHolder localVarPostBody = null;

// verify the required parameter 'param' is set
if (param == null) {
Expand Down Expand Up @@ -1136,7 +1175,10 @@ public void testQueryParameterCollectionFormat(List<String> pipe, List<String> i
</table>
*/
public ApiResponse<Void> testQueryParameterCollectionFormatWithHttpInfo(List<String> pipe, List<String> ioutil, List<String> http, List<String> url, List<String> context) throws ApiException {
Object localVarPostBody = null;
// BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests).
// When the body is set (e.g HTTP POST request), the object maybe have a null value which needs
// to be serialized as the 'null' value.
ApiClient.BodyHolder localVarPostBody = null;

// verify the required parameter 'pipe' is set
if (pipe == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ public Client testClassname(Client body) throws ApiException {
</table>
*/
public ApiResponse<Client> testClassnameWithHttpInfo(Client body) throws ApiException {
Object localVarPostBody = body;
// BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests).
// When the body is set (e.g HTTP POST request), the object maybe have a null value which needs
// to be serialized as the 'null' value.
ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(body);

// verify the required parameter 'body' is set
if (body == null) {
Expand Down
Loading