如何避免swagger codegen接口中的默认方法实现?
Posted
技术标签:
【中文标题】如何避免swagger codegen接口中的默认方法实现?【英文标题】:how to avoid default method implementation in swagger codegen interface? 【发布时间】:2018-10-15 03:32:04 【问题描述】:我想避免 maven 插件 swagger codegen 生成的接口中的“默认”实现。 例如,用 petstore 招摇: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 工具
openapi-generator
spring
有一个选项(language
已弃用,请使用generatorName
)
skipDefaultInterface
https://github.com/OpenAPITools/openapi-generator/blob/master/docs/generators/spring.md
【问题讨论】:
【参考方案1】:根据documentation,以下应该可以解决它:
<configOptions>
<skipDefaultInterface>true</skipDefaultInterface>
</configOptions>
【讨论】:
【参考方案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”版本。
【讨论】:
【参考方案3】:我在从 swagger codegen 分叉的项目中使用
适合我的示例:
<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>
【讨论】:
【参考方案4】:对于“spring”语言,“java8”参数既用于表示使用默认接口,也用于表示 Java8 的一般使用,例如当使用 Java8 日期库时。 相关票务:https://github.com/swagger-api/swagger-codegen/issues/8045https://github.com/swagger-api/swagger-codegen/issues/5614
【讨论】:
以上是关于如何避免swagger codegen接口中的默认方法实现?的主要内容,如果未能解决你的问题,请参考以下文章
Swagger-Codegen:如何将所有文件合并到一个文件中以进行客户端代码生成
将 swagger-codegen 项目导入现有的 Android 项目