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

NullReferenceException when deserializing object containing nullable struct using source generator #86483

Closed
wt-brett opened this issue May 19, 2023 · 2 comments · Fixed by #86896
Assignees
Milestone

Comments

@wt-brett
Copy link

Description

Calling JsonSerializer.Deserialize for an object containing a nullable struct property will throw a NullReferenceException when passing JsonTypeInfo.

Reproduction Steps

  1. Create a new Console App Project.
  2. Use the following code:
using System.Text.Json;
using System.Text.Json.Serialization;

var obj = new MyObj { MyStruct = new MyStruct { Property1 = 2 } };
var json = JsonSerializer.Serialize(obj, SerializerContext.Default.MyObj);
var result = JsonSerializer.Deserialize(json, SerializerContext.Default.MyObj);

public record class MyObj
{
    public MyStruct? MyStruct { get; init; }
}

public struct MyStruct
{
    public byte Property1 { get; init; }
}

[JsonSerializable(typeof(MyObj))]  
internal partial class SerializerContext : JsonSerializerContext
{
}

A NullReferenceException will be thrown on the var result = JsonSerializer.Deserialize(json, SerializerContext.Default.MyObj); line

Expected behavior

Calling JsonSerializer.Deserialize method does not throw NullReferenceException

Actual behavior

Exception Message:
Object reference not set to an instance of an object.

Stack Trace:

at System.Text.Json.Serialization.Converters.LargeObjectWithParameterizedConstructorConverter`1.InitializeConstructorArgumentCaches(ReadStack& state, JsonSerializerOptions options)
 at System.Text.Json.Serialization.Converters.ObjectWithParameterizedConstructorConverter`1.BeginRead(ReadStack& state, JsonSerializerOptions options)
 at System.Text.Json.Serialization.Converters.ObjectWithParameterizedConstructorConverter`1.ReadConstructorArguments(ReadStack& state, Utf8JsonReader& reader, JsonSerializerOptions options)
 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.Converters.NullableConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, Nullable`1& 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.TryReadAsObject(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, Object& value)
 at System.Text.Json.Serialization.Converters.LargeObjectWithParameterizedConstructorConverter`1.ReadAndCacheConstructorArgument(ReadStack& state, Utf8JsonReader& reader, JsonParameterInfo jsonParameterInfo)
 at System.Text.Json.Serialization.Converters.ObjectWithParameterizedConstructorConverter`1.ReadConstructorArguments(ReadStack& state, Utf8JsonReader& reader, JsonSerializerOptions options)
 at System.Text.Json.Serialization.Converters.ObjectWithParameterizedConstructorConverter`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, JsonSerializerOptions options, 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)

Regression?

Yes. This code worked correctly in .net 8 preview 3

Known Workarounds

Call var result = JsonSerializer.Deserialize<MyObj>(json); without using the JsonTypeInfo

Configuration

  • Which version of .NET is the code running on?
    .net 8 preview 4

  • What OS and version, and what distro if applicable?
    Windows 11

  • What is the architecture (x64, x86, ARM, ARM64)?
    x64

  • Do you know whether it is specific to that configuration?
    Not sure

Other information

No response

@ghost ghost added the untriaged New issue has not been triaged by the area owner label May 19, 2023
@ghost
Copy link

ghost commented May 19, 2023

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

Issue Details

Description

Calling JsonSerializer.Deserialize for an object containing a nullable struct property will throw a NullReferenceException when passing JsonTypeInfo.

Reproduction Steps

  1. Create a new Console App Project.
  2. Use the following code:
using System.Text.Json;
using System.Text.Json.Serialization;

var obj = new MyObj { MyStruct = new MyStruct { Property1 = 2 } };
var json = JsonSerializer.Serialize(obj, SerializerContext.Default.MyObj);
var result = JsonSerializer.Deserialize(json, SerializerContext.Default.MyObj);

public record class MyObj
{
    public MyStruct? MyStruct { get; init; }
}

public struct MyStruct
{
    public byte Property1 { get; init; }
}

[JsonSerializable(typeof(MyObj))]  
internal partial class SerializerContext : JsonSerializerContext
{
}

A NullReferenceException will be thrown on the var result = JsonSerializer.Deserialize(json, SerializerContext.Default.MyObj); line

Expected behavior

Calling JsonSerializer.Deserialize method does not throw NullReferenceException

Actual behavior

Exception Message:
Object reference not set to an instance of an object.

Stack Trace:

at System.Text.Json.Serialization.Converters.LargeObjectWithParameterizedConstructorConverter`1.InitializeConstructorArgumentCaches(ReadStack& state, JsonSerializerOptions options)
 at System.Text.Json.Serialization.Converters.ObjectWithParameterizedConstructorConverter`1.BeginRead(ReadStack& state, JsonSerializerOptions options)
 at System.Text.Json.Serialization.Converters.ObjectWithParameterizedConstructorConverter`1.ReadConstructorArguments(ReadStack& state, Utf8JsonReader& reader, JsonSerializerOptions options)
 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.Converters.NullableConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, Nullable`1& 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.TryReadAsObject(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, Object& value)
 at System.Text.Json.Serialization.Converters.LargeObjectWithParameterizedConstructorConverter`1.ReadAndCacheConstructorArgument(ReadStack& state, Utf8JsonReader& reader, JsonParameterInfo jsonParameterInfo)
 at System.Text.Json.Serialization.Converters.ObjectWithParameterizedConstructorConverter`1.ReadConstructorArguments(ReadStack& state, Utf8JsonReader& reader, JsonSerializerOptions options)
 at System.Text.Json.Serialization.Converters.ObjectWithParameterizedConstructorConverter`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, JsonSerializerOptions options, 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)

Regression?

Yes. This code worked correctly in .net 8 preview 3

Known Workarounds

Call var result = JsonSerializer.Deserialize<MyObj>(json); without using the JsonTypeInfo

Configuration

  • Which version of .NET is the code running on?
    .net 8 preview 4

  • What OS and version, and what distro if applicable?
    Windows 11

  • What is the architecture (x64, x86, ARM, ARM64)?
    x64

  • Do you know whether it is specific to that configuration?
    Not sure

Other information

No response

Author: wt-brett
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

@eiriktsarpalis eiriktsarpalis added bug and removed untriaged New issue has not been triaged by the area owner labels May 19, 2023
@eiriktsarpalis eiriktsarpalis added this to the 8.0.0 milestone May 19, 2023
@eiriktsarpalis eiriktsarpalis self-assigned this May 19, 2023
@eiriktsarpalis
Copy link
Member

It looks like the Nullable converter is not forwarding relevant metadata of the converter it is encapsulating. Minimal reproduction:

JsonSerializer.Deserialize<MyStruct?>("""{"Value":42}""");

public readonly struct MyStruct
{
    [JsonConstructor]
    public MyStruct(int value) => Value = value;
    public int Value { get; }
}

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label May 30, 2023
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label May 30, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Jun 29, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants