Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure Swashbuckle to ignore property on model

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 want to ignore on the models. I can't use JsonIgnore attribute because the legacy interfaces also need to serialize to JSON so I don't want to ignore the properties globally, just in the Swashbuckle configuration.

I found a method of doing this documented here:

https://github.com/domaindrivendev/Swashbuckle/issues/73

But this appears to be out of date with the current Swashbuckle release.

The method recommended for the old version of Swashbuckle is using an IModelFilter implementation as follows:

public class OmitIgnoredProperties : IModelFilter {     public void Apply(DataType model, DataTypeRegistry dataTypeRegistry, Type type)     {         var ignoredProperties = … // use reflection to find any properties on                                    // type decorated with the ignore attributes          foreach (var prop in ignoredProperties)              model.Properties.Remove(prop.Name);      } }  SwaggerSpecConfig.Customize(c => c.ModelFilter<OmitIgnoredProperties>()); 

But I'm unsure how to configure Swashbuckle to use the IModelFilter in the current version? I'm using Swashbuckle 5.5.3.

like image 251
mutex Avatar asked Dec 06 '16 22:12

mutex


People also ask

How do you ignore property in swagger?

If you mark field/property as internal or protected or private , it will be ignored automatically by swashbuckle in swagger documentation. Update: Obviously, those properties/fields won't be populated in request/response.

How do I hide the action method in swagger?

By adding this attribute on a controller or action and specifying IgnoreApi = true , it gets hidden from auto-generated documentation. However, this user has to apply this to around 80 controllers.

How do I hide a schema in swagger net core?

How to do it? add this property in your Swagger UI Options defaultModelsExpandDepth: -1 for hide schema section and for more reference refer this swagger.io/docs/open-source-tools/swagger-ui/usage/… Can you please add your swagger ui configuration settings in your question.

How do I hide models in Swagger UI?

To hide the "Models" section, add defaultModelsExpandDepth: -1 to the Swagger UI configuration code in your index. html . Note the option name uses plural Model*s* not Model . Swagger UI also has many other configuration options that control API documentation rendering.


1 Answers

If you need to do this but without using JsonIgnore (maybe you still need to serialize/deserialize the property) then just create a custom attribute.

[AttributeUsage(AttributeTargets.Property)] public class SwaggerExcludeAttribute : Attribute { } 

Then a schema filter similar to Johng's

public class SwaggerExcludeFilter : ISchemaFilter {     #region ISchemaFilter Members      public void Apply(Schema schema, SchemaRegistry schemaRegistry, Type type)     {         if (schema?.properties == null || type == null)             return;          var excludedProperties = type.GetProperties()                                      .Where(t =>                                              t.GetCustomAttribute<SwaggerExcludeAttribute>()                                              != null);          foreach (var excludedProperty in excludedProperties)         {             if (schema.properties.ContainsKey(excludedProperty.Name))                 schema.properties.Remove(excludedProperty.Name);         }     }      #endregion } 

Don't forget to register the filter

c.SchemaFilter<SwaggerExcludeFilter>(); 
like image 52
Richard Avatar answered Sep 20 '22 10:09

Richard