Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swagger codegen is overwriting my custom code in generated files

I used the swagger codegen to generate jaxrs server side classes and also client side java classes.

This is the command I used to generate the classes

java -jar modules/swagger-codegen-distribution/target/swagger-codegen-distribution-2.1.2-M1.jar   -i /Users/me/Workspace/swagger-codegen/samples/yaml/echo.yaml   -l jaxrs   -o samples/server/echo/java

The server code that was generated had a place holder to write my "magic".

public Response echo(@ApiParam(value = ""  )@HeaderParam("headerParam") String headerParam,
    @ApiParam(value = "",required=true) @QueryParam("message") String message)
      throws NotFoundException {
      // do some magic!
      return Response.ok().entity(new ApiResponseMessage(ApiResponseMessage.OK, "magic!")).build();
  }

I added the "magic" in the "echo" method and regenerated the code, only to see it wiped out. One way to avoid losing custom code is to modify the codegen template to generate interface instead of a class. Then I can have all the custom code in the implemented class.

I am trying to find out if there is a way I can preserve the custom "magic" even after regenerating the code or if there is a better way of handling this situation than changing the template to generate interfaces instead of classes.

like image 698
Nikhil S Avatar asked Mar 25 '15 10:03

Nikhil S


People also ask

What is swagger codegen ignore?

The latest master of Swagger Codegen allows you to specify files not to be overwritten in . swagger-codegen-ignore (similar to . gitignore) during code generation.

What is the use of swagger codegen?

Swagger Codegen is an open source project which allows generation of API client libraries (SDK generation), server stubs, and documentation automatically from an OpenAPI Specification.


1 Answers

The latest master of Swagger Codegen allows you to specify files not to be overwritten in .swagger-codegen-ignore (similar to .gitignore) during code generation.

Please pull the latest master of Swagger Codegen to give it a try.

UPDATE: On May 2018, about 50 top contributors and template creators of Swagger Codegen decided to fork Swagger Codegen to maintain a community-driven version called OpenAPI Generator. Please refer to the Q&A for more information.

like image 125
William Cheng Avatar answered Sep 30 '22 18:09

William Cheng