我正在使用 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
只知道直接在它旁边定义的 properties
和 has 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/