Open r-alukhanov opened 5 years ago
Is there any workaround available to make generated API controller methods throwing Exception?
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.
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.
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.
But your pull request from march 2019 is still not merged ;)
Is there any implementation of this "controllerThrowsExceptions" on Gradle? Can't find it over the documentation...
@r-alukhanov I can find your improvement in version 4.0.3 but they must have removed it in the latest version?
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 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?
https://github.com/swagger-api/swagger-codegen/pull/7437 there is one related issue to remove "throws Exception" F.Y.I.
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.
thank you @mcac0006
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...
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.
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
should become
One can make this feature configurable over the configuration parameter. For example in case of Maven plugin:
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