cancel
Showing results for 
Search instead for 
Did you mean: 

Append throws Exception on endpoint definition

Occasional Visitor

Append throws Exception on endpoint definition

Hi,
I have a springboot project and a swagger.yaml file to generate the java code through openapi maven plugin. 
I would like to append "throws Exception" on a method of generated api java interface. I read documentation but I didn't find anything. Is there a way to specify on swagger.yaml file which excpetion should be added to the endpoint?

 

Thanks!

1 REPLY 1
Staff

Re: Append throws Exception on endpoint definition

Hi,

 

Assuming you are referring to codegen 2.x version (latest version is 2.4.0), and to "spring" server generator, atm generated operation methods in interface and/or controller don't throw Exception.

 

There are ongoing efforts and tickets (e.g. https://github.com/swagger-api/swagger-codegen/issues/8755) to allow to add such behaviour as an option (and any help in this or other areas is welcome..), but with current version that is not possible.

 

The best way to achieve what you mention with current version is probably using custom templates (see https://github.com/swagger-api/swagger-codegen#modifying-the-client-library-format), adding the `throw Exception` to the custom template, and passing the `templateDirectory` parameter (https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen-maven-plugin/READ...).

In this case, you would copy the related template dir (https://github.com/swagger-api/swagger-codegen/tree/master/modules/swagger-codegen/src/main/resource...) to a dir of your choice (then referenced in parameter), and modify needed files (in this case `api.mustache` and `apiController.mustache`)

 

E.g.

 

api.mustache line 103

 

{{#jdk8}}default {{/jdk8}}{{#responseWrapper}}{{.}}<{{/responseWrapper}}ResponseEntity<{{>returnTypes}}>{{#responseWrapper}}>{{/responseWrapper}} {{#delegate-method}}_{{/delegate-method}}{{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}},{{/hasMore}}{{/allParams}}) throws Exception{{^jdk8}};{{/jdk8}}{{#jdk8}} {

 

 

New Here?
Join us and watch the welcome video:
Watch the new Interview
Top Kudoed Authors