Swashbuckle polymorphism support issue

最后都变了- 提交于 2020-12-12 11:56:32

问题


I am using Swashbuckle v3.0.

I am not sure weather this is a bug or not, but polymorphism is not working as it should. I have the following classes:

BasePersonDocumentDto
{
   Id,
   Number
}

IdentityDto: BasePersonDocumentDto
{

}

PassportDto: BasePersonDocumentDto
{
 VisaNumber
}

To apply Inheritance & Polymorphism, i have created a schema and document filters. I followed this answer Below are the code i used.

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);
    }
}

The generated definitions are:

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

Now to test, i create an InputDto that have a List property. I tried passing the following JSON, but both passed items are shown as BasePersonDocumentDto even though the discriminator is passed correctly. Here is the JSON i passed:

{  
   "documentsDto":[  
      {  
         "discriminator":"IdentityDto"
      },
      {  
         "visaNumber":"RRXX323TR",
         "discriminator":"PassportDto"
      }
   ]
}

回答1:


After sometime i was able to figure a way to solve out my issue. My case was very simple. I just had a base and some derived classes, but swagger only generated the base but not the derived.

To solve this issue, i used the code i posted in my question, which applies inheritance and also, adds an implicit column named "discriminator". This column is used to discriminate between the subtypes.

The final issue was when deserializing to an object, swagger wasn't able to use the "discriminator" column and as a result, it always mapped to base type. I found two solutions to solve this, the first was to implement a custom model binder, and the second was to implement a custom JsonConverter.

I went with the second option as it seems more logical since my issue was dealing with deserializing a Json. Along with using the JsonConverter with my base class, i also decorated it "KnownType" attribute.

[KnownType(typeof(IdentityDto))]
[KnownType(typeof(PassportDto))]
[JsonConverter(typeof(InheritanceConverter))]
BasePersonDocumentDto
{
   Id,
   Number
}

Below is the code for the 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
}


来源:https://stackoverflow.com/questions/51692739/swashbuckle-polymorphism-support-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!