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

ApiUtil class does not support UTF-8 response by default. #1463

Closed
FutureGadget opened this issue Nov 16, 2018 · 1 comment
Closed

ApiUtil class does not support UTF-8 response by default. #1463

FutureGadget opened this issue Nov 16, 2018 · 1 comment

Comments

@FutureGadget
Copy link

Description

ApiUtil class does not support UTF-8 response by default.

openapi-generator version

3.3.0

OpenAPI declaration file content or url
Command line used for generation

openapi-generator generate -i {filename} -g spring

Steps to reproduce
  1. write a spec including utf-8 chars in the response example

  2. generate spiring code using the command
    openapi-generator generate -i {filename} -g spring

  3. Run the spinrg application and test the API generated by the openapi-generator
    by requesting through swagger-ui web page.

  4. The response will be 500 error, and you will get the Caused by: java.io.CharConversionException: Not an ISO 8859-1 character: [거]
    ( "거" is the utf-8 character(Korean) that caused this problem )

Related issues/PRs
Suggest a fix/enhancement

The generated code in the ApiUtil.java should be modified as follows:
Before:

public static void setExampleResponse(NativeWebRequest req, String contentType, String example) {
        try {
            req.getNativeResponse(HttpServletResponse.class).addHeader("Content-Type", contentType);
            req.getNativeResponse(HttpServletResponse.class).getOutputStream().print(example);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

After:

public static void setExampleResponse(NativeWebRequest req, String contentType, String example) {
        try {
            req.getNativeResponse(HttpServletResponse.class).addHeader("Content-Type", contentType);
            req.getNativeResponse(HttpServletResponse.class).getWriter().print(example);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
@wing328
Copy link
Member

wing328 commented Nov 16, 2018

@FutureGadget thanks for reporting the issue. Can you file a PR so that we can review the change more easily?

Also please share the spec to reproduce the issue.

We do have test cases to cover double-byte characters but clearly it didn't catch the issue you mentioned so we probably need to update it to extend the coverage: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml#L692

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

No branches or pull requests

2 participants