ApiUtil class does not support UTF-8 response by default.
Created by: 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
-
write a spec including utf-8 chars in the response example
-
generate spiring code using the command
openapi-generator generate -i {filename} -g spring
-
Run the spinrg application and test the API generated by the openapi-generator by requesting through swagger-ui web page.
-
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);
}
}