Skip to content

Commit

Permalink
Enable property visibility tests in source-gen mode (#54526)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
layomia committed Jul 13, 2021
1 parent 6696768 commit 7b19cce
Show file tree
Hide file tree
Showing 67 changed files with 4,140 additions and 3,024 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,51 +47,63 @@ private static JsonPropertyInfo[] PersonPropInitFunc(JsonSerializerContext conte
properties[0] = JsonMetadataServices.CreatePropertyInfo(
options,
isProperty: true,
isPublic: true,
isVirtual: false,
declaringType: typeof(Person),
propertyTypeInfo: jsonContext.Int32,
converter: null,
getter: static (obj) => { return ((Person)obj).Age; },
setter: static (obj, value) => { ((Person)obj).Age = value; },
ignoreCondition: default,
hasJsonInclude: false,
numberHandling: default,
propertyName: nameof(Tests.Person.Age),
jsonPropertyName: null);

properties[1] = JsonMetadataServices.CreatePropertyInfo(
options,
isProperty: true,
isPublic: true,
isVirtual: false,
declaringType: typeof(Person),
propertyTypeInfo: jsonContext.String,
converter: null,
getter: static (obj) => { return ((Person)obj).Name; },
setter: static (obj, value) => { ((Person)obj).Name = value; },
ignoreCondition: default,
hasJsonInclude: false,
numberHandling: default,
propertyName: nameof(Tests.Person.Name),
jsonPropertyName: null);

properties[2] = JsonMetadataServices.CreatePropertyInfo(
options,
isProperty: true,
isPublic: true,
isVirtual: false,
declaringType: typeof(Person),
propertyTypeInfo: jsonContext.Person,
converter: null,
getter: static (obj) => { return ((Person)obj).Parent; },
setter: static (obj, value) => { ((Person)obj).Parent = value; },
ignoreCondition: default,
hasJsonInclude: false,
numberHandling: default,
propertyName: nameof(Tests.Person.Parent),
jsonPropertyName: null);

properties[3] = JsonMetadataServices.CreatePropertyInfo(
options,
isProperty: true,
isPublic: true,
isVirtual: false,
declaringType: typeof(Person),
propertyTypeInfo: jsonContext.String,
converter: null,
getter: static (obj) => { return ((Person)obj).PlaceOfBirth; },
setter: static (obj, value) => { ((Person)obj).PlaceOfBirth = value; },
ignoreCondition: default,
hasJsonInclude: false,
numberHandling: default,
propertyName: nameof(Tests.Person.PlaceOfBirth),
jsonPropertyName: null);
Expand Down
28 changes: 28 additions & 0 deletions src/libraries/System.Text.Json/Common/JsonHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;

namespace System.Text.Json
{
internal static partial class JsonHelpers
{
/// <summary>
/// Emulates Dictionary.TryAdd on netstandard.
/// </summary>
public static bool TryAdd<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, in TKey key, in TValue value) where TKey : notnull
{
#if NETSTANDARD2_0 || NETFRAMEWORK
if (!dictionary.ContainsKey(key))
{
dictionary[key] = value;
return true;
}

return false;
#else
return dictionary.TryAdd(key, value);
#endif
}
}
}
17 changes: 17 additions & 0 deletions src/libraries/System.Text.Json/Common/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Reflection;

namespace System.Text.Json.Reflection
{
internal static partial class ReflectionExtensions
{
public static bool IsVirtual(this PropertyInfo? propertyInfo)
{
Debug.Assert(propertyInfo != null);
return propertyInfo != null && (propertyInfo.GetMethod?.IsVirtual == true || propertyInfo.SetMethod?.IsVirtual == true);
}
}
}
4 changes: 3 additions & 1 deletion src/libraries/System.Text.Json/gen/ContextGenerationSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using System.Collections.Generic;
using System.Text.Json.Serialization;
using System.Text.Json.SourceGeneration.Reflection;
using System.Text.Json.Reflection;

namespace System.Text.Json.SourceGeneration
{
Expand All @@ -19,6 +19,8 @@ internal sealed class ContextGenerationSpec

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

public HashSet<TypeGenerationSpec>? NullableUnderlyingTypes { get; } = new();

public List<string> ContextClassDeclarationList { get; init; }

/// <summary>
Expand Down
Loading

0 comments on commit 7b19cce

Please sign in to comment.