Skip to content

Enable property visibility tests in source-gen mode #54526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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