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

JSON serializer source generator produces warnings for types with nullable generic parameters #61734

Closed
Tragetaschen opened this issue Nov 17, 2021 · 4 comments · Fixed by #74801
Labels
area-System.Text.Json bug source-generator Indicates an issue with a source generator feature
Milestone

Comments

@Tragetaschen
Copy link
Contributor

Tragetaschen commented Nov 17, 2021

Description

When the model type has a constructor with a generic type that has a nullable type argument, that nullability is not reproduced in the source-generated code for the JSON serializer and produces a nullability warning

Reproduction Steps

In a net6.0 classlib project:

using System.Text.Json.Serialization;

namespace JsonSerializerNullability;

public class Model
{
    public Model(Dictionary<string, object?> property) => Property = property;
    public Dictionary<string, object?> Property { get; }
}

[JsonSerializable(typeof(Model))]
internal partial class MyJsonSerializerContext : JsonSerializerContext
{ }

Expected behavior

No warnings should be produced

Actual behavior

Warning	CS8620	Argument of type 'Dictionary<string, object>' cannot be used for parameter 'property' of type 'Dictionary<string, object?>' in 'Model.Model(Dictionary<string, object?> property)' due to differences in the nullability of reference types.	JsonSerializerNullability	…\MyJsonSerializerContext.Model.g.cs	25	Active

Regression?

No response

Known Workarounds

No response

Configuration

net6.0 on version 6.0.0

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 Nov 17, 2021
@ghost
Copy link

ghost commented Nov 17, 2021

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

When the model type has a constructor with a generic type that has a nullable type argument, that nullability is not reproduced in the source-generated code for the JSON serializer and produces a nullability warning

Reproduction Steps

In a net6.0 classlib project:

using System.Text.Json.Serialization;

namespace JsonSerializerNullability;

public record Model
{
    public Model(Dictionary<string, object?> property) => Property = property;
    public Dictionary<string, object?> Property { get; }
}

[JsonSerializable(typeof(Model))]
internal partial class MyJsonSerializerContext : JsonSerializerContext
{ }

Expected behavior

No warnings should be produced

Actual behavior

Warning	CS8620	Argument of type 'Dictionary<string, object>' cannot be used for parameter 'property' of type 'Dictionary<string, object?>' in 'Model.Model(Dictionary<string, object?> property)' due to differences in the nullability of reference types.	JsonSerializerNullability	…\MyJsonSerializerContext.Model.g.cs	25	Active

Regression?

No response

Known Workarounds

No response

Configuration

net6.0 on version 6.0.0

Other information

No response

Author: Tragetaschen
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

@Tragetaschen Tragetaschen changed the title JSON serializer code generator produces nullability warnings for constructor arguments JSON serializer source generator produces nullability warnings for constructor arguments Nov 18, 2021
@eiriktsarpalis
Copy link
Member

eiriktsarpalis commented Nov 18, 2021

Related to #59464. This is actually related to nullability of generic parameters rather than constructor arguments. When working on a fix I thought I had added tests for nullable generic parameters

public GenericStruct<Uri?> NullableUriParameter { get; set; }
public GenericStruct<int[]?> NullableArrayParameter { get; set; }
public GenericStruct<MyPoco?> NullablePocoParameter { get; set; }

However it turns out this was never really being tested, since our test projects have their Nullable property set to annotations. We should service this.

@eiriktsarpalis eiriktsarpalis added bug and removed untriaged New issue has not been triaged by the area owner labels Nov 18, 2021
@eiriktsarpalis eiriktsarpalis added this to the 6.0.x milestone Nov 18, 2021
@eiriktsarpalis eiriktsarpalis self-assigned this Nov 18, 2021
@eiriktsarpalis eiriktsarpalis changed the title JSON serializer source generator produces nullability warnings for constructor arguments JSON serializer source generator produces nullability warnings for types with generic parameters Nov 18, 2021
@eiriktsarpalis eiriktsarpalis changed the title JSON serializer source generator produces nullability warnings for types with generic parameters JSON serializer source generator produces warnings for types with nullable reference type generic parameters Nov 18, 2021
@eiriktsarpalis eiriktsarpalis changed the title JSON serializer source generator produces warnings for types with nullable reference type generic parameters JSON serializer source generator produces warnings for types with nullable generic parameters Nov 18, 2021
@eiriktsarpalis eiriktsarpalis modified the milestones: 6.0.x, 8.0.0 Aug 26, 2022
@eiriktsarpalis eiriktsarpalis removed their assignment Aug 26, 2022
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Aug 30, 2022
krwq added a commit to krwq/runtime-1 that referenced this issue Aug 30, 2022
krwq added a commit that referenced this issue Aug 31, 2022
* Disable nullability warnings in JSON source generator

* Add testcase for #61734
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Aug 31, 2022
@eiriktsarpalis
Copy link
Member

Let's keep these open, given that suppressing warnings is not fundamentally addressing the issue.

krwq added a commit that referenced this issue Sep 1, 2022
…74861)

* Disable nullability warnings in JSON source generator

* Add testcase for #61734

Co-authored-by: Krzysztof Wicher <kwicher@microsoft.com>
@eiriktsarpalis eiriktsarpalis added the source-generator Indicates an issue with a source generator feature label Sep 2, 2022
@eiriktsarpalis
Copy link
Member

Addressed in #86334.

@eiriktsarpalis eiriktsarpalis modified the milestones: Future, 8.0.0 Jun 22, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Jul 22, 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