How to configure Swashbuckle to ignore property on model

后端 未结 15 1775
旧巷少年郎
旧巷少年郎 2020-12-02 15:10

I\'m using Swashbuckle to generate swagger documentation\\UI for a webapi2 project. Our models are shared with some legacy interfaces so there are a couple of properties I

15条回答
  •  旧时难觅i
    2020-12-02 15:50

    Here is what I used with Newtonsoft.Json.JsonIgnoreAttribute:

    internal class ApplySchemaVendorExtensions : Swashbuckle.Swagger.ISchemaFilter
    {
        public void Apply(Schema schema, SchemaRegistry schemaRegistry, Type type)
        {
            foreach (var prop in type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)
                                     .Where(p => p.GetCustomAttributes(typeof(Newtonsoft.Json.JsonIgnoreAttribute), true)?.Any() == true))
                if (schema?.properties?.ContainsKey(prop.Name) == true)
                    schema?.properties?.Remove(prop.Name);
        }
    }
    

提交回复
热议问题