RicoSuter / NJsonSchema

JSON Schema reader, generator and validator for .NET
http://NJsonSchema.org
MIT License
1.37k stars 529 forks source link

Bug: Public Fields not showing up when using System.Text.Json #1680

Open brandonryan opened 5 months ago

brandonryan commented 5 months ago

Public fields aren't being generated in the output schema when SerializerOptions.IncludeFields = true.

Reproduction: https://dotnetfiddle.net/amyqcs

Assuming it has something to do with SystemTextJsonReflectionService? I think I narrowed it down to this line:

if (accessorInfo.MemberInfo.DeclaringType != contextualType.Type ||
    (accessorInfo.MemberInfo is FieldInfo fieldInfo && (fieldInfo.IsPrivate || fieldInfo.IsStatic || !fieldInfo.IsDefined(typeof(DataMemberAttribute)))))
{
    continue;
}

I would expect it to not require the DataMemberAttribute if IncludeFields is set. Exiting too early.

I did notice there were no tests for this specific thing in System.Text.Json Tests so I think it just fell through the cracks.