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

JsonSerializer allows nulls in arrays of non-nullable T #101659

Closed
neon-sunset opened this issue Apr 28, 2024 · 6 comments
Closed

JsonSerializer allows nulls in arrays of non-nullable T #101659

neon-sunset opened this issue Apr 28, 2024 · 6 comments

Comments

@neon-sunset
Copy link
Contributor

Description

It appears that System.Text.Json treats the following objects in the same way:

record Nullable(string?[] Names);
record NonNullable(string[] Names);

Reproduction Steps

using System.Text.Json;

var json = """
{
    "Names": ["Alice", "Bob", null, "Charlie"]
}
""";

var shouldSucceed = JsonSerializer.Deserialize<Nullable>(json);
var shouldFail = JsonSerializer.Deserialize<NonNullable>(json);

throw new Exception("Unreachable!");

record Nullable(string?[] Names);
record NonNullable(string[] Names);

Expected behavior

Serializer throws an exception

Actual behavior

The "unreachable" exception is thrown

Regression?

No

Known Workarounds

Using custom converter

Configuration

.NET 8.0.3

Other information

No response

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Apr 28, 2024
Copy link
Contributor

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

@huoyaoyuan
Copy link
Member

I believe this is covered under #100144.

@neon-sunset
Copy link
Contributor Author

Ah, my bad, I tried searching for issues and could not find an appropriate one. Thanks.

@neon-sunset neon-sunset closed this as not planned Won't fix, can't repro, duplicate, stale Apr 28, 2024
@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Apr 28, 2024
@eiriktsarpalis
Copy link
Member

Note that non-nullable element types are out of scope for #100144, due to restrictions in their run-time representation.

@neon-sunset
Copy link
Contributor Author

Note that non-nullable element types are out of scope for #100144, due to restrictions in their run-time representation.

I see. Could there be a possibility of that changing in the future? (perhaps for source-generated serialization only?)

@eiriktsarpalis
Copy link
Member

Note that non-nullable element types are out of scope for #100144, due to restrictions in their run-time representation.

I see. Could there be a possibility of that changing in the future? (perhaps for source-generated serialization only?)

Unlikely. The serialization infrastructure is shared between reflection and source gen and both key they contracts on Type, which cannot distinguish between List<string> and List<string?>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants