简体   繁体   English

如何避免swagger codegen接口中的默认方法实现?

[英]how to avoid default method implementation in swagger codegen interface?

I would like to avoid "default" implementation in interface generate by the maven plugin swagger codegen.我想避免 maven 插件 swagger codegen 生成的界面中的“默认”实现。 For example, with petstore swagger : http://petstore.swagger.io/v2/swagger.json例如,使用 petstore 招摇: http : //petstore.swagger.io/v2/swagger.json

I generate interface with maven plugin :我用 maven 插件生成接口:

            <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 :我使用默认的方法实现生成像 PetApi.java 这样的接口:

    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: 2020 年 3 月更新:

According to new OpenAPI Tool openapi-generator根据新的 OpenAPI 工具openapi-generator
There is an option with spring ( language is DEPRECATED, use generatorName ) spring有一个选项( language已弃用,使用generatorName

skipDefaultInterface

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

With a "spring" language the "java8" parameter is used both to signal use of the default interface and to signal general use of Java8, eg when the Java8 date library is used.对于“spring”语言,“java8”参数用于表示默认接口的使用和Java8 的一般使用,例如当使用Java8 日期库时。
Related tickets:相关门票:
https://github.com/swagger-api/swagger-codegen/issues/8045 https://github.com/swagger-api/swagger-codegen/issues/8045
https://github.com/swagger-api/swagger-codegen/issues/5614 https://github.com/swagger-api/swagger-codegen/issues/5614

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).一个配置为仅生成模型类(java8=true 和 dateLibrary=java8-localdatetime),另一个配置为仅生成 api 接口(java=false 和 dateLibrary 为空)。

<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.注意:我使用的是插件的“v3”版本。

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我设法通过在从 swagger 代码生成分叉的项目中使用 < java8 > false </java8 > 来避免这些默认方法: 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM