Skip to content

Commit

Permalink
Fix NETStandard library using JSON source gen (#63520)
Browse files Browse the repository at this point in the history
NETStandard libraries using JSON source gen would fail to load on
.NETCore due to missing IsExternalInit in the assembly.

On .NETCore this is defined, whereas on .NETStandard JSON carries an
internal copy.  The compiler emits references to the internal type when
a NETStandard library calls init-only setters in JSON types, but these
references are not satisfied when the library runs on .NETCore.

Fix this by adding a type forward to JSON for the IsExternalInit type.
  • Loading branch information
ericstj authored Jan 10, 2022
1 parent 749c02b commit 7a26492
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

// The compiler emits a reference to the internal copy of this type in our non-NETCoreApp assembly
// so we must include a forward to be compatible with libraries compiled against non-NETCoreApp System.Text.Json
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Runtime.CompilerServices.IsExternalInit))]
5 changes: 5 additions & 0 deletions src/libraries/System.Text.Json/ref/System.Text.Json.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
<ItemGroup>
<Compile Include="System.Text.Json.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == '$(NetCoreAppCurrent)'">
<Compile Include="System.Text.Json.Typeforwards.netcoreapp.cs" />
</ItemGroup>

<ItemGroup Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net5.0'))">
<Compile Include="$(CommonPath)System\Runtime\CompilerServices\IsExternalInit.cs" Link="Common\System\Runtime\CompilerServices\IsExternalInit.cs" />
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\DynamicallyAccessedMembersAttribute.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

// The compiler emits a reference to the internal copy of this type in our non-NETCoreApp assembly
// so we must include a forward to be compatible with libraries compiled against non-NETCoreApp System.Text.Json
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Runtime.CompilerServices.IsExternalInit))]
1 change: 1 addition & 0 deletions src/libraries/System.Text.Json/src/System.Text.Json.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ System.Text.Json.Utf8JsonReader</PackageDescription>
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\UnconditionalSuppressMessageAttribute.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == '$(NetCoreAppCurrent)'">
<Compile Include="System.Text.Json.Typeforwards.netcoreapp.cs" />
<Compile Include="System\Text\Json\Serialization\JsonSerializerOptionsUpdateHandler.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<Import Project="System.Text.Json.TestLibrary.targets" />

<ItemGroup>
<ProjectReference Include="..\..\gen\System.Text.Json.SourceGeneration.Roslyn3.11.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<Import Project="System.Text.Json.TestLibrary.targets" />

<ItemGroup>
<ProjectReference Include="..\..\gen\System.Text.Json.SourceGeneration.Roslyn4.0.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project>
<PropertyGroup>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\System.Text.Json.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Include="TestClasses.cs" />
</ItemGroup>

<Target Name="FixIncrementalCoreCompileWithAnalyzers" BeforeTargets="CoreCompile">
<ItemGroup>
<CustomAdditionalCompileInputs Include="@(Analyzer)" />
</ItemGroup>
</Target>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.Json.Serialization;

namespace System.Text.Json.SourceGeneration.Tests.NETStandard
{
public class MyPoco
{
public string Value { get; set; }
}

[JsonSerializable(typeof(MyPoco))]
public partial class NETStandardSerializerContext : JsonSerializerContext
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
using Xunit;

namespace System.Text.Json.SourceGeneration.Tests.NETStandard
{
public class NETStandardContextTests
{
/// <summary>
/// Tests that we can serialize and deserialize a type defined in a NETStandard assembly.
/// This tests an issue where we were emitting source-gen logic that caused the compiler
/// to emit a reference to an internal definition of IsExternalInit that was missing
/// on later versions of .NET (since it was defined by the framework).
/// </summary>
[Fact]
public void RoundTripNETStandardDefinedSourceGenType()
{
MyPoco expected = new MyPoco() { Value = "Hello from NETStandard type."};

string json = JsonSerializer.Serialize(expected, NETStandardSerializerContext.Default.MyPoco);
MyPoco actual = JsonSerializer.Deserialize(json, NETStandardSerializerContext.Default.MyPoco);
Assert.Equal(expected.Value, actual.Value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@

<ItemGroup>
<ProjectReference Include="..\..\gen\System.Text.Json.SourceGeneration.Roslyn3.11.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\System.Text.Json.SourceGeneration.TestLibrary\System.Text.Json.TestLibrary.Roslyn3.11.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@

<ItemGroup>
<ProjectReference Include="..\..\gen\System.Text.Json.SourceGeneration.Roslyn4.0.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\System.Text.Json.SourceGeneration.TestLibrary\System.Text.Json.TestLibrary.Roslyn4.0.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<Compile Include="MetadataAndSerializationContextTests.cs" />
<Compile Include="MetadataContextTests.cs" />
<Compile Include="MixedModeContextTests.cs" />
<Compile Include="NETStandardContextTests.cs" />
<Compile Include="RealWorldContextTests.cs" />
<Compile Include="SerializationContextTests.cs" />
<Compile Include="SerializationLogicTests.cs" />
Expand Down

0 comments on commit 7a26492

Please sign in to comment.