Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update code generated by Swagger code-gen

I have generated the code from swagger.yaml file using swagger code-gen in spring.

Now I have updated the swagger.yaml file for my API and added few more HTTP operations.

Will it be possible to update the existing code generated previously automatically without merging it manually?

like image 538
Chandresh Mishra Avatar asked Dec 19 '17 11:12

Chandresh Mishra


2 Answers

I guess you are talking about the Controllers generated by codegen, that you have then implemented. They are overwritten after each generation, which means you will have to manually merge the code to add the changes every time... which is really annoying.

Well the best workflow I could find was to use the interfaceOnly option to generate only the model and interface classes in the target directory, and then manually create the controllers that implement those interfaces.

Lets say you update your API specification file with one more GET operation, the interface is regenerated with that new operation and you will just have to adjust your controller to implement that new method (super quick and easy with modern IDE), everything else remain the same and you have more control over your code (splitting controllers in different folders...etc...).

Here is config I used for the plugin:

<plugin>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>2.2.3</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>./api-contract/petstore.yml</inputSpec>
                <language>spring</language>
                <configOptions>
                    <sourceFolder>swagger</sourceFolder>
                    <java8>true</java8>
                    <interfaceOnly>true</interfaceOnly>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>

You can check a complete example project using Spring Boot with swagger-codegen-maven-plugin here.

Cheers

like image 172
Thomas Joeisseint Avatar answered Nov 07 '22 11:11

Thomas Joeisseint


Having the same issue, I found the solution to start with by applying git

  1. commit the current status
  2. run the generator
  3. use git to stage the intended changes but do not stage reversal of your manual edits
  4. commit and continue

I just start with this approach but it seems to work at least for php-slim where only one file (index.php) is changed when regenerating.

like image 35
Bernhard Avatar answered Nov 07 '22 11:11

Bernhard