是否可以使用 OpenApiGenerator 生成假客户端?
这是我当前的设置:
plugins {
id 'java'
id 'org.springframework.boot' version '2.7.11'
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
id "org.openapi.generator" version "6.3.0"
}
group = 'dev.rifaii'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign:4.0.2'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
}
def swaggerCodeGenConfig = {
generatorName = "spring"
remoteInputSpec = "someUrl"
outputDir = "$projectDir/external/src/main/java"
apiPackage = 'somepackageForApis'
modelPackage = 'somepackageForModels'
additionalProperties = [
packageName: "petstore",
interfaceOnly: 'true',
feign : 'true'
]
}
task buildExampleClient(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
configure swaggerCodeGenConfig
remoteInputSpec = "http://158.101.191.70:8081/v3/api-docs"
apiPackage = 'dev.rifaii.example.api'
modelPackage = 'dev.rifaii.example.model'
}
此任务当前正在生成什么:
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2023-05-09T00:05:09.505990300+03:00[Europe/Tallinn]")
@Validated
@Tag(name = "store", description = "the store API")
public interface StoreApi {
default Optional<NativeWebRequest> getRequest() {
return Optional.empty();
}
/**
* DELETE /store/order/{orderId} : Delete purchase order by ID
* For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors
*
* @param orderId ID of the order that needs to be deleted (required)
* @return Invalid ID supplied (status code 400)
* or Order not found (status code 404)
*/
@Operation(
operationId = "deleteOrder",
summary = "Delete purchase order by ID",
description = "For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors",
tags = { "store" },
responses = {
@ApiResponse(responseCode = "400", description = "Invalid ID supplied", content = {
@Content(mediaType = "*/*", schema = @Schema(implementation = Map.class))
}),
@ApiResponse(responseCode = "404", description = "Order not found")
}
)
@RequestMapping(
method = RequestMethod.DELETE,
value = "/store/order/{orderId}",
produces = { "*/*" }
)
default ResponseEntity<Void> deleteOrder(
@Parameter(name = "orderId", description = "ID of the order that needs to be deleted", required = true, in = ParameterIn.PATH) @PathVariable("orderId") Long orderId
) {
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}
}
我想要的只是让它生成看起来像这样的界面
@FeignClient("someClient")
public interface SomeClient {
@RequestMapping(method = RequestMethod.GET, value = "/example")
List<SomeObject> getExample();
}
在另一个项目中,我使用了旧版本的 swagger codegen 和 Springfox,我能够实现这一目标,但我不知道如何使用 openapi 生成器来做到这一点
@RequestMapping
在Feign客户端中长期以来一直被劝阻和不好的做法。它导致 CVE-2021-22044,并且由于上述安全问题,支持 @RequestMapping
的功能在 Feign 客户端中被完全删除。
话虽如此,基于
spring
生成器的文档: https://github.com/OpenAPITools/openapi-generator/blob/master/docs/generators/spring.md
将
library
配置指定为 spring-cloud
应该会产生您想要的结果。