how to avoid default method implementation in swagger codegen interface?
Asked Answered
R

5

14

I would like to avoid "default" implementation in interface generate by the maven plugin swagger codegen. For example, with petstore swagger : http://petstore.swagger.io/v2/swagger.json

I generate interface with maven 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>./src/main/resources/swagger/api.yml</inputSpec>
                        <language>spring</language>
                        <generateSupportingFiles>false</generateSupportingFiles>
                        <configOptions>
                            <interfaceOnly>true</interfaceOnly>
                            <java8>true</java8>
                        </configOptions>
                    </configuration>
                </execution>
            </executions>
        </plugin>

I generate interface like PetApi.java with default implementation of methods :

    default ResponseEntity<Void> addPet(@ApiParam(value = "Pet object that needs to be added to the store" ,required=true )  @Valid @RequestBody Pet body) {
    // do some magic!
    return new ResponseEntity<Void>(HttpStatus.OK);
    }

I would like to avoid it like

    ResponseEntity<Void> addPet(@ApiParam(value = "Pet object that needs to be added to the store" ,required=true )  @Valid @RequestBody Pet body);

Is it possible to do it ?

Update March 2020:

According to new OpenAPI Tool openapi-generator
There is an option with spring (language is DEPRECATED, use generatorName)

skipDefaultInterface

https://github.com/OpenAPITools/openapi-generator/blob/master/docs/generators/spring.md

Runnel answered 4/5, 2018 at 15:30 Comment(0)
O
7

With a "spring" language the "java8" parameter is used both to signal use of the default interface and to signal general use of Java8, e.g. when the Java8 date library is used.
Related tickets:
https://github.com/swagger-api/swagger-codegen/issues/8045
https://github.com/swagger-api/swagger-codegen/issues/5614

Offstage answered 13/9, 2018 at 7:14 Comment(0)
C
6

As per documentation the following should solve it:

<configOptions>
     <skipDefaultInterface>true</skipDefaultInterface>
</configOptions>
Contumacious answered 11/2, 2022 at 9:51 Comment(1)
github.com/swagger-api/swagger-codegen/issues/8378 Didn't work for me. There's open issue for this.Fairbanks
A
4

I resolved configuring two executions of same plugin. One with configuration to generate only the model classes (java8=true and dateLibrary=java8-localdatetime) and another to generate only the api interfaces (java=false and dateLibrary empty).

<plugin>
   <groupId>io.swagger.codegen.v3</groupId>
   <artifactId>swagger-codegen-maven-plugin</artifactId>
   <version>3.0.8</version>
   <executions>
      <execution>
         <id>gera-api-model</id>
         <goals>
            <goal>generate</goal>
         </goals>
         <configuration>
            <inputSpec>${project.basedir}/src/main/openapi-spec/openapi.yaml</inputSpec>
            <language>spring</language>

            <generateModels>true</generateModels>
            <generateApis>false</generateApis>
            <configOptions>
               <dateLibrary>java8-localdatetime</dateLibrary>
               <java8>true</java8> 
             </configOptions>
         </configuration>
      </execution>
      <execution>
         <id>gera-api-interface</id>
         <goals>
            <goal>generate</goal>
         </goals>
         <configuration>
            <inputSpec>${project.basedir}/src/main/openapi-spec/openapi.yaml</inputSpec>
            <language>spring</language>
            <generateModels>false</generateModels>
            <generateApis>true</generateApis>
            <configOptions>
               <java8>false</java8>
            </configOptions>
         </configuration>
      </execution>
   </executions>
   <configuration>
      <inputSpec>${project.basedir}/src/main/openapi-spec/openapi.yaml</inputSpec>
      <language>spring</language>
      <output>${project.build.directory}/generated-sources</output>
      <apiPackage>br.com.acme.myproject.api</apiPackage>
      <modelPackage>br.com.acme.myproject.model</modelPackage>
      <library>spring-mvc</library>
      <generateApiDocumentation>false</generateApiDocumentation>
      <generateModelDocumentation>false</generateModelDocumentation>
      <generateSupportingFiles>false</generateSupportingFiles>
      <generateApiTests>false</generateApiTests>
      <generateModelTests>false</generateModelTests>
      <configOptions>
         <bigDecimalAsString>true</bigDecimalAsString>
         <serializableModel>true</serializableModel>
         <reactive>false</reactive>
         <interfaceOnly>true</interfaceOnly>
      </configOptions>
   </configuration>
   <dependencies>
      <dependency>
         <groupId>io.swagger.codegen.v3</groupId>
         <artifactId>swagger-codegen-generators</artifactId>
         <version>1.0.8</version>
      </dependency>
   </dependencies>
</plugin>

Note: I am using the version 'v3' of the plugin.

Alysa answered 27/6, 2019 at 17:17 Comment(0)
D
1

I managed to avoid these default methods by using < java8 > false < /java8 > in the project forked from swagger codegen: https://github.com/OpenAPITools/openapi-generator

Example that works for me:

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>4.0.0</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${maven.multiModuleProjectDirectory}/api/target/generated/swagger-api-spec/swagger.json</inputSpec>  
                            <language>spring</language>
                            <library>spring-boot</library>
                <skipValidateSpec>true</skipValidateSpec>
                <generateSupportingFiles>true</generateSupportingFiles>
                <configOptions>
                    <sourceFolder>src/gen/java/main</sourceFolder>
                    <java8>false</java8>
                    <dateLibrary>java8</dateLibrary>
                    <interfaceOnly>false</interfaceOnly>
                    <groupId>com.company.groupid</groupId>
                    <artifactId>${project.artifactId}</artifactId>
                    <artifactVersion>${project.version}</artifactVersion>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>
Darr answered 15/5, 2019 at 11:51 Comment(0)
W
1

There is two different plugins for code generation based on an API specification document:

  1. io.swagger:swagger-codegen-maven-plugin (io.swagger.codegen.v3:swagger-codegen-maven-plugin in latest versions)
  2. org.openapitools:openapi-generator-maven-plugin

Each of them has a set of its own generators with different configuration.

The initial question was about the first one, but the solution found by author is for the second. In fact, now the Spring generator for swagger-codegen-maven-plugin has a similar option too: defaultInterfaces. The following example worked for me:

  <plugin>
    <groupId>io.swagger.codegen.v3</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>3.0.35</version>
    <executions>
    <execution>
      <id>generate-provider-v1</id>
      <phase>generate-resources</phase>
      <goals>
      <goal>generate</goal>
      </goals>
    </execution>
    </executions>
    <configuration>
    <inputSpec>${project.basedir}/src/main/resources/myopenapi.yaml</inputSpec>
    <output>${project.basedir}</output>
    <language>spring</language>
    <library>spring-boot</library>
    <modelPackage>my.package.model</modelPackage>
    <apiPackage>my.package.api</apiPackage>
    <configOptions>
      <defaultInterfaces>false</defaultInterfaces>
      <interfaceOnly>true</interfaceOnly>
      <dateLibrary>java8</dateLibrary>
    </configOptions>
    <modelNameSuffix>Dto</modelNameSuffix>
    <generateSupportingFiles>false</generateSupportingFiles>
    </configuration>
  </plugin>
Winstonwinstonn answered 27/10, 2022 at 20:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.