Skip to content

Commit

Permalink
Add C# 9 record support to serializer (#7108)
Browse files Browse the repository at this point in the history
Co-authored-by: Oisin Grehan <oisin.grehan@hiloenergie.com>
  • Loading branch information
ReubenBond and oising committed Jun 18, 2021
1 parent c3b1e6f commit 806afeb
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ public FieldInfoMember(SerializerGenerator generator, SemanticModel model, IName
/// <summary>
/// Gets a value indicating whether or not this field represents a property with an accessible, non-obsolete setter.
/// </summary>
public bool IsSettableProperty => this.Property?.SetMethod != null && this.model.IsAccessible(0, this.Property.SetMethod) && !this.IsObsolete;
public bool IsSettableProperty => Property?.SetMethod is { } setMethod && model.IsAccessible(0, setMethod) && !setMethod.IsInitOnly && !IsObsolete;

/// <summary>
/// Gets syntax representing the type of this field.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ public RoundTripSerializerTests(DefaultClusterFixture fixture) : base(fixture)
{
}

[Fact]
public async Task Serialize_TestMethodResultRecord()
{
var grain = this.GrainFactory.GetGrain<IRoundtripSerializationGrain>(GetRandomGrainId());
RetVal retVal = await grain.GetRetValForParamVal(new ParamVal(42));
Assert.Equal(42, retVal.Value);
}

[Fact]
public async Task Serialize_TestMethodResultEnum()
{
Expand Down
10 changes: 9 additions & 1 deletion test/Grains/TestGrainInterfaces/IValueTypeTestGrain.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Orleans;
Expand Down Expand Up @@ -144,8 +144,16 @@ public interface IRoundtripSerializationGrain : IGrainWithIntegerKey
Task<CampaignEnemyTestType> GetEnemyType();

Task<object> GetClosedGenericValue();

Task<RetVal> GetRetValForParamVal(ParamVal param);
}

[Serializable]
public record ParamVal(int Value);

[Serializable]
public record RetVal(int Value);

[Serializable]
[Immutable]
public class ImmutableType
Expand Down
5 changes: 5 additions & 0 deletions test/Grains/TestGrainInterfaces/IsExternalInit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace System.Runtime.CompilerServices
{
// required for record serialization support for downlevel
internal static class IsExternalInit {}
}
5 changes: 4 additions & 1 deletion test/Grains/TestGrains/RoundtripSerializationGrain.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading.Tasks;
Expand All @@ -20,5 +20,8 @@ public Task<object> GetClosedGenericValue()
var result = new List<ImmutableList<HashSet<Tuple<int, string>>>>();
return Task.FromResult((object)result);
}

// test record support
public Task<RetVal> GetRetValForParamVal(ParamVal param) => Task.FromResult(new RetVal(param.Value));
}
}

0 comments on commit 806afeb

Please sign in to comment.