OpenAPITools / OpenAPITools/openapi-generator

[REQ][Java][resttemplate] Being able to override response handling for a better error management

Open
#5,397 9 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Is your feature request related to a problem? Please describe.

restTemplate generated ApiClient handles response this way :

        if (responseEntity.getStatusCode().is2xxSuccessful()) {
            return responseEntity;
        } else {
            // The error handler built into the RestTemplate should handle 400 and 500 series errors.
            throw new RestClientException("API returned " + responseEntity.getStatusCode() + " and it wasn't handled by the RestTemplate error handler");
        }

It means, either we have a success response which is return with the right type, either we get a generic response with interesting information hidden in its message.

Describe the solution you'd like

Some methods from ApiClient are defined as private and others as protected which means it is meant to be extended.
It would be great to be able to override the invokeAPI method but, some method used in it are private and can't be used from child class. I don't want to copy/paste most of the class, i'm just interested in the response handling.

One solution: we could move response handling to a protected method :

protected <T> ResponseEntity<T> handleResponse(ResponseEntity<T> responseEntity) {
	if (responseEntity.getStatusCode().is2xxSuccessful()) {
            return responseEntity;
        } else {
            // The error handler built into the RestTemplate should handle 400 and 500 series errors.
            throw new RestClientException("API returned " + responseEntity.getStatusCode() + " and it wasn't handled by the RestTemplate error handler");
        }
}

so that, child class could be able to handle response in the way they want (store response status code in a handy way, even go further by handling error response body to extract error key and then throw specific business exception.

Describe alternatives you've considered

Other solution: from the existing private invokeApi, throw a newly defined error exception extending RestClientException, that contains status code and response body (like what is done with other generators, like jersey for example)

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 Java resttemplate ApiClient and trace invokeAPI, focusing on the private methods involved in response handling. Compare the proposed protected handleResponse approach with the alternative exception approach; done means callers can override or otherwise access response status and body details without copying most of ApiClient.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, spring
Domain
api, backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.