Skip to content

Commit f25b079

Browse files
Fix media type subset matching. Fixes #1015
1 parent f45d9fb commit f25b079

File tree

4 files changed

+55
-3
lines changed

4 files changed

+55
-3
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
3+
namespace Asp.Versioning.Http;
4+
5+
public class MediaTypeFixture : MinimalApiFixture
6+
{
7+
protected override void OnAddApiVersioning( ApiVersioningOptions options ) =>
8+
options.ApiVersionReader = new MediaTypeApiVersionReader();
9+
}

src/AspNetCore/Acceptance/Asp.Versioning.Mvc.Acceptance.Tests/Http/MinimalApiFixture.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,12 @@ protected override void OnConfigureEndpoints( IEndpointRouteBuilder endpoints )
7676
.WithApiVersionSet( orders )
7777
.IsApiVersionNeutral();
7878
}
79+
80+
protected override void OnAddApiVersioning( ApiVersioningOptions options )
81+
{
82+
options.ApiVersionReader = ApiVersionReader.Combine(
83+
new QueryStringApiVersionReader(),
84+
new UrlSegmentApiVersionReader(),
85+
new MediaTypeApiVersionReader() );
86+
}
7987
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
3+
namespace Asp.Versioning.Http;
4+
5+
using Asp.Versioning;
6+
using System.Net.Http;
7+
using System.Net.Http.Json;
8+
using static System.Net.Http.Headers.MediaTypeWithQualityHeaderValue;
9+
using static System.Net.Http.HttpMethod;
10+
using static System.Net.HttpStatusCode;
11+
12+
public class when_using_a_media_type : AcceptanceTest, IClassFixture<MediaTypeFixture>
13+
{
14+
[Fact]
15+
public async Task problem_details_should_be_returned_for_accept_header_with_unsupported_api_version()
16+
{
17+
// arrange
18+
using var request = new HttpRequestMessage( Post, "api/values" )
19+
{
20+
Headers = { Accept = { Parse( "application/json;v=3.0" ) } },
21+
Content = JsonContent.Create( new { test = true }, Parse( "application/json;v=3.0" ) ),
22+
};
23+
24+
// act
25+
var response = await Client.SendAsync( request );
26+
var problem = await response.Content.ReadAsProblemDetailsAsync();
27+
28+
// assert
29+
response.StatusCode.Should().Be( UnsupportedMediaType );
30+
problem.Type.Should().Be( ProblemDetailsDefaults.Unsupported.Type );
31+
}
32+
33+
public when_using_a_media_type( MediaTypeFixture fixture, ITestOutputHelper console )
34+
: base( fixture ) => console.WriteLine( fixture.DirectedGraphVisualizationUrl );
35+
}

src/AspNetCore/WebApi/src/Asp.Versioning.Http/Rfc7231ProblemDetailsWriter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Asp.Versioning;
99
internal sealed class Rfc7231ProblemDetailsWriter : IProblemDetailsWriter
1010
{
1111
private static readonly MediaTypeHeaderValue jsonMediaType = new( "application/json" );
12-
private static readonly MediaTypeHeaderValue problemDetailsJsonMediaType = new( "application/problem+json" );
12+
private static readonly MediaTypeHeaderValue problemDetailsJsonMediaType = new( ProblemDetailsDefaults.MediaType.Json );
1313
private readonly IProblemDetailsWriter decorated;
1414

1515
public Rfc7231ProblemDetailsWriter( IProblemDetailsWriter decorated ) => this.decorated = decorated;
@@ -36,8 +36,8 @@ public bool CanWrite( ProblemDetailsContext context )
3636
{
3737
var acceptHeaderValue = acceptHeader[i];
3838

39-
if ( jsonMediaType.IsSubsetOf( acceptHeaderValue ) ||
40-
problemDetailsJsonMediaType.IsSubsetOf( acceptHeaderValue ) )
39+
if ( acceptHeaderValue.IsSubsetOf( jsonMediaType ) ||
40+
acceptHeaderValue.IsSubsetOf( problemDetailsJsonMediaType ) )
4141
{
4242
return true;
4343
}

0 commit comments

Comments
 (0)