Skip to content

Commit a31f52a

Browse files
committed
chore(JsonApiContext): improve error handling if resource not defined on context graph
1 parent ca954e0 commit a31f52a

File tree

2 files changed

+9
-17
lines changed

2 files changed

+9
-17
lines changed

src/JsonApiDotNetCore/Internal/ContextGraph.cs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,23 @@ namespace JsonApiDotNetCore.Internal
88
public class ContextGraph : IContextGraph
99
{
1010
public List<ContextEntity> Entities { get; set; }
11-
public bool UsesDbContext { get; set; }
11+
public bool UsesDbContext { get; set; }
1212

1313
public ContextEntity GetContextEntity(string entityName)
14-
{
15-
return Entities
16-
.FirstOrDefault(e =>
17-
e.EntityName.ToLower() == entityName.ToLower());
18-
}
14+
=> Entities.SingleOrDefault(e => string.Equals(e.EntityName, entityName, StringComparison.OrdinalIgnoreCase));
1915

2016
public ContextEntity GetContextEntity(Type entityType)
21-
{
22-
return Entities
23-
.FirstOrDefault(e =>
24-
e.EntityType == entityType);
25-
}
17+
=> Entities.SingleOrDefault(e => e.EntityType == entityType);
2618

2719
public object GetRelationship<TParent>(TParent entity, string relationshipName)
2820
{
2921
var parentEntityType = entity.GetType();
3022

3123
var navigationProperty = parentEntityType
3224
.GetProperties()
33-
.FirstOrDefault(p => p.Name.ToLower() == relationshipName.ToLower());
25+
.SingleOrDefault(p => string.Equals(p.Name, relationshipName, StringComparison.OrdinalIgnoreCase));
3426

35-
if(navigationProperty == null)
27+
if (navigationProperty == null)
3628
throw new JsonApiException(400, $"{parentEntityType} does not contain a relationship named {relationshipName}");
3729

3830
return navigationProperty.GetValue(entity);
@@ -42,11 +34,9 @@ public string GetRelationshipName<TParent>(string relationshipName)
4234
{
4335
var entityType = typeof(TParent);
4436
return Entities
45-
.FirstOrDefault(e =>
46-
e.EntityType == entityType)
37+
.SingleOrDefault(e => e.EntityType == entityType)
4738
.Relationships
48-
.FirstOrDefault(r =>
49-
r.PublicRelationshipName.ToLower() == relationshipName.ToLower())
39+
.SingleOrDefault(r => string.Equals(r.PublicRelationshipName, relationshipName, StringComparison.OrdinalIgnoreCase))
5040
?.InternalRelationshipName;
5141
}
5242
}

src/JsonApiDotNetCore/Services/JsonApiContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ public IJsonApiContext ApplyContext<T>(object controller)
6262

6363
_controllerContext.ControllerType = controller.GetType();
6464
_controllerContext.RequestEntity = ContextGraph.GetContextEntity(typeof(T));
65+
if (_controllerContext.RequestEntity == null)
66+
throw new JsonApiException(500, $"A resource has not been properly defined for type '{typeof(T)}'. Ensure it has been registered on the ContextGraph.");
6567

6668
var context = _httpContextAccessor.HttpContext;
6769
var path = context.Request.Path.Value.Split('/');

0 commit comments

Comments
 (0)