Skip to content

Commit c24befc

Browse files
Resolving object type property error
1 parent 5c394f0 commit c24befc

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

Magic.IndexedDb/Helpers/PropertyMappingCache.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ public static bool IsSimpleType(Type type)
174174
if (Nullable.GetUnderlyingType(type) is Type underlyingType)
175175
type = underlyingType;
176176

177+
// Anonymous = complex
178+
if (IsAnonymousType(type))
179+
return true;
180+
177181
// Unwrap System.Text.Json.Nodes.JsonValueCustomized<T> (avoiding .FullName for perf)
178182
if (type.IsGenericType)
179183
{
@@ -207,6 +211,16 @@ public static bool IsSimpleType(Type type)
207211
return result;
208212
}
209213

214+
private static bool IsAnonymousType(Type type)
215+
{
216+
return Attribute.IsDefined(type, typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute))
217+
&& type.IsGenericType
218+
&& type.Name.Contains("AnonymousType")
219+
&& (type.Name.StartsWith("<>") || type.Name.StartsWith("VB$"))
220+
&& type.Namespace == null;
221+
}
222+
223+
210224

211225

212226

Magic.IndexedDb/Testing/Helpers/TestHelper.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,28 @@ private static List<string> CompareObjects(object expected, object actual, Prope
119119
continue;
120120
}
121121

122+
Type propType = property.PropertyType;
123+
124+
// 🔥 Special case: if it's object or anonymous, compare via JSON string
125+
if (propType == typeof(object) || IsAnonymousType(expectedValue.GetType()) || IsAnonymousType(actualValue.GetType()))
126+
{
127+
var expectedJson = JsonSerializer.Serialize(expectedValue);
128+
var actualJson = JsonSerializer.Serialize(actualValue);
129+
130+
if (expectedJson != actualJson)
131+
{
132+
differences.Add($" - ❌ {path}.{property.Name}: Expected JSON **[{expectedJson}]**, but got **[{actualJson}]**.");
133+
}
134+
135+
continue;
136+
}
137+
138+
// 💡 Deep compare if complex
122139
if (!expectedValue.Equals(actualValue))
123140
{
124-
if (PropertyMappingCache.IsComplexType(property.PropertyType))
141+
if (PropertyMappingCache.IsComplexType(propType))
125142
{
126-
var nestedProperties = property.PropertyType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
143+
var nestedProperties = propType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
127144
.Where(prop => !Attribute.IsDefined(prop, typeof(MagicNotMappedAttribute)))
128145
.ToArray();
129146

@@ -139,5 +156,13 @@ private static List<string> CompareObjects(object expected, object actual, Prope
139156

140157
return differences;
141158
}
159+
private static bool IsAnonymousType(Type type)
160+
{
161+
return Attribute.IsDefined(type, typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute))
162+
&& type.IsGenericType
163+
&& type.Name.Contains("AnonymousType")
164+
&& (type.Name.StartsWith("<>") || type.Name.StartsWith("VB$"))
165+
&& type.Namespace == null;
166+
}
142167
}
143168
}

0 commit comments

Comments
 (0)