Skip to content

Commit 6e24a80

Browse files
author
Bart Koelman
committed
Fixed: allow the use of quality factor in Accept header (content negotiation)
1 parent ff30254 commit 6e24a80

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

src/JsonApiDotNetCore/Middleware/JsonApiMiddleware.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ namespace JsonApiDotNetCore.Middleware
2323
/// </summary>
2424
public sealed class JsonApiMiddleware
2525
{
26+
private static readonly MediaTypeHeaderValue _mediaType = MediaTypeHeaderValue.Parse(HeaderConstants.MediaType);
27+
2628
private readonly RequestDelegate _next;
2729

2830
public JsonApiMiddleware(RequestDelegate next)
@@ -96,7 +98,7 @@ private static async Task<bool> ValidateContentTypeHeaderAsync(HttpContext httpC
9698
private static async Task<bool> ValidateAcceptHeaderAsync(HttpContext httpContext, JsonSerializerSettings serializerSettings)
9799
{
98100
StringValues acceptHeaders = httpContext.Request.Headers["Accept"];
99-
if (!acceptHeaders.Any() || acceptHeaders == HeaderConstants.MediaType)
101+
if (!acceptHeaders.Any())
100102
{
101103
return true;
102104
}
@@ -105,15 +107,17 @@ private static async Task<bool> ValidateAcceptHeaderAsync(HttpContext httpContex
105107

106108
foreach (var acceptHeader in acceptHeaders)
107109
{
108-
if (MediaTypeHeaderValue.TryParse(acceptHeader, out var headerValue))
110+
if (MediaTypeWithQualityHeaderValue.TryParse(acceptHeader, out var headerValue))
109111
{
112+
headerValue.Quality = null;
113+
110114
if (headerValue.MediaType == "*/*" || headerValue.MediaType == "application/*")
111115
{
112116
seenCompatibleMediaType = true;
113117
break;
114118
}
115119

116-
if (headerValue.MediaType == HeaderConstants.MediaType && !headerValue.Parameters.Any())
120+
if (_mediaType.Equals(headerValue))
117121
{
118122
seenCompatibleMediaType = true;
119123
break;
@@ -126,7 +130,7 @@ private static async Task<bool> ValidateAcceptHeaderAsync(HttpContext httpContex
126130
await FlushResponseAsync(httpContext.Response, serializerSettings, new Error(HttpStatusCode.NotAcceptable)
127131
{
128132
Title = "The specified Accept header value does not contain any supported media types.",
129-
Detail = $"Please include '{HeaderConstants.MediaType}' in the Accept header values."
133+
Detail = $"Please include '{_mediaType}' in the Accept header values."
130134
});
131135
return false;
132136
}

test/JsonApiDotNetCoreExampleTests/IntegrationTests/ContentNegotiation/AcceptHeaderTests.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
using FluentAssertions;
55
using JsonApiDotNetCore.Middleware;
66
using JsonApiDotNetCore.Serialization.Objects;
7-
using JsonApiDotNetCoreExample;
8-
using JsonApiDotNetCoreExample.Data;
9-
using JsonApiDotNetCoreExample.Models;
107
using Xunit;
118

129
namespace JsonApiDotNetCoreExampleTests.IntegrationTests.ContentNegotiation
@@ -86,7 +83,7 @@ public async Task Permits_JsonApi_without_parameters_in_Accept_headers()
8683
MediaTypeWithQualityHeaderValue.Parse(HeaderConstants.MediaType + "; profile=some"),
8784
MediaTypeWithQualityHeaderValue.Parse(HeaderConstants.MediaType + "; ext=other"),
8885
MediaTypeWithQualityHeaderValue.Parse(HeaderConstants.MediaType + "; unknown=unexpected"),
89-
MediaTypeWithQualityHeaderValue.Parse(HeaderConstants.MediaType)
86+
MediaTypeWithQualityHeaderValue.Parse(HeaderConstants.MediaType + "; q=0.3")
9087
};
9188

9289
// Act

test/JsonApiDotNetCoreExampleTests/IntegrationTests/ContentNegotiation/ContentTypeHeaderTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using FluentAssertions;
44
using JsonApiDotNetCore.Middleware;
55
using JsonApiDotNetCore.Serialization.Objects;
6-
using JsonApiDotNetCoreExample.Models;
76
using Xunit;
87

98
namespace JsonApiDotNetCoreExampleTests.IntegrationTests.ContentNegotiation

0 commit comments

Comments
 (0)