Skip to content

Commit e99fb18

Browse files
authored
Add more diagnostics to intermittently failing assert (#68414)
1 parent db56ff8 commit e99fb18

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonPropertyInfo.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,25 @@ private bool NumberHandingIsApplicable()
338338

339339
internal abstract object? GetValueAsObject(object obj);
340340

341+
#if DEBUG
342+
internal string GetDebugInfo(int indent = 0)
343+
{
344+
string ind = new string(' ', indent);
345+
StringBuilder sb = new();
346+
347+
sb.AppendLine($"{ind}{{");
348+
sb.AppendLine($"{ind} Name: {Name},");
349+
sb.AppendLine($"{ind} NameAsUtf8.Length: {(NameAsUtf8Bytes?.Length ?? -1)},");
350+
sb.AppendLine($"{ind} IsConfigured: {_isConfigured},");
351+
sb.AppendLine($"{ind} IsIgnored: {IsIgnored},");
352+
sb.AppendLine($"{ind} ShouldSerialize: {ShouldSerialize},");
353+
sb.AppendLine($"{ind} ShouldDeserialize: {ShouldDeserialize},");
354+
sb.AppendLine($"{ind}}}");
355+
356+
return sb.ToString();
357+
}
358+
#endif
359+
341360
internal bool HasGetter { get; set; }
342361
internal bool HasSetter { get; set; }
343362

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.Cache.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,14 @@ internal JsonPropertyInfo GetProperty(
187187
{
188188
if (propertyName.SequenceEqual(info.NameAsUtf8Bytes))
189189
{
190-
Debug.Assert(key == GetKey(info.NameAsUtf8Bytes.AsSpan()), "key does not match re-computed value for the same sequence (case-insensitive)");
190+
#if DEBUG
191+
ulong recomputedKey = GetKey(info.NameAsUtf8Bytes.AsSpan());
192+
if (key != recomputedKey)
193+
{
194+
string propertyNameStr = JsonHelpers.Utf8GetString(propertyName);
195+
Debug.Fail($"key {key} [propertyName={propertyNameStr}] does not match re-computed value {recomputedKey} for the same sequence (case-insensitive). {info.GetDebugInfo()}");
196+
}
197+
#endif
191198

192199
// Use the existing byte[] reference instead of creating another one.
193200
utf8PropertyName = info.NameAsUtf8Bytes!;
@@ -200,7 +207,14 @@ internal JsonPropertyInfo GetProperty(
200207
}
201208
else
202209
{
203-
Debug.Assert(key == GetKey(info.NameAsUtf8Bytes.AsSpan()), "key does not match re-computed value for the same sequence (case-sensitive)");
210+
#if DEBUG
211+
ulong recomputedKey = GetKey(info.NameAsUtf8Bytes.AsSpan());
212+
if (key != recomputedKey)
213+
{
214+
string propertyNameStr = JsonHelpers.Utf8GetString(propertyName);
215+
Debug.Fail($"key {key} [propertyName={propertyNameStr}] does not match re-computed value {recomputedKey} for the same sequence (case-sensitive). {info.GetDebugInfo()}");
216+
}
217+
#endif
204218
utf8PropertyName = info.NameAsUtf8Bytes;
205219
}
206220
}

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ internal string GetDebugInfo()
255255
bool propCacheInitialized = PropertyCache != null;
256256

257257
StringBuilder sb = new();
258-
sb.AppendLine($"{jtiTypeName} {{");
258+
sb.AppendLine("{");
259259
sb.AppendLine($" GetType: {jtiTypeName},");
260260
sb.AppendLine($" Type: {typeName},");
261261
sb.AppendLine($" ConverterStrategy: {strat},");
@@ -268,11 +268,8 @@ internal string GetDebugInfo()
268268
foreach (var property in PropertyCache!.List)
269269
{
270270
JsonPropertyInfo pi = property.Value!;
271-
sb.AppendLine($" {property.Key}: {{");
272-
sb.AppendLine($" Ignored: {pi.IsIgnored},");
273-
sb.AppendLine($" ShouldSerialize: {pi.ShouldSerialize},");
274-
sb.AppendLine($" ShouldDeserialize: {pi.ShouldDeserialize},");
275-
sb.AppendLine(" },");
271+
sb.AppendLine($" {property.Key}:");
272+
sb.AppendLine($"{pi.GetDebugInfo(indent: 6)},");
276273
}
277274

278275
sb.AppendLine(" },");

0 commit comments

Comments
 (0)