[BUG] [JAVA] jersey2 client cannot get response body from error response
Bug Report Checklist
- [ ] Have you provided a full/minimal spec to reproduce the issue?
- [x] Have you validated the input using an OpenAPI validator (example)?
- [x] Have you tested with the latest master to confirm the issue still exists?
- [x] Have you searched for related issues/PRs?
- [x] What's the actual output vs expected output?
- [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
When you are using the java jersey2 client and you get back an error response (500 for example), an ApiException is thrown. This exception should have the error response body in the message field, but instead it contains the default string "error". An exception is thrown in the code that tries to deserialize the json error response body.
Method threw 'javax.ws.rs.ProcessingException' exception. RESTEASY003145: Unable to find a MessageBodyReader of content-type application/json;charset=utf-8 and type class java.lang.String
It is highly valuable to have the response body, so you know why the server gave back an error.
openapi-generator version
I've used version 6.0.0 of the generator, but also tried the latest master (so 7.0.0)
Generation Details
-g java --additional-properties=library=jersey2
Steps to reproduce
- Create an API client with -g java --additional-properties=library=jersey2.
- Make sure the API you are calling returns an error response with a json body.
- Catch the exception and try to access the message field
- The fields only contains the String "error" instead of the body.
Related issues/PRs
#5609 is a related PR. This PR proposes a more sophisticated fix. I'm proposing to at least get the error body back in String format, which the code is trying to do already anyway.
Suggest a fix
The cause is this line (around 1166) in ApiClient.java:
respBody = String.valueOf(response.readEntity(String.class));
Which gives the exception: Method threw 'javax.ws.rs.ProcessingException' exception. RESTEASY003145: Unable to find a MessageBodyReader of content-type application/json;charset=utf-8 and type class java.lang.String
This should instead be:
respBody = String.valueOf(response.readEntity(Object.class));
Any Update on this??
Hoping it helps someone. We had the same problem using OpenAPI generator 7.2.0 with option "library": "jersey2"`
In the ApiClient class, buildHttpClient() method, the generator uses the ClientBuilder class from javax.ws.rs.client:
ClientBuilder clientBuilder = ClientBuilder.newBuilder();
In the newBuilder() method, FactoryFinder.find() is used to find and return the default ClientBuilder implementation. If you have a RESTEasy dependency, it will return a RESTEasy client.
To ensure using a jersey client, and solve the problem, changeClientBuilder clientBuilder = ClientBuilder.newBuilder(); to JerseyClientBuilder clientBuilder = new JerseyClientBuilder();