Forum Discussion

ayushipuri94's avatar
ayushipuri94
New Member
3 years ago

Swagger Codegen OneOf

Generating Java client from an OpenAPI Spec

 

 

"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"
            ]
          }
        ]
      },

 

 

 

What gets generated: 

 

 

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;
 ..

}

 

 

 

As shown, the InsuranceInfo object implements the OneOfInsuranceInfo interface but has no values. Payer, PolicyHolderInfo and dependentMemberInfo class are generated but they are not linked to the InsuranceInfo class anyhow. How do I populate the InsuranceInfo class? 

 

 

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    (Copying my answer from Stack Overflow)

     

    The issue is probably that the InsuranceInfo schema

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

    effectively disallows ALL properties. This is because additionalProperties: false only knows about the properties defined directly alongside it and has no visibility into oneOf subschemas.


    To resolve the issue, you can rewrite the InsuranceInfo schema without oneOf, as follows. This schema is basically "Option 2" from the original schema, except the dependentMemberInfo property is defined as optional.

    "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"
        }
      }
    }