Skip to content

Commit 4d34dcd

Browse files
author
Bart Koelman
committed
Added extension method to obtain ClrType of a resource instance (we'll need this later)
1 parent 3a2a399 commit 4d34dcd

File tree

9 files changed

+24
-16
lines changed

9 files changed

+24
-16
lines changed

src/JsonApiDotNetCore/AtomicOperations/LocalIdValidator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ private void AssertLocalIdIsAssigned(IIdentifiable resource)
9696
{
9797
if (resource.LocalId != null)
9898
{
99-
ResourceType resourceType = _resourceGraph.GetResourceType(resource.GetType());
99+
ResourceType resourceType = _resourceGraph.GetResourceType(resource.GetClrType());
100100
_localIdTracker.GetValue(resource.LocalId, resourceType);
101101
}
102102
}

src/JsonApiDotNetCore/AtomicOperations/OperationsProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ private void AssignStringId(IIdentifiable resource)
140140
{
141141
if (resource.LocalId != null)
142142
{
143-
ResourceType resourceType = _resourceGraph.GetResourceType(resource.GetType());
143+
ResourceType resourceType = _resourceGraph.GetResourceType(resource.GetClrType());
144144
resource.StringId = _localIdTracker.GetValue(resource.LocalId, resourceType);
145145
}
146146
}

src/JsonApiDotNetCore/Controllers/BaseJsonApiOperationsController.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ protected virtual void ValidateModelState(IList<OperationContainer> operations)
148148
Dictionary<string, ModelStateEntry?> modelStateDictionary = requestModelState.ToDictionary(tuple => tuple.key, tuple => tuple.entry);
149149

150150
throw new InvalidModelStateException(modelStateDictionary, typeof(IList<OperationContainer>), _options.IncludeExceptionStackTraceInErrors,
151-
_resourceGraph, (collectionType, index) => collectionType == typeof(IList<OperationContainer>) ? operations[index].Resource.GetType() : null);
151+
_resourceGraph,
152+
(collectionType, index) => collectionType == typeof(IList<OperationContainer>) ? operations[index].Resource.GetClrType() : null);
152153
}
153154
}
154155

src/JsonApiDotNetCore/Repositories/DbContextExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static IIdentifiable GetTrackedOrAttach(this DbContext dbContext, IIdenti
3535
ArgumentGuard.NotNull(dbContext, nameof(dbContext));
3636
ArgumentGuard.NotNull(identifiable, nameof(identifiable));
3737

38-
Type resourceClrType = identifiable.GetType();
38+
Type resourceClrType = identifiable.GetClrType();
3939
string? stringId = identifiable.StringId;
4040

4141
EntityEntry? entityEntry = dbContext.ChangeTracker.Entries().FirstOrDefault(entry => IsResource(entry, resourceClrType, stringId));

src/JsonApiDotNetCore/Resources/IdentifiableComparer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public bool Equals(IIdentifiable? left, IIdentifiable? right)
2222
return true;
2323
}
2424

25-
if (left is null || right is null || left.GetType() != right.GetType())
25+
if (left is null || right is null || left.GetClrType() != right.GetClrType())
2626
{
2727
return false;
2828
}
@@ -38,6 +38,6 @@ public bool Equals(IIdentifiable? left, IIdentifiable? right)
3838
public int GetHashCode(IIdentifiable obj)
3939
{
4040
// LocalId is intentionally omitted here, it is okay for hashes to collide.
41-
return HashCode.Combine(obj.GetType(), obj.StringId);
41+
return HashCode.Combine(obj.GetClrType(), obj.StringId);
4242
}
4343
}

src/JsonApiDotNetCore/Resources/IdentifiableExtensions.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ public static object GetTypedId(this IIdentifiable identifiable)
1111
{
1212
ArgumentGuard.NotNull(identifiable, nameof(identifiable));
1313

14-
PropertyInfo? property = identifiable.GetType().GetProperty(IdPropertyName);
14+
PropertyInfo? property = identifiable.GetClrType().GetProperty(IdPropertyName);
1515

1616
if (property == null)
1717
{
18-
throw new InvalidOperationException($"Resource of type '{identifiable.GetType()}' does not contain a property named '{IdPropertyName}'.");
18+
throw new InvalidOperationException($"Resource of type '{identifiable.GetClrType()}' does not contain a property named '{IdPropertyName}'.");
1919
}
2020

2121
object? propertyValue = property.GetValue(identifiable);
@@ -27,11 +27,18 @@ public static object GetTypedId(this IIdentifiable identifiable)
2727

2828
if (Equals(propertyValue, defaultValue))
2929
{
30-
throw new InvalidOperationException($"Property '{identifiable.GetType().Name}.{IdPropertyName}' should " +
30+
throw new InvalidOperationException($"Property '{identifiable.GetClrType().Name}.{IdPropertyName}' should " +
3131
$"have been assigned at this point, but it contains its default {property.PropertyType.Name} value '{propertyValue}'.");
3232
}
3333
}
3434

3535
return propertyValue!;
3636
}
37+
38+
public static Type GetClrType(this IIdentifiable identifiable)
39+
{
40+
ArgumentGuard.NotNull(identifiable, nameof(identifiable));
41+
42+
return identifiable.GetType();
43+
}
3744
}

src/JsonApiDotNetCore/Resources/ResourceDefinitionAccessor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public void OnDeserialize(IIdentifiable resource)
183183
{
184184
ArgumentGuard.NotNull(resource, nameof(resource));
185185

186-
dynamic resourceDefinition = ResolveResourceDefinition(resource.GetType());
186+
dynamic resourceDefinition = ResolveResourceDefinition(resource.GetClrType());
187187
resourceDefinition.OnDeserialize((dynamic)resource);
188188
}
189189

@@ -192,7 +192,7 @@ public void OnSerialize(IIdentifiable resource)
192192
{
193193
ArgumentGuard.NotNull(resource, nameof(resource));
194194

195-
dynamic resourceDefinition = ResolveResourceDefinition(resource.GetType());
195+
dynamic resourceDefinition = ResolveResourceDefinition(resource.GetClrType());
196196
resourceDefinition.OnSerialize((dynamic)resource);
197197
}
198198

src/JsonApiDotNetCore/Serialization/Response/ResourceObjectTreeNode.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,19 +251,19 @@ private ResourceObjectComparer()
251251
{
252252
}
253253

254-
public bool Equals(ResourceObject? x, ResourceObject? y)
254+
public bool Equals(ResourceObject? left, ResourceObject? right)
255255
{
256-
if (ReferenceEquals(x, y))
256+
if (ReferenceEquals(left, right))
257257
{
258258
return true;
259259
}
260260

261-
if (x is null || y is null || x.GetType() != y.GetType())
261+
if (left is null || right is null || left.GetType() != right.GetType())
262262
{
263263
return false;
264264
}
265265

266-
return x.Type == y.Type && x.Id == y.Id && x.Lid == y.Lid;
266+
return left.Type == right.Type && left.Id == right.Id && left.Lid == right.Lid;
267267
}
268268

269269
public int GetHashCode(ResourceObject obj)

src/JsonApiDotNetCore/Serialization/Response/ResponseModelAdapter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ private ResourceObjectTreeNode GetOrCreateTreeNode(IIdentifiable resource, Resou
187187

188188
private static ResourceType GetEffectiveResourceType(IIdentifiable resource, ResourceType declaredType)
189189
{
190-
Type runtimeResourceType = resource.GetType();
190+
Type runtimeResourceType = resource.GetClrType();
191191

192192
if (declaredType.ClrType == runtimeResourceType)
193193
{

0 commit comments

Comments
 (0)