How to configure Swashbuckle to ignore property on model for a specific api version only

守給你的承諾、 提交于 2021-02-11 14:39:48

问题


I needed to add a property to a model and I have implemented what is suggested in the selected answer here and it's working in that it removes the property I have tagged with SwaggerIgnorePropertyAttribute attribute of the model. My question is , if I have several API versions of my application,how to remove it from the swagger doc/schema for certain versions? I only care not to see the added property in swagger. I only really have 1 version of the model across the application even though I have several version of the app.

I added the version like this:

            services.AddSwaggerGen(
                swaggerOptions =>
                {
                    swaggerOptions.SwaggerDoc(
                        "v1",
                        new Info
                        {
                            Title = "Titlebla1",
                            Description = "bla1",
                            Version = "v1"
                        });

                    swaggerOptions.SwaggerDoc(
                        "v2",
                        new Info
                        {
                            Title = "Titlebla2",
                            Description = "bla2",
                            Version = "v2"
                        });
 etc

I know that I can find the version by using SwaggerDocument and doing something like this: swaggerDoc.Info.Version but how can I get access to SwaggerDocument from the Apply in my SwaggerExcludePropertySchemaFilter class below ?

private class SwaggerExcludePropertySchemaFilter : ISchemaFilter
        {
            public void Apply(Schema schema, SchemaFilterContext context)
            {
                if (schema?.Properties == null)
                {
                    return;
                }

                var excludedProperties = context.SystemType.GetProperties().Where(t => t.GetCustomAttribute<SwaggerIgnorePropertyAttribute>() != null);

                foreach (var excludedProperty in excludedProperties)
                {
                    var propertyToRemove = schema.Properties.Keys.SingleOrDefault(x => string.Equals(x, excludedProperty.Name, StringComparison.OrdinalIgnoreCase));

                    if (propertyToRemove != null)
                    {
                        schema.Properties.Remove(propertyToRemove);
                    }
                }

            }
        }

回答1:


Try use IDocumentFilter , see example:

 public class CustomSwaggerFilter : IDocumentFilter
{
    public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
    {
        var nonRequiredMYPropertyAPIs = swaggerDoc.Paths
            .Where(x => !x.Key.ToLower().Contains("v1") /*the version you want to remove the property */)
            .ToList();
        nonRequiredMYPropertyAPIs.ForEach(x => { 
                                            swaggerDoc.Components.Schemas["YOUR_CLASS_MODEL"]
                                            .Properties.Remove("PROPERTY_NAME_YOU_WANT_TO_IGNORE");
                                        });
    }
}

Don't forget register the filter:

   c.DocumentFilter<CustomSwaggerFilter>();


来源:https://stackoverflow.com/questions/65607495/how-to-configure-swashbuckle-to-ignore-property-on-model-for-a-specific-api-vers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!