Swagger Show Obsolete Message
Asked Answered
J

1

7

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)

Junette answered 1/4, 2022 at 1:21 Comment(0)
H
3

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.

Howlan answered 18/10, 2023 at 15:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.