CS0618 “ApiDescriptionExtensions.ActionAttributes(ApiDescription)”:“Deprecated: Use OperationFilterContext.ControllerActionDescriptor”
source code:
public class SecurityRequirementsOperationFilter : IOperationFilter
{
public void Apply(Operation operation, OperationFilterContext context)
{
var actionAttrs = context.ApiDescription.ActionAttributes();
if (actionAttrs.OfType<AbpAllowAnonymousAttribute>().Any())
{
return;
}
var controllerAttrs = context.ApiDescription.ControllerAttributes();
var actionAbpAuthorizeAttrs = actionAttrs.OfType<AbpAuthorizeAttribute>();
if (!actionAbpAuthorizeAttrs.Any() && controllerAttrs.OfType<AbpAllowAnonymousAttribute>().Any())
{
return;
}
var controllerAbpAuthorizeAttrs = controllerAttrs.OfType<AbpAuthorizeAttribute>();
if (controllerAbpAuthorizeAttrs.Any() || actionAbpAuthorizeAttrs.Any())
{
operation.Responses.Add("401", new Response { Description = "Unauthorized" });
var permissions = controllerAbpAuthorizeAttrs.Union(actionAbpAuthorizeAttrs)
.SelectMany(p => p.Permissions)
.Distinct();
if (permissions.Any())
{
operation.Responses.Add("403", new Response { Description = "Forbidden" });
}
operation.Security = new List<IDictionary<string, IEnumerable<string>>>
{
new Dictionary<string, IEnumerable<string>>
{
{ "bearerAuth", permissions }
}
};
}
}
}
Got the same message when I was using code this way:
var requestAttributes = context?.ApiDescription?.ActionAttributes()?.OfType<iPortAccessibleGetRequestAttribute>();
Found this post, not sure if its relevant anymore or not, but updated my code to this and it is working without the warning message. It would be good to know if this is the way it should be handled going forward.
Here is my new line:
var requestAttributes = context?.ControllerActionDescriptor?.GetControllerAndActionAttributes(inherit: true)?.OfType<iPortAccessibleGetRequestAttribute>();
See updated docs https://github.com/domaindrivendev/Swashbuckle.AspNetCore#operation-filters. As of v3.0.0, the canonical way of accessing action or controller attributes in a filter is via context.MethodInfo.
Apologies for all the changes in this contract - I've tried to restrict the breaking ones to major version increments.
Most helpful comment
See updated docs https://github.com/domaindrivendev/Swashbuckle.AspNetCore#operation-filters. As of v3.0.0, the canonical way of accessing action or controller attributes in a filter is via context.MethodInfo.
Apologies for all the changes in this contract - I've tried to restrict the breaking ones to major version increments.