Skip to content

Commit

Permalink
Small fix to InvalidStateFactory to take ValidationProblems triggered…
Browse files Browse the repository at this point in the history
… from inside of a controller action into account.
  • Loading branch information
KevinDockx committed May 14, 2020
1 parent 53ebc2d commit e39d73e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CourseLibrary.API.ResourceParameters
{
public class AuthorsResourceParameters
{
{
public string MainCategory { get; set; }
public string SearchQuery { get; set; }
public string SearchQuery { get; set; }
}
}
13 changes: 11 additions & 2 deletions Finished sample/CourseLibrary/CourseLibrary.API/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,17 @@ public void ConfigureServices(IServiceCollection services)

// if there are modelstate errors & all keys were correctly
// found/parsed we're dealing with validation errors
if ((context.ModelState.ErrorCount > 0) &&
(actionExecutingContext?.ActionArguments.Count == context.ActionDescriptor.Parameters.Count))
//
// if the context couldn't be cast to an ActionExecutingContext
// because it's a ControllerContext, we're dealing with an issue
// that happened after the initial input was correctly parsed.
// This happens, for example, when manually validating an object inside
// of a controller action. That means that by then all keys
// WERE correctly found and parsed. In that case, we're
// thus also dealing with a validation error.
if (context.ModelState.ErrorCount > 0 &&
(context is ControllerContext ||
actionExecutingContext?.ActionArguments.Count == context.ActionDescriptor.Parameters.Count))
{
problemDetails.Type = "https://courselibrary.com/modelvalidationproblem";
problemDetails.Status = StatusCodes.Status422UnprocessableEntity;
Expand Down

0 comments on commit e39d73e

Please sign in to comment.