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-client] String response body sent as 'application/json' #3623

Open
jmini opened this issue Aug 13, 2019 · 7 comments
Open

[java-client] String response body sent as 'application/json' #3623

jmini opened this issue Aug 13, 2019 · 7 comments

Comments

@jmini
Copy link
Member

jmini commented Aug 13, 2019

While I was working on a test suite for the java clients (I will post my results in #689)

I have noticed that if the server sends a response with Content-Type application/json and following body:

"Hello world"

It gets wrongly de-serialized by the JaxRS based java clients:
expected:<"[Hello world]"> but was:<"\"Hello world\"">

The default java-client (using okhttp and gson) do not have the issue.


Only a quoted string as JSON value used to be illegal (you find a lot of posts telling this), but the newest JSON RFC 7159 says that this is now valid.
The reference website JSONLint also tells that "Hello world" is a valid JSON value.

@auto-labeler
Copy link

auto-labeler bot commented Aug 13, 2019

👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

@jmini
Copy link
Member Author

jmini commented Aug 13, 2019

The java-client with the resttemplate library has the same problem.

@jmini
Copy link
Member Author

jmini commented Aug 13, 2019

One additional test-case, this is also a valid JSON value:

"Check the \"hello\" value"

@lyuanlai
Copy link

lyuanlai commented Feb 20, 2020

I am using openapi-generator 4.0.0-snapshot resttemplate to generate client-side codes. I run into error I suspect is the same root cause as this ticket. The generated code looks like

public RolePermission addRolePermission(String role, String body) throws RestClientException {
      Object postBody = body;
      ....

The endpoint accepts json, and returns json. This code got "Bad Request" from server. If I change to Object postBody = "\"" + body + "\"";, then server processes the request without problem.

Is there a quick fix?

@wing328
Copy link
Member

wing328 commented Feb 20, 2020

4.0.0-snapshot resttemplate t

Please try the latest master to see if the issue still occurs. You can download the SNAPSHOT JAR as mentioned in the readme.

@lyuanlai
Copy link

lyuanlai commented Feb 20, 2020

Using 4.3.0-SNAPSHOT does not fix it, same codes generated. The part of the mustache template for the generated code has not changed in a relevant way:

public ResponseEntity<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {{operationId}}WithHttpInfo({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) throws RestClientException {
        Object postBody = {{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}};

(Manually changing the codes the same way above correctly communicate with server again)

@arminzavada
Copy link

I experienced the same issue with a string bodied request.
I found, that it is because the sendBody function does not check if body is a String, in which case it should convert it to a buffer, and send that, rather then send it as a json

if (body instanceof String) { // added check
    Buffer buffer = Buffer.buffer((String) body);
    request.sendBuffer(buffer, responseHandler);
} else if (body instanceof byte[]) {
    Buffer buffer = Buffer.buffer((byte[]) body);
    request.sendBuffer(buffer, responseHandler);
} else if (body instanceof AsyncFile) {
    AsyncFile file = (AsyncFile) body;
    request.sendStream(file, responseHandler);
} else {
    request.sendJson(body, responseHandler);
}

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

No branches or pull requests

4 participants