Skip to content

Commit 60b017c

Browse files
author
Chris Martinez
committed
Refactored the header value to coalesce to the multi-value CSV format as defined in RFC 2616, section 4.2. The built-in APIs appear to always write the headers using the long form without any alternate configuration
1 parent 5296f05 commit 60b017c

File tree

5 files changed

+13
-32
lines changed

5 files changed

+13
-32
lines changed

src/Common/ReportApiVersionsAttribute.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ public sealed partial class ReportApiVersionsAttribute : ActionFilterAttribute
2222
{
2323
private const string ApiSupportedVersions = "api-supported-versions";
2424
private const string ApiDeprecatedVersions = "api-deprecated-versions";
25+
private const string ValueSeparator = ", ";
2526
}
2627
}
Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
using System.Diagnostics.Contracts;
2-
namespace Microsoft.Web.Http
1+
namespace Microsoft.Web.Http
32
{
43
using System.Collections.Generic;
54
using System.Diagnostics.CodeAnalysis;
5+
using System.Diagnostics.Contracts;
66
using System.Linq;
7-
using System.Net.Http;
87
using System.Net.Http.Headers;
98
using System.Web.Http;
109
using System.Web.Http.Filters;
10+
using static System.String;
1111

1212
/// <content>
1313
/// Provides the implementation for ASP.NET Web API.
@@ -40,40 +40,20 @@ public override void OnActionExecuted( HttpActionExecutedContext actionExecutedC
4040

4141
var headers = response.Headers;
4242

43-
if ( model.SupportedApiVersions.Count == 0 )
44-
{
45-
TryAddHeaderWithDefaultSupportedApiVersion( headers, actionExecutedContext.Request );
46-
return;
47-
}
48-
4943
AddApiVersionHeader( headers, ApiSupportedVersions, model.SupportedApiVersions );
5044
AddApiVersionHeader( headers, ApiDeprecatedVersions, model.DeprecatedApiVersions );
5145
}
5246

53-
private static void TryAddHeaderWithDefaultSupportedApiVersion( HttpHeaders headers, HttpRequestMessage request )
54-
{
55-
Contract.Requires( headers != null );
56-
Contract.Requires( request != null );
57-
58-
if ( headers.Contains( ApiSupportedVersions ) )
59-
{
60-
return;
61-
}
62-
63-
var supportedVersion = request.GetRequestedApiVersion() ?? request.GetConfiguration().GetDefaultApiVersion();
64-
headers.Add( ApiSupportedVersions, supportedVersion.ToString() );
65-
}
66-
6747
private static void AddApiVersionHeader( HttpHeaders headers, string headerName, IReadOnlyList<ApiVersion> versions )
6848
{
6949
Contract.Requires( headers != null );
70-
Contract.Requires( !string.IsNullOrEmpty( headerName ) );
50+
Contract.Requires( !IsNullOrEmpty( headerName ) );
7151
Contract.Requires( versions != null );
7252

7353
if ( versions.Count > 0 && !headers.Contains( headerName ) )
7454
{
75-
headers.Add( headerName, versions.Select( v => v.ToString() ).ToArray() );
55+
headers.Add( headerName, Join( ValueSeparator, versions.Select( v => v.ToString() ) ) );
7656
}
7757
}
7858
}
79-
}
59+
}

src/Microsoft.AspNetCore.Mvc.Versioning/ReportApiVersionsAttribute.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
using System.Diagnostics.Contracts;
1010
using System.Linq;
1111
using Versioning;
12-
using static ApiVersion;
12+
using static System.String;
1313

1414
/// <content>
1515
/// Provides additional implementation specific to ASP.NET Core.
@@ -48,13 +48,13 @@ public override void OnActionExecuted( ActionExecutedContext context )
4848
private static void AddApiVersionHeader( IHeaderDictionary headers, string headerName, IReadOnlyList<ApiVersion> versions )
4949
{
5050
Contract.Requires( headers != null );
51-
Contract.Requires( !string.IsNullOrEmpty( headerName ) );
51+
Contract.Requires( !IsNullOrEmpty( headerName ) );
5252
Contract.Requires( versions != null );
5353

5454
if ( versions.Count > 0 && !headers.ContainsKey( headerName ) )
5555
{
56-
headers.Add( headerName, versions.Select( v => v.ToString() ).ToArray() );
56+
headers.Add( headerName, Join( ValueSeparator, versions.Select( v => v.ToString() ).ToArray() ) );
5757
}
5858
}
5959
}
60-
}
60+
}

test/Microsoft.AspNet.WebApi.Versioning.Tests/ReportApiVersionsAttributeTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void on_action_executed_should_add_version_headers()
4242
attribute.OnActionExecuted( context );
4343

4444
// assert
45-
context.Response.Headers.GetValues( "api-supported-versions" ).Should().BeEquivalentTo( "1.0", "2.0" );
45+
context.Response.Headers.GetValues( "api-supported-versions" ).Single().Should().Be( "1.0, 2.0" );
4646
context.Response.Headers.GetValues( "api-deprecated-versions" ).Single().Should().Be( "0.5" );
4747
}
4848

test/Microsoft.AspNetCore.Mvc.Versioning.Tests/ReportApiVersionsAttributeTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void on_action_executed_should_add_version_headers()
4141
attribute.OnActionExecuted( context );
4242

4343
// assert
44-
context.HttpContext.Response.Headers["api-supported-versions"].Should().BeEquivalentTo( "1.0", "2.0" );
44+
context.HttpContext.Response.Headers["api-supported-versions"].Single().Should().Be( "1.0, 2.0" );
4545
context.HttpContext.Response.Headers["api-deprecated-versions"].Single().Should().Be( "0.5" );
4646
}
4747

0 commit comments

Comments
 (0)