Uh oh!
There was an error while loading. Please reload this page.
Add JsonIncludeAttribute & support for non-public accessors - #34675
Conversation
Dotnet-GitSync-Bot
commented
Apr 8, 2020
Note regarding the This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, to please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change. |
73b2624 to
ae62316CompareUh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
| namespace System.Text.Json.Serialization.Tests | ||
| { | ||
| public static partial class PropertyVisibilityTests |
There was a problem hiding this comment.
Currently this PR assumes that the property is still public, correct?(meaning the setter or getter can be non-public, but not both). Do we have tests with a private property where both setter and getter are private to verify?
There was a problem hiding this comment.
Yes, I added some tests showing that non-public properties (with various accessor visibility levels) are not included:
These tests ensure that we throw IOE when [JsonInclude] is used on a non-public properties:
Collection properties should be read only https://docs.microsoft.com/en-gb/visualstudio/code-quality/ca2227 Suppress FxCopAnalyzers warning CA2227 as System.Text.Json.JsonSerializer.Deserialize in .NET Core 3.1 cannot deserialise to read-only properties. There are two related issues that will allow System.Text.Json.JsonSerializer.Deserialize to deserialise to read-only properties in .NET Core 5.0: 1) Pull Request that includes support for non-public accessors: dotnet/runtime#34675 2) Issue regarding, among other things, adding to collections during deserialisation if the collection property has no setter: dotnet/runtime#30258
Changed private setters to public in model classes for consistency and because System.Text.Json does not currently support them. See dotnet/runtime#34675 for details, because this will be supported in .NET 5.
@layomia Is this available on .NET 5.0 RC1, maybe not. My Project has been updated to .NET 5.0 RC1 but it's not working yet. |
layomia
commented
Sep 17, 2020
@TanvirArjel the feature is in RC1, but it requires explicitly opting in with publicclassDataTableParamsDto{publicintDraw{get;set;}publicintStart{get;set;}publicintLength{get;set;}[JsonInclude]publicList<ColumnRequestItem>Columns{get;privateset;}[JsonInclude]publicList<OrderRequestItem>Order{get;privateset;}[JsonInclude]publicSearchRequestItemSearch{get;privateset;}} |
Fixes#29743.
Fixes#34453 (since I'm in this code).
New API approved in #34558.