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

JsonIgnore attribute is enforced even when conditions are not met #906

Open
Flimzes opened this issue Jun 8, 2023 · 2 comments
Open

JsonIgnore attribute is enforced even when conditions are not met #906

Flimzes opened this issue Jun 8, 2023 · 2 comments

Comments

@Flimzes
Copy link

Flimzes commented Jun 8, 2023

The new recommended way of defining that null items should be ignored by the json serializer (System.Text.Json) is to add a JsonIgnore attribute, with a condition, like this:
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]

A JsonSerializerOptions instance can be used instead, but there is currently no way of applying this to the built in httpClient methods - GetFromJson, PostAsJson and PutAsJson

Official documentation here: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/ignore-properties?pivots=dotnet-7-0

However, the odata client does not respect this condition, and ignores the property during deserialization and serialization despite the property having a value.

@swidz
Copy link

swidz commented Jun 17, 2023

Today I faced the same issue:

I was trying to decorate entity properties as follows

class MyEntity
{
 [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
 public Guid MyProperty{ get; set; }
}

The expected behaviour is not to send this property when trying to insert/update if it is not set.
But when getting list of entities from a GET query and it was a part of the response it should be set.

Currently is is ignored in both POST and GET calls

@swidz
Copy link

swidz commented Jun 17, 2023

This issue was also described in #888

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants