How to configure Swashbuckle to ignore property on model
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>();
Ignore Property From Swagger UI
You don't actually need to define own attribute for request models. If you are using Json.NET then use [JsonIgnore]
Can I ignore a property in an ASP.Net Core API model?
If you're using Swashbuckle, you could install the Swashbuckle.AspNetCore.Annotations NuGet package, then use the SwaggerSchema
attribute as follows:
namespace Models
{
public class Car
{
[SwaggerSchema(ReadOnly = true)]
public guid Id { get; set; }
public string Name { get; set; }
}
}
This will mark the property with the ReadOnly attribute in the generated OpenAPI document.
How to configure Swashbuckle to omit Template / Entity / Schema from the documentation
Set DefaultModelsExpandDepth
to -1 in your Swashbuckle / Swagger UI configuration:
app.UseSwaggerUI(c =>
{
...
c.DefaultModelsExpandDepth(-1);
}
Related Topics
What Does Synchronizationcontext Do
Opening a Folder in Explorer and Selecting a File
Debugging Automatic Properties
How to Convert an Object to a Byte Array in C#
Best Way to Invoke Any Cross-Threaded Code
How to Detect If a Property Exists on an Expandoobject
When Do We Need to Set Processstartinfo.Useshellexecute to True
Implicit VS Explicit Interface Implementation
Query an Xdocument for Elements by Name at Any Depth
Can't Connect to Localhost on SQL Server Express 2012/2016
Log4Net Rolling Daily Filename with Date in the File Name
Copy Constructor Versus Clone()
How to Determine the Standard Deviation (Stddev) of a Set of Values
How to Return PDF to Browser in MVC
Iterate Multi-Dimensional Array with Nested Foreach Statement