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

BUG [JAVA] Generated java client code from openapi-generator-maven-plugin using okhttp-gson library failing to serialize RequestBody with contentType text/plain #9734

Closed
4 of 6 tasks
prkumarm opened this issue Jun 9, 2021 · 1 comment · Fixed by #10885

Comments

@prkumarm
Copy link

prkumarm commented Jun 9, 2021

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Generated java client code from openapi-generator-maven-plugin using okhttp-gson library failing to serialize RequestBody with contentType text/plain.

When using httpok library(okhttp-gson) openapi-generator throws ApiException with message "Content type text/plain is not supported" for contentType text/plain.

I see below related mustache template code in serialize(Object obj, String contentType) method in ApiClient.mustache
https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache

Is this a expected behavior? if so, what is the work around to serialize RequestBody of type text/plain

openapi-generator version

5.1.0

OpenApi generator dependency
org.openapitools
openapi-generator-maven-plugin
4.3.1

okhttp dependency -

com.squareup.okhttp3
okhttp

OpenAPI declaration file content or url

N/A

Generation Details

Please refer steps defined below in "Steps to reproduce" section

Steps to reproduce

Step1 - Springfox generated contract with contentType text/plain

"/endPoint" : {
"post" : {
"tags" : [ "controller" ],
"summary" : "summary",
"operationId" : "operationId",
"requestBody" : {
"content" : {
"application/json" : {
"schema" : {
"type" : "string"
}
},
"text/plain" : {
"schema" : {
"type" : "string"
}
}
}
}

Step 2:
Generate client code using below Maven plugin for okhttp-gson library,
org.openapitools
openapi-generator-maven-plugin
4.3.1

Step3.
Invoke rest endpoint using generated client code.

Output: Below error message is thrown by the generatened ApiClient class in serialize method.

"Content type text/plain is not supported" for contentType text/plain"

Related issues/PRs

NA

Suggest a fix

NA

@prkumarm prkumarm changed the title [JAVA] Generated java client code from openapi-generator-maven-plugin using okhttp-gson library failing to serialize RequestBody with contentType text/plain BUG [JAVA] Generated java client code from openapi-generator-maven-plugin using okhttp-gson library failing to serialize RequestBody with contentType text/plain Jun 9, 2021
@fabiohecht
Copy link

fabiohecht commented Sep 30, 2021

Here is a workaround:

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import javax.activation.MimeType;
import javax.activation.MimeTypeParseException;
import okhttp3.RequestBody;

public class MyApiClient extends ApiClient {
    @Override
    public RequestBody serialize(Object obj, String contentType) throws ApiException {

        try {
            MimeType contentMimeType = new MimeType(contentType);

            if ("text/plain".equals(contentMimeType.getBaseType()) && obj instanceof String) {
                obj = ((String) obj).getBytes(getCharset(contentMimeType));
            }

        } catch (MimeTypeParseException e) {
            throw new ApiException("Can't parse mime type: " + contentType);
        }

        return super.serialize(obj, contentType);
    }

    private Charset getCharset(MimeType contentMimeType) {
        String charsetParameter = contentMimeType.getParameter("charset");
        if (null == charsetParameter) {
            return StandardCharsets.UTF_8;
        }
        return Charset.forName(charsetParameter);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants