13

I'm using Swagger 2.0 and swagger-codegen (actually the swagger-codegen-plugin for Maven) to specify,document and generate an API, with Java as the target language.

The project is already setup to build the server stubs (JAX-RS) and documentation, and Eclipse recognizes the generated code in the project buildPath.

I'm not sure of what is the proper workflow from here. :-/

I don't think I should modify the generated classes, otherwise my changes would be overwritten whenever I change the swagger spec, an I expect it will change as I think more about the API as the development goes on.

What should I do then? Inherit from the generated classes (which ones?) or include them in my own classes?

lpacheco
  • 976
  • 1
  • 14
  • 27
  • 1
    Note that the answer could be different depending on which specific generator you're using. You might want to add the specific generator module to your question. Also, consider using the Java tag, maybe replacing the "war" tag, which probably is not very useful. – Ted Epstein Dec 16 '16 at 19:28
  • Thx, @TedEpstein. Edited as suggested. – lpacheco Dec 16 '16 at 19:31

2 Answers2

4

There are two steps to the solution here.

  1. Add **/*Controller.java or **/*Impl.java to .swagger-codegen-ignore file. Depending on the language used the default implementation is provided in a *Controller.java or *Impl.java file. Once the default implementation is excluded from generation, you can implement the generated interfaces in your own class. The code in your own class will not get refreshed on mvn clean.

  2. .swagger-codegen-ignore file itself is an auto-generated file hence whatever you add in step 1 gets refreshed when you do a mvn clean. To avoid this keep your version of .swagger-codegen-ignore in your resources folder and add the below plugin to your pom, to copy the file at the start of the Maven lifecycle:

    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-resources-plugin</artifactId>
     <executions>
      <execution>
       <id>copy-resources</id>
       <phase>initialize</phase>
       <goals>
        <goal>copy-resources</goal>
       </goals>
       <configuration>
        <outputDirectory>${project.build.directory}/generated/swagger</outputDirectory>
        <resources>
         <resource>
          <directory>${basedir}/src/main/resources</directory>
          <includes>
           <include>.swagger-codegen-ignore</include>
          </includes>
         </resource>
        </resources>
       </configuration>
      </execution>
     </executions>
    </plugin>
Community
  • 1
  • 1
  • Thanks for the answer! That's almost exactly what I did. The difference is that instead of avoid cleaning the `.swagger-codegen-ignore` file I added it as a resource in the project and copy it to the `./target/generated-sources/swagger` directory at the beginning of the build lifecycle. – lpacheco Jan 22 '17 at 08:40
  • Won't the files in generated-sources get deleted the moment you do mvn clean? And how is this supposed to work between multiple developers? Generated sources aren't supposed to be committed to source control. – jbx Feb 12 '18 at 02:38
1

I believe you will need to update the Impl classes, e.g. PetApiServiceImpl.

If you want to skip certain files (e.g. Impl classes) during code regeneration, you can add the files to .swagger-codegen-ignore.

William Cheng
  • 10,137
  • 5
  • 54
  • 79
  • Do you mean I should overwrite the generated code? What happens when I make changes to the swagger spec and regenerate the code? – lpacheco Dec 19 '16 at 11:56
  • 1
    No, I mean you should put the `Impl` classes in .swagger-codegen-ignore so that these files won't be overwritten as you may have already implemented the logic in the `Impl` classes. – William Cheng Dec 19 '16 at 14:08
  • As I understand it, the .swagger-codegen-ignore should be placed in the generated-sources directory. If I do a `mvn clean` that directory is removed, isn't it? Anyway I could place it together with the `some-spec.swagger` file? – lpacheco Dec 19 '16 at 17:33
  • Also, I'm using the swagger-codegen-maven-plugin. Is there a way I could specify the files to skip generation as a configuration of the plugin? – lpacheco Dec 19 '16 at 17:37
  • Ok, so .swagger-codegen-ignore is auto-generated in the target\generated-sources\swagger and I just have to add the files to it, but it doesn't seem to be working. The codegen generates 2 Impl classes. I added one of them to the ignore file and added some random lines to start of both files. After a `mvn compile` both files are brand new. It seems that the codegen overwrote both. I'm using the swagger-codegen-maven-plugin 2.2.2-SNAPSHOT, if that is important. – lpacheco Dec 19 '16 at 17:54