JsonObjectSerializer ignores fields regardless of JsonIgnore.Condition
See original GitHub issueDescribe the bug
Using FieldBuilder
to build a search indexer, a property on my class with attribute [JsonIgnore(Condition = JsonIgnoreCondition.Never)]
is not included in the returned fields.
I dug in and found the issue to be the following code in JsonObjectSerializer
:
It checks for presence of JsonIgnoreAttribute
, but does not look at the Condition
passed.
Expected behavior
Properties with a JsonIgnore(Condition = JsonIgnoreCondition.Never)
attribute are serialized by JsonObjectSerializer
and FieldBuilder
picks up the field when building a search index.
Actual behavior (include Exception or Stack Trace)
Fields with JsonIgnoreAttribute
are unconditionally ignored by JsonObjectSerializer
(and by extension by FieldBuilder
with a serializer of that type).
To Reproduce
class A
{
[JsonIgnore(Condition = JsonIgnoreCondition.Never)]
public string Field { get; set; }
}
var fb = new FieldBuilder();
builder.Serializer = new JsonObjectSerializer();
var fields = builder.build(typeof(A));
// `fields` should contain A.Field, but it does not.
Environment:
- Name and version of the Library package used: Azure.Search.Document 11.2.0
- Hosting platform or OS and .NET runtime version (
dotnet --info
output for .NET Core projects):
.NET SDK (reflecting any global.json):
Version: 5.0.100
Commit: 5044b93829
Runtime Environment:
OS Name: Windows
OS Version: 10.0.19041
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\5.0.100\
Host (useful for support):
Version: 5.0.0
Commit: cf258a14b7
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (4 by maintainers)
Top GitHub Comments
I adapted the original code from some Newtonsoft.Json and didn’t realize System.Text.Json.JsonIgnoreAttribute had a condition. Thanks for pointing this out. In this case, it seems that if the attribute is declared and the value is
Always
we’ll ignore. The other values besidesNever
can only be known at runtime per instance, while this functionality is for for type member discovery.Are you able to work around this for now? @pakrym when do you think you’ll next ship an update to Azure.Core?
Thank you for your feedback. Tagging and routing to the team member best able to assist.