-
Notifications
You must be signed in to change notification settings - Fork 5.4k
NullReference when default is set as optional argument default for value-typed ctor parameter #43757
Copy link
Copy link
Closed
Description
Description
Source code
public record PositionalRecord(Guid st = default);
class Program
{
static void Main(string[] args)
{
var data = new PositionalRecord();
Console.WriteLine(data.ToString());
Console.WriteLine(JsonSerializer.Serialize(data));
}
}Expected output
PositionalRecord { st = 00000000-0000-0000-0000-000000000000 }
{"st":"00000000-0000-0000-0000-000000000000"}
Real output (NullReferenceException)
PositionalRecord { st = 00000000-0000-0000-0000-000000000000 }
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
at System.Text.Json.JsonParameterInfo`1.Initialize(Type runtimePropertyType, ParameterInfo parameterInfo, JsonPropertyInfo matchingProperty, JsonSerializerOptions options)
at System.Text.Json.JsonClassInfo.InitializeConstructorParameters(ConstructorInfo constructorInfo)
at System.Text.Json.JsonClassInfo..ctor(Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializerOptions.GetOrAddClass(Type type)
at System.Text.Json.JsonSerializerOptions.GetOrAddClassForRootType(Type type)
at System.Text.Json.WriteStack.Initialize(Type type, JsonSerializerOptions options, Boolean supportContinuation)
at System.Text.Json.JsonSerializer.WriteCore[TValue](Utf8JsonWriter writer, TValue& value, Type inputType, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.Serialize[TValue](TValue& value, Type inputType, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.Serialize[TValue](TValue value, JsonSerializerOptions options)
at RecordJson.Program.Main(String[] args) in /mnt/c/Users/cupsos/Desktop/git/RecordJson/RecordJson/Program.cs:line 15
Also same error occured when using non primitive structs like
- DateTime
- DateTimeOffset
- Index
- BigInteger
- user defined structs
- ..etc
Configuration
dotnet --info
.NET SDK (reflecting any global.json):
Version: 5.0.100-rc.2.20479.15
Commit: da7dfa8840
Runtime Environment:
OS Name: debian
OS Version: 10
OS Platform: Linux
RID: debian.10-x64
Base Path: /usr/share/dotnet/sdk/5.0.100-rc.2.20479.15/
Host (useful for support):
Version: 5.0.0-rc.2.20475.5
Commit: c5a3f49c88
.NET SDKs installed:
3.1.403 [/usr/share/dotnet/sdk]
5.0.100-rc.2.20479.15 [/usr/share/dotnet/sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 3.1.9 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.0-rc.2.20475.17 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 3.1.9 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.0-rc.2.20475.5 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
To install additional .NET runtimes or SDKs:
https://aka.ms/dotnet-download
csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<LangVersion>9.0</LangVersion>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
</Project>Other information
I don't know how, but RoslynPad can use record type with .NET Core 3.1.
And in this enviroment, no error happen with System.Text.Json 4.7.2 to 5.0.0-preview.2.20160.6,
but after that, same error (NullReferenceException) happen.
Reactions are currently unavailable

