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

[release/6.0] Handle parameterless ctors in structs in STJ's ReflectionEmitMemberAccessor #67901

Merged
merged 4 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
<Nullable>enable</Nullable>
<IncludeInternalObsoleteAttribute>true</IncludeInternalObsoleteAttribute>
<IsPackable>true</IsPackable>
<ServicingVersion>4</ServicingVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<ServicingVersion>5</ServicingVersion>
<PackageDescription>Provides high-performance and low-allocating types that serialize objects to JavaScript Object Notation (JSON) text and deserialize JSON text to objects, with UTF-8 support built-in. Also provides types to read and write JSON text encoded as UTF-8, and to create an in-memory document object model (DOM), that is read-only, for random access of the JSON elements within a structured view of the data.

Commonly Used Types:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ internal sealed class ReflectionEmitMemberAccessor : MemberAccessor
else
{
generator.Emit(OpCodes.Newobj, realMethod);
if (type.IsValueType)
{
// Since C# 10 it's now possible to have parameterless constructors in structs
generator.Emit(OpCodes.Box, type);
}
}

generator.Emit(OpCodes.Ret);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1324,6 +1324,71 @@ public class ClassWithIgnoredSameType
public ClassWithIgnoredSameType(ClassWithIgnoredSameType prop) { }
}

[Fact]
public void StructWithPropertyInit_DeseralizeEmptyObject()
{
string json = @"{}";
var obj = JsonSerializer.Deserialize<StructWithPropertyInit>(json);
Assert.Equal(42, obj.A);
Assert.Equal(0, obj.B);
}

[Fact]
public void StructWithPropertyInit_OverrideInitedProperty()
{
string json = @"{""A"":43}";
var obj = JsonSerializer.Deserialize<StructWithPropertyInit>(json);
Assert.Equal(43, obj.A);
Assert.Equal(0, obj.B);

json = @"{""A"":0,""B"":44}";
obj = JsonSerializer.Deserialize<StructWithPropertyInit>(json);
Assert.Equal(0, obj.A);
Assert.Equal(44, obj.B);

json = @"{""B"":45}";
obj = JsonSerializer.Deserialize<StructWithPropertyInit>(json);
Assert.Equal(42, obj.A); // JSON doesn't set A property so it's expected to be 42
Assert.Equal(45, obj.B);
}

public struct StructWithPropertyInit
{
public StructWithPropertyInit() {}
public long A { get; set; } = 42;
Copy link
Contributor

@RikkiGibson RikkiGibson Apr 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was under the impression we went back and made this an error in the same release we introduced field initializers on structs. It's now required to explicitly declare at least one constructor when field initializers are present.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, totally forgot about it, but if I add an explicit parameterless constructor the test case will still be valid for the issue

public long B { get; set; } = 0;
}

[Fact]
public void StructWithFieldInit_DeseralizeEmptyObject()
{
string json = @"{}";
var obj = JsonSerializer.Deserialize<StructWithFieldInit>(json);
Assert.Equal(0, obj.A);
Assert.Equal(42, obj.B);
}

public struct StructWithFieldInit
{
public StructWithFieldInit() {}
public long A = 0;
public long B = 42;
}

[Fact]
public void StructWithExplicitParameterlessCtor_DeseralizeEmptyObject()
{
string json = @"{}";
var obj = JsonSerializer.Deserialize<StructWithExplicitParameterlessCtor>(json);
Assert.Equal(42, obj.A);
}

public struct StructWithExplicitParameterlessCtor
{
public long A;
public StructWithExplicitParameterlessCtor() => A = 42;
}

public async Task TestClassWithDefaultCtorParams()
{
ClassWithDefaultCtorParams obj = new ClassWithDefaultCtorParams(
Expand Down