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

时间:2018-05-04 15:30:09

标签: java maven interface swagger codegen

我想避免maven插件swagger codegen生成的界面中的“默认”实现。 例如,随着petstore招摇:http://petstore.swagger.io/v2/swagger.json

我使用maven插件生成界面:

public static IServiceCollection AddAutoMapper(this IServiceCollection services)
{
     return services.AddAutoMapper(null, AppDomain.CurrentDomain.GetAssemblies());
}

我使用默认的方法实现生成类似PetApi.java的接口:

            <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>

我想像

一样避免它
    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);
    }

有可能吗?

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

我解决了配置同一插件的两次执行的问题。一种配置仅生成模型类(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”版本。

答案 2 :(得分:0)

我设法通过在swagger代码生成的项目中使用 false 来避免这些默认方法:https://github.com/OpenAPITools/openapi-generator

对我有用的示例:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="list-item">
  <div class="details">
    <a href="https://link_to_Item_1" />
  </div>
</div>
<div class="list-item">
  <div class="details">
    <a href="https://link_to_Item_2" />
  </div>
</div>
<div class="list-item">
  <div class="details">
    <a href="https://link_to_Item_3" />
  </div>
</div>