Skip to content

Improve Explorer API Version From Path in Web API #420

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -146,7 +147,17 @@ protected virtual void AddHeader( string name )
/// </summary>
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 )
{
Expand All @@ -161,6 +172,7 @@ protected virtual void UpdateUrlSegment()
Configuration = action.Configuration,
ActionDescriptor = action,
};

RemoveAllParametersExcept( parameter );
}

Expand Down Expand Up @@ -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<ApiParameterDescription>[] { ApiDescription.ParameterDescriptions, parameters };

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
Expand All @@ -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 );
Expand Down