我正在使用Swashbuckle v3.0。
我不确定这是否是错误,但是多态性无法正常工作。我有以下课程:
BasePersonDocumentDto
{
Id,
Number
}
IdentityDto: BasePersonDocumentDto
{
}
PassportDto: BasePersonDocumentDto
{
VisaNumber
}
要应用继承和多态性,我创建了一个架构和文档过滤器。 I followed this answer下面是我使用的代码。
public class PolymorphismSchemaFilter<T> : ISchemaFilter
{
private List<Type> derivedTypes = new List<Type>() { typeof(IdentityDto), typeof(PassportDto) };
public void Apply(Schema model, SchemaFilterContext context)
{
if (!derivedTypes.Contains(context.SystemType)) return;
var baseSchema = new Schema() { Ref = "#/definitions/" + typeof(T).Name };
var clonedBaseSchema = new Schema
{
Properties = model.Properties,
Type = model.Type,
Required = model.Required
};
model.AllOf = new List<Schema> { baseSchema, clonedBaseSchema };
//Reset properties for they are included in allOf, should be null but code does not handle it
model.Properties = new Dictionary<string, Schema>();
}
}
public class PolymorphismDocumentFilter<T> : IDocumentFilter
{
private List<Type> derivedTypes = new List<Type>() { typeof(IdentityDto), typeof(PassportDto) };
public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
{
const string discriminatorName = "discriminator";
var baseSchema = context.SchemaRegistry.Definitions[typeof(T).Name];
//Discriminator property
baseSchema.Discriminator = discriminatorName;
baseSchema.Required = new List<string> { discriminatorName };
if (!baseSchema.Properties.ContainsKey(discriminatorName))
baseSchema.Properties.Add(discriminatorName, new Schema { Type = "string" });
//Register dervied classes
foreach (var item in derivedTypes)
context.SchemaRegistry.GetOrRegister(item);
}
}
生成的定义是:
//This is for BasePersonDocumentDto and for IdentityDto
"BasePersonDocumentDto":{
"required":[
"discriminator"
],
"type":"object",
"properties":{
"number":{
"type":"string"
},
"id":{
"format":"int32",
"type":"integer"
},
"discriminator":{
"type":"string"
}
},
"discriminator":"discriminator"
}
"PassportDto":{
"required":[
"discriminator"
],
"type":"object",
"properties":{
"number":{
"type":"string"
},
"id":{
"format":"int32",
"type":"integer"
},
"visaNumber":{
"type":"string"
},
"discriminator":{
"type":"string"
}
},
"discriminator":"discriminator"
}
现在要测试,我创建一个具有List属性的InputDto。我尝试传递以下JSON,但是即使鉴别符正确传递,传递的两个项目也都显示为BasePersonDocumentDto。这是我传递的JSON:
{
"documentsDto":[
{
"discriminator":"IdentityDto"
},
{
"visaNumber":"RRXX323TR",
"discriminator":"PassportDto"
}
]
}
答案 0 :(得分:0)
一段时间后,我找到了解决问题的方法。我的情况很简单。我只有一个基类和一些派生类,但是昂首阔步只生成了基类,而没有生成派生类。
为解决此问题,我使用了我在问题中发布的代码,该代码适用于继承,并且还添加了一个名为“ discriminator”的隐式列。此列用于区分子类型。
最后一个问题是反序列化到对象时,摇摇欲坠无法使用“ discriminator”列,因此,它始终映射为基本类型。我发现了两种解决方案,第一种是实现自定义模型绑定程序,第二种是实现自定义JsonConverter。
我选择了第二个选项,因为它看起来更合乎逻辑,因为我的问题是处理反序列化Json。除了在我的基类中使用JsonConverter之外,我还将其装饰为“ KnownType”属性。
[KnownType(typeof(IdentityDto))]
[KnownType(typeof(PassportDto))]
[JsonConverter(typeof(InheritanceConverter))]
BasePersonDocumentDto
{
Id,
Number
}
下面是JsonConverter的代码:
/// <summary>
/// Json deserialization for inheritance structure with discriminator
/// </summary>
public class InheritanceConverter : JsonConverter
{
/// <summary>
/// Default name for the discriminator property
/// </summary>
private string _discriminator { get; set; } = "discriminator";
public InheritanceConverter()
{
}
/// <summary>
/// Discriminator name to map between types
/// </summary>
/// <param name="discriminator">The discriminator property name. The defualt value is 'discriminator'</param>
public InheritanceConverter(string discriminator)
{
if (!string.IsNullOrWhiteSpace(discriminator))
_discriminator = discriminator;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
//Check null
if (reader.TokenType == JsonToken.Null) return null;
//Parse json
var jDerivedObject = JObject.Load(reader);
//Get discriminator
var discriminator = jDerivedObject.Value<string>(_discriminator);
if (string.IsNullOrWhiteSpace(discriminator))
throw new Exception("Invalid discriminator value");
//Get the type
var derivedType = GetSubType(discriminator, objectType);
//Create a new instance of the target type
var derivedObject = Activator.CreateInstance(derivedType);
//Populate the derived object
serializer.Populate(jDerivedObject.CreateReader(), derivedObject);
return derivedObject;
}
//TODO:- validate based on the base and sub-types via the KnownTypeAttributes
public override bool CanConvert(Type objectType) => true;
//Not required
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
//Not required
public override bool CanWrite => false;
#region Methods
/// <summary>
/// Get sub-type via KnownTypeAttributes
/// </summary>
/// <param name="derivedTypeName">The target type name which corosponds to the discriminator</param>
private Type GetSubType(string derivedTypeName, Type baseType)
{
var knownTypes = baseType.GetCustomAttributes(false).Where(ca => ca.GetType().Name == "KnownTypeAttribute").ToList();
if (knownTypes == null || knownTypes.Count == 0)
throw new Exception(
string.Format("Couldn't find any KnownAttributes over the base {0}. Please define at least one KnownTypeAttribute to determine the sub-type", baseType.Name));
foreach (dynamic type in knownTypes)
{
if (type.Type != null && type.Type.Name.ToLower() == derivedTypeName.ToLower())
return type.Type;
}
throw new Exception(string.Format("Discriminator '{0}' doesn't match any of the defined sub-types via KnownTypeAttributes", derivedTypeName));
}
#endregion
}