Open brianluisgomez opened 6 months ago
Here is the generated code in the API call itself. Toward the end you'll see the selectHeaderContentType
method being called. This is where the content-type gets overwritten
public okhttp3.Call uploadMediaCall(String accountId, String mediaId, File body, String contentType, String cacheControl, final ApiCallback _callback) throws ApiException {
String basePath = null;
// Operation Servers
String[] localBasePaths = new String[] { "https://messaging.bandwidth.com/api/v2" };
// Determine Base Path to Use
if (localCustomBaseUrl != null){
basePath = localCustomBaseUrl;
} else if ( localBasePaths.length > 0 ) {
basePath = localBasePaths[localHostIndex];
} else {
basePath = null;
}
Object localVarPostBody = body;
// create path and map variables
String localVarPath = "/users/{accountId}/media/{mediaId}"
.replace("{" + "accountId" + "}", localVarApiClient.escapeString(accountId.toString()))
.replace("{" + "mediaId" + "}", localVarApiClient.escapeString(mediaId.toString()));
List<Pair> localVarQueryParams = new ArrayList<Pair>();
List<Pair> localVarCollectionQueryParams = new ArrayList<Pair>();
Map<String, String> localVarHeaderParams = new HashMap<String, String>();
Map<String, String> localVarCookieParams = new HashMap<String, String>();
Map<String, Object> localVarFormParams = new HashMap<String, Object>();
if (contentType != null) {
localVarHeaderParams.put("Content-Type", localVarApiClient.parameterToString(contentType));
}
if (cacheControl != null) {
localVarHeaderParams.put("Cache-Control", localVarApiClient.parameterToString(cacheControl));
}
final String[] localVarAccepts = {
"application/json"
};
final String localVarAccept = localVarApiClient.selectHeaderAccept(localVarAccepts);
if (localVarAccept != null) {
localVarHeaderParams.put("Accept", localVarAccept);
}
final String[] localVarContentTypes = {
"application/json",
"application/ogg",
"application/pdf",
"application/rtf",
"application/zip",
"application/x-tar",
"application/xml",
"application/gzip",
"application/x-bzip2",
"application/x-gzip",
"application/smil",
"application/javascript",
"audio/mp4",
"audio/mpeg",
"audio/ogg",
"audio/flac",
"audio/webm",
"audio/wav",
"audio/amr",
"audio/3gpp",
"image/bmp",
"image/gif",
"image/jpeg",
"image/pjpeg",
"image/png",
"image/svg+xml",
"image/tiff",
"image/webp",
"image/x-icon",
"text/css",
"text/csv",
"text/calendar",
"text/plain",
"text/javascript",
"text/vcard",
"text/vnd.wap.wml",
"text/xml",
"video/avi",
"video/mp4",
"video/mpeg",
"video/ogg",
"video/quicktime",
"video/webm",
"video/x-ms-wmv"
};
final String localVarContentType = localVarApiClient.selectHeaderContentType(localVarContentTypes);
if (localVarContentType != null) {
localVarHeaderParams.put("Content-Type", localVarContentType);
}
String[] localVarAuthNames = new String[] { "Basic" };
return localVarApiClient.buildCall(basePath, localVarPath, "PUT", localVarQueryParams, localVarCollectionQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAuthNames, _callback);
}
Possibly the same issue: ...
get:
tags:
- Anotace
summary: ....
operationId: getPlainStringOperation
responses:
"200":
description: ...
content:
text/plain; charset=utf-8:
schema:
type: string
leads to:
final String[] localVarAccepts = {
"text/plain; charset=utf-8", "application/json"
};
and results in:
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type
java.lang.Stringfrom Array value (token
JsonToken.START_ARRAY)
So in my case it's not a bug apparently. It added the "application/json" because (here missing) 400 and 500 responses containing structured JSON. However the API client should handle 200 as non JSON payload. Helped me to add the converted in front of the Jackson one.
@Bean
public ApiClient apiClient(RestTemplate restTemplate) {
restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter());
return new ApiClient(restTemplate).setBasePath(wsUrl);
}
Bug Report Checklist
Description
We have a spec where one of the request bodies allows for multiple content-types. If a user specifies a particular content type, say
image/jpeg
, the generated SDK will be default overwrite that content type always toapplication/json
with aselectHeaderContentType
function defined below. This should be changed so that passed in Content-Types are not overwritten by this generated function. I couldn't figure out any way to override this with mustache templates since I had no way to pull in the content-type type defined by the user when using the SDKopenapi-generator version
OpenAPI declaration file content or url
Here is a minimal version that just includes the part that's breaking
Steps to reproduce
Reproducible by using the generated SDK and designating a specific content-type in the UploadMedia call. If you check what content-type header is actually being sent out it is different from what was specified by the user