Skip to content

Commit d9a2d7e

Browse files
author
Matthias Gessinger
committed
Include deprecation date in deprecation log message
1 parent 2dc1263 commit d9a2d7e

File tree

7 files changed

+45
-11
lines changed

7 files changed

+45
-11
lines changed

src/Client/src/Asp.Versioning.Http.Client/ApiNotificationContext.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ public ApiNotificationContext( HttpResponseMessage response, ApiVersion apiVersi
2121
ApiVersion = apiVersion ?? throw new System.ArgumentNullException( nameof( apiVersion ) );
2222
}
2323

24+
/// <summary>
25+
/// Initializes a new instance of the <see cref="ApiNotificationContext"/> class.
26+
/// </summary>
27+
/// <param name="response">The current HTTP response.</param>
28+
/// <param name="apiVersion">The requested API version.</param>
29+
/// <param name="sunsetPolicy">The sunset policy which was previously read from the <paramref name="response"/>.</param>
30+
/// <param name="deprecationPolicy">The deprecation policy which was previously read from the <paramref name="response"/>.</param>
31+
public ApiNotificationContext( HttpResponseMessage response, ApiVersion apiVersion, SunsetPolicy? sunsetPolicy = null, DeprecationPolicy? deprecationPolicy = null )
32+
: this( response, apiVersion )
33+
{
34+
this.sunsetPolicy = sunsetPolicy;
35+
this.deprecationPolicy = deprecationPolicy;
36+
}
37+
2438
/// <summary>
2539
/// Gets the current HTTP response.
2640
/// </summary>

src/Client/src/Asp.Versioning.Http.Client/ApiVersionHandler.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ protected override async Task<HttpResponseMessage> SendAsync( HttpRequestMessage
4646

4747
var response = await base.SendAsync( request, cancellationToken ).ConfigureAwait( false );
4848

49-
if ( IsDeprecatedApi( response ) )
49+
if ( IsDeprecatedApi( response, out var deprecationPolicy ) )
5050
{
5151
response.RequestMessage ??= request;
52-
await notification.OnApiDeprecatedAsync( new( response, apiVersion ), cancellationToken ).ConfigureAwait( false );
52+
await notification.OnApiDeprecatedAsync( new( response, apiVersion, deprecationPolicy: deprecationPolicy ), cancellationToken ).ConfigureAwait( false );
5353
}
5454
else if ( IsNewApiAvailable( response ) )
5555
{
@@ -64,12 +64,13 @@ protected override async Task<HttpResponseMessage> SendAsync( HttpRequestMessage
6464
/// Determines whether the requested API is deprecated.
6565
/// </summary>
6666
/// <param name="response">The <see cref="HttpResponseMessage">HTTP response</see> from the requested API.</param>
67+
/// <param name="deprecationPolicy">The deprecation policy read from the <paramref name="response"/>.</param>
6768
/// <returns>True if the requested API has been deprecated; otherwise, false.</returns>
68-
protected virtual bool IsDeprecatedApi( HttpResponseMessage response )
69+
protected virtual bool IsDeprecatedApi( HttpResponseMessage response, out DeprecationPolicy deprecationPolicy )
6970
{
7071
ArgumentNullException.ThrowIfNull( response );
7172

72-
var deprecationPolicy = response.ReadDeprecationPolicy();
73+
deprecationPolicy = response.ReadDeprecationPolicy();
7374

7475
if ( deprecationPolicy.Date.HasValue && deprecationPolicy.Date <= DateTimeOffset.UtcNow )
7576
{

src/Client/src/Asp.Versioning.Http.Client/net#.0/ApiVersionHandlerLogger{T}.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ protected override void OnApiDeprecated( ApiNotificationContext context )
3636
var requestUrl = context.Response.RequestMessage!.RequestUri!;
3737
var apiVersion = context.ApiVersion;
3838
var sunsetPolicy = context.SunsetPolicy;
39+
var deprecationPolicy = context.DeprecationPolicy;
3940

40-
logger.ApiVersionDeprecated( requestUrl, apiVersion, sunsetPolicy );
41+
logger.ApiVersionDeprecated( requestUrl, apiVersion, sunsetPolicy, deprecationPolicy );
4142
}
4243

4344
/// <inheritdoc />

src/Client/src/Asp.Versioning.Http.Client/net#.0/ILoggerExtensions.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,33 @@ internal static void ApiVersionDeprecated(
1616
this ILogger logger,
1717
Uri requestUrl,
1818
ApiVersion apiVersion,
19-
SunsetPolicy sunsetPolicy )
19+
SunsetPolicy sunsetPolicy,
20+
DeprecationPolicy deprecationPolicy )
2021
{
2122
var sunsetDate = FormatDate( sunsetPolicy.Date );
22-
var additionalInfo = FormatLinks( sunsetPolicy );
23+
var deprecationDate = FormatDate( deprecationPolicy.Date );
24+
25+
var additionalInfoSunset = FormatLinks( sunsetPolicy );
26+
var additionalInfoDeprecation = FormatLinks( deprecationPolicy );
27+
28+
var additionalInfo = additionalInfoDeprecation.Concat( additionalInfoSunset ).ToArray();
2329

2430
ApiVersionDeprecated(
2531
logger,
2632
apiVersion.ToString(),
2733
requestUrl.OriginalString,
2834
sunsetDate,
35+
deprecationDate,
2936
additionalInfo );
3037
}
3138

32-
[LoggerMessage( EventId = 1, Level = Warning, Message = "API version {apiVersion} for {requestUrl} has been deprecated and will sunset on {sunsetDate}. Additional information: {links}" )]
39+
[LoggerMessage( EventId = 1, Level = Warning, Message = "API version {apiVersion} for {requestUrl} has been deprecated since {deprecationDate} and will sunset on {sunsetDate}. Additional information: {links}" )]
3340
static partial void ApiVersionDeprecated(
3441
ILogger logger,
3542
string apiVersion,
3643
string requestUrl,
3744
string sunsetDate,
45+
string deprecationDate,
3846
string[] links );
3947

4048
[MethodImpl( MethodImplOptions.AggressiveInlining )]
@@ -80,6 +88,16 @@ private static string[] FormatLinks( SunsetPolicy sunsetPolicy )
8088
return FormatLinks( sunsetPolicy.Links );
8189
}
8290

91+
private static string[] FormatLinks( DeprecationPolicy deprecationPolicy )
92+
{
93+
if ( !deprecationPolicy.HasLinks )
94+
{
95+
return [];
96+
}
97+
98+
return FormatLinks( deprecationPolicy.Links );
99+
}
100+
83101
private static string[] FormatLinks( IList<LinkHeaderValue> links )
84102
{
85103
// <Title> (<Language>[,<Language>]): <Url>

src/Client/test/Asp.Versioning.Http.Client.Tests/net#.0/ApiVersionHandlerLoggerTTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public async Task on_api_deprecated_should_log_message()
2121
};
2222
var context = new ApiNotificationContext( response, new ApiVersion( 1.0 ) );
2323
var date = DateTimeOffset.Now;
24-
var expected = "API version 1.0 for http://tempuri.org has been deprecated and will " +
24+
var expected = "API version 1.0 for http://tempuri.org has been deprecated since <unspecified> and will " +
2525
$"sunset on {date.ToUniversalTime()}. Additional information: " +
2626
"API Policy (en): http://tempuri.org/policy/en, " +
2727
"API Política (es): http://tempuri.org/policy/es";

src/Common/src/Common/DeprecationPolicyManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ namespace Asp.Versioning;
88
/// <remarks>
99
/// This class serves as a type alias to hide the generic arguments of <see cref="PolicyManager{TPolicy, TPolicyBuilder}"/>.
1010
/// </remarks>
11-
public partial class DeprecationPolicyManager : PolicyManager<DeprecationPolicy, DeprecationPolicyBuilder>
11+
public partial class DeprecationPolicyManager : PolicyManager<DeprecationPolicy, IDeprecationPolicyBuilder>
1212
{ }

src/Common/src/Common/SunsetPolicyManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ namespace Asp.Versioning;
88
/// <remarks>
99
/// This class serves as a type alias to hide the generic arguments of <see cref="PolicyManager{TPolicy, TPolicyBuilder}"/>.
1010
/// </remarks>
11-
public partial class SunsetPolicyManager : PolicyManager<SunsetPolicy, SunsetPolicyBuilder>
11+
public partial class SunsetPolicyManager : PolicyManager<SunsetPolicy, ISunsetPolicyBuilder>
1212
{ }

0 commit comments

Comments
 (0)