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

Serializing source-generated nullable init-only properties throws when using RespectNullableAnnotations #107968

Closed
jupjohn opened this issue Sep 18, 2024 · 1 comment · Fixed by #107985
Assignees
Labels
area-System.Text.Json bug in-pr There is an active PR which will close this issue when it is merged
Milestone

Comments

@jupjohn
Copy link

jupjohn commented Sep 18, 2024

Description

When serializing a null JSON property against a nullable init-only class property using a source generated JsonSerializerContext with RespectNullableAnnotations enabled, the serializer throws and exception claiming that the property doesn't allow null values.

This behaviour is not present when using a runtime type serializer instead of a source-generated one.

Reproduction Steps

using System.Text.Json;
using System.Text.Json.Serialization;

const string jsonObject =
    """
    {
        "NullableProperty": null
    }
    """;

var result = JsonSerializer.Deserialize(jsonObject, NullableJsonContext.Default.MyNullableObject);
Console.WriteLine(result!.NullableProperty);


public class MyNullableObject
{
    public string? NullableProperty { get; init; }
}

// Setting RespectNullableAnnotations to false will make this pass, as will changing the init accessor to a private setter
[JsonSourceGenerationOptions(RespectNullableAnnotations = true)]
[JsonSerializable(typeof(MyNullableObject))]
public partial class NullableJsonContext : JsonSerializerContext;

Expected behavior

The nullable property (NullableProperty in the example) serializes the property as a null value.

Actual behavior

Serialization throws a JsonException:

Unhandled exception. System.Text.Json.JsonException: The constructor parameter 'NullableProperty' on type 'MyNullableObject' doesn't allow null values. Consider updating its nullability annotation. Path: $.NullableProperty | LineNumber: 1 | BytePositionInLine: 28.
   at System.Text.Json.ThrowHelper.ThrowJsonException_ConstructorParameterDisallowNull(String parameterName, Type declaringType)
   at System.Text.Json.Serialization.Converters.LargeObjectWithParameterizedConstructorConverter`1.ReadAndCacheConstructorArgument(ReadStack& state, Utf8JsonReader& reader, JsonParameterInfo jsonParameterInfo)
   at System.Text.Json.Serialization.Converters.ObjectWithParameterizedConstructorConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   at System.Text.Json.Serialization.Converters.JsonMetadataServicesConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value, Boolean& isPopulatedValue)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, T& value, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.Deserialize(Utf8JsonReader& reader, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 utf8Json, JsonTypeInfo`1 jsonTypeInfo, Nullable`1 actualByteCount)
   at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 json, JsonTypeInfo`1 jsonTypeInfo)
   at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonTypeInfo`1 jsonTypeInfo)
   at Program.<Main>$(String[] args) in /.../Program.cs:line 11

Regression?

No response

Known Workarounds

Using a non-source generated serializer works:

JsonSerializer.Deserialize<MyNullableObject>(jsonObject);

Configuration

Tested on:

  • .NET 8
  • System.Text.Json 9.0.0-rc.1.24431.7
  • Windows & Linux
  • x64
  • Platform/arch independent

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 Sep 18, 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.

@eiriktsarpalis eiriktsarpalis added bug and removed untriaged New issue has not been triaged by the area owner labels Sep 18, 2024
@eiriktsarpalis eiriktsarpalis added this to the 9.0.0 milestone Sep 18, 2024
@eiriktsarpalis eiriktsarpalis self-assigned this Sep 18, 2024
@dotnet-policy-service dotnet-policy-service bot added the in-pr There is an active PR which will close this issue when it is merged label Sep 18, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Oct 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Text.Json bug in-pr There is an active PR which will close this issue when it is merged
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants