Skip to content

Commit a4d45b2

Browse files
authored
Add more diagnostics to LookupProperty (more diagnostics to flaky tests) (#68281)
* add more diagnostics to LookupProperty flaky tests * Fix nullability issue on Release build
1 parent c78bf2f commit a4d45b2

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandlePropertyName.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ internal static JsonPropertyInfo LookupProperty(
2727
#if DEBUG
2828
if (state.Current.JsonTypeInfo.PropertyInfoForTypeInfo.ConverterStrategy != ConverterStrategy.Object)
2929
{
30-
Debug.Fail(GetLookupPropertyDebugInfo(obj, unescapedPropertyName, ref state));
30+
string objTypeName = obj?.GetType().FullName ?? "<null>";
31+
Debug.Fail($"obj.GetType() => {objTypeName}; {state.Current.JsonTypeInfo.GetPropertyDebugInfo(unescapedPropertyName)}");
3132
}
3233
#endif
3334

@@ -68,16 +69,6 @@ internal static JsonPropertyInfo LookupProperty(
6869
return jsonPropertyInfo;
6970
}
7071

71-
#if DEBUG
72-
private static string GetLookupPropertyDebugInfo(object? obj, ReadOnlySpan<byte> unescapedPropertyName, ref ReadStack state)
73-
{
74-
JsonTypeInfo jti = state.Current.JsonTypeInfo;
75-
string objTypeName = obj?.GetType().FullName ?? "<null>";
76-
string propertyName = JsonHelpers.Utf8GetString(unescapedPropertyName);
77-
return $"ConverterStrategy is {jti.PropertyInfoForTypeInfo.ConverterStrategy}. propertyName = {propertyName}; obj.GetType() => {objTypeName}; DebugInfo={jti.GetDebugInfo()}";
78-
}
79-
#endif
80-
8172
[MethodImpl(MethodImplOptions.AggressiveInlining)]
8273
internal static ReadOnlySpan<byte> GetPropertyName(
8374
ref ReadStack state,

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,17 +172,22 @@ internal JsonPropertyInfo GetProperty(
172172
}
173173

174174
// No cached item was found. Try the main dictionary which has all of the properties.
175-
Debug.Assert(PropertyCache != null);
175+
#if DEBUG
176+
if (PropertyCache == null)
177+
{
178+
Debug.Fail($"Property cache is null. {GetPropertyDebugInfo(propertyName)}");
179+
}
180+
#endif
176181

177-
if (PropertyCache.TryGetValue(JsonHelpers.Utf8GetString(propertyName), out JsonPropertyInfo? info))
182+
if (PropertyCache!.TryGetValue(JsonHelpers.Utf8GetString(propertyName), out JsonPropertyInfo? info))
178183
{
179-
Debug.Assert(info != null);
184+
Debug.Assert(info != null, "PropertyCache contains null JsonPropertyInfo");
180185

181186
if (Options.PropertyNameCaseInsensitive)
182187
{
183188
if (propertyName.SequenceEqual(info.NameAsUtf8Bytes))
184189
{
185-
Debug.Assert(key == GetKey(info.NameAsUtf8Bytes.AsSpan()));
190+
Debug.Assert(key == GetKey(info.NameAsUtf8Bytes.AsSpan()), "key does not match re-computed value for the same sequence (case-insensitive)");
186191

187192
// Use the existing byte[] reference instead of creating another one.
188193
utf8PropertyName = info.NameAsUtf8Bytes!;
@@ -195,7 +200,7 @@ internal JsonPropertyInfo GetProperty(
195200
}
196201
else
197202
{
198-
Debug.Assert(key == GetKey(info.NameAsUtf8Bytes.AsSpan()));
203+
Debug.Assert(key == GetKey(info.NameAsUtf8Bytes.AsSpan()), "key does not match re-computed value for the same sequence (case-sensitive)");
199204
utf8PropertyName = info.NameAsUtf8Bytes;
200205
}
201206
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,12 @@ internal virtual void Configure()
241241
}
242242

243243
#if DEBUG
244+
internal string GetPropertyDebugInfo(ReadOnlySpan<byte> unescapedPropertyName)
245+
{
246+
string propertyName = JsonHelpers.Utf8GetString(unescapedPropertyName);
247+
return $"propertyName = {propertyName}; DebugInfo={GetDebugInfo()}";
248+
}
249+
244250
internal string GetDebugInfo()
245251
{
246252
ConverterStrategy strat = PropertyInfoForTypeInfo.ConverterStrategy;

0 commit comments

Comments
 (0)