Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net Core Validation Problem State - Binding validation not returning problem details

Same question posted here: https://github.com/aspnet/Mvc/issues/8564

I have an issue where when execution hits the controller and my code explicitly returns the ValidationProblemDetails response.

However, when binding validation prevents execution getting to the controller, I get the following JSON response (standard model state validation object).

{
  "Email": [
    "Invalid email address"
  ]
}

Why doesn't it return the validation problem details in the response?

I'm using the Microsoft.AspNetCore.App 2.1.4 package.

Request Model

public class RegistrationRequest
{
    [Description("Given Name")]
    [MaxLength(100)]
    [Required(ErrorMessage = "Given Name is required")]
    public string GivenName { get; set; }

    [MaxLength(100)]
    [Required(ErrorMessage = "Surname is required")]
    public string Surname { get; set; }

    [MaxLength(255)]
    [Required(ErrorMessage = "Email is required")]
    [EmailAddress(ErrorMessage = "Invalid email address")]
    public string Email { get; set; }

    [Required(ErrorMessage = "Password is required")]
    public string Password { get; set; }

    [Description("Confirm Password")]
    [Compare(nameof(Password), ErrorMessage = "Passwords do not match")]
    public string ConfirmPassword { get; set; }
}

Startup

public class Startup
{
    public IServiceProvider ConfigureServices(IServiceCollection services)
    {
        ...
        services.Configure<ApiBehaviorOptions>(options =>
        {
            options.InvalidModelStateResponseFactory = context =>
            {
                var problemDetails = new ValidationProblemDetails(context.ModelState)
                {
                    Instance = context.HttpContext.Request.Path,
                    Status = (int)HttpStatusCode.BadRequest,
                    Detail = "Please refer to the errors property for additional details"
                };

                return new BadRequestObjectResult(problemDetails)
                {
                    ContentTypes = "applicaton/json"
                };
            };
        });
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        ...
    }
}

Controller

[ApiController]
[Authorize]
[Route("users")]
public sealed class UserController : Controller
{
    public UserController(
        UserManager userManager,
        IMapper mappingProvider)
    {
        Manager = userManager;
        Mapper = mappingProvider;
    }

    private UserManager Manager { get; }
    private IMapper Mapper { get; }

    [HttpPost]
    [AllowAnonymous]
    [Consumes("application/json")]
    [Produces("application/json")]
    [ProducesResponseType(200)]
    [ProducesResponseType(400, Type = typeof(ValidationProblemDetails))]
    public async Task<IActionResult> Post([FromBody]ApiModels.RegistrationRequest request)
    {
        if (request == null) throw new ArgumentNullException(nameof(request));

        var user = Mapper.Map<DataModels.User>(request);

        var result = await Manager.Create(user, request.Password); // return OperationResult

        return result.ToActionResult();
    }
}

Extension methods to convert OperationResult to IActionResult

public static class OperationResultExtensions
{
    public static ValidationProblemDetails ToProblemDetails(this OperationResult result)
    {
        if (result == null) throw new ArgumentNullException(nameof(result));

        var problemDetails = new ValidationProblemDetails()
        {
            Status = (int)HttpStatusCode.BadRequest
        };

        if (problemDetails.Errors != null)
        {
            result.Errors
               .ToList()
               .ForEach(i => problemDetails.Errors.Add(i.Key, i.Value.ToArray()));
        }

        return problemDetails;
    }

    public static IActionResult ToActionResult(this OperationResult result)
    {
        switch (result.Status)
        {
            case HttpStatusCode.OK:
                return new OkResult();

            case HttpStatusCode.NotFound:
                return new NotFoundResult();

            case HttpStatusCode.BadRequest:
                var problems = result.ToProblemDetails();
                return new BadRequestObjectResult(problems);

            default:
                return new StatusCodeResult((int)result.Status);
        }
    }
}
like image 870
TheMagnificent11 Avatar asked Oct 07 '18 18:10

TheMagnificent11


1 Answers

You have Configure<ApiBehaviorOptions> before AddMvc: The call to AddMvc registers a class that implements IConfigureOptions<ApiBehaviorOptions>, which ends up overwriting the instance you've configured using services.Configure<ApiBehaviorOptions> and effectively resets the factory function.

All you need to do in order to get this working is switch the order in ConfigureServices:

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

services.Configure<ApiBehaviorOptions>(options =>
{
    options.InvalidModelStateResponseFactory = context =>
    {
        ...
    };
});

I've also noticed that although it's not exactly bold, the docs suggests that this ordering is important too:

Add the following code in Startup.ConfigureServices after services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

services.Configure<ApiBehaviorOptions>(options => ... );

like image 171
Kirk Larkin Avatar answered Sep 18 '22 00:09

Kirk Larkin