11

我想避免 maven 插件 swagger codegen 生成的接口中的“默认”实现。例如,使用宠物商店招摇:http://petstore.swagger.io/v2/swagger.json

我使用 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>

我使用默认的方法实现生成像 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);
    }

我想避免它喜欢

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

有可能吗?

2020 年 3 月更新:

根据新的 OpenAPI Toolopenapi-generator
有一个选项springlanguage已弃用,使用generatorName

skipDefaultInterface

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

4

4 回答 4

6

对于“spring”语言,“java8”参数既用于表示使用默认接口,也用于表示Java8 的一般使用,例如,当使用Java8 日期库时。
相关票证:
https ://github.com/swagger-api/swagger-codegen/issues/8045
https://github.com/swagger-api/swagger-codegen/issues/5614

于 2018-09-13T07:14:52.643 回答
2

我解决了配置同一插件的两次执行。一个配置只生成模型类(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>

注意:我使用的是插件的“v3”版本。

于 2019-06-27T17:17:24.993 回答
0

我设法通过在从 swagger codegen 分叉的项目中使用 <java8> false</java8> 来避免这些默认方法:https ://github.com/OpenAPITools/openapi-generator

对我有用的例子:

<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>
于 2019-05-15T11:51:02.390 回答
0

根据文档,以下应该解决它:

<configOptions>
     <skipDefaultInterface>true</skipDefaultInterface>
</configOptions>
于 2022-02-11T09:51:58.150 回答