Skip to content

Commit

Permalink
Resolve ILLink warnings in System.Text.Json (Round 1) (#51886)
Browse files Browse the repository at this point in the history
* Resolve ILLink warnings in System.Text.Json (Round 1)

Also resolve ILLink warnings in System.Net.Http.Json.

Contributes to #45623

* PR feedback
  • Loading branch information
eerhardt authored Apr 28, 2021
1 parent f664348 commit c73ef49
Show file tree
Hide file tree
Showing 37 changed files with 302 additions and 206 deletions.
61 changes: 41 additions & 20 deletions src/libraries/System.Net.Http.Json/ref/System.Net.Http.Json.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent);net5.0;netstandard2.0;net461</TargetFrameworks>
<ExcludeCurrentNetCoreAppFromPackage>true</ExcludeCurrentNetCoreAppFromPackage>
Expand All @@ -13,6 +13,11 @@
<ProjectReference Include="..\..\System.Net.Http\ref\System.Net.Http.csproj" />
<ProjectReference Include="..\..\System.Net.Primitives\ref\System.Net.Primitives.csproj" />
</ItemGroup>
<ItemGroup Condition="$([MSBuild]::GetTargetFrameworkIdentifier('$(TargetFramework)')) != '.NETCoreApp'">
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\DynamicallyAccessedMembersAttribute.cs" />
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\DynamicallyAccessedMemberTypes.cs" />
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\RequiresUnreferencedCodeAttribute.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\System.Text.Json\ref\System.Text.Json.csproj" />
</ItemGroup>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent);net5.0;netstandard2.0;net461</TargetFrameworks>
<ExcludeCurrentNetCoreAppFromPackage>true</ExcludeCurrentNetCoreAppFromPackage>
Expand All @@ -23,6 +23,10 @@
<Compile Include="System\Net\Http\Json\HttpContentJsonExtensions.netstandard.cs" />
<Compile Include="System\Net\Http\Json\TranscodingReadStream.cs" />
<Compile Include="System\Net\Http\Json\TranscodingWriteStream.cs" />
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\DynamicallyAccessedMembersAttribute.cs" />
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\DynamicallyAccessedMemberTypes.cs" />
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\RequiresUnreferencedCodeAttribute.cs" />
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\UnconditionalSuppressMessageAttribute.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' != 'netstandard2.0' and '$(TargetFramework)' != 'net461'">
<Reference Include="System.Memory" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,39 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
using System.Threading;
using System.Threading.Tasks;

[assembly: UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Target = "M:System.Net.Http.Json.HttpClientJsonExtensions.<GetFromJsonAsyncCore>d__12.MoveNext()",
Scope = "member",
Justification = "Workaround for https://github.com/mono/linker/issues/1416. The outer method is marked as RequiresUnreferencedCode.")]
[assembly: UnconditionalSuppressMessage("ReflectionAnalysis", "IL2077:UnrecognizedReflectionPattern",
Target = "M:System.Net.Http.Json.HttpClientJsonExtensions.<GetFromJsonAsyncCore>d__12.MoveNext()",
Scope = "member",
Justification = "Workaround for https://github.com/mono/linker/issues/1416. The outer method is marked as RequiresUnreferencedCode.")]
[assembly: UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Target = "M:System.Net.Http.Json.HttpClientJsonExtensions.<GetFromJsonAsyncCore>d__13`1.MoveNext()",
Scope = "member",
Justification = "Workaround for https://github.com/mono/linker/issues/1416. The outer method is marked as RequiresUnreferencedCode.")]
[assembly: UnconditionalSuppressMessage("ReflectionAnalysis", "IL2091:UnrecognizedReflectionPattern",
Target = "M:System.Net.Http.Json.HttpClientJsonExtensions.<GetFromJsonAsyncCore>d__13`1.MoveNext()",
Scope = "member",
Justification = "Workaround for https://github.com/mono/linker/issues/1416. The outer method is marked as RequiresUnreferencedCode.")]

namespace System.Net.Http.Json
{
/// <summary>
/// Contains the extensions methods for using JSON as the content-type in HttpClient.
/// </summary>
public static partial class HttpClientJsonExtensions
{
public static Task<object?> GetFromJsonAsync(this HttpClient client, string? requestUri, Type type, JsonSerializerOptions? options, CancellationToken cancellationToken = default)
[RequiresUnreferencedCode(HttpContentJsonExtensions.SerializationUnreferencedCodeMessage)]
public static Task<object?> GetFromJsonAsync(this HttpClient client, string? requestUri, [DynamicallyAccessedMembers(JsonHelpers.DeserializationMemberTypes)] Type type, JsonSerializerOptions? options, CancellationToken cancellationToken = default)
{
if (client == null)
{
Expand All @@ -25,7 +44,8 @@ public static partial class HttpClientJsonExtensions
return GetFromJsonAsyncCore(taskResponse, type, options, cancellationToken);
}

public static Task<object?> GetFromJsonAsync(this HttpClient client, Uri? requestUri, Type type, JsonSerializerOptions? options, CancellationToken cancellationToken = default)
[RequiresUnreferencedCode(HttpContentJsonExtensions.SerializationUnreferencedCodeMessage)]
public static Task<object?> GetFromJsonAsync(this HttpClient client, Uri? requestUri, [DynamicallyAccessedMembers(JsonHelpers.DeserializationMemberTypes)] Type type, JsonSerializerOptions? options, CancellationToken cancellationToken = default)
{
if (client == null)
{
Expand All @@ -36,7 +56,8 @@ public static partial class HttpClientJsonExtensions
return GetFromJsonAsyncCore(taskResponse, type, options, cancellationToken);
}

public static Task<TValue?> GetFromJsonAsync<TValue>(this HttpClient client, string? requestUri, JsonSerializerOptions? options, CancellationToken cancellationToken = default)
[RequiresUnreferencedCode(HttpContentJsonExtensions.SerializationUnreferencedCodeMessage)]
public static Task<TValue?> GetFromJsonAsync<[DynamicallyAccessedMembers(JsonHelpers.DeserializationMemberTypes)] TValue>(this HttpClient client, string? requestUri, JsonSerializerOptions? options, CancellationToken cancellationToken = default)
{
if (client == null)
{
Expand All @@ -47,7 +68,8 @@ public static partial class HttpClientJsonExtensions
return GetFromJsonAsyncCore<TValue>(taskResponse, options, cancellationToken);
}

public static Task<TValue?> GetFromJsonAsync<TValue>(this HttpClient client, Uri? requestUri, JsonSerializerOptions? options, CancellationToken cancellationToken = default)
[RequiresUnreferencedCode(HttpContentJsonExtensions.SerializationUnreferencedCodeMessage)]
public static Task<TValue?> GetFromJsonAsync<[DynamicallyAccessedMembers(JsonHelpers.DeserializationMemberTypes)] TValue>(this HttpClient client, Uri? requestUri, JsonSerializerOptions? options, CancellationToken cancellationToken = default)
{
if (client == null)
{
Expand Down Expand Up @@ -102,19 +124,24 @@ public static partial class HttpClientJsonExtensions
return GetFromJsonAsyncCore<TValue>(taskResponse, jsonTypeInfo, cancellationToken);
}

public static Task<object?> GetFromJsonAsync(this HttpClient client, string? requestUri, Type type, CancellationToken cancellationToken = default)
[RequiresUnreferencedCode(HttpContentJsonExtensions.SerializationUnreferencedCodeMessage)]
public static Task<object?> GetFromJsonAsync(this HttpClient client, string? requestUri, [DynamicallyAccessedMembers(JsonHelpers.DeserializationMemberTypes)] Type type, CancellationToken cancellationToken = default)
=> client.GetFromJsonAsync(requestUri, type, options: null, cancellationToken);

public static Task<object?> GetFromJsonAsync(this HttpClient client, Uri? requestUri, Type type, CancellationToken cancellationToken = default)
[RequiresUnreferencedCode(HttpContentJsonExtensions.SerializationUnreferencedCodeMessage)]
public static Task<object?> GetFromJsonAsync(this HttpClient client, Uri? requestUri, [DynamicallyAccessedMembers(JsonHelpers.DeserializationMemberTypes)] Type type, CancellationToken cancellationToken = default)
=> client.GetFromJsonAsync(requestUri, type, options: null, cancellationToken);

public static Task<TValue?> GetFromJsonAsync<TValue>(this HttpClient client, string? requestUri, CancellationToken cancellationToken = default)
[RequiresUnreferencedCode(HttpContentJsonExtensions.SerializationUnreferencedCodeMessage)]
public static Task<TValue?> GetFromJsonAsync<[DynamicallyAccessedMembers(JsonHelpers.DeserializationMemberTypes)] TValue>(this HttpClient client, string? requestUri, CancellationToken cancellationToken = default)
=> client.GetFromJsonAsync<TValue>(requestUri, options: null, cancellationToken);

public static Task<TValue?> GetFromJsonAsync<TValue>(this HttpClient client, Uri? requestUri, CancellationToken cancellationToken = default)
[RequiresUnreferencedCode(HttpContentJsonExtensions.SerializationUnreferencedCodeMessage)]
public static Task<TValue?> GetFromJsonAsync<[DynamicallyAccessedMembers(JsonHelpers.DeserializationMemberTypes)] TValue>(this HttpClient client, Uri? requestUri, CancellationToken cancellationToken = default)
=> client.GetFromJsonAsync<TValue>(requestUri, options: null, cancellationToken);

private static async Task<object?> GetFromJsonAsyncCore(Task<HttpResponseMessage> taskResponse, Type type, JsonSerializerOptions? options, CancellationToken cancellationToken)
[RequiresUnreferencedCode(HttpContentJsonExtensions.SerializationUnreferencedCodeMessage)]
private static async Task<object?> GetFromJsonAsyncCore(Task<HttpResponseMessage> taskResponse, [DynamicallyAccessedMembers(JsonHelpers.DeserializationMemberTypes)] Type type, JsonSerializerOptions? options, CancellationToken cancellationToken)
{
using (HttpResponseMessage response = await taskResponse.ConfigureAwait(false))
{
Expand All @@ -126,7 +153,8 @@ public static partial class HttpClientJsonExtensions
}
}

private static async Task<T?> GetFromJsonAsyncCore<T>(Task<HttpResponseMessage> taskResponse, JsonSerializerOptions? options, CancellationToken cancellationToken)
[RequiresUnreferencedCode(HttpContentJsonExtensions.SerializationUnreferencedCodeMessage)]
private static async Task<T?> GetFromJsonAsyncCore<[DynamicallyAccessedMembers(JsonHelpers.DeserializationMemberTypes)] T>(Task<HttpResponseMessage> taskResponse, JsonSerializerOptions? options, CancellationToken cancellationToken)
{
using (HttpResponseMessage response = await taskResponse.ConfigureAwait(false))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
using System.Threading;
Expand All @@ -10,7 +11,8 @@ namespace System.Net.Http.Json
{
public static partial class HttpClientJsonExtensions
{
public static Task<HttpResponseMessage> PostAsJsonAsync<TValue>(this HttpClient client, string? requestUri, TValue value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default)
[RequiresUnreferencedCode(HttpContentJsonExtensions.SerializationUnreferencedCodeMessage)]
public static Task<HttpResponseMessage> PostAsJsonAsync<[DynamicallyAccessedMembers(JsonHelpers.SerializationMemberTypes)] TValue>(this HttpClient client, string? requestUri, TValue value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default)
{
if (client == null)
{
Expand All @@ -21,7 +23,8 @@ public static Task<HttpResponseMessage> PostAsJsonAsync<TValue>(this HttpClient
return client.PostAsync(requestUri, content, cancellationToken);
}

public static Task<HttpResponseMessage> PostAsJsonAsync<TValue>(this HttpClient client, Uri? requestUri, TValue value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default)
[RequiresUnreferencedCode(HttpContentJsonExtensions.SerializationUnreferencedCodeMessage)]
public static Task<HttpResponseMessage> PostAsJsonAsync<[DynamicallyAccessedMembers(JsonHelpers.SerializationMemberTypes)] TValue>(this HttpClient client, Uri? requestUri, TValue value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default)
{
if (client == null)
{
Expand All @@ -32,10 +35,12 @@ public static Task<HttpResponseMessage> PostAsJsonAsync<TValue>(this HttpClient
return client.PostAsync(requestUri, content, cancellationToken);
}

public static Task<HttpResponseMessage> PostAsJsonAsync<TValue>(this HttpClient client, string? requestUri, TValue value, CancellationToken cancellationToken)
[RequiresUnreferencedCode(HttpContentJsonExtensions.SerializationUnreferencedCodeMessage)]
public static Task<HttpResponseMessage> PostAsJsonAsync<[DynamicallyAccessedMembers(JsonHelpers.SerializationMemberTypes)] TValue>(this HttpClient client, string? requestUri, TValue value, CancellationToken cancellationToken)
=> client.PostAsJsonAsync(requestUri, value, options: null, cancellationToken);

public static Task<HttpResponseMessage> PostAsJsonAsync<TValue>(this HttpClient client, Uri? requestUri, TValue value, CancellationToken cancellationToken)
[RequiresUnreferencedCode(HttpContentJsonExtensions.SerializationUnreferencedCodeMessage)]
public static Task<HttpResponseMessage> PostAsJsonAsync<[DynamicallyAccessedMembers(JsonHelpers.SerializationMemberTypes)] TValue>(this HttpClient client, Uri? requestUri, TValue value, CancellationToken cancellationToken)
=> client.PostAsJsonAsync(requestUri, value, options: null, cancellationToken);

public static Task<HttpResponseMessage> PostAsJsonAsync<TValue>(this HttpClient client, string? requestUri, TValue value, JsonTypeInfo<TValue> jsonTypeInfo, CancellationToken cancellationToken = default)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
using System.Threading;
Expand All @@ -10,7 +11,8 @@ namespace System.Net.Http.Json
{
public static partial class HttpClientJsonExtensions
{
public static Task<HttpResponseMessage> PutAsJsonAsync<TValue>(this HttpClient client, string? requestUri, TValue value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default)
[RequiresUnreferencedCode(HttpContentJsonExtensions.SerializationUnreferencedCodeMessage)]
public static Task<HttpResponseMessage> PutAsJsonAsync<[DynamicallyAccessedMembers(JsonHelpers.SerializationMemberTypes)] TValue>(this HttpClient client, string? requestUri, TValue value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default)
{
if (client == null)
{
Expand All @@ -21,7 +23,8 @@ public static Task<HttpResponseMessage> PutAsJsonAsync<TValue>(this HttpClient c
return client.PutAsync(requestUri, content, cancellationToken);
}

public static Task<HttpResponseMessage> PutAsJsonAsync<TValue>(this HttpClient client, Uri? requestUri, TValue value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default)
[RequiresUnreferencedCode(HttpContentJsonExtensions.SerializationUnreferencedCodeMessage)]
public static Task<HttpResponseMessage> PutAsJsonAsync<[DynamicallyAccessedMembers(JsonHelpers.SerializationMemberTypes)] TValue>(this HttpClient client, Uri? requestUri, TValue value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default)
{
if (client == null)
{
Expand All @@ -32,10 +35,12 @@ public static Task<HttpResponseMessage> PutAsJsonAsync<TValue>(this HttpClient c
return client.PutAsync(requestUri, content, cancellationToken);
}

public static Task<HttpResponseMessage> PutAsJsonAsync<TValue>(this HttpClient client, string? requestUri, TValue value, CancellationToken cancellationToken)
[RequiresUnreferencedCode(HttpContentJsonExtensions.SerializationUnreferencedCodeMessage)]
public static Task<HttpResponseMessage> PutAsJsonAsync<[DynamicallyAccessedMembers(JsonHelpers.SerializationMemberTypes)] TValue>(this HttpClient client, string? requestUri, TValue value, CancellationToken cancellationToken)
=> client.PutAsJsonAsync(requestUri, value, options: null, cancellationToken);

public static Task<HttpResponseMessage> PutAsJsonAsync<TValue>(this HttpClient client, Uri? requestUri, TValue value, CancellationToken cancellationToken)
[RequiresUnreferencedCode(HttpContentJsonExtensions.SerializationUnreferencedCodeMessage)]
public static Task<HttpResponseMessage> PutAsJsonAsync<[DynamicallyAccessedMembers(JsonHelpers.SerializationMemberTypes)] TValue>(this HttpClient client, Uri? requestUri, TValue value, CancellationToken cancellationToken)
=> client.PutAsJsonAsync(requestUri, value, options: null, cancellationToken);

public static Task<HttpResponseMessage> PutAsJsonAsync<TValue>(this HttpClient client, string? requestUri, TValue value, JsonTypeInfo<TValue> jsonTypeInfo, CancellationToken cancellationToken = default)
Expand Down
Loading

0 comments on commit c73ef49

Please sign in to comment.