Skip to content

Use the method parameter's runtime (instance) type to find the validator instead of its declared type #46

@icnocop

Description

@icnocop

Hi.

Thank you for SharpGrip.FluentValidation.AutoValidation.

Given an ASP.NET MVC endpoint like:

// POST api/Animals
public IActionResult Post([FromBody] CreateAnimalRequest request)
{
    if (!this.ModelState.IsValid)
    {
        return this.ValidationProblem(this.ModelState);
    }
}

With types defined:

public class CreateAnimalRequest
{
}

public class CreatePersonRequest : CreateAnimalRequest
{
    public string Name { get; set; }
}

public class CreateAnimalRequestValidator : AbstractValidator<CreateAnimalRequest>
{
}

public class CreatePersonRequestValidator : AbstractValidator<CreatePersonRequest>
{
}

When an HTTP POST request is sent to api/Animals with the body:

{
    "@odata.type": "#Animals.CreatePersonRequest",
    "Name": "John Doe"
}

Then the CreateAnimalRequestValidator is unexpectedly being used for validation.

I expected CreatePersonRequestValidator to be used instead.

During runtime, request has the correct type CreatePersonRequest.

The problem seems to be that FluentValidationAutoValidationActionFilter.cs uses the method parameter's declared type to find the validator, but it should be using the method parameter's runtime (instance) type instead.

https://github.com/SharpGrip/FluentValidation.AutoValidation/blob/master/FluentValidation.AutoValidation.Mvc/src/Filters/FluentValidationAutoValidationActionFilter.cs#L66C58-L66C71

Thank you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions