Skip to content

JsonSerializer should throw if nullable is enabled and non-null values are null during deserialization #38700

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
Trolldemorted opened this issue Jul 2, 2020 · 2 comments

Comments

@Trolldemorted
Copy link

Description

Imagine this program:

class Cat
{
    public string Name { get; set; } = default!;
    public string? Foo { get; set; }
    public override string ToString() { return Name ?? "ERROR"; }
}
class Program
{
    static void Main(string[] args)
    {
        var cat = JsonSerializer.Deserialize<Cat>("{}");
        Console.WriteLine(cat);
    }
}

I expected that the JsonSerializer would throw if the non-nullable values are not present in the json string.

Configuration

<TargetFramework>netcoreapp3.1</TargetFramework>

Regression?

no (?)

Other information

We have not found an easy workaround to conveniently enforce null safety for a huge amount of classes.

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Jul 2, 2020
@Tornhoof
Copy link
Contributor

Tornhoof commented Jul 2, 2020

Looks like a duplicate of #1256

@Trolldemorted
Copy link
Author

Sorry, you are right!

@layomia layomia removed the untriaged New issue has not been triaged by the area owner label Jul 7, 2020
@layomia layomia added this to the 5.0.0 milestone Jul 7, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 8, 2020
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Projects
None yet
Development

No branches or pull requests

4 participants