Skip to content

fix: non-generic id check in PostAsync #54

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

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 2 additions & 3 deletions src/JsonApiDotNetCore/Controllers/JsonApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,8 @@ public virtual async Task<IActionResult> PostAsync([FromBody] T entity)
_logger?.LogInformation($"Entity cannot be null returning 422");
return UnprocessableEntity();
}

var stringId = entity.Id.ToString();
if(stringId.Length > 0 && stringId != "0")

if(!entity.IsIdEmpty())
return Forbidden();

await _entities.CreateAsync(entity);
Expand Down
1 change: 1 addition & 0 deletions src/JsonApiDotNetCore/Models/IIdentifiable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace JsonApiDotNetCore.Models
public interface IIdentifiable
{
object Id { get; set; }
bool IsIdEmpty();
}

public interface IIdentifiable<T> : IIdentifiable
Expand Down
5 changes: 5 additions & 0 deletions src/JsonApiDotNetCore/Models/Identifiable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@ object IIdentifiable.Id
get { return Id; }
set { Id = (T)value; }
}

public bool IsIdEmpty()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will need to be virtual so users can override it. Also, since this won't work "out-of-the-box" for a lot of types. I'd like to add support for common, non-nullable types. I can work on this tonight.

{
return Id != null;
}
}
}