Skip to content

Commit dd2b643

Browse files
Enable trimming support with appropriate diagnostic and suppression annotations. Fixes #1094
1 parent d59ecae commit dd2b643

File tree

20 files changed

+139
-21
lines changed

20 files changed

+139
-21
lines changed

src/AspNetCore/OData/src/Asp.Versioning.OData.ApiExplorer/ApiExplorer/PartialODataDescriptionProvider.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ public ModelMetadata GetMetadataForType( Type modelType ) =>
201201
throw new NotImplementedException();
202202
}
203203

204+
[UnconditionalSuppressMessage( "ILLink", "IL2092" )]
205+
[UnconditionalSuppressMessage( "ILLink", "IL2093" )]
204206
private sealed class StubModelTypeBuilder : IModelTypeBuilder
205207
{
206208
public Type NewActionParameters( IEdmModel model, IEdmAction action, string controllerName, ApiVersion apiVersion ) =>

src/AspNetCore/OData/src/Asp.Versioning.OData.ApiExplorer/Asp.Versioning.OData.ApiExplorer.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<AssemblyTitle>ASP.NET Core API Versioning API Explorer for OData v4.0</AssemblyTitle>
99
<Description>The API Explorer extensions for ASP.NET Core API Versioning and OData v4.0.</Description>
1010
<PackageTags>Asp;AspNet;AspNetCore;Versioning;ApiExplorer;OData</PackageTags>
11+
<IsTrimmable>true</IsTrimmable>
1112
</PropertyGroup>
1213

1314
<ItemGroup>

src/AspNetCore/OData/src/Asp.Versioning.OData/Asp.Versioning.OData.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<AssemblyTitle>ASP.NET Core API Versioning with OData v4.0</AssemblyTitle>
99
<Description>A service API versioning library for Microsoft ASP.NET Core with OData v4.0.</Description>
1010
<PackageTags>Asp;AspNet;AspNetCore;Versioning;OData</PackageTags>
11+
<IsTrimmable>true</IsTrimmable>
1112
</PropertyGroup>
1213

1314
<ItemGroup>

src/AspNetCore/OData/src/Asp.Versioning.OData/DependencyInjection/IServiceCollectionExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ internal static ApplicationPartManager GetOrCreateApplicationPartManager( this I
3131
return partManager;
3232
}
3333

34+
[UnconditionalSuppressMessage( "ILLink", "IL2072", Justification = "Model configuration types are never trimmed" )]
3435
internal static void AddModelConfigurationsAsServices( this IServiceCollection services, ApplicationPartManager partManager )
3536
{
3637
var feature = new ModelConfigurationFeature();

src/AspNetCore/OData/src/Asp.Versioning.OData/OData/ODataMultiModelApplicationModelProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ private static Type GetDefaultApplicationModelProviderType()
135135
return Type.GetType( $"{TypeName}, {assemblyName}", throwOnError: true, ignoreCase: false )!;
136136
}
137137

138-
private static Func<IOptions<ODataOptions>, IApplicationModelProvider> CreateActivator( Type type )
138+
private static Func<IOptions<ODataOptions>, IApplicationModelProvider> CreateActivator(
139+
[DynamicallyAccessedMembers( DynamicallyAccessedMemberTypes.PublicConstructors )] Type type )
139140
{
140141
var options = Parameter( typeof( IOptions<ODataOptions> ), "options" );
141142
var @new = New( type.GetConstructors()[0], options );

src/AspNetCore/WebApi/src/Asp.Versioning.Mvc.ApiExplorer/ApiExplorerOptionsFactory{T}.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
namespace Asp.Versioning.ApiExplorer;
44

55
using Microsoft.Extensions.Options;
6+
using static System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes;
67

78
/// <summary>
89
/// Represents a factory to create API explorer options.
910
/// </summary>
1011
/// <typeparam name="T">The type of options to create.</typeparam>
1112
[CLSCompliant( false )]
12-
public class ApiExplorerOptionsFactory<T> : OptionsFactory<T> where T : ApiExplorerOptions
13+
public class ApiExplorerOptionsFactory<[DynamicallyAccessedMembers( PublicParameterlessConstructor )] T>
14+
: OptionsFactory<T> where T : ApiExplorerOptions
1315
{
1416
private readonly IOptions<ApiVersioningOptions> optionsHolder;
1517

src/AspNetCore/WebApi/src/Asp.Versioning.Mvc.ApiExplorer/Asp.Versioning.Mvc.ApiExplorer.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<AssemblyTitle>ASP.NET Core API Versioning API Explorer</AssemblyTitle>
99
<Description>The API Explorer extensions for ASP.NET Core API Versioning.</Description>
1010
<PackageTags>Asp;AspNet;AspNetCore;Versioning;ApiExplorer</PackageTags>
11+
<IsTrimmable>true</IsTrimmable>
1112
</PropertyGroup>
1213

1314
<ItemGroup>

src/AspNetCore/WebApi/src/Asp.Versioning.Mvc/Asp.Versioning.Mvc.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<Description>A service API versioning library for Microsoft ASP.NET Core MVC.</Description>
1010
<PackageTags>Asp;AspNet;AspNetCore;MVC;Versioning</PackageTags>
1111
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
12+
<IsTrimmable>true</IsTrimmable>
1213
</PropertyGroup>
1314

1415
<ItemGroup>

src/AspNetCore/WebApi/src/Asp.Versioning.Mvc/DependencyInjection/IApiVersioningBuilderExtensions.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace Microsoft.Extensions.DependencyInjection;
1616
using Microsoft.Extensions.Options;
1717
using System.Runtime.CompilerServices;
1818
using static ServiceDescriptor;
19+
using static System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes;
1920

2021
/// <summary>
2122
/// Provides ASP.NET Core MVC specific extension methods for <see cref="IApiVersioningBuilder"/>.
@@ -85,7 +86,11 @@ private static object CreateInstance( this IServiceProvider services, ServiceDes
8586
return ActivatorUtilities.GetServiceOrCreateInstance( services, descriptor.ImplementationType! );
8687
}
8788

88-
private static void TryReplace<TService, TImplementation, TReplacement>( this IServiceCollection services )
89+
private static void TryReplace<
90+
TService,
91+
TImplementation,
92+
[DynamicallyAccessedMembers( NonPublicConstructors | PublicConstructors )]
93+
TReplacement>( this IServiceCollection services )
8994
{
9095
var serviceType = typeof( TService );
9196
var implementationType = typeof( TImplementation );

src/AspNetCore/WebApi/src/Asp.Versioning.Mvc/Routing/ApiVersionUrlHelper.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,30 @@ public ApiVersionUrlHelper( ActionContext actionContext, IUrlHelper url )
9898
return current;
9999
}
100100

101-
if ( current is not RouteValueDictionary values )
101+
RouteValueDictionary values;
102+
103+
if ( current is null )
104+
{
105+
values = new() { { key, value } };
106+
return values;
107+
}
108+
109+
if ( current is RouteValueDictionary dictionary )
110+
{
111+
values = dictionary;
112+
}
113+
else if ( current is IEnumerable<KeyValuePair<string, object?>> kvps )
102114
{
103-
values = current == null ? new() : new( current );
115+
values = [];
116+
117+
foreach ( var kvp in kvps )
118+
{
119+
values.Add( kvp.Key, kvp.Value );
120+
}
121+
}
122+
else
123+
{
124+
return current;
104125
}
105126

106127
if ( !values.ContainsKey( key ) )

0 commit comments

Comments
 (0)