swagger-api / swagger-api/swagger-codegen
Default Java client does not support Content-Type "application/xml"
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
I have a swagger.yaml with a post method that produces application/json and which consumes XML. Here's an excerpt:
post:
summary: Store a model.
description: Stores the XML content of the specified model.
consumes:
- application/xml;charset=UTF-8
parameters:
- name: model
in: body
description: The model XML you want to store
schema:
type: string
required: true
I generated the Java client from the swagger.yaml using swagger-codegen-cli-2.2.1.jar and the --lang java option. I did not specify a --library option. (I doubt it's relevant but, just in case, I also specified these options in an associated json config file: apiPackage, groupId, artifactId and artifactVersion.)
However when I issue a POST request with an XML string as input I get this error in the client:
com.mycompany.myapi.ApiException: Content type "application/xml;charset=UTF-8" is not supported
at com.mycompany.myapi.ApiClient.serialize(ApiClient.java:889)
at com.mycompany.myapi.ApiClient.buildCall(ApiClient.java:1105)
at com.mycompany.myapi.client.DefaultApi.modelsPostCall(DefaultApi.java:523)
at com.mycompany.myapi.client.DefaultApi.modelsPostWithHttpInfo(DefaultApi.java:544)
The generated method in com.mycompany.myapi.ApiClient looks like this:
/**
* Serialize the given Java object into request body according to the object's
* class and the request Content-Type.
*
* @param obj The Java object
* @param contentType The request Content-Type
* @return The serialized request body
* @throws ApiException If fail to serialize the given object
*/
public RequestBody serialize(Object obj, String contentType) throws ApiException {
if (obj instanceof byte[]) {
// Binary (byte array) body parameter support.
return RequestBody.create(MediaType.parse(contentType), (byte[]) obj);
} else if (obj instanceof File) {
// File body parameter support.
return RequestBody.create(MediaType.parse(contentType), (File) obj);
} else if (isJsonMime(contentType)) {
String content;
if (obj != null) {
content = json.serialize(obj);
} else {
content = null;
}
return RequestBody.create(MediaType.parse(contentType), content);
} else {
throw new ApiException("Content type \"" + contentType + "\" is not supported");
}
}
In my case obj is a String. It looks like you don't support application\xml. Is this true? If so, how soon can we expect a fix please? Alternatively can you recommend a workaround I can use? I don't mind hacking the generated code to make it work until a proper fix is released.
Related issues
Is this related? http://stackoverflow.com/questions/33937787/how-to-write-swagger-api-that-accepts-xml-in-request-body . @wing328 suggested @splintor create a new issue but I can't tell whether that ever happened.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the generated ApiClient.java serialize method shown in the report and compare its handling of application/json with application/xml;charset=UTF-8. Reproduce the POST from the swagger.yaml excerpt using swagger-codegen-cli 2.2.1, then verify that the generated client accepts the XML request body without raising the unsupported-content-type error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- api
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100