Skip to content
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

Fix JsonIgnore behavior on init-only properties in source generation mode #112924

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ardabada
Copy link

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.

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Feb 25, 2025
@ardabada
Copy link
Author

@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
Copy link
Member

@eiriktsarpalis eiriktsarpalis Feb 26, 2025

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Text.Json community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

JsonIgnore attribute has no effect on init-only properties in source generation mode
2 participants