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

[Backport] Add C# 9.0 record support to serializer (#7108) #7119

Merged
merged 2 commits into from
Jun 21, 2021
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
7 changes: 4 additions & 3 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,18 @@
<SystemReflectionEmitVersion>4.7.0</SystemReflectionEmitVersion>
<SystemThreadingTasksExtensionsVersion>4.5.4</SystemThreadingTasksExtensionsVersion>
<SystemThreadingChannelsVersion>4.7.0</SystemThreadingChannelsVersion>
<SystemBuffersVersion>4.5.0</SystemBuffersVersion>
<SystemBuffersVersion>4.5.1</SystemBuffersVersion>
<SystemMemoryVersion>4.5.3</SystemMemoryVersion>
<SystemIOPipelinesVersion>4.7.0</SystemIOPipelinesVersion>
<SystemCodeDomVersion>4.7.0</SystemCodeDomVersion>
<SystemSecurityPermissionsVersion>4.7.0</SystemSecurityPermissionsVersion>
<SystemSecurityCryptographyProtectedDataVersion>4.7.0</SystemSecurityCryptographyProtectedDataVersion>
<SystemDiagnosticsDiagnosticsSourceVersion>4.7.0</SystemDiagnosticsDiagnosticsSourceVersion>
<SystemSecurityCryptographyVersion>4.7.0</SystemSecurityCryptographyVersion>

<!-- Microsoft packages -->
<MicrosoftBuildVersion>16.4.0</MicrosoftBuildVersion>
<MicrosoftCodeAnalysisVersion>3.4.0</MicrosoftCodeAnalysisVersion>
<MicrosoftCodeAnalysisVersion>3.9.0</MicrosoftCodeAnalysisVersion>
<MicrosoftWin32RegistryVersion>4.7.0</MicrosoftWin32RegistryVersion>
<MicrosoftBclAsyncInterfacesVersion>1.1.1</MicrosoftBclAsyncInterfacesVersion>
<MicrosoftNETFrameworkReferenceAssembliesVersion>1.0.0</MicrosoftNETFrameworkReferenceAssembliesVersion>
Expand All @@ -94,7 +95,7 @@
<MicrosoftAzureCosmosTableVersion>1.0.5</MicrosoftAzureCosmosTableVersion>
<MicrosoftAzureEventHubsVersion>4.1.0</MicrosoftAzureEventHubsVersion>
<AzureCoreVersion>1.3.0</AzureCoreVersion>
<AzureIdentityVersion>1.1.1</AzureIdentityVersion>
<AzureIdentityVersion>1.4.0</AzureIdentityVersion>
<AzureStorageBlobsVersion>12.4.4</AzureStorageBlobsVersion>
<AzureStorageQueuesVersion>12.3.2</AzureStorageQueuesVersion>
<MicrosoftDataSQLiteVersion>3.1.0</MicrosoftDataSQLiteVersion>
Expand Down
8 changes: 4 additions & 4 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<PropertyGroup Condition=" '$(OrleansBuildTimeCodeGen)' == 'true' ">
<DotNetHost Condition="'$(DotNetHost)' == ''">dotnet</DotNetHost>
<Asm>Orleans.CodeGenerator.MSBuild.Bootstrap</Asm>
<OrleansCodeGenCoreAssembly>$(MSBuildThisFileDirectory)src/BootstrapBuild/$(Asm)/bin/$(Configuration)/publish/netcoreapp3.1/$(Asm).dll</OrleansCodeGenCoreAssembly>
<OrleansCodeGenTasksAssembly>$(MSBuildThisFileDirectory)src/BootstrapBuild/$(Asm)/bin/$(Configuration)/publish/netcoreapp3.1/Orleans.CodeGenerator.MSBuild.Tasks.dll</OrleansCodeGenTasksAssembly>
<OrleansCodeGenCoreAssembly>$(MSBuildThisFileDirectory)src/BootstrapBuild/$(Asm)/bin/$(Configuration)/publish/$(Asm).dll</OrleansCodeGenCoreAssembly>
<OrleansCodeGenTasksAssembly>$(MSBuildThisFileDirectory)src/BootstrapBuild/$(Asm)/bin/$(Configuration)/publish/Orleans.CodeGenerator.MSBuild.Tasks.dll</OrleansCodeGenTasksAssembly>
<OrleansBootstrapBuildProject>$(MSBuildThisFileDirectory)src/BootstrapBuild/Orleans.CodeGenerator.MSBuild.Bootstrap/Orleans.CodeGenerator.MSBuild.Bootstrap.csproj</OrleansBootstrapBuildProject>
</PropertyGroup>

Expand All @@ -18,7 +18,7 @@
<Project>{CB36EF45-6E90-443F-AEE4-394677068147}</Project>
<Name>Orleans.CodeGenerator.MSBuild.Bootstrap</Name>
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
<AssetTargetFallback>netcoreapp3.1</AssetTargetFallback>
<AssetTargetFallback>net5.0</AssetTargetFallback>
<Visible>false</Visible>
<!-- Workaround. See: https://github.com/dotnet/sdk/issues/939#issuecomment-284641613 -->
<!-- This causes the 'Dependency' node in VS to show a warning icon. See https://github.com/dotnet/project-system/issues/2928 -->
Expand All @@ -27,7 +27,7 @@
<!-- /Workaround -->
</ProjectReference>
</ItemGroup>

<Import Condition=" '$(OrleansBuildTimeCodeGen)' == 'true' " Project="$(MSBuildThisFileDirectory)src/Orleans.CodeGenerator.MSBuild/build/Microsoft.Orleans.CodeGenerator.MSBuild.targets" /><!--End Orleans -->

<!-- Set InformationVersion here, since $(Version) is already set at this point. -->
Expand Down
2 changes: 1 addition & 1 deletion DotnetCLIVersion.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.1.301
5.0.301
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"sdk": {
"version": "3.1.301"
"version": "5.0.301"
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk" DefaultTargets="Build;PostBuildPublish">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<OutputType>Exe</OutputType>
<IsPackable>false</IsPackable>
<NoPackageAnalysis>true</NoPackageAnalysis>
Expand All @@ -13,11 +13,11 @@
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
<DevelopmentDependency>true</DevelopmentDependency>
<PublishRoot>bin\$(Configuration)\publish\</PublishRoot>
<PublishDir>$(PublishRoot)$(TargetFramework)</PublishDir>
<PublishDir>$(PublishRoot)</PublishDir>
<SourceDir>$(MSBuildThisFileDirectory)..\..\Orleans.CodeGenerator.MSBuild\</SourceDir>
<OrleansBuildTimeCodeGen>false</OrleansBuildTimeCodeGen>
</PropertyGroup>

<ItemGroup>
<Compile Include="$(SourceDir)**/*.cs" Exclude="$(SourceDir)obj/**/*.cs;$(SourceDir)bin/**/*.cs" Condition="'$(DesignTimeBuild)' != 'true'">
<!-- Hide the files and folder structure in the editor. -->
Expand All @@ -27,7 +27,7 @@
<ProjectReference Include="..\..\Orleans.CodeGenerator\Orleans.CodeGenerator.csproj" PrivateAssets="all" />
<ProjectReference Include="..\..\Orleans.CodeGenerator.MSBuild.Tasks\Orleans.CodeGenerator.MSBuild.Tasks.csproj" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Runtime" Version="$(SystemRuntimeVersion)" />
<PackageReference Include="Newtonsoft.Json" Version="$(NewtonsoftJsonVersion)" />
Expand Down
7 changes: 7 additions & 0 deletions src/Orleans.Analyzers/AnalyzerReleases.Shipped.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Release 3.3.0

### New Rules

Rule ID | Category | Severity | Notes
--------|----------|----------|--------------------
ORLEANS0001 | Usage | Error | [AlwaysInterleave] must only be used on the grain interface method and not the grain class method
3 changes: 3 additions & 0 deletions src/Orleans.Analyzers/AnalyzerReleases.Unshipped.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
; Unshipped analyzer release
; https://github.com/dotnet/roslyn-analyzers/blob/master/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md

7 changes: 6 additions & 1 deletion src/Orleans.Analyzers/Orleans.Analyzers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<IncludeBuildOutput>false</IncludeBuildOutput>
<DevelopmentDependency>true</DevelopmentDependency>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="$(MicrosoftCodeAnalysisVersion)" PrivateAssets="all" />
<PackageReference Update="NETStandard.Library" PrivateAssets="all" />
Expand All @@ -20,4 +20,9 @@
<None Include="$(OutputPath)\$(AssemblyName).xml" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
</ItemGroup>

<ItemGroup>
<AdditionalFiles Include="AnalyzerReleases.Shipped.md" />
<AdditionalFiles Include="AnalyzerReleases.Unshipped.md" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/Orleans.CodeGenerator.MSBuild/AssemblyResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal class AssemblyResolver : IDisposable
public AssemblyResolver()
{
this.resolverRependencyContext = DependencyContext.Load(typeof(AssemblyResolver).Assembly);
var codegenPath = Path.GetDirectoryName(new Uri(typeof(AssemblyResolver).Assembly.CodeBase).LocalPath);
var codegenPath = Path.GetDirectoryName(new Uri(typeof(AssemblyResolver).Assembly.Location).LocalPath);
this.assemblyResolver = new CompositeCompilationAssemblyResolver(
new ICompilationAssemblyResolver[]
{
Expand Down
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 @@ -7,7 +7,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="$(MicrosoftCodeAnalysisVersion)" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftTestSdkVersion)" />
<PackageReference Include="xunit" Version="$(xUnitVersion)" />
<PackageReference Include="xunit.runner.visualstudio" Version="$(xUnitVersion)" />
Expand All @@ -22,8 +21,8 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\Orleans.CodeGenerator\Orleans.CodeGenerator.csproj" />
<ProjectReference Include="..\..\..\src\Orleans.Core.Abstractions\Orleans.Core.Abstractions.csproj" />
<ProjectReference Include="..\..\..\src\Orleans.CodeGenerator\Orleans.CodeGenerator.csproj" />
<ProjectReference Include="..\..\..\src\Orleans.Core\Orleans.Core.csproj" />
</ItemGroup>

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
5 changes: 3 additions & 2 deletions test/Extensions/TesterAzureUtils/Tester.AzureUtils.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<DefineConstants>TRACE;TESTER_AZUREUTILS;ORLEANS_PERSISTENCE</DefineConstants>
<TargetFrameworks>$(TestTargetFrameworks)</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="$(FluentAssertionsVersion)" />
<PackageReference Include="xunit" Version="$(xUnitVersion)" />
Expand All @@ -14,6 +14,7 @@
<PackageReference Include="Microsoft.Azure.Cosmos.Table" Version="$(MicrosoftAzureCosmosTableVersion)" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftTestSdkVersion)" />
<PackageReference Include="Azure.Identity" Version="$(AzureIdentityVersion)" />
<PackageReference Include="System.Security.Cryptography.ProtectedData" Version="$(SystemSecurityCryptographyProtectedDataVersion)" />
</ItemGroup>

<ItemGroup>
Expand All @@ -26,7 +27,7 @@
<ProjectReference Include="$(SourceRoot)\src\Azure\Orleans.Reminders.AzureStorage\Orleans.Reminders.AzureStorage.csproj" />
<ProjectReference Include="$(SourceRoot)\src\Azure\Orleans.GrainDirectory.AzureStorage\Orleans.GrainDirectory.AzureStorage.csproj" />
</ItemGroup>

<ItemGroup Condition="!$(TargetFramework.StartsWith('netcoreapp'))">
<ProjectReference Include="$(SourceRoot)test\TestInfrastructure\Orleans.TestingHost.AppDomain\Orleans.TestingHost.AppDomain.csproj" />
</ItemGroup>
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);

Choose a reason for hiding this comment

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

@ReubenBond Does this mean records have to have the Serializable attribute?

Copy link
Member Author

@ReubenBond ReubenBond Sep 15, 2021

Choose a reason for hiding this comment

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

The same rules apply for records as for other types: they don't need [Serializable] if they directly appear in another serializable type or a grain interface method signature. I'd recommend adding [Serializable] to any serializable type, though, since that is a more explicit way of telling the code generator to generate a serializer for a type.


[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));
}
}
2 changes: 2 additions & 0 deletions test/Tester/Tester.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftTestSdkVersion)" />
<PackageReference Include="System.Diagnostics.EventLog" Version="$(SystemDiagnosticsEventLogVersion)" />
<PackageReference Include="Azure.Identity" Version="$(AzureIdentityVersion)" />
<PackageReference Include="System.Security.Cryptography.ProtectedData" Version="$(SystemSecurityCryptographyProtectedDataVersion)" />
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="$(SystemDiagnosticsPerformanceCounterVersion)" />
</ItemGroup>

<ItemGroup>
Expand Down