Skip to content

Commit bdbe1c9

Browse files
author
Chris Martinez
committed
Make updating the API version parameter description for a path route parameter reliable. Related #419
1 parent 7e53818 commit bdbe1c9

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

src/Microsoft.AspNet.WebApi.Versioning.ApiExplorer/Description/ApiVersionParameterDescriptionContext.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace Microsoft.Web.Http.Description
22
{
3+
using Microsoft.Web.Http.Routing;
34
using Microsoft.Web.Http.Versioning;
45
using System;
56
using System.Collections.Generic;
@@ -146,7 +147,17 @@ protected virtual void AddHeader( string name )
146147
/// </summary>
147148
protected virtual void UpdateUrlSegment()
148149
{
149-
var parameter = ApiDescription.ParameterDescriptions.FirstOrDefault( p => p.Source == FromUri && p.ParameterDescriptor == null );
150+
// use the route constraints to determine the user-defined name of the route parameter; expect and support only
151+
var constraints = ApiDescription.Route.Constraints;
152+
var routeParameterName = constraints.Where( p => p.Value is ApiVersionRouteConstraint ).Select( p => p.Key ).FirstOrDefault();
153+
154+
if ( string.IsNullOrEmpty( routeParameterName ) )
155+
{
156+
return;
157+
}
158+
159+
// find and update the parameter description for api version route parameter
160+
var parameter = ApiDescription.ParameterDescriptions.FirstOrDefault( p => routeParameterName.Equals( p.Name, OrdinalIgnoreCase ) );
150161

151162
if ( parameter == null )
152163
{
@@ -161,6 +172,7 @@ protected virtual void UpdateUrlSegment()
161172
Configuration = action.Configuration,
162173
ActionDescriptor = action,
163174
};
175+
164176
RemoveAllParametersExcept( parameter );
165177
}
166178

@@ -208,7 +220,7 @@ ApiParameterDescription NewApiVersionParameter( string name, ApiParameterSource
208220

209221
void RemoveAllParametersExcept( ApiParameterDescription parameter )
210222
{
211-
// note: in a scenario where multiple api version parameters are allowed, we can remove all other parameters because
223+
// in a scenario where multiple api version parameters are allowed, we can remove all other parameters because
212224
// the api version must be specified in the path. this will avoid unwanted, duplicate api version parameters
213225
var collections = new ICollection<ApiParameterDescription>[] { ApiDescription.ParameterDescriptions, parameters };
214226

test/Microsoft.AspNet.WebApi.Versioning.ApiExplorer.Tests/Description/ApiVersionParameterDescriptionContextTest.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace Microsoft.Web.Http.Description
22
{
33
using FluentAssertions;
4+
using Microsoft.Web.Http.Routing;
45
using Microsoft.Web.Http.Versioning;
56
using Moq;
67
using System.Collections.ObjectModel;
@@ -11,6 +12,7 @@
1112
using System.Web.Http.Controllers;
1213
using System.Web.Http.Description;
1314
using System.Web.Http.Filters;
15+
using System.Web.Http.Routing;
1416
using Xunit;
1517
using static Microsoft.Web.Http.Versioning.ApiVersionParameterLocation;
1618
using static System.Web.Http.Description.ApiParameterSource;
@@ -93,7 +95,12 @@ public void add_parameter_should_add_descriptor_for_path()
9395
// arrange
9496
var configuration = new HttpConfiguration();
9597
var action = NewActionDescriptor();
96-
var description = new ApiDescription() { ActionDescriptor = action };
98+
var route = new HttpRoute() { Constraints = { ["api-version"] = new ApiVersionRouteConstraint() } };
99+
var description = new ApiDescription()
100+
{
101+
ActionDescriptor = action,
102+
Route = route,
103+
};
97104
var version = new ApiVersion( 1, 0 );
98105
var options = new ApiExplorerOptions( configuration );
99106
var context = new ApiVersionParameterDescriptionContext( description, version, options );
@@ -129,7 +136,12 @@ public void add_parameter_should_remove_other_descriptors_after_path_parameter_i
129136
// arrange
130137
var configuration = new HttpConfiguration();
131138
var action = NewActionDescriptor();
132-
var description = new ApiDescription() { ActionDescriptor = action };
139+
var route = new HttpRoute() { Constraints = { ["api-version"] = new ApiVersionRouteConstraint() } };
140+
var description = new ApiDescription()
141+
{
142+
ActionDescriptor = action,
143+
Route = route,
144+
};
133145
var version = new ApiVersion( 1, 0 );
134146
var options = new ApiExplorerOptions( configuration );
135147
var context = new ApiVersionParameterDescriptionContext( description, version, options );
@@ -151,7 +163,12 @@ public void add_parameter_should_not_add_query_parameter_after_path_parameter_ha
151163
// arrange
152164
var configuration = new HttpConfiguration();
153165
var action = NewActionDescriptor();
154-
var description = new ApiDescription() { ActionDescriptor = action };
166+
var route = new HttpRoute() { Constraints = { ["api-version"] = new ApiVersionRouteConstraint() } };
167+
var description = new ApiDescription()
168+
{
169+
ActionDescriptor = action,
170+
Route = route,
171+
};
155172
var version = new ApiVersion( 1, 0 );
156173
var options = new ApiExplorerOptions( configuration );
157174
var context = new ApiVersionParameterDescriptionContext( description, version, options );

0 commit comments

Comments
 (0)