Swagger Generate Subtypes based on enum giving an error

问题描述 投票:0回答:0

我有以下API输入类

@JsonTypeInfo(use = Id.NAME, include = As.PROPERTY, property = "importType", visible = true)
@JsonSubTypes({@Type(value = MriImportRequest.class, name = "MRI")})
@Schema(
    description = "Entity",
    discriminatorProperty = "importType",
    discriminatorMapping = {@DiscriminatorMapping(schema = MriImportRequest.class, value = "MRI")})
public abstract class FileImportInput {

  @Schema(
      type = "ImportType",
      example = "MRI",
      required = true,
      description = "The `type` attribute identifies the type of import like MRI")
  @NotNull
  private ImportType importType;

  @NotNull
  @NotEmpty
  @JsonProperty("resourceUri")
  @Schema(
      type = "string",
      example = "riskdata/v1/exposuresets/es1/1223/portfolios/12",
      description = "Resource identification string",
      required = true)
  private String resourceUri;

  public ImportType getImportType() {
    return importType;
  }

  public void setImportType(ImportType importType) {
    this.importType = importType;
  }

  public String getResourceUri() {
    return resourceUri;
  }

  public void setResourceUri(String resourceUri) {
    this.resourceUri = resourceUri;
  }
}

基于导入类型值,我定义了这个 MriImportRequest 类

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonTypeName("MRI")
public class MriImportRequest extends FileImportInput {

  @Schema(
      type = "MriImportSettings",
      example = "{ \"geohaz\":true}",
      description = "Contains all settings")
  @JsonProperty("settings")
  private MriImportSettings settings;

  public MriImportSettings getSettings() {
    return settings;
  }

  public void setSettings(MriImportSettings settings) {
    this.settings = settings;
  }
}

我在 swagger 代码生成的 POM 中有以下条目

<plugin>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <executions>
        <execution>
            <configuration>
                <apiPackage>com.rms.sdk.api</apiPackage>
                <generateApiTests>false</generateApiTests>
                <inputSpec>${project.build.directory}/generated-resources/openapi.yaml</inputSpec>
                <invokerPackage>com.rms.sdk.invoker</invokerPackage>
                <language>java</language>
                <modelPackage>com.rms.sdk.model</modelPackage>
            </configuration>
            <goals>
                <goal>generate</goal>
            </goals>
            <phase>generate-resources</phase>
        </execution>
    </executions>
    <groupId>io.swagger.codegen.v3</groupId>
    <version>${swagger.codegen.version}</version>
</plugin>

在 swagger 生成的类中,出现以下错误。我无法理解为什么

当我尝试构建我得到的项目时,不兼容的类型:java.lang.String cannot be converted to com.rms.sdk.model.FileImportInput.ImportTypeEnum,下面是出错的代码,它是 this.importType线。

public FileImportInput() {
  this.importType = this.getClass().getSimpleName();
}
java swagger-codegen openapi-generator
© www.soinside.com 2019 - 2024. All rights reserved.