如何避免 swagger codegen 接口中的默认方法实现?
how to avoid default method implementation in swagger codegen interface?
我想避免 "default" 在 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);
可以吗?
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
对于 "spring" 语言,"java8" 参数既用于指示默认接口的使用,也用于指示 Java8 的一般使用,例如当使用 Java8 日期库时。
相关工单:
https://github.com/swagger-api/swagger-codegen/issues/8045
https://github.com/swagger-api/swagger-codegen/issues/5614
我在从 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>
我解决了配置同一插件的两次执行。一个配置为仅生成模型 类(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'。
根据 documentation 下面应该解决它:
<configOptions>
<skipDefaultInterface>true</skipDefaultInterface>
</configOptions>
我想避免 "default" 在 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);
可以吗?
Update March 2020:
According to new OpenAPI Tool
openapi-generator
There is an option withspring
(language
is DEPRECATED, usegeneratorName
)
skipDefaultInterface
https://github.com/OpenAPITools/openapi-generator/blob/master/docs/generators/spring.md
对于 "spring" 语言,"java8" 参数既用于指示默认接口的使用,也用于指示 Java8 的一般使用,例如当使用 Java8 日期库时。
相关工单:
https://github.com/swagger-api/swagger-codegen/issues/8045
https://github.com/swagger-api/swagger-codegen/issues/5614
我在从 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>
我解决了配置同一插件的两次执行。一个配置为仅生成模型 类(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'。
根据 documentation 下面应该解决它:
<configOptions>
<skipDefaultInterface>true</skipDefaultInterface>
</configOptions>