Skip to content

Commit a0a4e51

Browse files
committed
fix(identifiable): use a string id instead of the object id
includes conversion methods
1 parent 41a793d commit a0a4e51

File tree

4 files changed

+57
-20
lines changed

4 files changed

+57
-20
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Reflection;
3+
4+
namespace JsonApiDotNetCore.Internal
5+
{
6+
public static class TypeHelper
7+
{
8+
public static object ConvertType(object value, Type type)
9+
{
10+
if(value == null)
11+
return null;
12+
13+
if (type.GetTypeInfo().IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
14+
type = Nullable.GetUnderlyingType(type);
15+
16+
var stringValue = value.ToString();
17+
18+
if(type == typeof(Guid))
19+
return Guid.Parse(stringValue);
20+
21+
return Convert.ChangeType(stringValue, type);
22+
}
23+
}
24+
}

src/JsonApiDotNetCore/Models/IIdentifiable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ namespace JsonApiDotNetCore.Models
22
{
33
public interface IIdentifiable
44
{
5-
object Id { get; set; }
5+
string Id { get; set; }
66
}
77

88
public interface IIdentifiable<T> : IIdentifiable
Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
using System;
2+
using System.Reflection;
3+
using JsonApiDotNetCore.Internal;
4+
15
namespace JsonApiDotNetCore.Models
26
{
37
public class Identifiable : Identifiable<int>
@@ -7,10 +11,31 @@ public class Identifiable<T> : IIdentifiable<T>, IIdentifiable
711
{
812
public virtual T Id { get; set; }
913

10-
object IIdentifiable.Id
14+
string IIdentifiable.Id
15+
{
16+
get { return GetStringId(Id); }
17+
set { Id = (T)GetConcreteId(value); }
18+
}
19+
20+
protected virtual string GetStringId(object value)
21+
{
22+
var type = typeof(T);
23+
var stringValue = value.ToString();
24+
25+
if(type == typeof(Guid))
26+
{
27+
var guid = Guid.Parse(stringValue);
28+
return (guid == Guid.Empty ? string.Empty : stringValue);
29+
}
30+
31+
if(stringValue == "0") return string.Empty;
32+
33+
return stringValue;
34+
}
35+
36+
protected virtual object GetConcreteId(string value)
1137
{
12-
get { return Id; }
13-
set { Id = (T)value; }
38+
return TypeHelper.ConvertType(value, typeof(T));
1439
}
1540
}
1641
}

src/JsonApiDotNetCore/Serialization/JsonApiDeSerializer.cs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private static object DataToObject(DocumentData data, IJsonApiContext context)
6060
var identifiableEntity = (IIdentifiable)entity;
6161

6262
if (data.Id != null)
63-
identifiableEntity.Id = ChangeType(data.Id, identifiableEntity.Id.GetType());
63+
identifiableEntity.Id = data.Id;
6464

6565
return identifiableEntity;
6666
}
@@ -80,7 +80,7 @@ private static object _setEntityAttributes(
8080
object newValue;
8181
if (attributeValues.TryGetValue(attr.PublicAttributeName.Dasherize(), out newValue))
8282
{
83-
var convertedValue = ChangeType(newValue, entityProperty.PropertyType);
83+
var convertedValue = TypeHelper.ConvertType(newValue, entityProperty.PropertyType);
8484
entityProperty.SetValue(entity, convertedValue);
8585
}
8686
}
@@ -112,27 +112,15 @@ private static object _setRelationships(
112112
if (data == null) continue;
113113

114114
var newValue = data["id"];
115-
var convertedValue = ChangeType(newValue, entityProperty.PropertyType);
115+
var convertedValue = TypeHelper.ConvertType(newValue, entityProperty.PropertyType);
116116
entityProperty.SetValue(entity, convertedValue);
117117
}
118118
}
119119

120120
return entity;
121121
}
122122

123-
private static object ChangeType(object value, Type conversion)
124-
{
125-
var t = conversion;
126-
127-
if (t.GetTypeInfo().IsGenericType && t.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
128-
{
129-
if (value == null)
130-
return null;
131123

132-
t = Nullable.GetUnderlyingType(t);
133-
}
134-
135-
return Convert.ChangeType(value, t);
136-
}
124+
137125
}
138126
}

0 commit comments

Comments
 (0)