diff --git a/src/Microsoft.AspNet.WebApi.Versioning.ApiExplorer/Description/ApiVersionParameterDescriptionContext.cs b/src/Microsoft.AspNet.WebApi.Versioning.ApiExplorer/Description/ApiVersionParameterDescriptionContext.cs index ae9e8e9d..2f005c2e 100644 --- a/src/Microsoft.AspNet.WebApi.Versioning.ApiExplorer/Description/ApiVersionParameterDescriptionContext.cs +++ b/src/Microsoft.AspNet.WebApi.Versioning.ApiExplorer/Description/ApiVersionParameterDescriptionContext.cs @@ -1,5 +1,6 @@ namespace Microsoft.Web.Http.Description { + using Microsoft.Web.Http.Routing; using Microsoft.Web.Http.Versioning; using System; using System.Collections.Generic; @@ -146,7 +147,17 @@ protected virtual void AddHeader( string name ) /// protected virtual void UpdateUrlSegment() { - var parameter = ApiDescription.ParameterDescriptions.FirstOrDefault( p => p.Source == FromUri && p.ParameterDescriptor == null ); + // use the route constraints to determine the user-defined name of the route parameter; expect and support only one + var constraints = ApiDescription.Route.Constraints; + var routeParameterName = constraints.Where( p => p.Value is ApiVersionRouteConstraint ).Select( p => p.Key ).FirstOrDefault(); + + if ( string.IsNullOrEmpty( routeParameterName ) ) + { + return; + } + + // find and update the parameter description for the api version route parameter + var parameter = ApiDescription.ParameterDescriptions.FirstOrDefault( p => routeParameterName.Equals( p.Name, OrdinalIgnoreCase ) ); if ( parameter == null ) { @@ -161,6 +172,7 @@ protected virtual void UpdateUrlSegment() Configuration = action.Configuration, ActionDescriptor = action, }; + RemoveAllParametersExcept( parameter ); } @@ -208,7 +220,7 @@ ApiParameterDescription NewApiVersionParameter( string name, ApiParameterSource void RemoveAllParametersExcept( ApiParameterDescription parameter ) { - // note: in a scenario where multiple api version parameters are allowed, we can remove all other parameters because + // in a scenario where multiple api version parameters are allowed, we can remove all other parameters because // the api version must be specified in the path. this will avoid unwanted, duplicate api version parameters var collections = new ICollection[] { ApiDescription.ParameterDescriptions, parameters }; diff --git a/test/Microsoft.AspNet.WebApi.Versioning.ApiExplorer.Tests/Description/ApiVersionParameterDescriptionContextTest.cs b/test/Microsoft.AspNet.WebApi.Versioning.ApiExplorer.Tests/Description/ApiVersionParameterDescriptionContextTest.cs index 64cea7f1..42a9aa0c 100644 --- a/test/Microsoft.AspNet.WebApi.Versioning.ApiExplorer.Tests/Description/ApiVersionParameterDescriptionContextTest.cs +++ b/test/Microsoft.AspNet.WebApi.Versioning.ApiExplorer.Tests/Description/ApiVersionParameterDescriptionContextTest.cs @@ -1,6 +1,7 @@ namespace Microsoft.Web.Http.Description { using FluentAssertions; + using Microsoft.Web.Http.Routing; using Microsoft.Web.Http.Versioning; using Moq; using System.Collections.ObjectModel; @@ -11,6 +12,7 @@ using System.Web.Http.Controllers; using System.Web.Http.Description; using System.Web.Http.Filters; + using System.Web.Http.Routing; using Xunit; using static Microsoft.Web.Http.Versioning.ApiVersionParameterLocation; using static System.Web.Http.Description.ApiParameterSource; @@ -93,7 +95,12 @@ public void add_parameter_should_add_descriptor_for_path() // arrange var configuration = new HttpConfiguration(); var action = NewActionDescriptor(); - var description = new ApiDescription() { ActionDescriptor = action }; + var route = new HttpRoute() { Constraints = { ["api-version"] = new ApiVersionRouteConstraint() } }; + var description = new ApiDescription() + { + ActionDescriptor = action, + Route = route, + }; var version = new ApiVersion( 1, 0 ); var options = new ApiExplorerOptions( configuration ); var context = new ApiVersionParameterDescriptionContext( description, version, options ); @@ -129,7 +136,12 @@ public void add_parameter_should_remove_other_descriptors_after_path_parameter_i // arrange var configuration = new HttpConfiguration(); var action = NewActionDescriptor(); - var description = new ApiDescription() { ActionDescriptor = action }; + var route = new HttpRoute() { Constraints = { ["api-version"] = new ApiVersionRouteConstraint() } }; + var description = new ApiDescription() + { + ActionDescriptor = action, + Route = route, + }; var version = new ApiVersion( 1, 0 ); var options = new ApiExplorerOptions( configuration ); var context = new ApiVersionParameterDescriptionContext( description, version, options ); @@ -151,7 +163,12 @@ public void add_parameter_should_not_add_query_parameter_after_path_parameter_ha // arrange var configuration = new HttpConfiguration(); var action = NewActionDescriptor(); - var description = new ApiDescription() { ActionDescriptor = action }; + var route = new HttpRoute() { Constraints = { ["api-version"] = new ApiVersionRouteConstraint() } }; + var description = new ApiDescription() + { + ActionDescriptor = action, + Route = route, + }; var version = new ApiVersion( 1, 0 ); var options = new ApiExplorerOptions( configuration ); var context = new ApiVersionParameterDescriptionContext( description, version, options );