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

[AOT] Enable analysis and annotate Http.Results #46082

Merged
merged 23 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from 19 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
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
58 changes: 58 additions & 0 deletions src/Http/Http.Abstractions/test/RouteValueDictionaryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,57 @@ public void CreateFromObject_MixedCaseThrows()
Assert.Equal(message, exception.Message, ignoreCase: true);
}

[Fact]
public void CreateFromObject_Struct_ReadValues()
{
// Arrange
var obj = new StructAddress() { City = "Singapore" };

// Act
var dict = new RouteValueDictionary(obj);

// Assert
Assert.NotNull(dict._propertyStorage);
AssertEmptyArrayStorage(dict);
Assert.Collection(
dict.OrderBy(kvp => kvp.Key),
kvp => { Assert.Equal("City", kvp.Key); Assert.Equal("Singapore", kvp.Value); },
kvp => { Assert.Equal("State", kvp.Key); Assert.Null(kvp.Value); });
}

[Fact]
public void CreateFromObject_NullableStruct_ReadValues()
{
// Arrange
StructAddress? obj = new StructAddress() { City = "Singapore" };

// Act
var dict = new RouteValueDictionary(obj);

// Assert
Assert.NotNull(dict._propertyStorage);
AssertEmptyArrayStorage(dict);
Assert.Collection(
dict.OrderBy(kvp => kvp.Key),
kvp => { Assert.Equal("City", kvp.Key); Assert.Equal("Singapore", kvp.Value); },
kvp => { Assert.Equal("State", kvp.Key); Assert.Null(kvp.Value); });
}

[Fact]
public void CreateFromObject_NullStruct_ReadValues()
{
// Arrange
StructAddress? obj = null;

// Act
var dict = new RouteValueDictionary(obj);

// Assert
Assert.Null(dict._propertyStorage);
AssertEmptyArrayStorage(dict);
Assert.Empty(dict);
}

// Our comparer is hardcoded to be OrdinalIgnoreCase no matter what.
[Fact]
public void Comparer_IsOrdinalIgnoreCase()
Expand Down Expand Up @@ -2164,4 +2215,11 @@ private class Address

public string? State { get; set; }
}

private struct StructAddress
{
public string? City { get; set; }

public string? State { get; set; }
}
}
19 changes: 17 additions & 2 deletions src/Http/Http.Results/src/AcceptedAtRoute.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// 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;
using Microsoft.AspNetCore.Internal;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
Expand All @@ -22,11 +24,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 +50,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
20 changes: 18 additions & 2 deletions src/Http/Http.Results/src/AcceptedAtRouteOfT.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// 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;
using Microsoft.AspNetCore.Internal;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
Expand All @@ -24,11 +26,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 +54,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
19 changes: 17 additions & 2 deletions src/Http/Http.Results/src/CreatedAtRoute.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// 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;
using Microsoft.AspNetCore.Internal;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
Expand All @@ -22,11 +24,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 +50,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
20 changes: 18 additions & 2 deletions src/Http/Http.Results/src/CreatedAtRouteOfT.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// 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;
using Microsoft.AspNetCore.Internal;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
Expand All @@ -24,11 +26,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 +54,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
4 changes: 4 additions & 0 deletions src/Http/Http.Results/src/HttpResultsHelper.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.Text;
using System.Text.Json;
using Microsoft.AspNetCore.Internal;
Expand All @@ -15,6 +16,9 @@ internal static partial class HttpResultsHelper
internal const string DefaultContentType = "text/plain; charset=utf-8";
private static readonly Encoding DefaultEncoding = Encoding.UTF8;

// Remove once https://github.com/dotnet/aspnetcore/pull/46008 is done.
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "<Pending>")]
[UnconditionalSuppressMessage("AOT", "IL3050:Calling members annotated with 'RequiresDynamicCodeAttribute' may break functionality when AOT compiling.", Justification = "<Pending>")]
public static Task WriteResultAsJsonAsync<T>(
HttpContext httpContext,
ILogger logger,
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 All @@ -19,6 +19,7 @@
<Compile Include="$(SharedSourceRoot)ProblemDetails\ProblemDetailsDefaults.cs" LinkBase="Shared" />
<Compile Include="$(SharedSourceRoot)ApiExplorerTypes\*.cs" LinkBase="Shared" />
<Compile Include="$(SharedSourceRoot)RoutingMetadata\AcceptsMetadata.cs" LinkBase="Shared" />
<Compile Include="$(SharedSourceRoot)RouteValueDictionaryTrimmerWarning.cs" LinkBase="Shared" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading