Skip to content

Commit 064c5c9

Browse files
author
Matthias Gessinger
committed
Check for ordering of effective dates of policies
1 parent 70fb848 commit 064c5c9

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/Abstractions/src/Asp.Versioning.Abstractions/DeprecationPolicy.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,24 @@ public DeprecationPolicy() { }
5555
/// </summary>
5656
/// <param name="link">The link which provides information about the deprecation policy.</param>
5757
public DeprecationPolicy( LinkHeaderValue link ) => Links.Add( link );
58+
59+
/// <summary>
60+
/// Returns a boolean to indicate if this policy is effective at the given <paramref name="dateTimeOffset"/>.
61+
/// </summary>
62+
/// <param name="dateTimeOffset">The point in time to serve as a reference.</param>
63+
/// <returns>A boolean which indicates if this policy is effective.</returns>
64+
public bool IsEffective( DateTimeOffset? dateTimeOffset )
65+
{
66+
if ( dateTimeOffset is not { } when )
67+
{
68+
return true;
69+
}
70+
71+
if ( Date is not { } date )
72+
{
73+
return true;
74+
}
75+
76+
return date < when;
77+
}
5878
}

src/Common/src/Common/DefaultApiVersionReporter.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,20 @@ public void Report( HttpResponse response, ApiVersionModel apiVersionModel )
9494
var version = context.GetRequestedApiVersion();
9595
#endif
9696
var name = metadata.Name;
97+
DateTimeOffset? sunsetDate = null;
9798

9899
if ( sunsetPolicyManager.TryResolvePolicy( name, version, out var sunsetPolicy ) )
99100
{
101+
sunsetDate = sunsetPolicy.Date;
100102
response.WriteSunsetPolicy( sunsetPolicy );
101103
}
102104

103105
if ( deprecationPolicyManager.TryResolvePolicy( name, version, out var deprecationPolicy ) )
104106
{
105-
response.WriteDeprecationPolicy( deprecationPolicy );
107+
if ( deprecationPolicy.IsEffective( sunsetDate ) )
108+
{
109+
response.WriteDeprecationPolicy( deprecationPolicy );
110+
}
106111
}
107112
}
108113
}

0 commit comments

Comments
 (0)