-
Notifications
You must be signed in to change notification settings - Fork 5k
Fix JsonIgnore behavior on init-only properties in source generation mode #112924
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
base: main
Are you sure you want to change the base?
Conversation
…n serializer source generator
@dotnet-policy-service agree |
@@ -132,5 +155,28 @@ public class Class_PropertyWith_ProtectedInitOnlySetter_WithAttribute | |||
[JsonInclude] | |||
public int MyInt { get; protected init; } | |||
} | |||
|
|||
public class Class_WithIgnoredInitOnlyProperty_WithDefaultValue |
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.
Why is having a default value important in this context? Shouldn't we also be testing for the non-default case?
@@ -1514,6 +1514,11 @@ private void ProcessMember( | |||
continue; | |||
} | |||
|
|||
if (property.DefaultIgnoreCondition == JsonIgnoreCondition.Always && !property.IsRequired) |
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.
We still need to handle ignored required properties somehow, even if we can't remove them from the constructor delegate.
This change fixes #101877 where the
[JsonIgnore]
attribute applied to init-only properties is ignored in source generation mode. In the current behavior, the generated object initializer mistakenly invokes the init for properties marked with[JsonIgnore]
, which causes the value provided by the constructor to be overwritten (or set to null) during deserialization.