openapi - Swagger Codegen OneOf 生成不正确

标签 openapi swagger-codegen

我正在使用 OpenAPISpec 文档生成 JavaClient。我使用 swagger-codegen 3.0 来生成代码。 OpenAPISpec 版本是 3.0.1

下面是我遇到问题的 OpenAPI 片段:

"RequestWithInsuranceInfo": {
        "type": "object",
        "description": "This request schema will produce a response containing an out of pocket estimate for the given service using the patient's insurance information.",
        "additionalProperties": false,
        "properties": {
          "insuranceInfo": {
            "$ref": "#/components/schemas/InsuranceInfo"
          },
          "service": {
            "type": "object",
            "additionalProperties": false,
            "description": "Schema to use when the patient's benefit info is not given in the request.",
            "properties": {
              "codes": {
                "type": "array",
                "items": {
                  "$ref": "#/components/schemas/ServiceCode"
                }
              },
              "provider": {
                "$ref": "#/components/schemas/Provider"
              },
              "costs": {
                "$ref": "#/components/schemas/ServiceCosts"
              }
            },
            "required": [
              "codes",
              "provider",
              "costs"
            ]
          }
        }
      },
"InsuranceInfo": {
        "description": "Information about the payer, plan, and members.",
        "additionalProperties": false,
        "oneOf": [
          {
            "type": "object",
            "additionalProperties": false,
            "title": "Option 1: Patient Is Policy Holder",
            "description": "Schema to use when the patient the primary on the insurance plan.",
            "properties": {
              "payer": {
                "$ref": "#/components/schemas/Payer"
              },
              "policyHolderInfo": {
                "$ref": "#/components/schemas/PolicyHolderInfo"
              }
            },
            "required": [
              "payer",
              "policyHolderInfo"
            ]
          },
          {
            "type": "object",
            "additionalProperties": false,
            "title": "Option 2: Patient Is Dependent",
            "description": "Schema to use when the patient is a dependent on the insurance plan.",
            "properties": {
              "payer": {
                "$ref": "#/components/schemas/Payer"
              },
              "dependentMemberInfo": {
                "$ref": "#/components/schemas/DependentMemberInfo"
              },
              "policyHolderInfo": {
                "$ref": "#/components/schemas/PolicyHolderInfo"
              }
            },
            "required": [
              "payer",
              "dependentMemberInfo",
              "policyHolderInfo"
            ]
          }
        ]
      },

下面是生成的代码:

public class InsuranceInfo implements OneOfInsuranceInfo {

  @Override
  public boolean equals(java.lang.Object o) {..}

  @Override
  public int hashCode() {..}

  @Override
  public String toString() {..}

  private String toIndentedString(java.lang.Object o) {..}
}


public interface OneOfInsuranceInfo {

}


public class RequestWithInsuranceInfo implements OneOfRequest {
  @SerializedName("insuranceInfo")
  private InsuranceInfo insuranceInfo = null;

  @SerializedName("service")
  private RequestWithInsuranceInfoService service = null;
 ..

}

public class Payer {
  @SerializedName("id")
  private String id = null;
  
  ..
}

public class PolicyHolderInfo {
  @SerializedName("memberId")
  private String memberId = null;

  @SerializedName("firstName")
  private String firstName = null;

  @SerializedName("lastName")
  private String lastName = null;

  @SerializedName("dateOfBirth")
  private LocalDate dateOfBirth = null;

  ..
}

public class DependentMemberInfo {
  @SerializedName("memberId")
  private String memberId = null;

  @SerializedName("firstName")
  private String firstName = null;

  @SerializedName("lastName")
  private String lastName = null;

  @SerializedName("dateOfBirth")
  private LocalDate dateOfBirth = null;

  ..

}

如图所示,InsuranceInfo 对象实现了 OneOfInsuranceInfo 接口(interface),但没有变量。生成了 Payer、PolicyHolderInfo 和 dependentMemberInfo 类,但它们无论如何都没有链接到 InsuranceInfo 类。如何填充 InsuranceInfo 类?

最佳答案

问题可能是 InsuranceInfo 模式

"InsuranceInfo": {
  "description": "Information about the payer, plan, and members.",

  "additionalProperties": false,
  "oneOf": [
    { ... },
    { ... }
  ]
}

有效地禁止所有属性。这是因为 additionalProperties: false 只知道直接在它旁边定义的 propertieshas no visibility进入 oneOf 子模式。


要解决此问题,您可以在不使用 oneOf 的情况下重写 InsuranceInfo 架构,如下所示。此模式基本上是原始模式的“选项 2”,除了 dependentMemberInfo 属性被定义为可选。

"InsuranceInfo": {
  "description": "Information about the payer, plan, and members.",
  "additionalProperties": false,
  "type": "object",
  "required": [
    "payer",
    "policyHolderInfo"
  ],
  "properties": {
    "payer": {
      "$ref": "#/components/schemas/Payer"
    },
    "dependentMemberInfo": {
      "$ref": "#/components/schemas/DependentMemberInfo"
    },
    "policyHolderInfo": {
      "$ref": "#/components/schemas/PolicyHolderInfo"
    }
  }
}

关于openapi - Swagger Codegen OneOf 生成不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70429287/

相关文章:

swagger - 重做不显示身份验证部分

swagger - 如何在 OpenAPI (Swagger) 中描述这个 POST JSON 请求正文?

azure - 在 Azure API 管理开发人员门户中,如何以友好的格式显示 openapi 请求正文架构?

asp.net-core - Swagger Swashbuckle 多态性不适用于接口(interface)类型

flutter - 从 Swagger-codegen 生成 DART 包

openapi - 如何使用 $ref 从另一个 OpenAPI 文件引用路径?

java - 使用 LinkedHashMap 而不是 HashMap 用于 Swagger 字典类型(Java codegen)?

查询和/或正文中的 Swagger 参数

eclipse - 找不到处理 swagger-codegen-maven-plugin 的市场条目

java - 生成swagger java客户端代码时出错