-
-
Notifications
You must be signed in to change notification settings - Fork 158
Return 404 or 200 on error/non existent resource #636
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
Conversation
@bart-degreed anything you're missing? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comments. I only tested getting a single missing item, for example /books/123.
responseContent = JsonConvert.SerializeObject(context.Object); | ||
var requestedModel = _currentRequest.GetRequestResource(); | ||
var errors = new ErrorCollection(); | ||
errors.Add(new Error(404, $"The resource with type '{requestedModel.ResourceName}' and id '{_currentRequest.BaseId}' could not be found")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The resource of type...
and should "id" be uppercase?
} | ||
else | ||
{ | ||
if (_getRelationship == null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check is duplicated in both true
and false
blocks of the outer if
statement. I believe it can be moved to the top, outside the if
clauses, so it appears only once.
They are actually different: Relationship vs Relationships
|
||
namespace JsonApiDotNetCore.Internal | ||
{ | ||
public class ResourceNotFoundException : Exception |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This type appears to be unused and can be removed.
private readonly ILogger _logger; | ||
|
||
public DefaultExceptionFilter(ILoggerFactory loggerFactory) | ||
public DefaultExceptionFilter(ILoggerFactory loggerFactory, ICurrentRequest currentRequest) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The private field that is assigned from this parameter is never used.
@@ -110,6 +110,10 @@ public ResourceLinks GetResourceLinks(string resourceName, string id) | |||
/// <inheritdoc/> | |||
public RelationshipLinks GetRelationshipLinks(RelationshipAttribute relationship, IIdentifiable parent) | |||
{ | |||
if(parent == null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there should be a space between the if
keyword and the opening parenthesis.
|
||
// lol | ||
var relationshipValue = typeof(TResource).GetProperty(relationship.InternalRelationshipName).GetValue(entity) ; | ||
var relEmpty = relationshipValue == null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused variable
if (entity == null) | ||
|
||
|
||
// lol |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unclear comment
return null; | ||
} | ||
var listCast = (IList) relationshipValue; | ||
if(listCast != null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Expression is always true. And should test for IEnumerable
instead of IList
.
} | ||
|
||
//if (entity == null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was this block commented out? Should it be removed instead?
I did some more testing today and found that this PR throws a var relationshipValue = typeof(TResource).GetProperty(relationship.InternalRelationshipName).GetValue(entity) ; in the debugger, public sealed class Pathway : Identifiable
{
[NotMapped]
[HasManyThrough("tags", nameof(Tags))]
public List<Tag> JsonTags { get; set; }
public List<PathwayTag> Tags { get; set; }
} where
So I tried to fix this by adding this: object relationshipValue;
if (relationship is HasManyThroughAttribute hasManyThroughRelationship)
{
relationshipValue = hasManyThroughRelationship.ThroughProperty.GetValue(entity);
}
else
{
relationshipValue = typeof(TResource).GetProperty(relationship.InternalRelationshipName).GetValue(entity);
} this works, unless there is no match found. In that case, this method returns // triggered by GET /articles/1/{relationshipName}
public virtual async Task<object> GetRelationshipAsync(TId id, string relationshipName)
{
var relationship = GetRelationship(relationshipName);
var resource = await GetRelationshipsAsync(id, relationshipName);
return relationship.GetValue(resource);
} because |
Hi @wisepotato I believe I fixed all issues as part of #714. See the tests in FetchingRelationshipsTests and the last two of UpdatingRelationshipsTests. Would you be okay if we close this PR? If I've missed anything, please let me know. |
Closes #631
Problem
Upon getting to a resource that is non-existent, the following object is returned:
Proposed solution
When a
200
is applicable, i.e. the route exists, but nothing can be returned, i.e. when a user with id1
exists and a GET request is sent tohttp://example.com/users/1/books
. But it should return a 404 if user1
does not exist, also forhttp://example.com/users/1
.We also require a descriptive error message, instead of just returning 404.
Done
200
withnull
for data. ( GET/api/v1/todoItems/412/oneToOnePerson
)200
with[]
for data ( GET/api/v1/todoItems/1/stakeHolders
)200
withnull
for data. ( GET/api/v1/todoItems/412/relationships/oneToOnePerson
)200
with[]
for data ( GET/api/v1/todoItems/1/relationships/stakeHolders
)