Skip to content

Commit

Permalink
Merge pull request #395 from json-api-dotnet/fix/#382
Browse files Browse the repository at this point in the history
fix(Identifiable): handle null id values
  • Loading branch information
jaredcnance authored Sep 4, 2018
2 parents b073a0b + 5965fb3 commit 468b761
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
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);
}
}
}

0 comments on commit 468b761

Please sign in to comment.