OpenAPITools / OpenAPITools/openapi-generator

[Java][Spring] Make generated API controller methods throwing Exception.

Open
#1,468 14 comments 24 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Spring allows general error handling based on exceptions. It requires the controller method throws plain Java exceptions. Generated API interface should not restrict this behavior. In other words the code

    @RequestMapping(value = "/users", method = RequestMethod.POST)
    ResponseEntity usersPost(@RequestBody UserCreation userCreationRequest);

should become

    @RequestMapping(value = "/users", method = RequestMethod.POST)
    ResponseEntity usersPost(@RequestBody UserCreation userCreationRequest) throws Exception;

One can make this feature configurable over the configuration parameter. For example in case of Maven plugin:

<supportingFilesToGenerate>ApiUtil.java</supportingFilesToGenerate> 
<controllerThrowsExceptions>java.io.IOException,com.example.NotFoundException</controllerThrowsExceptions> 
openapi-generator version

openapi-generator-maven-plugin, version 3.3.3

Related issues/PRs

Similar request in swagger-codegen project:
https://github.com/swagger-api/swagger-codegen/issues/5686

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 by locating the Java Spring controller generation path and the Maven plugin configuration handling supporting files. Verify how generated controller method declarations are produced, then confirm that the requested exception behavior is configurable and reflected in generated output.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.