How to configure Swashbuckle to ignore property on model

后端 未结 15 1738
旧巷少年郎
旧巷少年郎 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条回答
  •  失恋的感觉
    2020-12-02 15:54

    Well, with a bit of poking I found a way to do this using ISchemaFilter:

    public class ApplyCustomSchemaFilters : ISchemaFilter
    {
        public void Apply(Schema schema, SchemaRegistry schemaRegistry, Type type)
        {
            var excludeProperties = new[] {"myProp1", "myProp2", "myProp3"};
    
            foreach(var prop in excludeProperties)
                if (schema.properties.ContainsKey(prop))
                    schema.properties.Remove(prop);
        }
    }
    

    then when calling httpConfiguration.EnableSwagger I set the SwaggerDocsConfig to use this SchemaFilter as follows:

    c.SchemaFilter();
    

    Hope this helps someone. I'd still be curious on whether it's possible to use the IModelFilter somehow though.

提交回复
热议问题