|
1 | 1 | using System.Net; |
| 2 | +using JetBrains.Annotations; |
2 | 3 | using JsonApiDotNetCore.Configuration; |
3 | 4 | using JsonApiDotNetCore.Errors; |
4 | 5 | using JsonApiDotNetCore.Serialization.Objects; |
|
8 | 9 | namespace JsonApiDotNetCore.Middleware; |
9 | 10 |
|
10 | 11 | /// <inheritdoc /> |
| 12 | +[PublicAPI] |
11 | 13 | public class JsonApiContentNegotiator : IJsonApiContentNegotiator |
12 | 14 | { |
13 | 15 | private readonly IJsonApiOptions _options; |
@@ -71,9 +73,9 @@ private IReadOnlySet<JsonApiMediaTypeExtension> ValidateAcceptHeader(IReadOnlyLi |
71 | 73 | string[] acceptHeaderValues = HttpContext.Request.Headers.GetCommaSeparatedValues("Accept"); |
72 | 74 | JsonApiMediaType? bestMatch = null; |
73 | 75 |
|
74 | | - if (acceptHeaderValues.Length == 0 && possibleMediaTypes.Contains(JsonApiMediaType.Default)) |
| 76 | + if (acceptHeaderValues.Length == 0) |
75 | 77 | { |
76 | | - bestMatch = JsonApiMediaType.Default; |
| 78 | + bestMatch = GetDefaultMediaType(possibleMediaTypes, requestMediaType); |
77 | 79 | } |
78 | 80 | else |
79 | 81 | { |
@@ -149,6 +151,23 @@ private IReadOnlySet<JsonApiMediaTypeExtension> ValidateAcceptHeader(IReadOnlyLi |
149 | 151 | return bestMatch.Extensions; |
150 | 152 | } |
151 | 153 |
|
| 154 | + /// <summary> |
| 155 | + /// Returns the JSON:API media type (possibly including extensions) to use when no Accept header was sent. |
| 156 | + /// </summary> |
| 157 | + /// <param name="possibleMediaTypes"> |
| 158 | + /// The media types returned from <see cref="GetPossibleMediaTypes" />. |
| 159 | + /// </param> |
| 160 | + /// <param name="requestMediaType"> |
| 161 | + /// The media type from in the Content-Type header. |
| 162 | + /// </param> |
| 163 | + /// <returns> |
| 164 | + /// The default media type to use, or <c>null</c> if not available. |
| 165 | + /// </returns> |
| 166 | + protected virtual JsonApiMediaType? GetDefaultMediaType(IReadOnlyList<JsonApiMediaType> possibleMediaTypes, JsonApiMediaType? requestMediaType) |
| 167 | + { |
| 168 | + return possibleMediaTypes.Contains(JsonApiMediaType.Default) ? JsonApiMediaType.Default : null; |
| 169 | + } |
| 170 | + |
152 | 171 | /// <summary> |
153 | 172 | /// 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 |
154 | 173 | /// must always be the same as in the response body. |
|
0 commit comments