Skip to content

Commit 468b761

Browse files
authored
Merge pull request #395 from json-api-dotnet/fix/#382
fix(Identifiable): handle null id values
2 parents b073a0b + 5965fb3 commit 468b761

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

Diff for: src/JsonApiDotNetCore/Models/Identifiable.cs

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public string StringId
3737
/// </summary>
3838
protected virtual string GetStringId(object value)
3939
{
40+
if(value == null)
41+
return string.Empty;
42+
4043
var type = typeof(T);
4144
var stringValue = value.ToString();
4245

Diff for: test/UnitTests/Models/IdentifiableTests.cs

+11-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ public void Setting_StringId_To_Null_Sets_Id_As_Default()
2121
Assert.Equal(0, resource.Id);
2222
}
2323

24-
private class IntId : Identifiable { }
24+
[Fact]
25+
public void GetStringId_Returns_EmptyString_If_Object_Is_Null()
26+
{
27+
var resource = new IntId();
28+
var stringId = resource.ExposedGetStringId(null);
29+
Assert.Equal(string.Empty, stringId);
30+
}
31+
32+
private class IntId : Identifiable {
33+
public string ExposedGetStringId(object value) => GetStringId(value);
34+
}
2535
}
2636
}

0 commit comments

Comments
 (0)