question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JsonObjectSerializer ignores fields regardless of JsonIgnore.Condition

See original GitHub issue

Describe 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:

https://github.com/Azure/azure-sdk-for-net/blob/3a97c6f4823f03f321b4f88edc6a6282c7445bcd/sdk/core/Azure.Core/src/Serialization/JsonObjectSerializer.cs#L107-L110

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:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
heathscommented, Mar 15, 2021

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 besides Never 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?

1reaction
jsquirecommented, Mar 15, 2021

Thank you for your feedback. Tagging and routing to the team member best able to assist.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - @JsonIgnore with Conditions
I've tried using @JSONIgnore, but that simply ignores the element regardless of conditions. I'm new to this space. Any ideas?
Read more >
How to ignore properties with System.Text.Json
To ignore individual properties, use the [JsonIgnore] attribute. The following example shows a type to serialize. It also shows the JSON ...
Read more >
How can we ignore the fields during JSON serialization in ...
If there are fields in Java objects that do not wish to be serialized, we can use the @JsonIgnore annotation in the Jackson...
Read more >
Jackson Ignore Properties on Marshalling
This tutorial will show how to ignore certain fields when serializing an object to JSON using Jackson 2.x.
Read more >
Spring Boot — Dynamically ignore fields while serializing ...
Jackson library provides @JsonIgnore & @JsonIgnoreProperties annotations to ignore fields while serializing One catch with @JsonIgnore ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found