From b7b7515c08ca6ae5441d3d2f4e92dd9c710005c7 Mon Sep 17 00:00:00 2001 From: Bart Koelman Date: Wed, 8 Apr 2020 15:10:04 +0200 Subject: [PATCH] Pass boolean instead of options to exception constructor --- .../Controllers/BaseJsonApiController.cs | 4 +-- .../Exceptions/InvalidModelStateException.cs | 30 +++++++++++-------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/JsonApiDotNetCore/Controllers/BaseJsonApiController.cs b/src/JsonApiDotNetCore/Controllers/BaseJsonApiController.cs index b90c9a4991..99fb055737 100644 --- a/src/JsonApiDotNetCore/Controllers/BaseJsonApiController.cs +++ b/src/JsonApiDotNetCore/Controllers/BaseJsonApiController.cs @@ -114,7 +114,7 @@ public virtual async Task PostAsync([FromBody] T entity) throw new ResourceIdInPostRequestNotAllowedException(); if (_jsonApiOptions.ValidateModelState && !ModelState.IsValid) - throw new InvalidModelStateException(ModelState, typeof(T), _jsonApiOptions); + throw new InvalidModelStateException(ModelState, typeof(T), _jsonApiOptions.IncludeExceptionStackTraceInErrors); entity = await _create.CreateAsync(entity); @@ -130,7 +130,7 @@ public virtual async Task PatchAsync(TId id, [FromBody] T entity) throw new InvalidRequestBodyException(null, null, null); if (_jsonApiOptions.ValidateModelState && !ModelState.IsValid) - throw new InvalidModelStateException(ModelState, typeof(T), _jsonApiOptions); + throw new InvalidModelStateException(ModelState, typeof(T), _jsonApiOptions.IncludeExceptionStackTraceInErrors); var updatedEntity = await _update.UpdateAsync(id, entity); return Ok(updatedEntity); diff --git a/src/JsonApiDotNetCore/Exceptions/InvalidModelStateException.cs b/src/JsonApiDotNetCore/Exceptions/InvalidModelStateException.cs index 577e987572..cbb717786c 100644 --- a/src/JsonApiDotNetCore/Exceptions/InvalidModelStateException.cs +++ b/src/JsonApiDotNetCore/Exceptions/InvalidModelStateException.cs @@ -3,7 +3,6 @@ using System.Linq; using System.Net; using System.Reflection; -using JsonApiDotNetCore.Configuration; using JsonApiDotNetCore.Models; using JsonApiDotNetCore.Models.JsonApiDocuments; using Microsoft.AspNetCore.Mvc.ModelBinding; @@ -17,13 +16,14 @@ public class InvalidModelStateException : Exception { public IList Errors { get; } - public InvalidModelStateException(ModelStateDictionary modelState, Type resourceType, IJsonApiOptions options) + public InvalidModelStateException(ModelStateDictionary modelState, Type resourceType, + bool includeExceptionStackTraceInErrors) { - Errors = FromModelState(modelState, resourceType, options); + Errors = FromModelState(modelState, resourceType, includeExceptionStackTraceInErrors); } private static List FromModelState(ModelStateDictionary modelState, Type resourceType, - IJsonApiOptions options) + bool includeExceptionStackTraceInErrors) { List errors = new List(); @@ -31,9 +31,10 @@ private static List FromModelState(ModelStateDictionary modelState, Type { var propertyName = pair.Key; PropertyInfo property = resourceType.GetProperty(propertyName); - + // TODO: Need access to ResourceContext here, in order to determine attribute name when not explicitly set. - string attributeName = property?.GetCustomAttribute().PublicAttributeName ?? property?.Name; + string attributeName = + property?.GetCustomAttribute().PublicAttributeName ?? property?.Name; foreach (var modelError in pair.Value.Errors) { @@ -43,7 +44,7 @@ private static List FromModelState(ModelStateDictionary modelState, Type } else { - errors.Add(FromModelError(modelError, attributeName, options)); + errors.Add(FromModelError(modelError, attributeName, includeExceptionStackTraceInErrors)); } } } @@ -51,19 +52,22 @@ private static List FromModelState(ModelStateDictionary modelState, Type return errors; } - private static Error FromModelError(ModelError modelError, string attributeName, IJsonApiOptions options) + private static Error FromModelError(ModelError modelError, string attributeName, + bool includeExceptionStackTraceInErrors) { var error = new Error(HttpStatusCode.UnprocessableEntity) { Title = "Input validation failed.", Detail = modelError.ErrorMessage, - Source = attributeName == null ? null : new ErrorSource - { - Pointer = $"/data/attributes/{attributeName}" - } + Source = attributeName == null + ? null + : new ErrorSource + { + Pointer = $"/data/attributes/{attributeName}" + } }; - if (options.IncludeExceptionStackTraceInErrors && modelError.Exception != null) + if (includeExceptionStackTraceInErrors && modelError.Exception != null) { error.Meta.IncludeExceptionStackTrace(modelError.Exception); }