Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swagger Show Obsolete Message

Take this for example:

[Obsolete("Please use /Auth/Register instead")]

Swaggers UI shows its Obsolte but doesnt show the message, so is it possible to have the message inside the attribute show in the Swagger UI? (Without having to change library like this post says) Im using Swashbuckle.AspNetCore (Default with ASP.NET) v6.3 (OpenAPI / Swagger v2.0, v3.0)

like image 506
Nova1545 Avatar asked May 23 '26 07:05

Nova1545


1 Answers

This could be done in Swashbuckle with operation filter:

internal class ObsoleteOperationFilter : IOperationFilter
{
    public void Apply(OpenApiOperation operation, OperationFilterContext context)
    {
        operation.Description = operation.Deprecated ? context.MethodInfo.GetCustomAttribute<ObsoleteAttribute>()?.Message : operation.Description;
    }
}

Then add the filter in the SwaggerGen configuration:

builder.Services.AddSwaggerGen(options =>
{
    options.OperationFilter<ObsoleteOperationFilter>();
    ...
});

Now description of the operation would be replaced with a message from Obsolete attribute.

like image 86
vnau Avatar answered May 27 '26 08:05

vnau