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

Make ApiExplorer for minimal APIs trim-compatible #56827

Merged
merged 18 commits into from
Jul 23, 2024

Conversation

captainsafia
Copy link
Member

Part of #56023.

This PR removes the trim warnings originating from Mvc.ApiExplorer when used on an application using minimal APIs + OpenAPI. The bulk of these trim warnings originate from the use of ParameterBindingCache to determine the status of parameters on a route handler and CoerecedAwaitable to unwrap awaitable response types. The use of unbounded reflection from these types is replaced by the introduction IParameterBindingMetadata and updates to RDF and RDG to produce this metadata as they analyze endpoints.

This PR also sets ProducesResponseType metadata at the framework level for response types that are unwrapped from an awaitable to replace the use of CoercedAwaitable in EndpointMetadataApiDescriptionProvider. There's also some ✨ surgical ✨ changes to the ApiDescriptionProvider implementation to work around issues are the ambiguity between typeof(void) as a null-type versus an explicitly set type in the ApiResponseProvider.

Specific changes include:

  • Update EndpointMetadataApiDescriptionProviderTests to call into RequestDelegateFactory so we can pick up metadata inserted by the code generation layer from ApiExplorer
  • Emit ParameterBindingMetadata class in source generated code and add ParameterBindingMetadata to all parameters that are discovered during source gen
  • Update ApiDescriptionProvider implementation to use parameter-binding metadata as the source of truth for parameters on an endpoint instead of MethodInfo.GetParameters
  • Set typeof(string) for ProducesResponseTypeMetadata inferred by the platform for string-types to avoid falling into traps around typeof(void) vs. null as unset response type

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc label Jul 16, 2024
@captainsafia captainsafia force-pushed the safia/apiexplorer-trim branch from 873ea08 to 11abf6e Compare July 16, 2024 17:01
@captainsafia captainsafia requested review from wtgodbe and a team as code owners July 16, 2024 17:01
@captainsafia captainsafia requested a review from a team as a code owner July 16, 2024 19:22
Copy link
Member

@eerhardt eerhardt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The trimming infrastructure related changes LGTM

@captainsafia captainsafia force-pushed the safia/apiexplorer-trim branch 2 times, most recently from 06f1097 to a636d4a Compare July 16, 2024 23:09
@captainsafia
Copy link
Member Author

captainsafia commented Jul 17, 2024

After adding the NativeAoT test project in a636d4a, I realized there is another layer of trim-incompatible issues that we are running into.

In addition to relying on the ParameterBindingMethodCache to resolve info related on parameters at the top-level EndpointMetadataApiDescriptionProvider layer, we also end up bringing in this dependency when we set ModelMetadata on the ApiParameterDescription, and ApiResponseTypes.

ModelMetadata = CreateModelMetadata(paramType),

apiResponseType.ModelMetadata = CreateModelMetadata(apiResponseType.Type);

ModelMetadata = CreateModelMetadata(responseType),

CreateModelMetadata returns an EndpointModelMetadata:

private static EndpointModelMetadata CreateModelMetadata(Type type) =>
new(ModelMetadataIdentity.ForType(type));

EndpointModelMetadata extends MVC's ModelMetadata which takes a dependency on ParameterBindingMethodCache yet again:

public abstract class ModelMetadata : IEquatable<ModelMetadata?>, IModelMetadataProvider
{
/// <summary>
/// The default value of <see cref="ModelMetadata.Order"/>.
/// </summary>
public static readonly int DefaultOrder = 10000;
private static readonly ParameterBindingMethodCache ParameterBindingMethodCache
= new(throwOnInvalidMethod: false);

This dependency was introduced when we added support for a TryParseModelBinder in MVC (see 3817558).

So there's another layer of changes that need to be made here to quarantine the ParameterBindingCache out of the shared ModelMetadata implementation, use the information we're already storing in IParameterBindingMetadata for MVC scenarios, and hopefully not break the controller-based APIs. We may also end up needing to leverage a feature switch in here somewhere...

Copy link
Member

@eerhardt eerhardt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The trimming related changes LGTM. Just one minor comment that can be addressed in a follow-up if you don't want to reset CI.

<argument>ILLink</argument>
<argument>IL2026</argument>
<property name="Scope">member</property>
<property name="Target">M:Microsoft.AspNetCore.Mvc.ModelBinding.ModelMetadata.#ctor(Microsoft.AspNetCore.Mvc.ModelBinding.Metadata.ModelMetadataIdentity)</property>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a Justification here? same as:

<property name="Justification">This warning is left in the product so developers get an ILLink warning when trimming an app when Microsoft.AspNetCore.SignalR.Hub.IsCustomAwaitableSupported=true.</property>

@captainsafia
Copy link
Member Author

/backport to release/9.0-preview7

Copy link
Contributor

Started backporting to release/9.0-preview7: https://github.com/dotnet/aspnetcore/actions/runs/10050522403

@captainsafia captainsafia enabled auto-merge (squash) July 23, 2024 21:30
@captainsafia captainsafia merged commit 8da6daa into main Jul 23, 2024
26 checks passed
@captainsafia captainsafia deleted the safia/apiexplorer-trim branch July 23, 2024 23:39
@dotnet-policy-service dotnet-policy-service bot added this to the 9.0-rc1 milestone Jul 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-openapi
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants