Spring boot 带多态性的Swagger OpenAPI客户端代码生成问题
我试图从现有的控制器中生成Swagger文档和Java瘦客户端。以下是示例类:Spring boot 带多态性的Swagger OpenAPI客户端代码生成问题,spring-boot,swagger,openapi,swagger-codegen,openapi-generator,Spring Boot,Swagger,Openapi,Swagger Codegen,Openapi Generator,我试图从现有的控制器中生成Swagger文档和Java瘦客户端。以下是示例类: @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "clazz") @JsonSubTypes({ @JsonSubTypes.Type(value = Cat.class, name = "Cat"), @JsonSubTypes.Type(value = Fish.class, name
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "clazz")
@JsonSubTypes({
@JsonSubTypes.Type(value = Cat.class, name = "Cat"),
@JsonSubTypes.Type(value = Fish.class, name = "Fish"),
@JsonSubTypes.Type(value = Frog.class, name = "Frog")
})
public interface Animal {
String getName();
int getType();
}
这是控制器
@RestController
@RequestMapping("/animals")
public class AnimalController {
@GetMapping(value = "/")
public Animal[] getAnimalsByType(@RequestParam(required = false) String animalType) {
AnimalType type = StringUtils.isNotBlank(animalType) ? AnimalType.valueOf(animalType) : null;
Reflections reflections = new Reflections("com.xyz.openapi.server.model");
return reflections.getSubTypesOf(Animal.class).stream()
.map(animal -> {
try {
return animal.getDeclaredConstructor().newInstance();
} catch (InstantiationException | IllegalAccessException |
NoSuchMethodException | InvocationTargetException e) {
throw new RuntimeException(e);
}
})
.filter(animal -> animalType == null || type.index() == animal.getType())
.toArray(Animal[] :: new);
}
}
下面是build.gradle中的客户端代码生成任务
plugins {
id "java"
id "org.springframework.boot" version "2.3.1.RELEASE"
id "io.spring.dependency-management" version "1.0.9.RELEASE"
id "org.openapi.generator" version "5.1.0"
}
openApiGenerate {
inputSpec.set("$rootDir/api-docs.json")
outputDir.set("$rootDir/thinclient")
groupId.set("com.xyz.openapi")
id.set("openapi-thinclient")
version.set("0.0.1-SNAPSHOT")
apiPackage.set("com.xyz.openapi.thinclient.api")
invokerPackage.set("com.xyz.openapi.thinclient.invoker")
modelPackage.set("com.xyz.openapi.thinclient.model")
generatorName.set("java");
library.set("resttemplate")
configOptions.put("serializationLibrary", "jackson")
}
当我使用GradleOpenApiGenerate为java生成客户机代码时,我得到的客户机API如下所示
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", date = "2021-05-24T19:20:33.823042600+05:30[Asia/Calcutta]")
@Component("com.xyz.openapi.thinclient.api.AnimalControllerApi")
public class AnimalControllerApi {
private ApiClient apiClient;
public AnimalControllerApi() {
this(new ApiClient());
}
@Autowired
public AnimalControllerApi(ApiClient apiClient) {
this.apiClient = apiClient;
}
public ApiClient getApiClient() {
return apiClient;
}
public void setApiClient(ApiClient apiClient) {
this.apiClient = apiClient;
}
/**
*
*
* <p><b>200</b> - OK
* @param animalType (optional)
* @return List<OneOfCatFishFrog>
* @throws RestClientException if an error occurs while attempting to invoke the API
*/
public **List<OneOfCatFishFrog>** getAnimalsByType(String animalType) throws RestClientException {
return getAnimalsByTypeWithHttpInfo(animalType).getBody();
}
}
@javax.annotation.Generated(value=“org.openapitools.codegen.languages.JavaClientCodegen”,date=“2021-05-24T19:20:33.823042600+05:30[亚洲/加尔各答]”)
@组件(“com.xyz.openapi.thinclient.api.AnimalControllerApi”)
公共级动物控制{
私人ApiClient ApiClient;
公共动物控制{
这个(新的ApiClient());
}
@自动连线
公共动物控制中心(ApiClient ApiClient){
this.apiClient=apiClient;
}
公共ApiClient getApiClient(){
归还客户;
}
公共无效setApiClient(ApiClient ApiClient){
this.apiClient=apiClient;
}
/**
*
*
*200-正常
*@param animalType(可选)
*@return ListOneOfCatFishFrog
*@如果在尝试调用API时发生错误,则会引发RestClientException
*/
公共**列表**getAnimalsByType(字符串animalType)引发RestClientException{
返回GetAnimalByTypewithHttpInfo(animalType).getBody();
}
}
API的返回类型生成为List
,而不是List
。我需要做些什么改变来完成这件事吗?请建议