Skip to content

fix(Identifiable): handle null id values #395

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
Sep 4, 2018
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
3 changes: 3 additions & 0 deletions src/JsonApiDotNetCore/Models/Identifiable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public string StringId
/// </summary>
protected virtual string GetStringId(object value)
{
if(value == null)
return string.Empty;

var type = typeof(T);
var stringValue = value.ToString();

Expand Down
12 changes: 11 additions & 1 deletion test/UnitTests/Models/IdentifiableTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ public void Setting_StringId_To_Null_Sets_Id_As_Default()
Assert.Equal(0, resource.Id);
}

private class IntId : Identifiable { }
[Fact]
public void GetStringId_Returns_EmptyString_If_Object_Is_Null()
{
var resource = new IntId();
var stringId = resource.ExposedGetStringId(null);
Assert.Equal(string.Empty, stringId);
}

private class IntId : Identifiable {
public string ExposedGetStringId(object value) => GetStringId(value);
}
}
}