Skip to content
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

Fix json Serializer (all Proprties should be used) #73121

Closed
wants to merge 5 commits into from
Closed
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
19 changes: 12 additions & 7 deletions src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1014,19 +1014,24 @@ private TypeGenerationSpec GetOrAddTypeGenerationSpec(Type type, JsonSourceGener
propGenSpecList = new List<PropertyGenerationSpec>();
Dictionary<string, PropertyGenerationSpec>? ignoredMembers = null;

const BindingFlags bindingFlags =
BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.DeclaredOnly;
const BindingFlags BindingFlagsDeclaredOnly =
BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.DeclaredOnly;

const BindingFlags BindingFlagsAll =
BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.NonPublic;

bool propertyOrderSpecified = false;

for (Type? currentType = type; currentType != null; currentType = currentType.BaseType)
{
PropertyGenerationSpec spec;

foreach (PropertyInfo propertyInfo in currentType.GetProperties(bindingFlags))
foreach (PropertyInfo propertyInfo in (currentType.IsInterface ? currentType.GetProperties(BindingFlagsAll) : currentType.GetProperties(BindingFlagsDeclaredOnly)))
{
bool isVirtual = propertyInfo.IsVirtual();

Expand All @@ -1040,7 +1045,7 @@ private TypeGenerationSpec GetOrAddTypeGenerationSpec(Type type, JsonSourceGener
CacheMemberHelper(propertyInfo.GetDiagnosticLocation());
}

foreach (FieldInfo fieldInfo in currentType.GetFields(bindingFlags))
foreach (FieldInfo fieldInfo in currentType.GetFields(BindingFlagsDeclaredOnly))
{
if (PropertyIsOverriddenAndIgnored(fieldInfo.Name, fieldInfo.FieldType, currentMemberIsVirtual: false, ignoredMembers))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,16 @@ private void AddPropertiesAndParametersUsingReflection()
{
Debug.Assert(PropertyInfoForTypeInfo.ConverterStrategy == ConverterStrategy.Object);

const BindingFlags BindingFlags =
BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.DeclaredOnly;
const BindingFlags BindingFlagsDeclaredOnly =
BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.DeclaredOnly;

const BindingFlags BindingFlagsAll =
BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.NonPublic;

Dictionary<string, JsonPropertyInfo>? ignoredMembers = null;
bool propertyOrderSpecified = false;
Expand All @@ -66,7 +71,7 @@ private void AddPropertiesAndParametersUsingReflection()
// Walk through the inheritance hierarchy, starting from the most derived type upward.
for (Type? currentType = Type; currentType != null; currentType = currentType.BaseType)
{
PropertyInfo[] properties = currentType.GetProperties(BindingFlags);
PropertyInfo[] properties = currentType.IsInterface ? currentType.GetProperties(BindingFlagsAll) : currentType.GetProperties(BindingFlagsDeclaredOnly);

// PropertyCache is not accessed by other threads until the current JsonTypeInfo instance
// is finished initializing and added to the cache in JsonSerializerOptions.
Expand Down Expand Up @@ -106,7 +111,7 @@ private void AddPropertiesAndParametersUsingReflection()
}
}

foreach (FieldInfo fieldInfo in currentType.GetFields(BindingFlags))
foreach (FieldInfo fieldInfo in currentType.GetFields(BindingFlagsDeclaredOnly))
{
string fieldName = fieldInfo.Name;

Expand Down