Skip to content

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

@Tragetaschen

Description

@Tragetaschen

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions