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
System.Text.Json IgnoreReadOnlyProperties not working for enumerables #80113
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsDescriptionGiven a class public class Test
{
public int MyProperty1 { get; set; } = 69;
public int MyProperty2 { get; } = 420;
public IEnumerable<int> Collection1 { get; set; } = new int[] { 69, 420 };
public IEnumerable<int> Collection2 { get; } = new int[] { 69, 420 };
}; by setting IgnoreReadOnlyProperties to true, one would expect that only "MyProperty1" and "Collection1" would be serialized. Reproduction StepsTest class namespace T
{
public class Test
{
public int MyProperty1 { get; set; } = 69;
public int MyProperty2 { get; } = 420;
public IEnumerable<int> Collection1 { get; set; } = new int[] { 69, 420 };
public IEnumerable<int> Collection2 { get; } = new int[] { 69, 420 };
};
} Program.cs using System.Text.Json;
using T;
var opt = new JsonSerializerOptions()
{
IgnoreReadOnlyProperties = true,
WriteIndented = true,
};
var obj = new Test();
var json = JsonSerializer.Serialize(obj, opt);
Console.WriteLine(json); Expected behavior{
"MyProperty1": 69,
"Collection1": [
69,
420
]
} Actual behavior{
"MyProperty1": 69,
"Collection1": [
69,
420
],
"Collection2": [
69,
420
]
} Regression?No response Known WorkaroundsNo response ConfigurationNo response Other informationNo response
|
Properties of collections types that only have setters are not supported. But should they? I'm missing whats the point of the reasoning for not supporting that. |
I'm marking this as 8.0 for now because we should define behavior for this in #78556 and they should be looked at hollistically |
Shouldn't this behaviour be made configurable from the options? |
Moving to Future. |
Same for getter-only arrays. They get serialized, even though they are obviously not deserializable (can't set or resize the array) My test program: using System.Text.Json;
var test = new Test();
Console.WriteLine(JsonSerializer.Serialize(test, new JsonSerializerOptions { IgnoreReadOnlyProperties = true }));
public partial class Test
{
public int Id => 123;
public int[] SomeArrayProp => new[] { 456 };
public IEnumerable<int> SomeIEnumProp { get { yield return 789; } }
} |
Description
Given a class
by setting IgnoreReadOnlyProperties to true, one would expect that only "MyProperty1" and "Collection1" would be serialized.
Collection2 Gets also serialized for whatever reason.
Reproduction Steps
Test class
Program.cs
Csproj
Expected behavior
Actual behavior
Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information
No response
The text was updated successfully, but these errors were encountered: