diff --git a/src/JsonApiDotNetCore/Middleware/JsonApiContentNegotiator.cs b/src/JsonApiDotNetCore/Middleware/JsonApiContentNegotiator.cs index 5a3c08ee2b..9cc366b282 100644 --- a/src/JsonApiDotNetCore/Middleware/JsonApiContentNegotiator.cs +++ b/src/JsonApiDotNetCore/Middleware/JsonApiContentNegotiator.cs @@ -1,4 +1,5 @@ using System.Net; +using JetBrains.Annotations; using JsonApiDotNetCore.Configuration; using JsonApiDotNetCore.Errors; using JsonApiDotNetCore.Serialization.Objects; @@ -8,6 +9,7 @@ namespace JsonApiDotNetCore.Middleware; /// +[PublicAPI] public class JsonApiContentNegotiator : IJsonApiContentNegotiator { private readonly IJsonApiOptions _options; @@ -71,9 +73,9 @@ private IReadOnlySet ValidateAcceptHeader(IReadOnlyLi string[] acceptHeaderValues = HttpContext.Request.Headers.GetCommaSeparatedValues("Accept"); JsonApiMediaType? bestMatch = null; - if (acceptHeaderValues.Length == 0 && possibleMediaTypes.Contains(JsonApiMediaType.Default)) + if (acceptHeaderValues.Length == 0) { - bestMatch = JsonApiMediaType.Default; + bestMatch = GetDefaultMediaType(possibleMediaTypes, requestMediaType); } else { @@ -149,6 +151,23 @@ private IReadOnlySet ValidateAcceptHeader(IReadOnlyLi return bestMatch.Extensions; } + /// + /// Returns the JSON:API media type (possibly including extensions) to use when no Accept header was sent. + /// + /// + /// The media types returned from . + /// + /// + /// The media type from in the Content-Type header. + /// + /// + /// The default media type to use, or null if not available. + /// + protected virtual JsonApiMediaType? GetDefaultMediaType(IReadOnlyList possibleMediaTypes, JsonApiMediaType? requestMediaType) + { + return possibleMediaTypes.Contains(JsonApiMediaType.Default) ? JsonApiMediaType.Default : null; + } + /// /// Gets the list of possible combinations of JSON:API extensions that are available at the current endpoint. The set of extensions in the request body /// must always be the same as in the response body.