Skip to content

Commit 7b19cce

Browse files
authored
Enable property visibility tests in source-gen mode (#54526)
* Enable property visibility tests in source-gen mode * Address feedback * Fix S.N.H.Json tests * Fix S.T.J tests * Fix S.N.H.J tests ii
1 parent 6696768 commit 7b19cce

File tree

67 files changed

+4140
-3024
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+4140
-3024
lines changed

src/libraries/System.Net.Http.Json/tests/FunctionalTests/JsonContext/Person.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,51 +47,63 @@ private static JsonPropertyInfo[] PersonPropInitFunc(JsonSerializerContext conte
4747
properties[0] = JsonMetadataServices.CreatePropertyInfo(
4848
options,
4949
isProperty: true,
50+
isPublic: true,
51+
isVirtual: false,
5052
declaringType: typeof(Person),
5153
propertyTypeInfo: jsonContext.Int32,
5254
converter: null,
5355
getter: static (obj) => { return ((Person)obj).Age; },
5456
setter: static (obj, value) => { ((Person)obj).Age = value; },
5557
ignoreCondition: default,
58+
hasJsonInclude: false,
5659
numberHandling: default,
5760
propertyName: nameof(Tests.Person.Age),
5861
jsonPropertyName: null);
5962

6063
properties[1] = JsonMetadataServices.CreatePropertyInfo(
6164
options,
6265
isProperty: true,
66+
isPublic: true,
67+
isVirtual: false,
6368
declaringType: typeof(Person),
6469
propertyTypeInfo: jsonContext.String,
6570
converter: null,
6671
getter: static (obj) => { return ((Person)obj).Name; },
6772
setter: static (obj, value) => { ((Person)obj).Name = value; },
6873
ignoreCondition: default,
74+
hasJsonInclude: false,
6975
numberHandling: default,
7076
propertyName: nameof(Tests.Person.Name),
7177
jsonPropertyName: null);
7278

7379
properties[2] = JsonMetadataServices.CreatePropertyInfo(
7480
options,
7581
isProperty: true,
82+
isPublic: true,
83+
isVirtual: false,
7684
declaringType: typeof(Person),
7785
propertyTypeInfo: jsonContext.Person,
7886
converter: null,
7987
getter: static (obj) => { return ((Person)obj).Parent; },
8088
setter: static (obj, value) => { ((Person)obj).Parent = value; },
8189
ignoreCondition: default,
90+
hasJsonInclude: false,
8291
numberHandling: default,
8392
propertyName: nameof(Tests.Person.Parent),
8493
jsonPropertyName: null);
8594

8695
properties[3] = JsonMetadataServices.CreatePropertyInfo(
8796
options,
8897
isProperty: true,
98+
isPublic: true,
99+
isVirtual: false,
89100
declaringType: typeof(Person),
90101
propertyTypeInfo: jsonContext.String,
91102
converter: null,
92103
getter: static (obj) => { return ((Person)obj).PlaceOfBirth; },
93104
setter: static (obj, value) => { ((Person)obj).PlaceOfBirth = value; },
94105
ignoreCondition: default,
106+
hasJsonInclude: false,
95107
numberHandling: default,
96108
propertyName: nameof(Tests.Person.PlaceOfBirth),
97109
jsonPropertyName: null);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Collections.Generic;
5+
6+
namespace System.Text.Json
7+
{
8+
internal static partial class JsonHelpers
9+
{
10+
/// <summary>
11+
/// Emulates Dictionary.TryAdd on netstandard.
12+
/// </summary>
13+
public static bool TryAdd<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, in TKey key, in TValue value) where TKey : notnull
14+
{
15+
#if NETSTANDARD2_0 || NETFRAMEWORK
16+
if (!dictionary.ContainsKey(key))
17+
{
18+
dictionary[key] = value;
19+
return true;
20+
}
21+
22+
return false;
23+
#else
24+
return dictionary.TryAdd(key, value);
25+
#endif
26+
}
27+
}
28+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Diagnostics;
5+
using System.Reflection;
6+
7+
namespace System.Text.Json.Reflection
8+
{
9+
internal static partial class ReflectionExtensions
10+
{
11+
public static bool IsVirtual(this PropertyInfo? propertyInfo)
12+
{
13+
Debug.Assert(propertyInfo != null);
14+
return propertyInfo != null && (propertyInfo.GetMethod?.IsVirtual == true || propertyInfo.SetMethod?.IsVirtual == true);
15+
}
16+
}
17+
}

src/libraries/System.Text.Json/gen/ContextGenerationSpec.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
using System.Collections.Generic;
55
using System.Text.Json.Serialization;
6-
using System.Text.Json.SourceGeneration.Reflection;
6+
using System.Text.Json.Reflection;
77

88
namespace System.Text.Json.SourceGeneration
99
{
@@ -19,6 +19,8 @@ internal sealed class ContextGenerationSpec
1919

2020
public List<TypeGenerationSpec> RootSerializableTypes { get; } = new();
2121

22+
public HashSet<TypeGenerationSpec>? NullableUnderlyingTypes { get; } = new();
23+
2224
public List<string> ContextClassDeclarationList { get; init; }
2325

2426
/// <summary>

0 commit comments

Comments
 (0)