Skip to content

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

@FutureGadget

Description

@FutureGadget
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);
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions