【问题标题】:Swagger Codegen OneOf generating incorrectlySwagger Codegen OneOf 生成不正确
【发布时间】:2022-01-22 12:16:12
【问题描述】:

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

以下是我遇到的问题的 OpenAPI sn-p:

"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 接口,但没有变量。 Payer、PolicyHolderInfo 和dependentMemberInfo 类已生成,但它们无论如何都没有链接到InsuranceInfo 类。如何填充 InsuranceInfo 类?

【问题讨论】:

    标签: openapi swagger-codegen


    【解决方案1】:

    问题可能是 InsuranceInfo 架构

    "InsuranceInfo": {
      "description": "Information about the payer, plan, and members.",
    
      "additionalProperties": false,
      "oneOf": [
        { ... },
        { ... }
      ]
    }
    

    实际上禁止所有属性。这是因为additionalProperties: false 只知道直接定义在它旁边的propertieshas no visibilityoneOf 子模式中。


    要解决此问题,您可以在不使用 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"
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-07
      • 1970-01-01
      • 2020-06-15
      • 2021-05-14
      • 1970-01-01
      • 1970-01-01
      • 2019-06-23
      • 1970-01-01
      相关资源
      最近更新 更多