Skip to content

fixes issue 727 [JsonIgnore] attribute ignored when using $expand - #728

Merged
xuzhg merged 4 commits into
OData:mainfrom
mattperdeck:main
Dec 9, 2022
Merged

fixes issue 727 [JsonIgnore] attribute ignored when using $expand#728
xuzhg merged 4 commits into
OData:mainfrom
mattperdeck:main

Conversation

@mattperdeck

Copy link
Copy Markdown
Contributor

Fixes #727

Cause of the bug

I found that when using $expand, the MapProperty methods of the default implementations for IPropertyMapper (for System.Text.Json and Newtonsoft.Json) contain code to interpret attributes applied to fields in the view model on their own. That is, they do not rely on the standard libraries to serialise the object, apply attributes, etc.

Also, the only attribute they support is the property to rename a field - JsonProperty for Newtonsoft.Json, JsonPropertyName for System.Text.Json. Every other attribute would be ignored, including JsonIgnore. This explains why the bug is happening.

Solution in this pull request

  1. Updated the MapProperty methods, so they check whether the field has the JsonIgnore attribute, and if so, return null (to indicate to ignore the field).
  2. Updated the ToDictionary.... methods calling MapProperty so they no longer throw when MapProperty returns null. Instead, they simply do not add the field to the dictionairy. Also updated comments and error messages to reflect this change in the contract of the MapProperty method.
  3. Updated affected unit tests, making sure that the new contract for MapProperty etc. is tested properly. Added unit tests for Newtonsoft Json version of MapProperty.

…ow return null if the field should not be serialized at all (as in, ignored)
…Newtonsoft.Json and System.Text), so they return null if the field has the JsonIgnore attribute
…ull if the field has the JsonIgnore attribute
IEdmProperty property = _type.Properties().Single(s => s.Name == propertyName);
PropertyInfo info = GetPropertyInfo(property);

JsonIgnoreAttribute jsonIgnore = GetJsonIgnore(info);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really the direction we want to go? Are we planning to support all System.Text.Json attributes?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is newtonsoft related package.
I think we should support most of the newtonsoft property related attributes.

@corranrogue9 corranrogue9 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to decide our policy on which attributes we are supporting

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

Successfully merging this pull request may close these issues.

[JsonIgnore] attribute ignored when using $expand

3 participants