From d620f1e0bd7e87cdd0aad859391a4b3b4bba0952 Mon Sep 17 00:00:00 2001 From: jaredcnance Date: Tue, 14 Nov 2017 21:04:46 -0600 Subject: [PATCH] chore(JsonApiContext): improve error handling if resource not defined on context graph --- .../Internal/ContextGraph.cs | 24 ++++++------------- .../Services/JsonApiContext.cs | 2 ++ 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/src/JsonApiDotNetCore/Internal/ContextGraph.cs b/src/JsonApiDotNetCore/Internal/ContextGraph.cs index aae5c2179b..fd29794194 100644 --- a/src/JsonApiDotNetCore/Internal/ContextGraph.cs +++ b/src/JsonApiDotNetCore/Internal/ContextGraph.cs @@ -8,21 +8,13 @@ namespace JsonApiDotNetCore.Internal public class ContextGraph : IContextGraph { public List Entities { get; set; } - public bool UsesDbContext { get; set; } + public bool UsesDbContext { get; set; } public ContextEntity GetContextEntity(string entityName) - { - return Entities - .FirstOrDefault(e => - e.EntityName.ToLower() == entityName.ToLower()); - } + => Entities.SingleOrDefault(e => string.Equals(e.EntityName, entityName, StringComparison.OrdinalIgnoreCase)); public ContextEntity GetContextEntity(Type entityType) - { - return Entities - .FirstOrDefault(e => - e.EntityType == entityType); - } + => Entities.SingleOrDefault(e => e.EntityType == entityType); public object GetRelationship(TParent entity, string relationshipName) { @@ -30,9 +22,9 @@ public object GetRelationship(TParent entity, string relationshipName) var navigationProperty = parentEntityType .GetProperties() - .FirstOrDefault(p => p.Name.ToLower() == relationshipName.ToLower()); + .SingleOrDefault(p => string.Equals(p.Name, relationshipName, StringComparison.OrdinalIgnoreCase)); - if(navigationProperty == null) + if (navigationProperty == null) throw new JsonApiException(400, $"{parentEntityType} does not contain a relationship named {relationshipName}"); return navigationProperty.GetValue(entity); @@ -42,11 +34,9 @@ public string GetRelationshipName(string relationshipName) { var entityType = typeof(TParent); return Entities - .FirstOrDefault(e => - e.EntityType == entityType) + .SingleOrDefault(e => e.EntityType == entityType) .Relationships - .FirstOrDefault(r => - r.PublicRelationshipName.ToLower() == relationshipName.ToLower()) + .SingleOrDefault(r => string.Equals(r.PublicRelationshipName, relationshipName, StringComparison.OrdinalIgnoreCase)) ?.InternalRelationshipName; } } diff --git a/src/JsonApiDotNetCore/Services/JsonApiContext.cs b/src/JsonApiDotNetCore/Services/JsonApiContext.cs index a93d76acdc..b223e47bf5 100644 --- a/src/JsonApiDotNetCore/Services/JsonApiContext.cs +++ b/src/JsonApiDotNetCore/Services/JsonApiContext.cs @@ -62,6 +62,8 @@ public IJsonApiContext ApplyContext(object controller) _controllerContext.ControllerType = controller.GetType(); _controllerContext.RequestEntity = ContextGraph.GetContextEntity(typeof(T)); + if (_controllerContext.RequestEntity == null) + throw new JsonApiException(500, $"A resource has not been properly defined for type '{typeof(T)}'. Ensure it has been registered on the ContextGraph."); var context = _httpContextAccessor.HttpContext; var path = context.Request.Path.Value.Split('/');