openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

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

Open r-alukhanov opened this issue 7 years ago • 14 comments

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

r-alukhanov avatar Nov 16 '18 11:11 r-alukhanov

Is there any workaround available to make generated API controller methods throwing Exception?

ilyas-keser avatar Jun 12 '19 09:06 ilyas-keser

I have applied something very similar to this implementation and filed a pull request, but it seems to have grinded to a halt.

Check it out here: https://github.com/OpenAPITools/openapi-generator/pull/2482

We can't use @ControllerAdvice while OpenAPI forcefully avoids declaring a throwable in the signatures.

mcac0006 avatar Jun 12 '19 09:06 mcac0006

I consider to not use OpenApi in Spring Boot projects because of the code generator. If it forces you to write bad code, avoid it.

ilyas-keser avatar Jun 12 '19 09:06 ilyas-keser

I don't think that is a generally good idea - the code generator is an enabler to consume the OpenAPI spec to your language/framework of preference. It is why there are various generators from which you could choose from, improve upon, or even create one of your own.

It's not like there is another true alternative to OpenAPI (RAML has lost the game for a long time now…).

The pull request (https://github.com/OpenAPITools/openapi-generator/pull/2482) I have filed does that: it amends the Spring code generator to be able to declare Throwables should you need to, and it works perfectly fine.

mcac0006 avatar Jun 12 '19 09:06 mcac0006

But your pull request from march 2019 is still not merged ;)

ilyas-keser avatar Jun 12 '19 10:06 ilyas-keser

Is there any implementation of this "controllerThrowsExceptions" on Gradle? Can't find it over the documentation...

dcassiani avatar Oct 12 '19 17:10 dcassiani

@r-alukhanov I can find your improvement in version 4.0.3 but they must have removed it in the latest version?

maximevdk avatar Nov 06 '19 17:11 maximevdk

Hi,

This feature has finally been merged a few months ago to master (https://github.com/OpenAPITools/openapi-generator/pull/2482) using the unhandledException flag.

mcac0006 avatar Nov 07 '19 08:11 mcac0006

@mcac0006 unhandledException looks a global flag ? which will generate "throws Exception" for all APIs? if so, is there a way to tell generator which API need throws, which API doesn't need it?

purple-dragon avatar Dec 17 '19 08:12 purple-dragon

https://github.com/swagger-api/swagger-codegen/pull/7437 there is one related issue to remove "throws Exception" F.Y.I.

purple-dragon avatar Dec 17 '19 08:12 purple-dragon

Hi @purple-dragon,

I don't think there is that level of granularity in the codegen, besides declaring the operationId inside the spec. Therefore, unhandledException would be inherited to all endpoints.

mcac0006 avatar Dec 17 '19 08:12 mcac0006

thank you @mcac0006

purple-dragon avatar Dec 17 '19 08:12 purple-dragon

My 2 cents .... How about defining and throwing RunTimeExceptions as per the need ?... This way, we do not need the generated API Controller to declare an exception, but still the ControllerAdvice can be used with the ExceptionHandler for the RunTimeException.... If both OpenApi and Global Exception handling has to go hand in hand, then this may be considered...

reach-kishore avatar Mar 02 '20 22:03 reach-kishore

I like the latest proposal of @reach-kishore . Any updates on that ? I am currently blocked in my attempt to implement ControllerAdvice in my application because of that.

tloubrieu-jpl avatar Apr 30 '24 23:04 tloubrieu-jpl