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

System.Text.Json source generation should accept init-only properties with JsonIgnoreCondition.Always #66003

Closed
Tracked by #79161
brantburnett opened this issue Mar 1, 2022 · 6 comments
Labels
area-System.Text.Json bug source-generator Indicates an issue with a source generator feature
Milestone

Comments

@brantburnett
Copy link
Contributor

Description

Currently, using JSON source generation with init-only properties is not supported. However, source generation fails if there are any init-only properties present on a class at all, even those marked with JsonIgnoreCondition.Always. Since such properties are not serialized or deserialized, it seems like they could be ignored for this rule as well.

Reproduction Steps

public class Poco
{
    public string Message { get; set; }

    [JsonIgnore(Condition = JsonIgnoreCondition.Always)]
    public object Info { get; init; }
}
[JsonSerializable(typeof(Poco))]
public partial class MySerializerContext : JsonSerializerContext
{
}

Expected behavior

Source is successfully generated and compiles, ignoring the presence of the init-only property.

Actual behavior

SYSLIB1037 The type 'Poco' defines init-only properties, deserialization of which is currently not supported in source generation mode

Regression?

No

Known Workarounds

Do not include any init-only properties at all, even ignored.

Configuration

.NET 6

Other information

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Mar 1, 2022
@ghost
Copy link

ghost commented Mar 1, 2022

Tagging subscribers to this area: @dotnet/area-system-text-json
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

Currently, using JSON source generation with init-only properties is not supported. However, source generation fails if there are any init-only properties present on a class at all, even those marked with JsonIgnoreCondition.Always. Since such properties are not serialized or deserialized, it seems like they could be ignored for this rule as well.

Reproduction Steps

public class Poco
{
    public string Message { get; set; }

    [JsonIgnore(Condition = JsonIgnoreCondition.Always)]
    public object Info { get; init; }
}
[JsonSerializable(typeof(Poco))]
public partial class MySerializerContext : JsonSerializerContext
{
}

Expected behavior

Source is successfully generated and compiles, ignoring the presence of the init-only property.

Actual behavior

SYSLIB1037 The type 'Poco' defines init-only properties, deserialization of which is currently not supported in source generation mode

Regression?

No

Known Workarounds

Do not include any init-only properties at all, even ignored.

Configuration

.NET 6

Other information

No response

Author: brantburnett
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

@eiriktsarpalis
Copy link
Member

Can reproduce. Likely related to #63443.

@eiriktsarpalis eiriktsarpalis added bug and removed untriaged New issue has not been triaged by the area owner labels Mar 3, 2022
@eiriktsarpalis eiriktsarpalis added this to the 7.0.0 milestone Mar 3, 2022
@eiriktsarpalis eiriktsarpalis modified the milestones: 7.0.0, Future May 10, 2022
@eiriktsarpalis
Copy link
Member

We won't have time to work on this for .NET 7, moving to Future.

@brantburnett
Copy link
Contributor Author

@eiriktsarpalis would you be interested in a community PR to address this, or is testing bandwidth also a concern?

@eiriktsarpalis
Copy link
Member

Hi @brantburnett, we're always open to community contributions.

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label May 13, 2022
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Jul 28, 2022
@eiriktsarpalis eiriktsarpalis modified the milestones: Future, 8.0.0 Sep 2, 2022
@eiriktsarpalis eiriktsarpalis added the source-generator Indicates an issue with a source generator feature label Jun 16, 2023
@eiriktsarpalis
Copy link
Member

Fixed by #79828.

@ghost ghost locked as resolved and limited conversation to collaborators Jul 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Text.Json bug source-generator Indicates an issue with a source generator feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants