您可以在这里找到Fullswagger.json:
Pastebin 12.custom
SwaggerExcludeAttribute
:
[AttributeUsage(AttributeTargets.Property)]
public class SwaggerExcludeAttribute : Attribute
{
}
3.custom
SwaggerExcludeFilter
:
public class SwaggerExcludeFilter : ISchemaFilter
{
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
if (schema?.Properties == null)
{
return;
}
var excludedProperties =
context.Type.GetProperties().Where(
t => t.GetCustomAttribute<SwaggerExcludeAttribute>() != null);
foreach (var excludedProperty in excludedProperties)
{
var propertyToRemove =
schema.Properties.Keys.SingleOrDefault(
x => x.ToLower() == excludedProperty.Name.ToLower());
if (propertyToRemove != null)
{
schema.Properties.Remove(propertyToRemove);
}
}
}
}
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
c.SchemaFilter<SwaggerExcludeFilter>();
});
services.AddDbContext<WebApi3_1Context>(options =>
options.UseSqlServer(Configuration.GetConnectionString("WebApi3_1Context")));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
5。测试我的模型:
public class Test
{
public int Id { get; set; }
public string Name { get; set; }
[SwaggerExclude]
public Item Item { get; set; }
}
public class Item
{
public int Id { get; set; }
public string ItemName { get; set; }
public List<Person> Person { get; set; }
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
您可以将
[JsonIgnore]
添加到您不想在模型中可用的属性中。这可能有助于您减少这种循环。 另外,您可以尝试使用来限制模型的深度
app.UseSwaggerUI(c =>
{
c.DefaultModelRendering(ModelRendering.Model);
c.DefaultModelExpandDepth(1);
});
I通过添加一个删除所有外键和虚拟属性的schemafilter来解决此问题。使用swashbuckle.aspnetcore7.3.1
public class RemoveModelPropsFilter : ISchemaFilter
{
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
if (schema?.Properties == null)
{
return;
}
//Get dictionary of schema props
var dic = new Dictionary<string, OpenApiSchema>(StringComparer.InvariantCultureIgnoreCase);
foreach (var schemaProperty in schema.Properties)
{
dic.Add(schemaProperty.Key, schemaProperty.Value);
}
//Remove all foreign key props
foreach (var prop in context.Type.GetProperties())
{
if (Attribute.IsDefined(prop, typeof(ForeignKeyAttribute)))
{
var name = dic.FirstOrDefault(c => c.Key.Equals(prop.Name, StringComparison.OrdinalIgnoreCase)).Key;
schema.Properties.Remove(name);
schema.Properties.Add(name, new OpenApiSchema());
}
}
//Remove all virtual props
var excludedProperties = context.Type.GetProperties().Where(t => t.GetGetMethod().IsVirtual);
foreach (PropertyInfo excludedProperty in excludedProperties)
{
if (dic.ContainsKey(excludedProperty.Name))
{
var name = dic.FirstOrDefault(c => c.Key.Equals(excludedProperty.Name, StringComparison.OrdinalIgnoreCase)).Key;
schema.Properties.Remove(name);
schema.Properties.Add(name, new OpenApiSchema());
}
}
}
}