Skip to content
Merged
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
30 changes: 25 additions & 5 deletions src/JsonApiDotNetCore/Models/Identifiable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,55 @@
namespace JsonApiDotNetCore.Models
{
public class Identifiable : Identifiable<int>
{}
{ }

public class Identifiable<T> : IIdentifiable<T>
{
/// <summary>
/// The resource identifier
/// </summary>
public virtual T Id { get; set; }

/// <summary>
/// The string representation of the `Id`.
///
/// This is used in serialization and deserialization.
/// The getters should handle the conversion
/// from `typeof(T)` to a string and the setter vice versa.
///
/// To override this behavior, you can either implement the
/// <see cref="IIdentifiable{T}"> interface directly or override
/// `GetStringId` and `GetTypedId` methods.
/// </summary>
[NotMapped]
public string StringId
{
get => GetStringId(Id);
set => Id = GetTypedId(value);
}

/// <summary>
/// Convert the provided resource identifier to a string.
/// </summary>
protected virtual string GetStringId(object value)
{
var type = typeof(T);
var stringValue = value.ToString();

if(type == typeof(Guid))
if (type == typeof(Guid))
{
var guid = Guid.Parse(stringValue);
return guid == Guid.Empty ? string.Empty : stringValue;
}

return stringValue == "0"
? string.Empty
return stringValue == "0"
? string.Empty
: stringValue;
}

/// <summary>
/// Convert a string to a typed resource identifier.
/// </summary>
protected virtual T GetTypedId(string value)
{
var convertedValue = TypeHelper.ConvertType(value, typeof(T));
Expand Down