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

Net8 FluentAutovalidation and the default ModelStateInvalidFilter #17

Closed
David-Mulder-at-VLK opened this issue Oct 16, 2023 · 4 comments · Fixed by #18
Closed

Net8 FluentAutovalidation and the default ModelStateInvalidFilter #17

David-Mulder-at-VLK opened this issue Oct 16, 2023 · 4 comments · Fixed by #18
Assignees
Labels
bug Something isn't working
Milestone

Comments

@David-Mulder-at-VLK
Copy link

LS,
I have a Net 8 api project with an ApiController with the following method

[HttpGet("{companyName}/{groupName}")]

public async Task<IActionResult> GetData([FromRoute] string companyName, [FromRoute] string groupName)

I have FluentValidationAutoValidation configured just as the example in the ReadMe, so with

// Disable the built-in .NET model (data annotations) validation.
configuration.DisableBuiltInModelValidation = true;

// Only validate controllers decorated with the FluentValidationAutoValidation attribute.
configuration.ValidationStrategy = ValidationStrategy.Annotations;

The ApiControler has not been decorated with the [FluentValidationAutoValidation] attribute.

When calling the api, I get a 400 error. In the logging I see:

...
Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder: Debug: Done attempting to validate the bound parameter 'companyName' of type 'System.String'.
...
Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder: Debug: Done attempting to validate the bound parameter 'groupName' of type 'System.String'.
...
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Trace: Action Filter: Before executing OnActionExecuting on filter Microsoft.AspNetCore.Mvc.ModelBinding.UnsupportedContentTypeFilter.
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Trace: Action Filter: After executing OnActionExecuting on filter Microsoft.AspNetCore.Mvc.ModelBinding.UnsupportedContentTypeFilter.
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Trace: Action Filter: Before executing OnActionExecutionAsync on filter SharpGrip.FluentValidation.AutoValidation.Mvc.Filters.FluentValidationAutoValidationActionFilter.
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Trace: Action Filter: Before executing OnActionExecuting on filter Microsoft.AspNetCore.Mvc.Infrastructure.ModelStateInvalidFilter.
Microsoft.AspNetCore.Mvc.Infrastructure.ModelStateInvalidFilter: Debug: The request has model state errors, returning an error response.
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Trace: Action Filter: After executing OnActionExecuting on filter Microsoft.AspNetCore.Mvc.Infrastructure.ModelStateInvalidFilter.
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Debug: Request was short circuited at action filter 'Microsoft.AspNetCore.Mvc.Infrastructure.ModelStateInvalidFilter'.
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Trace: Action Filter: After executing OnActionExecutionAsync on filter SharpGrip.FluentValidation.AutoValidation.Mvc.Filters.FluentValidationAutoValidationActionFilter.
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Trace: Action Filter: Before executing OnActionExecuted on filter Microsoft.AspNetCore.Mvc.ModelBinding.UnsupportedContentTypeFilter.
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Trace: Action Filter: After executing OnActionExecuted on filter Microsoft.AspNetCore.Mvc.ModelBinding.UnsupportedContentTypeFilter.
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Trace: Result Filter: Before executing OnResultExecuting on filter Microsoft.AspNetCore.Mvc.Infrastructure.ClientErrorResultFilter.
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Trace: Result Filter: After executing OnResultExecuting on filter Microsoft.AspNetCore.Mvc.Infrastructure.ClientErrorResultFilter.
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Trace: Result Filter: Before executing OnResultExecuting on filter Microsoft.AspNetCore.Mvc.ProducesAttribute.
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Trace: Result Filter: After executing OnResultExecuting on filter Microsoft.AspNetCore.Mvc.ProducesAttribute.
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Trace: Before executing action result Microsoft.AspNetCore.Mvc.BadRequestObjectResult.

So somehow the default MVC ModelStateInvalidFilter now determines that the model is invalid.
If I set DisableBuiltInModelValidation to false, this behavior disappears.

Any ideas?

Thanks,
David.

@mvdgun
Copy link
Member

mvdgun commented Oct 16, 2023

Hi @David-Mulder-at-VLK, which version of the library are you using?

@David-Mulder-at-VLK
Copy link
Author

Hi @mvdgun ,
Version 1.3.0 of SharpGrip.FluentValidation.AutoValidation.Mvc and FluentValidation.AspNetCore Version 11.3.0".

@David-Mulder-at-VLK David-Mulder-at-VLK changed the title Net8 FuluentAutovalidation and the default ModelStateInvalidFilter Net8 FluentAutovalidation and the default ModelStateInvalidFilter Oct 16, 2023
@mvdgun mvdgun self-assigned this Oct 19, 2023
@mvdgun mvdgun added this to the v1.3 milestone Oct 19, 2023
@mvdgun mvdgun added the bug Something isn't working label Oct 19, 2023
@mvdgun
Copy link
Member

mvdgun commented Oct 19, 2023

Hi @David-Mulder-at-VLK, I just released v1.3.1 of the library fixing your issue.

@David-Mulder-at-VLK
Copy link
Author

Hi Mauro @mvdgun ,
Thanks a lot for this quick response and fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants