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

[9.0-preview.5] OpenAPI with native AoT usage generates trim warnings #56023

Open
1 task done
martincostello opened this issue Jun 2, 2024 · 2 comments
Open
1 task done

Comments

@martincostello
Copy link
Member

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Playing about with the native AoT support for the OpenAPI functionality with nightly builds of .NET 9 preview.5, trim warnings are generated from assemblies referenced by Microsoft.AspNetCore.OpenApi. This causes an application using TreatWarningsAsErrors=true to fail to compile when published.

Below are the warnings that appear to come from the framework itself when using MapOpenApi():

    ILC : Trim analysis error IL2026: Microsoft.AspNetCore.Mvc.ApiExplorer.EndpointMetadataApiDescriptionProvider.CreateApiDescription(RouteEndpoint,String,MethodInfo,Boolean): Using member 'Microsoft.AspNetCore.Http.PropertyAsParameterInfo.Flatten(ParameterInfo[],ParameterBindingMethodCache)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Uses unbounded Reflection to access parameter type constructors.
    ILC : AOT analysis error IL3050: Microsoft.AspNetCore.Mvc.ApiExplorer.EndpointMetadataApiDescriptionProvider.AddSupportedResponseTypes(IList`1<ApiResponseType>,Type,EndpointMetadataCollection): Using member 'Microsoft.Extensions.Internal.CoercedAwaitableInfo.IsTypeAwaitable(Type,CoercedAwaitableInfo&)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. Dynamically generates calls to FSharpAsync.
    ILC : Trim analysis error IL2067: Microsoft.AspNetCore.Mvc.ApiExplorer.EndpointMetadataApiDescriptionProvider.AddSupportedResponseTypes(IList`1<ApiResponseType>,Type,EndpointMetadataCollection): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.NonPublicMethods', 'DynamicallyAccessedMemberTypes.PublicProperties', 'DynamicallyAccessedMemberTypes.NonPublicProperties' in call to 'Microsoft.Extensions.Internal.CoercedAwaitableInfo.IsTypeAwaitable(Type,CoercedAwaitableInfo&)'. The parameter 'returnType' of method 'Microsoft.AspNetCore.Mvc.ApiExplorer.EndpointMetadataApiDescriptionProvider.AddSupportedResponseTypes(IList`1<ApiResponseType>,Type,EndpointMetadataCollection)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
    ILC : Trim analysis error IL2026: Microsoft.AspNetCore.Mvc.ApiExplorer.EndpointMetadataApiDescriptionProvider.GetBindingSourceAndName(ParameterInfo,RoutePattern,Boolean): Using member 'Microsoft.AspNetCore.Http.ParameterBindingMethodCache.HasBindAsyncMethod(ParameterInfo)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Performs reflection on type hierarchy. This cannot be statically analyzed.
    ILC : AOT analysis error IL3050: Microsoft.AspNetCore.Mvc.ApiExplorer.EndpointMetadataApiDescriptionProvider.GetBindingSourceAndName(ParameterInfo,RoutePattern,Boolean): Using member 'Microsoft.AspNetCore.Http.ParameterBindingMethodCache.HasBindAsyncMethod(ParameterInfo)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. Performs reflection on type hierarchy. This cannot be statically analyzed.
    ILC : Trim analysis error IL2026: Microsoft.AspNetCore.Mvc.ApiExplorer.EndpointMetadataApiDescriptionProvider.GetBindingSourceAndName(ParameterInfo,RoutePattern,Boolean): Using member 'Microsoft.AspNetCore.Http.ParameterBindingMethodCache.HasTryParseMethod(Type)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Performs reflection on type hierarchy. This cannot be statically analyzed.
    ILC : AOT analysis error IL3050: Microsoft.AspNetCore.Mvc.ApiExplorer.EndpointMetadataApiDescriptionProvider.GetBindingSourceAndName(ParameterInfo,RoutePattern,Boolean): Using member 'Microsoft.AspNetCore.Http.ParameterBindingMethodCache.HasTryParseMethod(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. Performs reflection on type hierarchy. This cannot be statically analyzed.
    ILC : Trim analysis error IL2026: Microsoft.AspNetCore.Mvc.ApiExplorer.EndpointMetadataApiDescriptionProvider.GetBindingSourceAndName(ParameterInfo,RoutePattern,Boolean): Using member 'Microsoft.AspNetCore.Http.ParameterBindingMethodCache.HasTryParseMethod(Type)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Performs reflection on type hierarchy. This cannot be statically analyzed.
    ILC : AOT analysis error IL3050: Microsoft.AspNetCore.Mvc.ApiExplorer.EndpointMetadataApiDescriptionProvider.GetBindingSourceAndName(ParameterInfo,RoutePattern,Boolean): Using member 'Microsoft.AspNetCore.Http.ParameterBindingMethodCache.HasTryParseMethod(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. Performs reflection on type hierarchy. This cannot be statically analyzed.
    ILC : Trim analysis error IL2026: Microsoft.AspNetCore.Mvc.ModelBinding.ModelMetadata.InitializeTypeInformation(): Using member 'System.ComponentModel.TypeDescriptor.GetConverter(Type)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Generic TypeConverters may require the generic types to be annotated. For example, NullableConverter requires the underlying type to be DynamicallyAccessedMembers All.
    ILC : Trim analysis error IL2072: Microsoft.AspNetCore.Mvc.ModelBinding.ModelMetadata.InitializeTypeInformation(): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.All' in call to 'System.ComponentModel.TypeDescriptor.GetConverter(Type)'. The return value of method 'Microsoft.AspNetCore.Mvc.ModelBinding.ModelMetadata.ModelType.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
    ILC : Trim analysis error IL2072: Microsoft.AspNetCore.Mvc.ModelBinding.ModelMetadata.InitializeTypeInformation(): 'queryType' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'Microsoft.Extensions.Internal.ClosedGenericMatcher.ExtractGenericInterface(Type,Type)'. The return value of method 'Microsoft.AspNetCore.Mvc.ModelBinding.ModelMetadata.ModelType.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
    ILC : Trim analysis error IL2072: Microsoft.AspNetCore.Mvc.ModelBinding.ModelMetadata.InitializeTypeInformation(): 'queryType' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'Microsoft.Extensions.Internal.ClosedGenericMatcher.ExtractGenericInterface(Type,Type)'. The return value of method 'Microsoft.AspNetCore.Mvc.ModelBinding.ModelMetadata.ModelType.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
    ILC : Trim analysis error IL2026: Microsoft.AspNetCore.Mvc.ModelBinding.ModelMetadata.FindTryParseMethod(Type): Using member 'Microsoft.AspNetCore.Http.ParameterBindingMethodCache.FindTryParseMethod(Type)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Performs reflection on type hierarchy. This cannot be statically analyzed.
    ILC : AOT analysis error IL3050: Microsoft.AspNetCore.Mvc.ModelBinding.ModelMetadata.FindTryParseMethod(Type): Using member 'Microsoft.AspNetCore.Http.ParameterBindingMethodCache.FindTryParseMethod(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. Performs reflection on type hierarchy. This cannot be statically analyzed.
    ILC : Trim analysis error IL2026: Microsoft.AspNetCore.Mvc.ModelBinding.ModelMetadata..cctor(): Using member 'Microsoft.AspNetCore.Http.ParameterBindingMethodCache.ParameterBindingMethodCache(Boolean)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Uses unbounded Reflection to inspect property types.

The repro below includes some others from my own code, but ignore those as I was just messing around trying to replicate some NSwag functionality I have to generate examples using the new functionality for OpenAPI (from where the suggestion in #56022 came from).

Expected Behavior

The application builds with no trim warnings from ASP.NET Core itself when MapOpenApi() is used in an application published for native AoT.

Steps To Reproduce

  1. Clone martincostello/api@b490334
  2. Run build.ps1 -SkipTests from the root of the repository

Exceptions (if any)

None.

.NET Version

9.0.100-preview.5.24281.15

Anything else?

No response

@captainsafia
Copy link
Member

@martincostello Sorry for the delay on this! I ran into some troublesome issues trying to install Powershell 7 on my DevBox to run the build script from your repo. I ended up setting up the repro on another machine. 😅

AFAICT, the warnings here fall into three categories:

  • Warnings in Microsoft.OpenApi related to the use of reflection to resolve prettified display names for certain enums used in the app.
  • Warnings in the EndpointMetadataApiDescriptionProvider related to invocations in ParameterBindingMethodCache for resolving whether parameters are parsable or have BindAsync invocations
  • Warnings in DefaultApiDescriptionProvider related to calls into MVC's model binding layer

Of the 3 categories above, the 2nd is the most important to tackle. I think there's a way to fix this though by piggy-backing off the static analysis that RDG does.

@captainsafia
Copy link
Member

Some updates on this:

Assuming all goes smoothly, we should have this wave of work resolved by preview7/RC1. Marking this as preview7 for now since I anticipate getting the EndpointMetadataApiDescriptionProvider-related work done by then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants