Skip to content

Commit a9c46c1

Browse files
Restore functionality to report versions when unmatched. Fixes #876. Fixes #918.
1 parent 4c53a86 commit a9c46c1

File tree

34 files changed

+338
-144
lines changed

34 files changed

+338
-144
lines changed

src/AspNet/Acceptance/Asp.Versioning.WebApi.Acceptance.Tests/Http/Basic/given a versioned ApiController/when using a query string and split into two types.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public async Task then_get_should_return_200( string controller, string apiVersi
3434
}
3535

3636
[Fact]
37-
public async Task then_get_should_return_404_for_an_unsupported_version()
37+
public async Task then_get_should_return_400_for_an_unsupported_version()
3838
{
3939
// arrange
4040

@@ -44,7 +44,7 @@ public async Task then_get_should_return_404_for_an_unsupported_version()
4444
var problem = await response.Content.ReadAsProblemDetailsAsync();
4545

4646
// assert
47-
response.StatusCode.Should().Be( NotFound );
47+
response.StatusCode.Should().Be( BadRequest );
4848
problem.Type.Should().Be( ProblemDetailsDefaults.Unsupported.Type );
4949
}
5050

src/AspNet/Acceptance/Asp.Versioning.WebApi.Acceptance.Tests/Http/UsingConventions/given a versioned ApiController using conventions/when using a query string and split into two types.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public async Task then_get_should_return_200( string controller, string apiVersi
3535
}
3636

3737
[Fact]
38-
public async Task then_get_should_return_404_for_an_unsupported_version()
38+
public async Task then_get_should_return_400_for_an_unsupported_version()
3939
{
4040
// arrange
4141

@@ -45,7 +45,7 @@ public async Task then_get_should_return_404_for_an_unsupported_version()
4545
var problem = await response.Content.ReadAsProblemDetailsAsync();
4646

4747
// assert
48-
response.StatusCode.Should().Be( NotFound );
48+
response.StatusCode.Should().Be( BadRequest );
4949
problem.Type.Should().Be( ProblemDetailsDefaults.Unsupported.Type );
5050
}
5151

src/AspNet/Acceptance/Asp.Versioning.WebApi.Acceptance.Tests/Http/UsingNamespace/given a versioned ApiController per namespace/when using a query string.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public async Task then_get_should_return_200( Type controllerType, string apiVer
3232
}
3333

3434
[Fact]
35-
public async Task then_get_should_return_404_for_an_unsupported_version()
35+
public async Task then_get_should_return_400_for_an_unsupported_version()
3636
{
3737
// arrange
3838

@@ -42,7 +42,7 @@ public async Task then_get_should_return_404_for_an_unsupported_version()
4242
var problem = await response.Content.ReadAsProblemDetailsAsync();
4343

4444
// assert
45-
response.StatusCode.Should().Be( NotFound );
45+
response.StatusCode.Should().Be( BadRequest );
4646
problem.Type.Should().Be( ProblemDetailsDefaults.Unsupported.Type );
4747
}
4848

src/AspNet/Acceptance/Asp.Versioning.WebApi.Acceptance.Tests/OData/Advanced/given a versioned ODataController mixed with Web API controllers/when people is any version.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace given_a_versioned_ODataController_mixed_with_Web_API_controllers;
99
public class when_people_is_any_version : AdvancedAcceptanceTest
1010
{
1111
[Fact]
12-
public async Task then_patch_should_return_404_for_an_unsupported_version()
12+
public async Task then_patch_should_return_400_for_an_unsupported_version()
1313
{
1414
// arrange
1515
var person = new { lastName = "Me" };
@@ -19,7 +19,7 @@ public async Task then_patch_should_return_404_for_an_unsupported_version()
1919
var problem = await response.Content.ReadAsProblemDetailsAsync();
2020

2121
// assert
22-
response.StatusCode.Should().Be( NotFound );
22+
response.StatusCode.Should().Be( BadRequest );
2323
problem.Type.Should().Be( ProblemDetailsDefaults.Unsupported.Type );
2424
}
2525

src/AspNet/Acceptance/Asp.Versioning.WebApi.Acceptance.Tests/OData/Basic/given a versioned ODataController/when using a query string and split into two types.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public async Task then_get_should_return_200( string requestUrl )
2828
}
2929

3030
[Fact]
31-
public async Task then_get_should_return_404_for_an_unsupported_version()
31+
public async Task then_get_should_return_400_for_an_unsupported_version()
3232
{
3333
// arrange
3434

@@ -38,7 +38,7 @@ public async Task then_get_should_return_404_for_an_unsupported_version()
3838
var problem = await response.Content.ReadAsProblemDetailsAsync();
3939

4040
// assert
41-
response.StatusCode.Should().Be( NotFound );
41+
response.StatusCode.Should().Be( BadRequest );
4242
problem.Type.Should().Be( ProblemDetailsDefaults.Unsupported.Type );
4343
}
4444

@@ -73,7 +73,7 @@ public async Task then_patch_should_return_405_if_supported_in_any_version( stri
7373
}
7474

7575
[Fact]
76-
public async Task then_patch_should_return_404_for_an_unsupported_version()
76+
public async Task then_patch_should_return_400_for_an_unsupported_version()
7777
{
7878
// arrange
7979
var person = new { id = 42, firstName = "John", lastName = "Doe", email = "[email protected]" };
@@ -83,7 +83,7 @@ public async Task then_patch_should_return_404_for_an_unsupported_version()
8383
var problem = await response.Content.ReadAsProblemDetailsAsync();
8484

8585
// assert
86-
response.StatusCode.Should().Be( NotFound );
86+
response.StatusCode.Should().Be( BadRequest );
8787
problem.Type.Should().Be( ProblemDetailsDefaults.Unsupported.Type );
8888
}
8989

src/AspNet/Acceptance/Asp.Versioning.WebApi.Acceptance.Tests/OData/Basic/given a versioned ODataController/when using a query string.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public async Task then_get_should_return_200( string requestUrl )
2424
}
2525

2626
[Fact]
27-
public async Task then_get_should_return_404_for_an_unsupported_version()
27+
public async Task then_get_should_return_400_for_an_unsupported_version()
2828
{
2929
// arrange
3030

@@ -34,7 +34,7 @@ public async Task then_get_should_return_404_for_an_unsupported_version()
3434
var problem = await response.Content.ReadAsProblemDetailsAsync();
3535

3636
// assert
37-
response.StatusCode.Should().Be( NotFound );
37+
response.StatusCode.Should().Be( BadRequest );
3838
problem.Type.Should().Be( ProblemDetailsDefaults.Unsupported.Type );
3939
}
4040

src/AspNet/Acceptance/Asp.Versioning.WebApi.Acceptance.Tests/OData/ODataAcceptanceTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public async Task then_the_service_document_should_be_versionX2Dspecific( string
3737
}
3838

3939
[Fact]
40-
public async Task then_the_service_document_should_return_404_for_an_unsupported_version()
40+
public async Task then_the_service_document_should_return_400_for_an_unsupported_version()
4141
{
4242
// arrange
4343

@@ -47,7 +47,7 @@ public async Task then_the_service_document_should_return_404_for_an_unsupported
4747
var problem = await response.Content.ReadAsProblemDetailsAsync();
4848

4949
// assert
50-
response.StatusCode.Should().Be( NotFound );
50+
response.StatusCode.Should().Be( BadRequest );
5151
problem.Type.Should().Be( ProblemDetailsDefaults.Unsupported.Type );
5252
}
5353

@@ -83,7 +83,7 @@ public async Task then_X24metadata_should_be_versionX2Dspecific( string apiVersi
8383
}
8484

8585
[Fact]
86-
public async Task then_X24metadata_should_return_404_for_an_unsupported_version()
86+
public async Task then_X24metadata_should_return_400_for_an_unsupported_version()
8787
{
8888
// arrange
8989
Client.DefaultRequestHeaders.Clear();
@@ -93,7 +93,7 @@ public async Task then_X24metadata_should_return_404_for_an_unsupported_version(
9393
var problem = await response.Content.ReadAsProblemDetailsAsync();
9494

9595
// assert
96-
response.StatusCode.Should().Be( NotFound );
96+
response.StatusCode.Should().Be( BadRequest );
9797
problem.Type.Should().Be( ProblemDetailsDefaults.Unsupported.Type );
9898
}
9999

src/AspNet/Acceptance/Asp.Versioning.WebApi.Acceptance.Tests/OData/UsingConventions/given a versioned ODataController using conventions/when using a query string and split into two types.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public async Task then_get_should_return_200( string requestUrl )
2828
}
2929

3030
[Fact]
31-
public async Task then_get_should_return_404_for_an_unsupported_version()
31+
public async Task then_get_should_return_400_for_an_unsupported_version()
3232
{
3333
// arrange
3434

@@ -38,7 +38,7 @@ public async Task then_get_should_return_404_for_an_unsupported_version()
3838
var problem = await response.Content.ReadAsProblemDetailsAsync();
3939

4040
// assert
41-
response.StatusCode.Should().Be( NotFound );
41+
response.StatusCode.Should().Be( BadRequest );
4242
problem.Type.Should().Be( ProblemDetailsDefaults.Unsupported.Type );
4343
}
4444

@@ -73,7 +73,7 @@ public async Task then_patch_should_return_405_if_supported_in_any_version( stri
7373
}
7474

7575
[Fact]
76-
public async Task then_patch_should_return_404_for_an_unsupported_version()
76+
public async Task then_patch_should_return_400_for_an_unsupported_version()
7777
{
7878
// arrange
7979
var person = new { id = 42, firstName = "John", lastName = "Doe", email = "[email protected]" };
@@ -83,7 +83,7 @@ public async Task then_patch_should_return_404_for_an_unsupported_version()
8383
var problem = await response.Content.ReadAsProblemDetailsAsync();
8484

8585
// assert
86-
response.StatusCode.Should().Be( NotFound );
86+
response.StatusCode.Should().Be( BadRequest );
8787
problem.Type.Should().Be( ProblemDetailsDefaults.Unsupported.Type );
8888
}
8989

src/AspNet/Acceptance/Asp.Versioning.WebApi.Acceptance.Tests/OData/UsingConventions/given a versioned ODataController using conventions/when using a query string.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public async Task then_get_should_return_200( string requestUrl )
2424
}
2525

2626
[Fact]
27-
public async Task then_get_should_return_404_for_an_unsupported_version()
27+
public async Task then_get_should_return_400_for_an_unsupported_version()
2828
{
2929
// arrange
3030

@@ -34,7 +34,7 @@ public async Task then_get_should_return_404_for_an_unsupported_version()
3434
var problem = await response.Content.ReadAsProblemDetailsAsync();
3535

3636
// assert
37-
response.StatusCode.Should().Be( NotFound );
37+
response.StatusCode.Should().Be( BadRequest );
3838
problem.Type.Should().Be( ProblemDetailsDefaults.Unsupported.Type );
3939
}
4040

src/AspNet/WebApi/src/Asp.Versioning.WebApi/Dispatcher/HttpResponseExceptionFactory.cs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ namespace Asp.Versioning.Dispatcher;
99
using System.Web.Http.Tracing;
1010
using static System.Net.HttpStatusCode;
1111

12-
#pragma warning disable CA2000 // Dispose objects before losing scope
13-
1412
internal sealed class HttpResponseExceptionFactory
1513
{
1614
private const string Allow = nameof( Allow );
@@ -64,7 +62,8 @@ internal HttpResponseException NewUnmatchedException(
6462
}
6563
}
6664

67-
var versionsOnlyByMediaType = Options.ApiVersionReader.VersionsByMediaType( allowMultipleLocations: false );
65+
var options = Options;
66+
var versionsOnlyByMediaType = options.ApiVersionReader.VersionsByMediaType( allowMultipleLocations: false );
6867

6968
if ( versionsOnlyByMediaType )
7069
{
@@ -75,9 +74,28 @@ internal HttpResponseException NewUnmatchedException(
7574
if ( couldMatch )
7675
{
7776
properties ??= request.ApiVersionProperties();
78-
response = properties.RequestedApiVersion is ApiVersion apiVersion
79-
? CreateResponseForUnsupportedApiVersion( apiVersion, NotFound )
80-
: CreateNotFound( conventionRouteResult );
77+
78+
if ( properties.RequestedApiVersion is ApiVersion apiVersion )
79+
{
80+
HttpStatusCode statusCode;
81+
var matchedUrlSegment = !string.IsNullOrEmpty( properties.RouteParameter );
82+
83+
if ( matchedUrlSegment )
84+
{
85+
statusCode = NotFound;
86+
}
87+
else
88+
{
89+
var versionsByUrlOnly = options.ApiVersionReader.VersionsByUrl( allowMultipleLocations: false );
90+
statusCode = versionsByUrlOnly ? NotFound : options.UnsupportedApiVersionStatusCode;
91+
}
92+
93+
response = CreateResponseForUnsupportedApiVersion( apiVersion, statusCode );
94+
}
95+
else
96+
{
97+
response = CreateNotFound( conventionRouteResult );
98+
}
8199
}
82100
else
83101
{

0 commit comments

Comments
 (0)