【问题标题】:Hide complex read only property from Swagger UI从 Swagger UI 隐藏复杂的只读属性
【发布时间】:2021-07-01 12:32:21
【问题描述】:

我在 .NET 5 mvc/api 项目中使用 Swashbuckle.AspNetCore 6.1.4 OpenAPI 库。

我有一个带有过滤器模型的端点,Swagger 使用它来创建查询参数文档。

问题是正在显示一个复杂的属性(自定义对象)。

这是一个基础抽象类

public abstract class BaseFilter
{
    /// <summary>
    /// The page number
    /// </summary>
    /// <example>1</example>
    public int Page { get; init; } = 1;
    
    /// <summary>
    /// The number of items per page
    /// </summary>
    /// <example>20</example>
    public int Count { get; init; } = 20;

    /// <summary>
    /// The flag that indicates whether sort is ascending (default) or descending
    /// </summary>
    /// <example>false</example>
    public bool IsDescending { get; init; } = false;
    
    /// <summary>
    /// The case insensitive field to sort by
    /// </summary>
    /// <example>name</example>
    public string? SortBy { get; init; } = null;

    // I DON'T WANT TO DISPLAY THIS
    public Pagination Pagination => new() {PageNumber = Page, PageSize = Count};
}

我的过滤器继承了基类并继承了复杂的属性

public class BrandFilter
    : BaseFilter
{
    /// <summary>
    /// The case insensitive name
    /// </summary>
    /// <example>foo</example>
    public string? Name { get; init; } = null;
}

Swagger UI 显示了这一点(见图)

我想隐藏。

那里有几个过时的线程使用ISchemaFilter 排除具有给定属性的任何属性。这不起作用。

实际上,如果我在SwaggerGenOptions 中添加以下内容

c.SchemaFilter<SampleFilter>();

和过滤器

private class SampleFilter
    : ISchemaFilter
{
    public void Apply(OpenApiSchema schema, SchemaFilterContext schemaFilterContext)
    {
        if (schema.Properties is null || !schema.Properties.Any())
        {
            return; 
        }

        // it never reaches here

        // TODO logic to remove properties with X attribute or with a specific name
    }
}

OpenApiSchema 似乎总是有 0 个属性。

我试过 jsonIgnoring 它,(不使用 newtonsoft)也没有任何运气。

如何防止 swagger 考虑到只读的复杂属性?

PS:这是我的招摇配置

public static IServiceCollection AddOpenApi(this IServiceCollection services)
{
    var mainAssemblyName = typeof(Startup).Assembly.GetName().Name;
    var applicationAssemblyName = typeof(BaseFilter).Assembly.GetName().Name;
    
    services.AddSwaggerGen(
        c =>
        {
            c.SwaggerDoc(
                "v1",
                new OpenApiInfo
                {
                    Title = mainAssemblyName,
                    Version = "v1",
                    Description = "Client Query Service",
                    Contact = new OpenApiContact
                    {
                        Name = "Foo",
                        Email = "diegosasw@test.com"
                    }
                });

            var xmlCommentsWebApi = Path.Combine(System.AppContext.BaseDirectory, $"{mainAssemblyName}.xml");
            c.IncludeXmlComments(xmlCommentsWebApi);
            var xmlCommentsApplication = Path.Combine(System.AppContext.BaseDirectory, $"{applicationAssemblyName}.xml");
            c.IncludeXmlComments(xmlCommentsApplication);

            var availableStatuses = Enum.GetValues<Status>().Where(x => x != Status.None);
            var statusParameters = 
                availableStatuses
                    .Select(x => new OpenApiString(x.GetDisplayName()))
                    .Cast<IOpenApiAny>().ToList();
            
            c.MapType<Status>(
                () =>
                new OpenApiSchema
                {
                    Type = "string",
                    Enum = statusParameters,
                    Nullable = true
                });
            
            c.SchemaFilter<SampleFilter>();
        });
    
    return services;
}

public static void UseOpenApi(this IApplicationBuilder app, string assemblyPath)
{
    app.UseSwagger();
    app.UseSwaggerUI(c => c.SwaggerEndpoint($"{assemblyPath}/swagger/v1/swagger.json", "Client Query Service v1"));
}

【问题讨论】:

    标签: swagger openapi swashbuckle.aspnetcore


    【解决方案1】:

    答案比我想象的要简单。

    Swashbuckle 尊重可访问性。因此,将该属性设置为 internal 就足够了。

    我还是想知道为什么OpenApiSchema不包含任何属性,但原来的问题已经解决了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 2019-12-01
      • 2019-02-08
      • 1970-01-01
      • 2021-12-31
      • 1970-01-01
      • 2019-09-28
      相关资源
      最近更新 更多