OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
20.73k stars 6.32k forks source link

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

Open r-alukhanov opened 5 years ago

r-alukhanov commented 5 years ago

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

ilyas-keser commented 5 years ago

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

mcac0006 commented 5 years ago

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.

ilyas-keser commented 5 years ago

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.

mcac0006 commented 5 years ago

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.

ilyas-keser commented 5 years ago

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

dcassiani commented 4 years ago

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

maximevdk commented 4 years ago

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

mcac0006 commented 4 years ago

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.

purple-dragon commented 4 years ago

@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 commented 4 years ago

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

mcac0006 commented 4 years ago

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.

purple-dragon commented 4 years ago

thank you @mcac0006

reach-kishore commented 4 years ago

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...

tloubrieu-jpl commented 2 months ago

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.