Skip to content

Commit

Permalink
add controller detection in line with .net controller conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
mvdgun committed Dec 4, 2024
1 parent 3b79ae3 commit 55bb386
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using System;
using System.Linq;
using System.Threading.Tasks;
using FluentValidation;
using Microsoft.AspNetCore.Http;
Expand Down Expand Up @@ -31,7 +32,7 @@ public FluentValidationAutoValidationActionFilter(IFluentValidationAutoValidatio

public async Task OnActionExecutionAsync(ActionExecutingContext actionExecutingContext, ActionExecutionDelegate next)

Check warning on line 33 in FluentValidation.AutoValidation.Mvc/src/Filters/FluentValidationAutoValidationActionFilter.cs

View workflow job for this annotation

GitHub Actions / build

Refactor this method to reduce its Cognitive Complexity from 50 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)

Check warning on line 33 in FluentValidation.AutoValidation.Mvc/src/Filters/FluentValidationAutoValidationActionFilter.cs

View workflow job for this annotation

GitHub Actions / build

Rename parameter 'actionExecutingContext' to 'context' to match the interface declaration. (https://rules.sonarsource.com/csharp/RSPEC-927)

Check warning on line 33 in FluentValidation.AutoValidation.Mvc/src/Filters/FluentValidationAutoValidationActionFilter.cs

View workflow job for this annotation

GitHub Actions / build

Refactor this method to reduce its Cognitive Complexity from 50 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)
{
if (actionExecutingContext.Controller is ControllerBase || actionExecutingContext.Controller.GetType().HasCustomAttribute<ControllerAttribute>())
if (IsValidController(actionExecutingContext.Controller))
{
var endpoint = actionExecutingContext.HttpContext.GetEndpoint();
var controllerActionDescriptor = (ControllerActionDescriptor) actionExecutingContext.ActionDescriptor;
Expand Down Expand Up @@ -119,6 +120,21 @@ public async Task OnActionExecutionAsync(ActionExecutingContext actionExecutingC
await next();
}

private bool IsValidController(object controller)

Check warning on line 123 in FluentValidation.AutoValidation.Mvc/src/Filters/FluentValidationAutoValidationActionFilter.cs

View workflow job for this annotation

GitHub Actions / build

Make 'IsValidController' a static method. (https://rules.sonarsource.com/csharp/RSPEC-2325)
{
var controllerType = controller.GetType();

if (controllerType.HasCustomAttribute<NonControllerAttribute>())
{
return false;
}

return controller is ControllerBase ||
controllerType.HasCustomAttribute<ControllerAttribute>() ||
controllerType.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase) ||
controllerType.InheritsFromTypeWithNameEndingIn("Controller");
}

private bool HasValidBindingSource(BindingSource? bindingSource)
{
return (autoValidationMvcConfiguration.EnableBodyBindingSourceAutomaticValidation && bindingSource == BindingSource.Body) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,20 @@ public static bool HasCustomAttribute<TAttribute>(this Type type) where TAttribu
{
return type.CustomAttributes.Any(attribute => attribute.AttributeType == typeof(TAttribute));
}

public static bool InheritsFromTypeWithNameEndingIn(this Type type, string name)
{
while (type.BaseType != null)
{
type = type.BaseType;

if (type.Name.EndsWith(name, StringComparison.OrdinalIgnoreCase))
{
return true;
}
}

return false;
}
}
}

0 comments on commit 55bb386

Please sign in to comment.