Skip to content

Commit

Permalink
[AOT] Enable analysis and annotate Http.Results
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK committed Jan 17, 2023
1 parent 6f1752a commit 4022f3d
Show file tree
Hide file tree
Showing 17 changed files with 436 additions and 34 deletions.
1 change: 1 addition & 0 deletions eng/TrimmableProjects.props
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<TrimmableProject Include="Microsoft.AspNetCore.Http.Abstractions" />
<TrimmableProject Include="Microsoft.AspNetCore.Http.Extensions" />
<TrimmableProject Include="Microsoft.AspNetCore.Http.Features" />
<TrimmableProject Include="Microsoft.AspNetCore.Http.Results" />
<TrimmableProject Include="Microsoft.AspNetCore.Http" />
<TrimmableProject Include="Microsoft.AspNetCore.Metadata" />
<TrimmableProject Include="Microsoft.AspNetCore.Routing.Abstractions" />
Expand Down
18 changes: 16 additions & 2 deletions src/Http/Http.Results/src/AcceptedAtRoute.cs
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.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http.Metadata;
Expand All @@ -22,11 +23,24 @@ public sealed class AcceptedAtRoute : IResult, IEndpointMetadataProvider, IStatu
/// provided.
/// </summary>
/// <param name="routeValues">The route data to use for generating the URL.</param>
[RequiresUnreferencedCode(RouteValueDictionaryTrimmerWarning.Warning)]
internal AcceptedAtRoute(object? routeValues)
: this(routeName: null, routeValues: routeValues)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="AcceptedAtRoute"/> class with the values
/// provided.
/// </summary>
/// <param name="routeName">The name of the route to use for generating the URL.</param>
/// <param name="routeValues">The route data to use for generating the URL.</param>
[RequiresUnreferencedCode(RouteValueDictionaryTrimmerWarning.Warning)]
internal AcceptedAtRoute(string? routeName, object? routeValues)
: this(routeName, new RouteValueDictionary(routeValues))
{
}

/// <summary>
/// Initializes a new instance of the <see cref="AcceptedAtRoute"/> class with the values
/// provided.
Expand All @@ -35,10 +49,10 @@ internal AcceptedAtRoute(object? routeValues)
/// <param name="routeValues">The route data to use for generating the URL.</param>
internal AcceptedAtRoute(
string? routeName,
object? routeValues)
RouteValueDictionary routeValues)
{
RouteName = routeName;
RouteValues = new RouteValueDictionary(routeValues);
RouteValues = routeValues;
}

/// <summary>
Expand Down
19 changes: 17 additions & 2 deletions src/Http/Http.Results/src/AcceptedAtRouteOfT.cs
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.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http.Metadata;
Expand All @@ -24,11 +25,25 @@ public sealed class AcceptedAtRoute<TValue> : IResult, IEndpointMetadataProvider
/// </summary>
/// <param name="routeValues">The route data to use for generating the URL.</param>
/// <param name="value">The value to format in the entity body.</param>
[RequiresUnreferencedCode(RouteValueDictionaryTrimmerWarning.Warning)]
internal AcceptedAtRoute(object? routeValues, TValue? value)
: this(routeName: null, routeValues: routeValues, value: value)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="AcceptedAtRoute"/> class with the values
/// provided.
/// </summary>
/// <param name="routeName">The name of the route to use for generating the URL.</param>
/// <param name="routeValues">The route data to use for generating the URL.</param>
/// <param name="value">The value to format in the entity body.</param>
[RequiresUnreferencedCode(RouteValueDictionaryTrimmerWarning.Warning)]
internal AcceptedAtRoute(string? routeName, object? routeValues, TValue? value)
: this(routeName, new RouteValueDictionary(routeValues), value)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="AcceptedAtRoute"/> class with the values
/// provided.
Expand All @@ -38,12 +53,12 @@ internal AcceptedAtRoute(object? routeValues, TValue? value)
/// <param name="value">The value to format in the entity body.</param>
internal AcceptedAtRoute(
string? routeName,
object? routeValues,
RouteValueDictionary routeValues,
TValue? value)
{
Value = value;
RouteName = routeName;
RouteValues = new RouteValueDictionary(routeValues);
RouteValues = routeValues;
HttpResultsHelper.ApplyProblemDetailsDefaultsIfNeeded(Value, StatusCode);
}

Expand Down
18 changes: 16 additions & 2 deletions src/Http/Http.Results/src/CreatedAtRoute.cs
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.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http.Metadata;
Expand All @@ -22,11 +23,24 @@ public sealed class CreatedAtRoute : IResult, IEndpointMetadataProvider, IStatus
/// provided.
/// </summary>
/// <param name="routeValues">The route data to use for generating the URL.</param>
[RequiresUnreferencedCode(RouteValueDictionaryTrimmerWarning.Warning)]
internal CreatedAtRoute(object? routeValues)
: this(routeName: null, routeValues: routeValues)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="CreatedAtRoute"/> class with the values
/// provided.
/// </summary>
/// <param name="routeName">The name of the route to use for generating the URL.</param>
/// <param name="routeValues">The route data to use for generating the URL.</param>
[RequiresUnreferencedCode(RouteValueDictionaryTrimmerWarning.Warning)]
internal CreatedAtRoute(string? routeName, object? routeValues)
: this(routeName, new RouteValueDictionary(routeValues))
{
}

/// <summary>
/// Initializes a new instance of the <see cref="CreatedAtRoute"/> class with the values
/// provided.
Expand All @@ -35,10 +49,10 @@ internal CreatedAtRoute(object? routeValues)
/// <param name="routeValues">The route data to use for generating the URL.</param>
internal CreatedAtRoute(
string? routeName,
object? routeValues)
RouteValueDictionary routeValues)
{
RouteName = routeName;
RouteValues = new RouteValueDictionary(routeValues);
RouteValues = routeValues;
}

/// <summary>
Expand Down
19 changes: 17 additions & 2 deletions src/Http/Http.Results/src/CreatedAtRouteOfT.cs
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.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http.Metadata;
Expand All @@ -24,11 +25,25 @@ public sealed class CreatedAtRoute<TValue> : IResult, IEndpointMetadataProvider,
/// </summary>
/// <param name="routeValues">The route data to use for generating the URL.</param>
/// <param name="value">The value to format in the entity body.</param>
[RequiresUnreferencedCode(RouteValueDictionaryTrimmerWarning.Warning)]
internal CreatedAtRoute(object? routeValues, TValue? value)
: this(routeName: null, routeValues: routeValues, value: value)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="CreatedAtRoute"/> class with the values
/// provided.
/// </summary>
/// <param name="routeName">The name of the route to use for generating the URL.</param>
/// <param name="routeValues">The route data to use for generating the URL.</param>
/// <param name="value">The value to format in the entity body.</param>
[RequiresUnreferencedCode(RouteValueDictionaryTrimmerWarning.Warning)]
internal CreatedAtRoute(string? routeName, object? routeValues, TValue? value)
: this(routeName, new RouteValueDictionary(routeValues), value)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="CreatedAtRoute"/> class with the values
/// provided.
Expand All @@ -38,12 +53,12 @@ internal CreatedAtRoute(object? routeValues, TValue? value)
/// <param name="value">The value to format in the entity body.</param>
internal CreatedAtRoute(
string? routeName,
object? routeValues,
RouteValueDictionary routeValues,
TValue? value)
{
Value = value;
RouteName = routeName;
RouteValues = new RouteValueDictionary(routeValues);
RouteValues = routeValues;
HttpResultsHelper.ApplyProblemDetailsDefaultsIfNeeded(Value, StatusCode);
}

Expand Down
8 changes: 8 additions & 0 deletions src/Http/Http.Results/src/HttpResultsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ public static Task WriteResultAsJsonAsync<T>(

// In this case the polymorphism is not
// relevant and we don't need to box.
#pragma warning disable IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code
#pragma warning disable IL3050 // Calling members annotated with 'RequiresDynamicCodeAttribute' may break functionality when AOT compiling.
return httpContext.Response.WriteAsJsonAsync(
value,
options: jsonSerializerOptions,
contentType: contentType);
#pragma warning restore IL3050 // Calling members annotated with 'RequiresDynamicCodeAttribute' may break functionality when AOT compiling.
#pragma warning restore IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code
}

var runtimeType = value.GetType();
Expand All @@ -48,11 +52,15 @@ public static Task WriteResultAsJsonAsync<T>(
// and avoid source generators issues.
// https://github.com/dotnet/aspnetcore/issues/43894
// https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-polymorphism
#pragma warning disable IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code
#pragma warning disable IL3050 // Calling members annotated with 'RequiresDynamicCodeAttribute' may break functionality when AOT compiling.
return httpContext.Response.WriteAsJsonAsync(
value,
runtimeType,
options: jsonSerializerOptions,
contentType: contentType);
#pragma warning restore IL3050 // Calling members annotated with 'RequiresDynamicCodeAttribute' may break functionality when AOT compiling.
#pragma warning restore IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code
}

public static Task WriteResultAsContentAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>aspnetcore</PackageTags>
<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
<IsTrimmable>true</IsTrimmable>
<RootNamespace>Microsoft.AspNetCore.Http.Result</RootNamespace>
</PropertyGroup>

Expand Down
10 changes: 10 additions & 0 deletions src/Http/Http.Results/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*REMOVED*static Microsoft.AspNetCore.Http.HttpResults.EmptyHttpResult.Instance.get -> Microsoft.AspNetCore.Http.HttpResults.EmptyHttpResult!
Microsoft.AspNetCore.Http.HttpResults.EmptyHttpResult (forwarded, contained in Microsoft.AspNetCore.Http.Abstractions)
Microsoft.AspNetCore.Http.HttpResults.EmptyHttpResult.ExecuteAsync(Microsoft.AspNetCore.Http.HttpContext! httpContext) -> System.Threading.Tasks.Task! (forwarded, contained in Microsoft.AspNetCore.Http.Abstractions)
static Microsoft.AspNetCore.Http.Results.AcceptedAtRoute(string? routeName, Microsoft.AspNetCore.Routing.RouteValueDictionary! routeValues, object? value = null) -> Microsoft.AspNetCore.Http.IResult!
static Microsoft.AspNetCore.Http.Results.AcceptedAtRoute<TValue>(string? routeName, Microsoft.AspNetCore.Routing.RouteValueDictionary! routeValues, TValue? value = default(TValue?)) -> Microsoft.AspNetCore.Http.IResult!
static Microsoft.AspNetCore.Http.HttpResults.EmptyHttpResult.Instance.get -> Microsoft.AspNetCore.Http.HttpResults.EmptyHttpResult! (forwarded, contained in Microsoft.AspNetCore.Http.Abstractions)
static Microsoft.AspNetCore.Http.Results.Created() -> Microsoft.AspNetCore.Http.IResult!
*REMOVED*static Microsoft.AspNetCore.Http.Results.Created(string! uri, object? value) -> Microsoft.AspNetCore.Http.IResult!
Expand All @@ -12,11 +14,19 @@ static Microsoft.AspNetCore.Http.Results.Created(string? uri, object? value) ->
static Microsoft.AspNetCore.Http.Results.Created(System.Uri? uri, object? value) -> Microsoft.AspNetCore.Http.IResult!
static Microsoft.AspNetCore.Http.Results.Created<TValue>(string? uri, TValue? value) -> Microsoft.AspNetCore.Http.IResult!
static Microsoft.AspNetCore.Http.Results.Created<TValue>(System.Uri? uri, TValue? value) -> Microsoft.AspNetCore.Http.IResult!
static Microsoft.AspNetCore.Http.Results.CreatedAtRoute(string? routeName, Microsoft.AspNetCore.Routing.RouteValueDictionary! routeValues, object? value = null) -> Microsoft.AspNetCore.Http.IResult!
static Microsoft.AspNetCore.Http.Results.CreatedAtRoute<TValue>(string? routeName, Microsoft.AspNetCore.Routing.RouteValueDictionary! routeValues, TValue? value = default(TValue?)) -> Microsoft.AspNetCore.Http.IResult!
static Microsoft.AspNetCore.Http.Results.RedirectToRoute(string? routeName, Microsoft.AspNetCore.Routing.RouteValueDictionary? routeValues, bool permanent = false, bool preserveMethod = false, string? fragment = null) -> Microsoft.AspNetCore.Http.IResult!
static Microsoft.AspNetCore.Http.TypedResults.AcceptedAtRoute(string? routeName, Microsoft.AspNetCore.Routing.RouteValueDictionary! routeValues) -> Microsoft.AspNetCore.Http.HttpResults.AcceptedAtRoute!
static Microsoft.AspNetCore.Http.TypedResults.AcceptedAtRoute<TValue>(TValue? value, string? routeName, Microsoft.AspNetCore.Routing.RouteValueDictionary! routeValues) -> Microsoft.AspNetCore.Http.HttpResults.AcceptedAtRoute<TValue>!
static Microsoft.AspNetCore.Http.TypedResults.Created() -> Microsoft.AspNetCore.Http.HttpResults.Created!
static Microsoft.AspNetCore.Http.TypedResults.Created(string? uri) -> Microsoft.AspNetCore.Http.HttpResults.Created!
static Microsoft.AspNetCore.Http.TypedResults.Created(System.Uri? uri) -> Microsoft.AspNetCore.Http.HttpResults.Created!
static Microsoft.AspNetCore.Http.TypedResults.Created<TValue>(string? uri, TValue? value) -> Microsoft.AspNetCore.Http.HttpResults.Created<TValue>!
static Microsoft.AspNetCore.Http.TypedResults.Created<TValue>(System.Uri? uri, TValue? value) -> Microsoft.AspNetCore.Http.HttpResults.Created<TValue>!
static Microsoft.AspNetCore.Http.TypedResults.CreatedAtRoute(string? routeName, Microsoft.AspNetCore.Routing.RouteValueDictionary! routeValues) -> Microsoft.AspNetCore.Http.HttpResults.CreatedAtRoute!
static Microsoft.AspNetCore.Http.TypedResults.CreatedAtRoute<TValue>(TValue? value, string? routeName, Microsoft.AspNetCore.Routing.RouteValueDictionary! routeValues) -> Microsoft.AspNetCore.Http.HttpResults.CreatedAtRoute<TValue>!
static Microsoft.AspNetCore.Http.TypedResults.RedirectToRoute(string? routeName, Microsoft.AspNetCore.Routing.RouteValueDictionary? routeValues, bool permanent = false, bool preserveMethod = false, string? fragment = null) -> Microsoft.AspNetCore.Http.HttpResults.RedirectToRouteHttpResult!
*REMOVED*static Microsoft.AspNetCore.Http.Results.Created<TValue>(System.Uri! uri, TValue? value) -> Microsoft.AspNetCore.Http.IResult!
*REMOVED*static Microsoft.AspNetCore.Http.Results.Created<TValue>(string! uri, TValue? value) -> Microsoft.AspNetCore.Http.IResult!
*REMOVED*static Microsoft.AspNetCore.Http.TypedResults.Created(System.Uri! uri) -> Microsoft.AspNetCore.Http.HttpResults.Created!
Expand Down
33 changes: 32 additions & 1 deletion src/Http/Http.Results/src/RedirectToRouteHttpResult.cs
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 Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
Expand All @@ -19,6 +20,7 @@ public sealed partial class RedirectToRouteHttpResult : IResult
/// provided.
/// </summary>
/// <param name="routeValues">The parameters for the route.</param>
[RequiresUnreferencedCode(RouteValueDictionaryTrimmerWarning.Warning)]
internal RedirectToRouteHttpResult(object? routeValues)
: this(routeName: null, routeValues: routeValues)
{
Expand All @@ -30,6 +32,7 @@ internal RedirectToRouteHttpResult(object? routeValues)
/// </summary>
/// <param name="routeName">The name of the route.</param>
/// <param name="routeValues">The parameters for the route.</param>
[RequiresUnreferencedCode(RouteValueDictionaryTrimmerWarning.Warning)]
internal RedirectToRouteHttpResult(
string? routeName,
object? routeValues)
Expand All @@ -45,6 +48,7 @@ internal RedirectToRouteHttpResult(
/// <param name="routeValues">The parameters for the route.</param>
/// <param name="permanent">If set to true, makes the redirect permanent (301).
/// Otherwise a temporary redirect is used (302).</param>
[RequiresUnreferencedCode(RouteValueDictionaryTrimmerWarning.Warning)]
internal RedirectToRouteHttpResult(
string? routeName,
object? routeValues,
Expand All @@ -62,6 +66,7 @@ internal RedirectToRouteHttpResult(
/// <param name="permanent">If set to true, makes the redirect permanent (301).
/// Otherwise a temporary redirect is used (302).</param>
/// <param name="fragment">The fragment to add to the URL.</param>
[RequiresUnreferencedCode(RouteValueDictionaryTrimmerWarning.Warning)]
internal RedirectToRouteHttpResult(
string? routeName,
object? routeValues,
Expand All @@ -82,15 +87,41 @@ internal RedirectToRouteHttpResult(
/// <param name="preserveMethod">If set to true, make the temporary redirect (307)
/// or permanent redirect (308) preserve the initial request method.</param>
/// <param name="fragment">The fragment to add to the URL.</param>
[RequiresUnreferencedCode(RouteValueDictionaryTrimmerWarning.Warning)]
internal RedirectToRouteHttpResult(
string? routeName,
object? routeValues,
bool permanent,
bool preserveMethod,
string? fragment) : this(
routeName,
routeValues == null ? null : new RouteValueDictionary(routeValues),
permanent,
preserveMethod,
fragment)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="RedirectToRouteHttpResult"/> with the values
/// provided.
/// </summary>
/// <param name="routeName">The name of the route.</param>
/// <param name="routeValues">The parameters for the route.</param>
/// <param name="permanent">If set to true, makes the redirect permanent (301).
/// Otherwise a temporary redirect is used (302).</param>
/// <param name="preserveMethod">If set to true, make the temporary redirect (307)
/// or permanent redirect (308) preserve the initial request method.</param>
/// <param name="fragment">The fragment to add to the URL.</param>
internal RedirectToRouteHttpResult(
string? routeName,
RouteValueDictionary? routeValues,
bool permanent,
bool preserveMethod,
string? fragment)
{
RouteName = routeName;
RouteValues = routeValues == null ? null : new RouteValueDictionary(routeValues);
RouteValues = routeValues;
PreserveMethod = preserveMethod;
Permanent = permanent;
Fragment = fragment;
Expand Down
Loading

0 comments on commit 4022f3d

Please sign in to comment.