OpenAPITools / OpenAPITools/openapi-generator

[BUG] [JAVA] jersey2 client cannot get response body from error response

Open
#12,969 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • 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
  1. Create an API client with -g java --additional-properties=library=jersey2.
  2. Make sure the API you are calling returns an error response with a json body.
  3. Catch the exception and try to access the message field
  4. 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));

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the generated jersey2 client’s ApiClient.java around line 1166, where the error response body is read. Reproduce a JSON error response using a client generated with the stated jersey2 option and inspect the caught ApiException message. Done means the exception exposes the response body instead of only the default "error" string.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.