Skip to content

Commit c1456ff

Browse files
author
Chris Martinez
committed
Current and lowest implemented API selectors now fall back to configured, default API version. Fixes #4
1 parent 3f70942 commit c1456ff

File tree

10 files changed

+67
-43
lines changed

10 files changed

+67
-43
lines changed

src/Common/Versioning/CurrentImplementationApiVersionSelector.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ namespace Microsoft.AspNetCore.Mvc.Versioning
1313
#if WEBAPI
1414
using HttpRequest = System.Net.Http.HttpRequestMessage;
1515
#endif
16-
using static ApiVersion;
1716

1817
/// <summary>
1918
/// Represents an <see cref="IApiVersionSelector">API version selector</see> which selects the API version of the
@@ -24,6 +23,18 @@ namespace Microsoft.AspNetCore.Mvc.Versioning
2423
#endif
2524
public class CurrentImplementationApiVersionSelector : IApiVersionSelector
2625
{
26+
private readonly ApiVersioningOptions options;
27+
28+
/// <summary>
29+
/// Initializes a new instance of the <see cref="CurrentImplementationApiVersionSelector"/> class.
30+
/// </summary>
31+
/// <param name="options">The <see cref="ApiVersioningOptions">API versioning options</see> associated with the selector.</param>
32+
public CurrentImplementationApiVersionSelector( ApiVersioningOptions options )
33+
{
34+
Arg.NotNull( options, nameof( options ) );
35+
this.options = options;
36+
}
37+
2738
/// <summary>
2839
/// Selects an API version given the specified HTTP request and API version information.
2940
/// </summary>
@@ -40,13 +51,13 @@ public virtual ApiVersion SelectVersion( HttpRequest request, ApiVersionModel mo
4051
switch ( model.ImplementedApiVersions.Count )
4152
{
4253
case 0:
43-
return Default;
54+
return options.DefaultApiVersion;
4455
case 1:
4556
var version = model.ImplementedApiVersions[0];
46-
return version.Status == null ? version : Default;
57+
return version.Status == null ? version : options.DefaultApiVersion;
4758
}
4859

49-
return model.ImplementedApiVersions.Where( v => v.Status == null ).Max( v => v ) ?? Default;
60+
return model.ImplementedApiVersions.Where( v => v.Status == null ).Max( v => v ) ?? options.DefaultApiVersion;
5061
}
5162
}
5263
}

src/Common/Versioning/LowestImplementedApiVersionSelector.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ namespace Microsoft.AspNetCore.Mvc.Versioning
1313
#if WEBAPI
1414
using HttpRequest = System.Net.Http.HttpRequestMessage;
1515
#endif
16-
using static ApiVersion;
1716

1817
/// <summary>
1918
/// Represents an <see cref="IApiVersionSelector">API version selector</see> which selects the lowest
@@ -24,6 +23,18 @@ namespace Microsoft.AspNetCore.Mvc.Versioning
2423
#endif
2524
public class LowestImplementedApiVersionSelector : IApiVersionSelector
2625
{
26+
private readonly ApiVersioningOptions options;
27+
28+
/// <summary>
29+
/// Initializes a new instance of the <see cref="LowestImplementedApiVersionSelector"/> class.
30+
/// </summary>
31+
/// <param name="options">The <see cref="ApiVersioningOptions">API versioning options</see> associated with the selector.</param>
32+
public LowestImplementedApiVersionSelector( ApiVersioningOptions options )
33+
{
34+
Arg.NotNull( options, nameof( options ) );
35+
this.options = options;
36+
}
37+
2738
/// <summary>
2839
/// Selects an API version given the specified HTTP request and API version information.
2940
/// </summary>
@@ -40,13 +51,13 @@ public virtual ApiVersion SelectVersion( HttpRequest request, ApiVersionModel mo
4051
switch ( model.ImplementedApiVersions.Count )
4152
{
4253
case 0:
43-
return Default;
54+
return options.DefaultApiVersion;
4455
case 1:
4556
var version = model.ImplementedApiVersions[0];
46-
return version.Status == null ? version : Default;
57+
return version.Status == null ? version : options.DefaultApiVersion;
4758
}
4859

49-
return model.ImplementedApiVersions.Where( v => v.Status == null ).Min( v => v ) ?? Default;
60+
return model.ImplementedApiVersions.Where( v => v.Status == null ).Min( v => v ) ?? options.DefaultApiVersion;
5061
}
5162
}
5263
}

test/Common/Versioning/MaxSelectVersionData.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ public override IEnumerator<object[]> GetEnumerator()
3636
{
3737
Supported(),
3838
Deprecated(),
39-
Expected( new ApiVersion( 1, 0 ) )
39+
Expected( new ApiVersion( 42, 0 ) )
4040
};
4141

4242
yield return new object[]
4343
{
4444
Supported( new ApiVersion( 1, 1, "RC1" ) ),
4545
Deprecated(),
46-
Expected( new ApiVersion( 1, 0 ) )
46+
Expected( new ApiVersion( 42, 0 ) )
4747
};
4848

4949
yield return new object[]
@@ -57,7 +57,7 @@ public override IEnumerator<object[]> GetEnumerator()
5757
{
5858
Supported( new ApiVersion( 0, 8, "Beta" ), new ApiVersion( 0, 9, "RC" ) ),
5959
Deprecated(),
60-
Expected( new ApiVersion( 1, 0 ) )
60+
Expected( new ApiVersion( 42, 0 ) )
6161
};
6262
}
6363
}

test/Common/Versioning/MinSelectVersionData.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public override IEnumerator<object[]> GetEnumerator()
2020

2121
yield return new object[]
2222
{
23-
Supported( new ApiVersion( 1, 0 ), new ApiVersion( 2, 0 ) ),
23+
Supported( new ApiVersion( 0, 9, "RC" ), new ApiVersion( 1, 0 ), new ApiVersion( 2, 0 ) ),
2424
Deprecated( new ApiVersion( 3, 0 ) ),
2525
Expected( new ApiVersion( 1, 0 ) )
2626
};
@@ -36,14 +36,14 @@ public override IEnumerator<object[]> GetEnumerator()
3636
{
3737
Supported(),
3838
Deprecated(),
39-
Expected( new ApiVersion( 1, 0 ) )
39+
Expected( new ApiVersion( 42, 0 ) )
4040
};
4141

4242
yield return new object[]
4343
{
4444
Supported( new ApiVersion( 1, 1, "RC1" ) ),
4545
Deprecated(),
46-
Expected( new ApiVersion( 1, 0 ) )
46+
Expected( new ApiVersion( 42, 0 ) )
4747
};
4848

4949
yield return new object[]
@@ -57,7 +57,7 @@ public override IEnumerator<object[]> GetEnumerator()
5757
{
5858
Supported( new ApiVersion( 0, 8, "Beta" ), new ApiVersion( 0, 9, "RC" ) ),
5959
Deprecated(),
60-
Expected( new ApiVersion( 1, 0 ) )
60+
Expected( new ApiVersion( 42, 0 ) )
6161
};
6262
}
6363
}

test/Microsoft.AspNet.WebApi.Versioning.Tests/Dispatcher/ApiVersionControllerSelectorTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ public void select_controller_should_use_api_version_selector_for_attributeX2Dba
498498
configuration.AddApiVersioning( o =>
499499
{
500500
o.AssumeDefaultVersionWhenUnspecified = true;
501-
o.ApiVersionSelector = new LowestImplementedApiVersionSelector();
501+
o.ApiVersionSelector = new LowestImplementedApiVersionSelector( o );
502502
} );
503503
configuration.Routes.MapHttpRoute( "Default", "{controller}/{id}", new { id = Optional } );
504504
configuration.EnsureInitialized();
@@ -718,7 +718,7 @@ public void select_controller_should_assume_current_version_for_attributeX2Dbase
718718
configuration.AddApiVersioning( o =>
719719
{
720720
o.AssumeDefaultVersionWhenUnspecified = true;
721-
o.ApiVersionSelector = new CurrentImplementationApiVersionSelector();
721+
o.ApiVersionSelector = new CurrentImplementationApiVersionSelector( o );
722722
} );
723723
configuration.MapHttpAttributeRoutes();
724724
configuration.EnsureInitialized();
@@ -760,7 +760,7 @@ public void select_controller_should_assume_current_version_for_conventionX2Dbas
760760
configuration.AddApiVersioning( o =>
761761
{
762762
o.AssumeDefaultVersionWhenUnspecified = true;
763-
o.ApiVersionSelector = new CurrentImplementationApiVersionSelector();
763+
o.ApiVersionSelector = new CurrentImplementationApiVersionSelector( o );
764764
} );
765765
configuration.Routes.MapHttpRoute( "Default", "api/{controller}/{id}", new { id = Optional } );
766766
configuration.EnsureInitialized();

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,26 @@
33
using FluentAssertions;
44
using System;
55
using System.Collections.Generic;
6-
using System.Linq;
76
using System.Net.Http;
87
using Xunit;
98

109
public class CurrentImplementationApiVersionSelectorTest
1110
{
1211
[Theory]
1312
[ClassData( typeof( MaxSelectVersionData ) )]
14-
public void select_version_should_return_max_api_version( IEnumerable<ApiVersion> supported, IEnumerable<ApiVersion> deprecated, ApiVersion version )
13+
public void select_version_should_return_max_api_version( IEnumerable<ApiVersion> supportedVersions, IEnumerable<ApiVersion> deprecatedVersions, ApiVersion expectedVersion )
1514
{
1615
// arrange
17-
var selector = new CurrentImplementationApiVersionSelector();
16+
var options = new ApiVersioningOptions() { DefaultApiVersion = new ApiVersion( 42, 0 ) };
17+
var selector = new CurrentImplementationApiVersionSelector( options );
1818
var request = new HttpRequestMessage();
19-
var model = new ApiVersionModel( supported, deprecated );
19+
var model = new ApiVersionModel( supportedVersions, deprecatedVersions );
2020

2121
// act
2222
var selectedVersion = selector.SelectVersion( request, model );
2323

2424
// assert
25-
selectedVersion.Should().Be( version );
25+
selectedVersion.Should().Be( expectedVersion );
2626
}
2727
}
2828
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,26 @@
33
using FluentAssertions;
44
using System;
55
using System.Collections.Generic;
6-
using System.Linq;
76
using System.Net.Http;
87
using Xunit;
98

109
public class LowestImplementedApiVersionSelectorTest
1110
{
1211
[Theory]
1312
[ClassData( typeof( MinSelectVersionData ) )]
14-
public void select_version_should_return_min_api_version( IEnumerable<ApiVersion> supported, IEnumerable<ApiVersion> deprecated, ApiVersion version )
13+
public void select_version_should_return_min_api_version( IEnumerable<ApiVersion> supportedVersions, IEnumerable<ApiVersion> deprecatedVersions, ApiVersion expectedVersion )
1514
{
1615
// arrange
17-
var selector = new LowestImplementedApiVersionSelector();
16+
var options = new ApiVersioningOptions() { DefaultApiVersion = new ApiVersion( 42, 0 ) };
17+
var selector = new LowestImplementedApiVersionSelector( options );
1818
var request = new HttpRequestMessage();
19-
var versionInfo = new ApiVersionModel( supported, deprecated );
19+
var versionInfo = new ApiVersionModel( supportedVersions, deprecatedVersions );
2020

2121
// act
2222
var selectedVersion = selector.SelectVersion( request, versionInfo );
2323

2424
// assert
25-
selectedVersion.Should().Be( version );
25+
selectedVersion.Should().Be( expectedVersion );
2626
}
2727
}
2828
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ public async Task select_best_candidate_should_use_api_version_selector_for_attr
280280
Action<ApiVersioningOptions> versioningSetup = o =>
281281
{
282282
o.AssumeDefaultVersionWhenUnspecified = true;
283-
o.ApiVersionSelector = new LowestImplementedApiVersionSelector();
283+
o.ApiVersionSelector = new LowestImplementedApiVersionSelector( o );
284284
};
285285
Action<IRouteBuilder> routesSetup = r => r.MapRoute( "default", "{controller}/{action=Get_2015_11_15}/{id?}" );
286286

@@ -383,10 +383,10 @@ public async Task select_best_candidate_should_assume_current_version_for_attrib
383383
// arrange
384384
var currentVersion = new ApiVersion( 4, 0 );
385385
var controllerType = typeof( AttributeRoutedTest4Controller ).GetTypeInfo();
386-
Action<ApiVersioningOptions> setup = options =>
386+
Action<ApiVersioningOptions> setup = o =>
387387
{
388-
options.AssumeDefaultVersionWhenUnspecified = true;
389-
options.ApiVersionSelector = new CurrentImplementationApiVersionSelector();
388+
o.AssumeDefaultVersionWhenUnspecified = true;
389+
o.ApiVersionSelector = new CurrentImplementationApiVersionSelector( o );
390390
};
391391

392392
using ( var server = new WebServer( setupApiVersioning: setup ) )
@@ -407,10 +407,10 @@ public async Task select_best_candidate_should_assume_current_version_for_conven
407407
// arrange
408408
var currentVersion = new ApiVersion( 3, 0 );
409409
var controllerType = typeof( TestVersion2Controller ).GetTypeInfo();
410-
Action<ApiVersioningOptions> versioningSetup = options =>
410+
Action<ApiVersioningOptions> versioningSetup = o =>
411411
{
412-
options.AssumeDefaultVersionWhenUnspecified = true;
413-
options.ApiVersionSelector = new CurrentImplementationApiVersionSelector();
412+
o.AssumeDefaultVersionWhenUnspecified = true;
413+
o.ApiVersionSelector = new CurrentImplementationApiVersionSelector( o );
414414
};
415415
Action<IRouteBuilder> routeSetup = routes => routes.MapRoute( "default", "api/{controller}/{action=Get}/{id?}" );
416416

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,19 @@ public class CurrentImplementationApiVersionSelectorTest
1111
{
1212
[Theory]
1313
[ClassData( typeof( MaxSelectVersionData ) )]
14-
public void select_version_should_return_max_api_version( IEnumerable<ApiVersion> supported, IEnumerable<ApiVersion> deprecated, ApiVersion version )
14+
public void select_version_should_return_max_api_version( IEnumerable<ApiVersion> supportedVersions, IEnumerable<ApiVersion> deprecatedVersions, ApiVersion expectedVersion )
1515
{
1616
// arrange
17-
var selector = new CurrentImplementationApiVersionSelector();
17+
var options = new ApiVersioningOptions() { DefaultApiVersion = new ApiVersion( 42, 0 ) };
18+
var selector = new CurrentImplementationApiVersionSelector( options );
1819
var request = new Mock<HttpRequest>().Object;
19-
var model = new ApiVersionModel( supported, deprecated );
20+
var model = new ApiVersionModel( supportedVersions, deprecatedVersions );
2021

2122
// act
2223
var selectedVersion = selector.SelectVersion( request, model );
2324

2425
// assert
25-
selectedVersion.Should().Be( version );
26+
selectedVersion.Should().Be( expectedVersion );
2627
}
2728
}
2829
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,19 @@ public class LowestImplementedApiVersionSelectorTest
1111
{
1212
[Theory]
1313
[ClassData( typeof( MinSelectVersionData ) )]
14-
public void select_version_should_return_min_api_version( IEnumerable<ApiVersion> supported, IEnumerable<ApiVersion> deprecated, ApiVersion version )
14+
public void select_version_should_return_min_api_version( IEnumerable<ApiVersion> supportedVersions, IEnumerable<ApiVersion> deprecatedVersions, ApiVersion expectedVersion )
1515
{
1616
// arrange
17-
var selector = new LowestImplementedApiVersionSelector();
17+
var options = new ApiVersioningOptions() { DefaultApiVersion = new ApiVersion( 42, 0 ) };
18+
var selector = new LowestImplementedApiVersionSelector( options );
1819
var request = new Mock<HttpRequest>().Object;
19-
var versionInfo = new ApiVersionModel( supported, deprecated );
20+
var versionInfo = new ApiVersionModel( supportedVersions, deprecatedVersions );
2021

2122
// act
2223
var selectedVersion = selector.SelectVersion( request, versionInfo );
2324

2425
// assert
25-
selectedVersion.Should().Be( version );
26+
selectedVersion.Should().Be( expectedVersion );
2627
}
2728
}
2829
}

0 commit comments

Comments
 (0)