Skip to content

Commit fb39354

Browse files
author
Matthias Gessinger
committed
Pass DeprecationPolicyManager through all service layers
1 parent c882dc2 commit fb39354

File tree

16 files changed

+113
-17
lines changed

16 files changed

+113
-17
lines changed

src/AspNet/OData/src/Asp.Versioning.WebApi.OData.ApiExplorer/ApiExplorer/ODataApiExplorer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,7 @@ private void PopulateActionDescriptions(
566566
ApiVersion = apiVersion,
567567
IsDeprecated = deprecated,
568568
SunsetPolicy = SunsetPolicyManager.ResolvePolicyOrDefault( metadata.Name, apiVersion ),
569+
DeprecationPolicy = DeprecationPolicyManager.ResolvePolicyOrDefault( metadata.Name, apiVersion ),
569570
Properties = { [typeof( IEdmModel )] = routeBuilderContext.EdmModel },
570571
};
571572

src/AspNet/OData/test/Asp.Versioning.WebApi.OData.Tests/Controllers/VersionedMetadataControllerTest.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public async Task options_should_return_expected_headers()
3333
resolver.AddService(
3434
typeof( ISunsetPolicyManager ),
3535
( sp, t ) => new SunsetPolicyManager( sp.GetRequiredService<HttpConfiguration>().GetApiVersioningOptions() ) );
36+
resolver.AddService(
37+
typeof( IDeprecationPolicyManager ),
38+
( sp, t ) => new DeprecationPolicyManager( sp.GetRequiredService<HttpConfiguration>().GetApiVersioningOptions() ) );
3639
configuration.DependencyResolver = resolver;
3740
configuration.AddApiVersioning(
3841
options =>

src/AspNet/WebApi/src/Asp.Versioning.WebApi.ApiExplorer/ApiExplorer/VersionedApiExplorer.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class VersionedApiExplorer : IApiExplorer
3333
private readonly Lazy<ApiDescriptionGroupCollection> apiDescriptionsHolder;
3434
private IDocumentationProvider? documentationProvider;
3535
private ISunsetPolicyManager? sunsetPolicyManager;
36+
private IDeprecationPolicyManager? deprecationPolicyManager;
3637

3738
/// <summary>
3839
/// Initializes a new instance of the <see cref="VersionedApiExplorer"/> class.
@@ -105,6 +106,16 @@ protected ISunsetPolicyManager SunsetPolicyManager
105106
set => sunsetPolicyManager = value;
106107
}
107108

109+
/// <summary>
110+
/// Gets or sets the manager used to resolve deprecation policies for API descriptions.
111+
/// </summary>
112+
/// <value>The configured <see cref="IDeprecationPolicyManager">deprecation policy manager</see>.</value>
113+
protected IDeprecationPolicyManager DeprecationPolicyManager
114+
{
115+
get => deprecationPolicyManager ??= Configuration.GetDeprecationPolicyManager();
116+
set => deprecationPolicyManager = value;
117+
}
118+
108119
/// <summary>
109120
/// Gets a collection of HTTP methods supported by the action.
110121
/// </summary>
@@ -227,11 +238,13 @@ protected virtual ApiDescriptionGroupCollection InitializeApiDescriptions()
227238
}
228239

229240
var routes = FlattenRoutes( Configuration.Routes ).ToArray();
230-
var policyManager = Configuration.GetSunsetPolicyManager();
241+
var sunsetPolicyManager = Configuration.GetSunsetPolicyManager();
242+
var deprecationPolicyManager = Configuration.GetDeprecationPolicyManager();
231243

232244
foreach ( var apiVersion in FlattenApiVersions( controllerMappings ) )
233245
{
234-
var sunsetPolicy = policyManager.TryGetPolicy( apiVersion, out var policy ) ? policy : default;
246+
sunsetPolicyManager.TryGetPolicy( apiVersion, out var sunsetPolicy );
247+
deprecationPolicyManager.TryGetPolicy( apiVersion, out var deprecationPolicy );
235248

236249
for ( var i = 0; i < routes.Length; i++ )
237250
{
@@ -244,6 +257,7 @@ protected virtual ApiDescriptionGroupCollection InitializeApiDescriptions()
244257
ExploreRouteControllers( controllerMappings, route, apiVersion );
245258

246259
apiDescriptionGroup.SunsetPolicy = sunsetPolicy;
260+
apiDescriptionGroup.DeprecationPolicy = deprecationPolicy;
247261

248262
// Remove ApiDescription that will lead to ambiguous action matching.
249263
// E.g. a controller with Post() and PostComment(). When the route template is {controller}, it produces POST /controller and POST /controller.
@@ -878,6 +892,7 @@ private void PopulateActionDescriptions(
878892
ApiVersion = apiVersion,
879893
IsDeprecated = deprecated,
880894
SunsetPolicy = SunsetPolicyManager.ResolvePolicyOrDefault( metadata.Name, apiVersion ),
895+
DeprecationPolicy = DeprecationPolicyManager.ResolvePolicyOrDefault( metadata.Name, apiVersion ),
881896
};
882897

883898
foreach ( var supportedResponseFormatter in supportedResponseFormatters )

src/AspNet/WebApi/src/Asp.Versioning.WebApi.ApiExplorer/Description/ApiDescriptionGroup.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ public ApiDescriptionGroup( ApiVersion apiVersion, string name )
5151
/// <value>The defined sunset policy defined for the API, if any.</value>
5252
public SunsetPolicy? SunsetPolicy { get; set; }
5353

54+
/// <summary>
55+
/// Gets or sets described API deprecation policy.
56+
/// </summary>
57+
/// <value>The defined deprecation policy defined for the API, if any.</value>
58+
public DeprecationPolicy? DeprecationPolicy { get; set; }
59+
5460
/// <summary>
5561
/// Gets a collection of API descriptions for the current version.
5662
/// </summary>

src/AspNet/WebApi/src/Asp.Versioning.WebApi.ApiExplorer/Description/VersionedApiDescription.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ public ApiVersion ApiVersion
4949
/// <value>The defined <see cref="Versioning.SunsetPolicy">sunset policy</see> defined for the API, if any.</value>
5050
public SunsetPolicy? SunsetPolicy { get; set; }
5151

52+
/// <summary>
53+
/// Gets or sets the described API deprecation policy.
54+
/// </summary>
55+
/// <value>The defined <see cref="Versioning.DeprecationPolicy">deprecation policy</see> defined for the API, if any.</value>
56+
public DeprecationPolicy? DeprecationPolicy { get; set; }
57+
5258
/// <summary>
5359
/// Gets or sets the response description.
5460
/// </summary>

src/AspNet/WebApi/src/Asp.Versioning.WebApi/DependencyResolverExtensions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,8 @@ internal static IProblemDetailsFactory GetProblemDetailsFactory( this HttpConfig
3838
internal static ISunsetPolicyManager GetSunsetPolicyManager( this HttpConfiguration configuration ) =>
3939
configuration.DependencyResolver.GetService<ISunsetPolicyManager>() ??
4040
configuration.ApiVersioningServices().GetRequiredService<ISunsetPolicyManager>();
41+
42+
internal static IDeprecationPolicyManager GetDeprecationPolicyManager( this HttpConfiguration configuration ) =>
43+
configuration.DependencyResolver.GetService<IDeprecationPolicyManager>() ??
44+
configuration.ApiVersioningServices().GetRequiredService<IDeprecationPolicyManager>();
4145
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ namespace Asp.Versioning.ApiExplorer;
1515
using Microsoft.OData.Edm;
1616
using System.Diagnostics.CodeAnalysis;
1717
using System.Runtime.CompilerServices;
18-
using static System.StringComparison;
1918
using static ODataMetadataOptions;
19+
using static System.StringComparison;
2020
using Opts = Microsoft.Extensions.Options.Options;
2121

2222
/// <summary>
@@ -181,10 +181,11 @@ protected virtual void ExploreQueryOptions( IEnumerable<ApiDescription> apiDescr
181181
[MethodImpl( MethodImplOptions.AggressiveInlining )]
182182
private static int ApiVersioningOrder()
183183
{
184-
var policyManager = new SunsetPolicyManager( Opts.Create( new ApiVersioningOptions() ) );
184+
var sunsetPolicyManager = new SunsetPolicyManager( Opts.Create( new ApiVersioningOptions() ) );
185+
var deprecationPolicyManager = new DeprecationPolicyManager( Opts.Create( new ApiVersioningOptions() ) );
185186
var options = Opts.Create( new ApiExplorerOptions() );
186187
var provider = new EmptyModelMetadataProvider();
187-
return new VersionedApiDescriptionProvider( policyManager, provider, options ).Order;
188+
return new VersionedApiDescriptionProvider( sunsetPolicyManager, deprecationPolicyManager, provider, options ).Order;
188189
}
189190

190191
[MethodImpl( MethodImplOptions.AggressiveInlining )]

src/AspNetCore/WebApi/src/Asp.Versioning.Http/ApiExplorer/ApiVersionDescription.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,19 @@ public class ApiVersionDescription
1414
/// <param name="groupName">The group name for the API version.</param>
1515
/// <param name="deprecated">Indicates whether the API version is deprecated.</param>
1616
/// <param name="sunsetPolicy">The defined <see cref="SunsetPolicy">sunset policy</see>, if any.</param>
17+
/// <param name="deprecationPolicy">The defined <see cref="DeprecationPolicy">deprecation policy</see>, if any.</param>
1718
public ApiVersionDescription(
1819
ApiVersion apiVersion,
1920
string groupName,
2021
bool deprecated = false,
21-
SunsetPolicy? sunsetPolicy = default )
22+
SunsetPolicy? sunsetPolicy = default,
23+
DeprecationPolicy? deprecationPolicy = default )
2224
{
2325
ApiVersion = apiVersion;
2426
GroupName = groupName;
2527
IsDeprecated = deprecated;
2628
SunsetPolicy = sunsetPolicy;
29+
DeprecationPolicy = deprecationPolicy;
2730
}
2831

2932
/// <summary>
@@ -54,4 +57,10 @@ public ApiVersionDescription(
5457
/// </summary>
5558
/// <value>The defined sunset policy defined for the API, if any.</value>
5659
public SunsetPolicy? SunsetPolicy { get; }
60+
61+
/// <summary>
62+
/// Gets described API deprecation policy.
63+
/// </summary>
64+
/// <value>The defined deprecation policy defined for the API, if any.</value>
65+
public DeprecationPolicy? DeprecationPolicy { get; }
5766
}

src/AspNetCore/WebApi/src/Asp.Versioning.Http/DependencyInjection/IServiceCollectionExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ private static void AddApiVersioningServices( IServiceCollection services )
144144
services.AddSingleton( static sp => sp.GetRequiredService<IOptions<ApiVersioningOptions>>().Value.ApiVersionSelector );
145145
services.TryAddSingleton<IReportApiVersions, DefaultApiVersionReporter>();
146146
services.TryAddSingleton<ISunsetPolicyManager, SunsetPolicyManager>();
147+
services.TryAddSingleton<IDeprecationPolicyManager, DeprecationPolicyManager>();
147148
services.TryAddEnumerable( Transient<IValidateOptions<ApiVersioningOptions>, ValidateApiVersioningOptions>() );
148149
services.TryAddEnumerable( Transient<IPostConfigureOptions<RouteOptions>, ApiVersioningRouteOptionsSetup>() );
149150
services.TryAddEnumerable( Singleton<MatcherPolicy, ApiVersionMatcherPolicy>() );
@@ -281,7 +282,7 @@ private static void TryAddErrorObjectJsonOptions( IServiceCollection services )
281282
}
282283
}
283284

284-
// TEMP: this is a marker class to test whether Error Objects have been explicitly added. remove in 9.0+
285+
// TEMP: this is a marker class to test whether Error Objects have been explicitly added. remove in 9.0+
285286
#pragma warning disable CA1812 // Avoid uninstantiated internal classes
286287
private sealed class ErrorObjectsAdded { }
287288
}

src/AspNetCore/WebApi/src/Asp.Versioning.Mvc.ApiExplorer/ApiDescriptionExtensions.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,20 @@ public static bool IsDeprecated( this ApiDescription apiDescription )
6565
/// Sets the API sunset policy associated with the API description.
6666
/// </summary>
6767
/// <param name="apiDescription">The <see cref="ApiDescription">API description</see> to set the sunset policy for.</param>
68-
/// <param name="sunsetPolicy">The associated <see cref="SunsetPolicy">sunst policy</see>.</param>
68+
/// <param name="sunsetPolicy">The associated <see cref="SunsetPolicy">sunset policy</see>.</param>
6969
/// <remarks>This API is meant for infrastructure and should not be used by application code.</remarks>
7070
[EditorBrowsable( EditorBrowsableState.Never )]
7171
public static void SetSunsetPolicy( this ApiDescription apiDescription, SunsetPolicy sunsetPolicy ) => apiDescription.SetProperty( sunsetPolicy );
7272

73+
/// <summary>
74+
/// Sets the API deprecation policy associated with the API description.
75+
/// </summary>
76+
/// <param name="apiDescription">The <see cref="ApiDescription">API description</see> to set the sunset policy for.</param>
77+
/// <param name="deprecationPolicy">The associated <see cref="DeprecationPolicy">deprecation policy</see>.</param>
78+
/// <remarks>This API is meant for infrastructure and should not be used by application code.</remarks>
79+
[EditorBrowsable( EditorBrowsableState.Never )]
80+
public static void SetDeprecationPolicy( this ApiDescription apiDescription, DeprecationPolicy deprecationPolicy ) => apiDescription.SetProperty( deprecationPolicy );
81+
7382
/// <summary>
7483
/// Attempts to update the relate path of the specified API description and remove the corresponding parameter according to the specified options.
7584
/// </summary>

0 commit comments

Comments
 (0)