swagger-api / swagger-api/swagger-codegen

[Java] String as POST body not supported

Open
#8,224 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

Code generator for java language produces a client which does not support using a POST method with a String body and TEXT_PLAIN media type. This can be seen in the serialize method in the generated ApiClient.java:

    /**
     * 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");
        }
    }

As you can see, if the body (obj) is neither a byte[] nor File, and the content type is not a json type, we go to the exception case.

This file comes from the okhttp-gson version of ApiClient.mustache, which is the default.
I propose that we change that file to have the same serialize method as the generic Java one (which is also the same as e.g. the resteasy version). I suspect the code above is just an old leftover that was already fixed for the other Java library versions.

Would a PR to that effect be accepted?

Swagger-codegen version

This affects 2.3.1, as well as the latest master (2.4.0-SNAPSHOT).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache and inspect its serialize method. Compare it with modules/swagger-codegen/src/main/resources/Java/ApiClient.mustache and the other Java library templates mentioned in the issue. Done means the generated okhttp-gson Java client supports a String POST body with a text/plain media type instead of raising the unsupported-content-type exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.