Swashbuckle.aspnetcore: Wrong obsolete information or missing method?

Created on 24 Jul 2018  路  1Comment  路  Source: domaindrivendev/Swashbuckle.AspNetCore

Using version 3.0

Having this code:
var hasAuthorize = context.ApiDescription.ControllerAttributes().OfType<AuthorizeAttribute>().Any();

Will give you the following obsolete warning:

Deprecated: Use TryGetMethodInfo

This is however not possible since TryGetMethodInfo will return

controllerActionDescriptor.MethodInfo

and the original method would return

controllerActionDescriptor.ControllerTypeInfo

as can be seen here:
https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/9e19afa50d70c9ef646fa41ee65290163de0e20a/src/Swashbuckle.AspNetCore.SwaggerGen/Generator/ApiDescriptionExtensions.cs#L19

Solution:
I would suggest adding the following method to the class:

        public static bool TryGetControllerTypeInfo(this ApiDescription apiDescription, out ControllerTypeInfo controllerTypeInfo)
        {
            var controllerActionDescriptor = apiDescription.ActionDescriptor as ControllerActionDescriptor;

            controllerTypeInfo = controllerActionDescriptor?.ControllerTypeInfo;

            return (controllerTypeInfo != null);
        }

and change the obsolete text to

Deprecated: Use TryGetControllerTypeInfo

Most helpful comment

To get the controller attributes you just need to dig a little deeper on the MethodInfo:

var controllerAttributes = context.MethodInfo.DeclaringType.GetTypeInfo().GetCustomAttributes(true);

>All comments

To get the controller attributes you just need to dig a little deeper on the MethodInfo:

var controllerAttributes = context.MethodInfo.DeclaringType.GetTypeInfo().GetCustomAttributes(true);
Was this page helpful?
0 / 5 - 0 ratings