From 957373169d3ceb1b8fb7e821dd441debe68b59c0 Mon Sep 17 00:00:00 2001 From: Bart Koelman <10324372+bkoelman@users.noreply.github.com> Date: Tue, 26 Nov 2024 17:28:21 +0100 Subject: [PATCH] Enable override of chosen media type when Accept header is missing (used to workaround NSwag bug in OpenAPI) --- .../Middleware/JsonApiContentNegotiator.cs | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) 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. pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy