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

[release/9.0-preview7] Make ApiExplorer for minimal APIs trim-compatible #56937

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 0 additions & 3 deletions .azure/pipelines/ci-public.yml
Original file line number Diff line number Diff line change
Expand Up @@ -556,9 +556,6 @@ stages:
beforeBuild:
- powershell: "& ./src/Servers/IIS/tools/UpdateIISExpressCertificate.ps1; & ./src/Servers/IIS/tools/update_schema.ps1"
displayName: Setup IISExpress test certificates and schema
afterBuild:
- powershell: ./eng/scripts/CompareMinifiedJsFiles.ps1
displayName: Check for changes in generated minified .js files
artifacts:
- name: Windows_Test_Logs_Attempt_$(System.JobAttempt)
path: artifacts/log/
Expand Down
41 changes: 22 additions & 19 deletions AspNetCore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -1674,8 +1674,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ResultsOfTGenerator", "src\
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "OpenApi", "OpenApi", "{2299CCD8-8F9C-4F2B-A633-9BF4DA81022B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.OpenApi.Tests", "src\OpenApi\test\Microsoft.AspNetCore.OpenApi.Tests.csproj", "{3AEFB466-6310-4F3F-923F-9154224E3629}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.OpenApi", "src\OpenApi\src\Microsoft.AspNetCore.OpenApi.csproj", "{EFC8EA45-572D-4D8D-A597-9045A2D8EC40}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.RateLimiting", "src\Middleware\RateLimiting\src\Microsoft.AspNetCore.RateLimiting.csproj", "{8EE73488-2B92-42BD-96C9-0DD65405C828}"
Expand Down Expand Up @@ -1818,6 +1816,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GetDocumentSample", "src\To
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorUnitedApp.Client", "src\Components\Samples\BlazorUnitedApp.Client\BlazorUnitedApp.Client.csproj", "{757CBDE0-5D0A-4FD8-99F3-6C20BDDD4E63}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{B32FF7A7-9CB3-4DCD-AE97-3B2594DB9DAC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AspNetCore.OpenApi.Tests", "src\OpenApi\test\Microsoft.AspNetCore.OpenApi.Tests\Microsoft.AspNetCore.OpenApi.Tests.csproj", "{B9BBC1A8-7F58-4F43-94C3-5F3CB125CEF7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -10147,22 +10149,6 @@ Global
{9716D0D0-2251-44DD-8596-67D253EEF41C}.Release|x64.Build.0 = Release|Any CPU
{9716D0D0-2251-44DD-8596-67D253EEF41C}.Release|x86.ActiveCfg = Release|Any CPU
{9716D0D0-2251-44DD-8596-67D253EEF41C}.Release|x86.Build.0 = Release|Any CPU
{3AEFB466-6310-4F3F-923F-9154224E3629}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3AEFB466-6310-4F3F-923F-9154224E3629}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3AEFB466-6310-4F3F-923F-9154224E3629}.Debug|arm64.ActiveCfg = Debug|Any CPU
{3AEFB466-6310-4F3F-923F-9154224E3629}.Debug|arm64.Build.0 = Debug|Any CPU
{3AEFB466-6310-4F3F-923F-9154224E3629}.Debug|x64.ActiveCfg = Debug|Any CPU
{3AEFB466-6310-4F3F-923F-9154224E3629}.Debug|x64.Build.0 = Debug|Any CPU
{3AEFB466-6310-4F3F-923F-9154224E3629}.Debug|x86.ActiveCfg = Debug|Any CPU
{3AEFB466-6310-4F3F-923F-9154224E3629}.Debug|x86.Build.0 = Debug|Any CPU
{3AEFB466-6310-4F3F-923F-9154224E3629}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3AEFB466-6310-4F3F-923F-9154224E3629}.Release|Any CPU.Build.0 = Release|Any CPU
{3AEFB466-6310-4F3F-923F-9154224E3629}.Release|arm64.ActiveCfg = Release|Any CPU
{3AEFB466-6310-4F3F-923F-9154224E3629}.Release|arm64.Build.0 = Release|Any CPU
{3AEFB466-6310-4F3F-923F-9154224E3629}.Release|x64.ActiveCfg = Release|Any CPU
{3AEFB466-6310-4F3F-923F-9154224E3629}.Release|x64.Build.0 = Release|Any CPU
{3AEFB466-6310-4F3F-923F-9154224E3629}.Release|x86.ActiveCfg = Release|Any CPU
{3AEFB466-6310-4F3F-923F-9154224E3629}.Release|x86.Build.0 = Release|Any CPU
{EFC8EA45-572D-4D8D-A597-9045A2D8EC40}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EFC8EA45-572D-4D8D-A597-9045A2D8EC40}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EFC8EA45-572D-4D8D-A597-9045A2D8EC40}.Debug|arm64.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -10995,6 +10981,22 @@ Global
{757CBDE0-5D0A-4FD8-99F3-6C20BDDD4E63}.Release|x64.Build.0 = Release|Any CPU
{757CBDE0-5D0A-4FD8-99F3-6C20BDDD4E63}.Release|x86.ActiveCfg = Release|Any CPU
{757CBDE0-5D0A-4FD8-99F3-6C20BDDD4E63}.Release|x86.Build.0 = Release|Any CPU
{B9BBC1A8-7F58-4F43-94C3-5F3CB125CEF7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B9BBC1A8-7F58-4F43-94C3-5F3CB125CEF7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B9BBC1A8-7F58-4F43-94C3-5F3CB125CEF7}.Debug|arm64.ActiveCfg = Debug|Any CPU
{B9BBC1A8-7F58-4F43-94C3-5F3CB125CEF7}.Debug|arm64.Build.0 = Debug|Any CPU
{B9BBC1A8-7F58-4F43-94C3-5F3CB125CEF7}.Debug|x64.ActiveCfg = Debug|Any CPU
{B9BBC1A8-7F58-4F43-94C3-5F3CB125CEF7}.Debug|x64.Build.0 = Debug|Any CPU
{B9BBC1A8-7F58-4F43-94C3-5F3CB125CEF7}.Debug|x86.ActiveCfg = Debug|Any CPU
{B9BBC1A8-7F58-4F43-94C3-5F3CB125CEF7}.Debug|x86.Build.0 = Debug|Any CPU
{B9BBC1A8-7F58-4F43-94C3-5F3CB125CEF7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B9BBC1A8-7F58-4F43-94C3-5F3CB125CEF7}.Release|Any CPU.Build.0 = Release|Any CPU
{B9BBC1A8-7F58-4F43-94C3-5F3CB125CEF7}.Release|arm64.ActiveCfg = Release|Any CPU
{B9BBC1A8-7F58-4F43-94C3-5F3CB125CEF7}.Release|arm64.Build.0 = Release|Any CPU
{B9BBC1A8-7F58-4F43-94C3-5F3CB125CEF7}.Release|x64.ActiveCfg = Release|Any CPU
{B9BBC1A8-7F58-4F43-94C3-5F3CB125CEF7}.Release|x64.Build.0 = Release|Any CPU
{B9BBC1A8-7F58-4F43-94C3-5F3CB125CEF7}.Release|x86.ActiveCfg = Release|Any CPU
{B9BBC1A8-7F58-4F43-94C3-5F3CB125CEF7}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -11824,7 +11826,6 @@ Global
{046F43BC-BEE4-48B7-8C09-ED0A1054A2D7} = {AA5ABFBC-177C-421E-B743-005E0FD1248B}
{9716D0D0-2251-44DD-8596-67D253EEF41C} = {323C3EB6-1D15-4B3D-918D-699D7F64DED9}
{2299CCD8-8F9C-4F2B-A633-9BF4DA81022B} = {017429CC-C5FB-48B4-9C46-034E29EE2F06}
{3AEFB466-6310-4F3F-923F-9154224E3629} = {2299CCD8-8F9C-4F2B-A633-9BF4DA81022B}
{EFC8EA45-572D-4D8D-A597-9045A2D8EC40} = {2299CCD8-8F9C-4F2B-A633-9BF4DA81022B}
{8EE73488-2B92-42BD-96C9-0DD65405C828} = {1D865E78-7A66-4CA9-92EE-2B350E45281F}
{41FF4F96-98D2-4482-A2A7-4B179E80D285} = {1D865E78-7A66-4CA9-92EE-2B350E45281F}
Expand Down Expand Up @@ -11893,6 +11894,8 @@ Global
{6A19D94D-2BC6-4198-BE2E-342688FDBA4B} = {A1B75FC7-A777-4412-A635-D0C9ED8FE7A0}
{D8F7091E-A2D1-4E81-BA7C-97EAE392D683} = {A1B75FC7-A777-4412-A635-D0C9ED8FE7A0}
{757CBDE0-5D0A-4FD8-99F3-6C20BDDD4E63} = {5FE1FBC1-8CE3-4355-9866-44FE1307C5F1}
{B32FF7A7-9CB3-4DCD-AE97-3B2594DB9DAC} = {2299CCD8-8F9C-4F2B-A633-9BF4DA81022B}
{B9BBC1A8-7F58-4F43-94C3-5F3CB125CEF7} = {B32FF7A7-9CB3-4DCD-AE97-3B2594DB9DAC}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3E8720B3-DBDD-498C-B383-2CC32A054E8F}
Expand Down
1 change: 1 addition & 0 deletions eng/RequiresDelayedBuildProjects.props
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<RequiresDelayedBuild Include="$(RepoRoot)src\Grpc\JsonTranscoding\test\Microsoft.AspNetCore.Grpc.Swagger.Tests\Microsoft.AspNetCore.Grpc.Swagger.Tests.csproj" />
<RequiresDelayedBuild Include="$(RepoRoot)src\Grpc\JsonTranscoding\test\testassets\IntegrationTestsWebsite\IntegrationTestsWebsite.csproj" />
<RequiresDelayedBuild Include="$(RepoRoot)src\Grpc\JsonTranscoding\test\testassets\Sandbox\Sandbox.csproj" />
<RequiresDelayedBuild Include="$(RepoRoot)src\OpenApi\test\Microsoft.AspNetCore.OpenApi.NativeAotTests\Microsoft.AspNetCore.OpenApi.NativeAotTests.proj" />
<RequiresDelayedBuild Include="$(RepoRoot)src\ProjectTemplates\test\Templates.Blazor.Tests\Templates.Blazor.Tests.csproj" />
<RequiresDelayedBuild Include="$(RepoRoot)src\SignalR\server\SignalR\test\Microsoft.AspNetCore.SignalR.TrimmingTests\Microsoft.AspNetCore.SignalR.TrimmingTests.proj" />
<RequiresDelayedBuild Include="$(RepoRoot)eng\Npm.Workspace.FunctionalTests.nodeproj" />
Expand Down
3 changes: 3 additions & 0 deletions eng/TrimmableProjects.props
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@
<TrimmableProject Include="Microsoft.AspNetCore.SpaServices.Extensions" />
<TrimmableProject Include="Microsoft.AspNetCore.StaticFiles" />
<TrimmableProject Include="Microsoft.AspNetCore.WebSockets" />
<TrimmableProject Include="Microsoft.AspNetCore.Mvc.Abstractions" />
<TrimmableProject Include="Microsoft.AspNetCore.Mvc.ApiExplorer" />
<TrimmableProject Include="Microsoft.AspNetCore.Mvc.Core" />
<TrimmableProject Include="Microsoft.AspNetCore.SignalR.Client.Core" />
<TrimmableProject Include="Microsoft.AspNetCore.SignalR.Client" />
<TrimmableProject Include="Microsoft.AspNetCore.Http.Connections.Client" />
Expand Down
2 changes: 2 additions & 0 deletions eng/testing/linker/SupportFiles/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@
<ProjectReference Include="$(RepoRoot)src\DataProtection\Extensions\src\Microsoft.AspNetCore.DataProtection.Extensions.csproj" />
<ProjectReference Include="$(RepoRoot)src\SignalR\clients\csharp\Client\src\Microsoft.AspNetCore.SignalR.Client.csproj" />
<ProjectReference Include="$(RepoRoot)src\SignalR\server\SignalR\src\Microsoft.AspNetCore.SignalR.csproj" />
<ProjectReference Include="$(RepoRoot)src\OpenApi\src\Microsoft.AspNetCore.OpenApi.csproj" />
<ProjectReference Include="$(RepoRoot)src\Http\Http.Extensions\gen\Microsoft.AspNetCore.Http.RequestDelegateGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>

</Project>
22 changes: 21 additions & 1 deletion eng/testing/linker/project.csproj.template
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
<!-- Workaround while there is no SDK available that understands the TFM; suppress unsupported version errors. -->
<NETCoreAppMaximumVersion>99.9</NETCoreAppMaximumVersion>
<_ExtraTrimmerArgs>{ExtraTrimmerArgs} $(_ExtraTrimmerArgs)</_ExtraTrimmerArgs>
<InterceptorsPreviewNamespaces>$(InterceptorsPreviewNamespaces);Microsoft.AspNetCore.Http.Generated</InterceptorsPreviewNamespaces>
<!-- Ensure individual warnings are shown when publishing -->
<TrimmerSingleWarn>false</TrimmerSingleWarn>
<!-- But ignore the single warn files marked below to suppress their known warnings. -->
<NoWarn>$(NoWarn);IL2104</NoWarn>
{AdditionalProperties}
</PropertyGroup>

Expand All @@ -21,5 +26,20 @@
<ItemGroup>
{AdditionalProjectReferences}
</ItemGroup>


<!-- Single warn the following assemblies, which have known warnings, so the warnings can be suppressed for now.
Remove this (and the above NoWarn IL2104) once https://github.com/microsoft/OpenAPI.NET/pull/1717 is merged. -->
<Target Name="ConfigureTrimming"
BeforeTargets="PrepareForILLink">
<ItemGroup>
<IlcArg Include="--singlewarnassembly:Microsoft.OpenApi" />
</ItemGroup>
<ItemGroup>
<ManagedAssemblyToLink Condition="'%(Filename)' == 'Microsoft.OpenApi'">
<IsTrimmable>true</IsTrimmable>
<TrimmerSingleWarn>true</TrimmerSingleWarn>
</ManagedAssemblyToLink>
</ItemGroup>
</Target>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Reflection;

namespace Microsoft.AspNetCore.Http.Metadata;

/// <summary>
/// Exposes metadata about the parameter binding details associated with a parameter
/// in the endpoints handler.
/// </summary>
/// <remarks>
/// This metadata is injected by the RequestDelegateFactory and RequestDelegateGenerator components
/// and is primarily intended for consumption by the EndpointMetadataApiDescriptionProvider in
/// ApiExplorer.
/// </remarks>
public interface IParameterBindingMetadata
{
/// <summary>
/// The name of the parameter.
/// </summary>
string Name { get; }

/// <summary>
/// <see langword="true "/> is the parameter is associated with a type that implements <see cref="IParsable{TSelf}" /> or exposes a <c>TryParse</c> method.
/// </summary>
bool HasTryParse { get; }

/// <summary>
/// <see langword="true"/> if the parameter is associated with a type that implements a <c>BindAsync</c> method.
/// </summary>
bool HasBindAsync { get; }

/// <summary>
/// The <see cref="ParameterInfo"/> associated with the parameter.
/// </summary>
ParameterInfo ParameterInfo { get; }

/// <summary>
/// <see langword="true"/> if the parameter is optional.
/// </summary>
bool IsOptional { get; }
}
6 changes: 6 additions & 0 deletions src/Http/Http.Abstractions/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ Microsoft.AspNetCore.Http.HostString.HostString(string? value) -> void
Microsoft.AspNetCore.Http.HostString.Value.get -> string?
Microsoft.AspNetCore.Http.HttpValidationProblemDetails.HttpValidationProblemDetails(System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string!, string![]!>>! errors) -> void
Microsoft.AspNetCore.Http.Metadata.IDisableHttpMetricsMetadata
Microsoft.AspNetCore.Http.Metadata.IParameterBindingMetadata
Microsoft.AspNetCore.Http.Metadata.IParameterBindingMetadata.HasBindAsync.get -> bool
Microsoft.AspNetCore.Http.Metadata.IParameterBindingMetadata.HasTryParse.get -> bool
Microsoft.AspNetCore.Http.Metadata.IParameterBindingMetadata.IsOptional.get -> bool
Microsoft.AspNetCore.Http.Metadata.IParameterBindingMetadata.Name.get -> string!
Microsoft.AspNetCore.Http.Metadata.IParameterBindingMetadata.ParameterInfo.get -> System.Reflection.ParameterInfo!
8 changes: 6 additions & 2 deletions src/Http/Http.Extensions/gen/RequestDelegateGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.AspNetCore.Analyzers.Infrastructure;
using Microsoft.AspNetCore.App.Analyzers.Infrastructure;
using Microsoft.AspNetCore.Http.RequestDelegateGenerator.StaticRouteHandlerModel;
using Microsoft.AspNetCore.Http.RequestDelegateGenerator.StaticRouteHandlerModel.Emitters;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Operations;

namespace Microsoft.AspNetCore.Http.RequestDelegateGenerator;

Expand Down Expand Up @@ -243,6 +241,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
var hasJsonBody = endpoints.Any(endpoint => endpoint.EmitterContext.HasJsonBody || endpoint.EmitterContext.HasJsonBodyOrService || endpoint.EmitterContext.HasJsonBodyOrQuery);
var hasResponseMetadata = endpoints.Any(endpoint => endpoint.EmitterContext.HasResponseMetadata);
var requiresPropertyAsParameterInfo = endpoints.Any(endpoint => endpoint.EmitterContext.RequiresPropertyAsParameterInfo);
var requiresParameterBindingMetadataClass = endpoints.Any(endpoint => endpoint.EmitterContext.RequiresParameterBindingMetadataClass);

using var stringWriter = new StringWriter(CultureInfo.InvariantCulture);
using var codeWriter = new CodeWriter(stringWriter, baseIndent: 0);
Expand All @@ -262,6 +261,11 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
codeWriter.WriteLine(RequestDelegateGeneratorSources.PropertyAsParameterInfoClass);
}

if (requiresParameterBindingMetadataClass)
{
codeWriter.WriteLine(RequestDelegateGeneratorSources.ParameterBindingMetadataClass);
}

return stringWriter.ToString();
});

Expand Down
30 changes: 30 additions & 0 deletions src/Http/Http.Extensions/gen/RequestDelegateGeneratorSources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,36 @@ public override bool IsDefined(Type attributeType, bool inherit)
}
""";

public static string ParameterBindingMetadataClass = $$"""
{{GeneratedCodeAttribute}}
file sealed class ParameterBindingMetadata: IParameterBindingMetadata
{
internal ParameterBindingMetadata(
string name,
ParameterInfo parameterInfo,
bool hasTryParse = false,
bool hasBindAsync = false,
bool isOptional = false)
{
Name = name;
ParameterInfo = parameterInfo;
HasTryParse = hasTryParse;
HasBindAsync = hasBindAsync;
IsOptional = isOptional;
}

public string Name { get; }

public bool HasTryParse { get; }

public bool HasBindAsync { get; }

public ParameterInfo ParameterInfo { get; }

public bool IsOptional { get; }
}
""";

public static string AntiforgeryMetadataType = """
file sealed class AntiforgeryMetadata : IAntiforgeryMetadata
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ internal sealed class EmitterContext
public bool HasBindAsync { get; set; }
public bool HasParsable { get; set; }
public bool RequiresPropertyAsParameterInfo { get; set; }
public bool RequiresParameterBindingMetadataClass { get; set; }
public bool RequiresLoggingHelper { get; set; }
public bool HasEndpointMetadataProvider { get; set; }
public bool HasEndpointParameterMetadataProvider { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private EndpointParameter(Endpoint endpoint, IPropertySymbol property, IParamete
PropertyAsParameterInfoConstruction = parameter is not null
? $"new PropertyAsParameterInfo({(IsOptional ? "true" : "false")}, {propertyInfo}, {parameter.GetParameterInfoFromConstructorCode()})"
: $"new PropertyAsParameterInfo({(IsOptional ? "true" : "false")}, {propertyInfo})";
endpoint.EmitterContext.RequiresPropertyAsParameterInfo = IsProperty && IsEndpointParameterMetadataProvider;
endpoint.EmitterContext.RequiresPropertyAsParameterInfo = IsProperty;
ProcessEndpointParameterSource(endpoint, property, attributeBuilder.ToImmutable(), wellKnownTypes);
}

Expand Down
Loading
Loading