-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Allow source generation for classes with ignored init-only props #69327
Conversation
* Skip init-only prop warning during source generation if the property is always ignored * Add tests Fixes dotnet#66033
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue Details
Fixes #66033
|
|
||
|
||
[Fact] | ||
[ActiveIssue("https://github.com/dotnet/runtime/issues/58770", TestPlatforms.Browser)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the issue with browser?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I honestly am unsure, I copied this from all the other tests in the file.
@@ -49,6 +49,18 @@ public virtual async Task NonPublicInitOnlySetter_With_JsonInclude(Type type) | |||
Assert.Equal(@"{""MyInt"":1}", await Serializer.SerializeWrapper(obj)); | |||
} | |||
|
|||
[Theory] | |||
[InlineData(typeof(ClassWithPublic_InitOnlyProperty_WithJsonIgnoreProperty))] | |||
public async Task PublicInitOnlySetter_With_JsonIgnore(Type type) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also add tests for these polymorphic scenarios (https://dotnetfiddle.net/caqjtK), here and in the unit tests? Ensure that no warnings are emitted for ignored properties.
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
public class Program
{
public static void Main()
{
string json = JsonSerializer.Serialize(new Derived1());
Console.WriteLine(json);
json = JsonSerializer.Serialize(new Derived2());
Console.WriteLine(json);
}
public class Base1
{
public int MyInt { get; init; } = 3;
}
public class Derived1 : Base1
{
[JsonIgnore]
public new int MyInt { get; init; } = 4;
}
public class Base2
{
public virtual int MyInt { get; init; } = 3;
}
public class Derived2 : Base2
{
[JsonIgnore]
public override int MyInt { get; init; } = 4;
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also add tests for these polymorphic scenarios (https://dotnetfiddle.net/caqjtK), here and in the unit tests? Ensure that no warnings are emitted for ignored properties.
I've added the tests, but I'm running into trouble with the shadowed property scenario. That scenario fails at runtime during deserialization with "Deserialization of init-only properties is currently not supported in source generation mode."
Looking at it briefly, this feels like an issue with shadowed properties in general. PropertyIsOverridenAndIgnored
doesn't appear to take shadowing into account. However, I'm unsure what the design for polymorphism is supposed to do with this scenario. Is this a separate issue related to polymorphism and ignored fields we should fix first?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@layomia Did you have any thoughts on how polymorphism, shadowed properties, and JsonIgnore should be working together? Is this a separate bug or working as designed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Echoing @eiriktsarpalis's comment, we have some broader issues that we need to address with |
Skip init-only prop warning during source generation if the property
is always ignored
Add tests
Fixes #66003