Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MVC AutoValidation not performed with a null binding source. #38

Open
jp0550 opened this issue Sep 26, 2024 · 1 comment
Open

MVC AutoValidation not performed with a null binding source. #38

jp0550 opened this issue Sep 26, 2024 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@jp0550
Copy link

jp0550 commented Sep 26, 2024

I have a dotnet 8 MVC project I'm working with that uses binding that does define the source ([FromBody], [FromQuery]) and I have several hundred throughout the project like this

ex
public IActionResult PostForm(FormObject obj) { ...

As a result, the binding source is null (this is not an APIController so inferred binding rules are not applied). Debugging into the runtime shows that when the binding source is null, and is not inferred, the ValueProviders simply go through each one to binding where it can:

DefaultModelBindingContext.cs

private static IValueProvider FilterValueProvider(IValueProvider valueProvider, BindingSource? bindingSource)
{
    if (bindingSource == null || bindingSource.IsGreedy)
    {
        return valueProvider;
    }

    if (valueProvider is not IBindingSourceValueProvider bindingSourceValueProvider)
    {
        return valueProvider;
    }

    return bindingSourceValueProvider.Filter(bindingSource) ?? EmptyValueProvider;
}

My problem comes in FluentValidationAutoValidationActionFilter where is is trying to determine if it has a valid binding source, but null is not allowed.

                            if (subject != null && parameterType.IsCustomType() &&
                                !hasAutoValidateNeverAttribute && (hasAutoValidateAlwaysAttribute || HasValidBindingSource(bindingSource)) &&
                                serviceProvider.GetValidator(parameterType) is IValidator validator)

My workaround is to replace the built-in filter with a customer version above which accepts null as a valid binding source. Can this be provided by default with the library, or can you advise a solution that doesn't involve adding the alwaysvalidate attribute to every method that needs it? I attempted to add the InferParameterBindingInfoConvention that ApiControllers to use MVC controllers however it did not accurately determine the binding and broke most methods in the application.

Thanks!

@mvdgun
Copy link
Member

mvdgun commented Sep 28, 2024

Hi there, currently thinking about a EnableNullBindingSourceAutomaticValidation with a default value of false. So you could enable that one from the configuration. When enabled and the binding source is null the check will pass and your validators will run.

@mvdgun mvdgun self-assigned this Sep 28, 2024
@mvdgun mvdgun added the enhancement New feature or request label Sep 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants