Skip to content

chore(JsonApiContext): improve error handling if resource not defined… #195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 7 additions & 17 deletions src/JsonApiDotNetCore/Internal/ContextGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,23 @@ namespace JsonApiDotNetCore.Internal
public class ContextGraph : IContextGraph
{
public List<ContextEntity> 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>(TParent entity, string relationshipName)
{
var parentEntityType = entity.GetType();

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);
Expand All @@ -42,11 +34,9 @@ public string GetRelationshipName<TParent>(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;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/JsonApiDotNetCore/Services/JsonApiContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public IJsonApiContext ApplyContext<T>(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('/');
Expand Down