-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix NETStandard library using JSON source gen (#63520)
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
Showing
12 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
src/libraries/System.Text.Json/ref/System.Text.Json.Typeforwards.netcoreapp.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/libraries/System.Text.Json/src/System.Text.Json.Typeforwards.netcoreapp.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
...tem.Text.Json.SourceGeneration.TestLibrary/System.Text.Json.TestLibrary.Roslyn3.11.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
8 changes: 8 additions & 0 deletions
8
...stem.Text.Json.SourceGeneration.TestLibrary/System.Text.Json.TestLibrary.Roslyn4.0.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
17 changes: 17 additions & 0 deletions
17
.../tests/System.Text.Json.SourceGeneration.TestLibrary/System.Text.Json.TestLibrary.targets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
17 changes: 17 additions & 0 deletions
17
...aries/System.Text.Json/tests/System.Text.Json.SourceGeneration.TestLibrary/TestClasses.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/NETStandardContextTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters