Swagger Codegen OneOf 生成不正确
Posted
技术标签:
【中文标题】Swagger Codegen OneOf 生成不正确【英文标题】:Swagger Codegen OneOf generating incorrectly 【发布时间】: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 类?
【问题讨论】:
【参考方案1】:问题可能是 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"
【讨论】:
以上是关于Swagger Codegen OneOf 生成不正确的主要内容,如果未能解决你的问题,请参考以下文章
Swagger-Codegen:如何将所有文件合并到一个文件中以进行客户端代码生成
Swagger-Codegen 不使用供应商扩展 x-discriminator-value
java:swagger-codegen生成CSharp(C#) Client
将生成的 Flask 应用程序代码(Swagger-Codegen)粘合到后端实现的最简洁方法