Last active
November 22, 2019 13:28
-
-
Save martea/7f5210222076cedc371988ee1d123c11 to your computer and use it in GitHub Desktop.
Swashbuckle depricated / Obsolete OAS3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using Microsoft.OpenApi.Models; | |
using Swashbuckle.AspNetCore.SwaggerGen; | |
public class DepricatedOperationFilter : IOperationFilter | |
{ | |
public void Apply(OpenApiOperation operation, OperationFilterContext context) | |
{ | |
operation.Deprecated = context.MethodInfo.GetCustomAttributes(false).OfType<System.ObsoleteAttribute>().Any(); | |
} | |
} | |
public class DepricatedParameterFilter : IParameterFilter | |
{ | |
public void Apply(OpenApiParameter parameter, ParameterFilterContext context) | |
{ | |
parameter.Deprecated = context.PropertyInfo.GetCustomAttributes(false).OfType<System.ObsoleteAttribute>().Any(); | |
} | |
} | |
public class DepricatedSchemaFilter : ISchemaFilter | |
{ | |
public void Apply(OpenApiSchema schema, SchemaFilterContext context) | |
{ | |
schema.Deprecated = context.ApiModel.Type.GetCustomAttributes(false).OfType<System.ObsoleteAttribute>().Any(); | |
} | |
} | |
public class DepricatedSchemaPropertyFilter : ISchemaFilter | |
{ | |
public void Apply(OpenApiSchema schema, SchemaFilterContext context) | |
{ | |
var type = context.ApiModel.Type; | |
if (schema?.Properties == null || type == null) | |
return; | |
var obsoletedProperties = type.GetProperties().Where(t => t.GetCustomAttributes(false).OfType<ObsoleteAttribute>().Any()); | |
foreach (var property in obsoletedProperties) | |
{ | |
if (schema.Properties.ContainsKey(property.Name)) | |
{ | |
schema.Properties[property.Name].Deprecated = true; | |
} | |
} | |
} | |
} | |
//REGISTER | |
options.OperationFilter<DepricatedOperationFilter>(); | |
options.ParameterFilter<DepricatedParameterFilter>(); | |
options.SchemaFilter<DepricatedSchemaFilter>(); | |
options.SchemaFilter<DepricatedSchemaPropertyFilter>(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment