Skip to content

Commit 8227a85

Browse files
author
Bart Koelman
committed
Fill error.source.header where applicable
1 parent cd1338d commit 8227a85

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

src/JsonApiDotNetCore/Middleware/JsonApiMiddleware.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,11 @@ private async Task<bool> ValidateIfMatchHeaderAsync(HttpContext httpContext, Jso
108108
{
109109
await FlushResponseAsync(httpContext.Response, serializerSettings, new ErrorObject(HttpStatusCode.PreconditionFailed)
110110
{
111-
Title = "Detection of mid-air edit collisions using ETags is not supported."
111+
Title = "Detection of mid-air edit collisions using ETags is not supported.",
112+
Source = new ErrorSource
113+
{
114+
Header = "If-Match"
115+
}
112116
});
113117

114118
return false;
@@ -149,7 +153,11 @@ private static async Task<bool> ValidateContentTypeHeaderAsync(string allowedCon
149153
await FlushResponseAsync(httpContext.Response, serializerSettings, new ErrorObject(HttpStatusCode.UnsupportedMediaType)
150154
{
151155
Title = "The specified Content-Type header value is not supported.",
152-
Detail = $"Please specify '{allowedContentType}' instead of '{contentType}' for the Content-Type header value."
156+
Detail = $"Please specify '{allowedContentType}' instead of '{contentType}' for the Content-Type header value.",
157+
Source = new ErrorSource
158+
{
159+
Header = "Content-Type"
160+
}
153161
});
154162

155163
return false;
@@ -195,7 +203,11 @@ private static async Task<bool> ValidateAcceptHeaderAsync(MediaTypeHeaderValue a
195203
await FlushResponseAsync(httpContext.Response, serializerSettings, new ErrorObject(HttpStatusCode.NotAcceptable)
196204
{
197205
Title = "The specified Accept header value does not contain any supported media types.",
198-
Detail = $"Please include '{allowedMediaTypeValue}' in the Accept header values."
206+
Detail = $"Please include '{allowedMediaTypeValue}' in the Accept header values.",
207+
Source = new ErrorSource
208+
{
209+
Header = "Accept"
210+
}
199211
});
200212

201213
return false;

test/JsonApiDotNetCoreTests/IntegrationTests/ContentNegotiation/AcceptHeaderTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ public async Task Denies_JsonApi_with_parameters_in_Accept_headers()
198198
error.StatusCode.Should().Be(HttpStatusCode.NotAcceptable);
199199
error.Title.Should().Be("The specified Accept header value does not contain any supported media types.");
200200
error.Detail.Should().Be("Please include 'application/vnd.api+json' in the Accept header values.");
201+
error.Source.Header.Should().Be("Accept");
201202
}
202203

203204
[Fact]
@@ -244,6 +245,7 @@ public async Task Denies_JsonApi_in_Accept_headers_at_operations_endpoint()
244245
error.StatusCode.Should().Be(HttpStatusCode.NotAcceptable);
245246
error.Title.Should().Be("The specified Accept header value does not contain any supported media types.");
246247
error.Detail.Should().Be("Please include 'application/vnd.api+json; ext=\"https://jsonapi.org/ext/atomic\"' in the Accept header values.");
248+
error.Source.Header.Should().Be("Accept");
247249
}
248250
}
249251
}

test/JsonApiDotNetCoreTests/IntegrationTests/ContentNegotiation/ContentTypeHeaderTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ public async Task Denies_unknown_ContentType_header()
9999
error.StatusCode.Should().Be(HttpStatusCode.UnsupportedMediaType);
100100
error.Title.Should().Be("The specified Content-Type header value is not supported.");
101101
error.Detail.Should().Be("Please specify 'application/vnd.api+json' instead of 'text/html' for the Content-Type header value.");
102+
error.Source.Header.Should().Be("Content-Type");
102103
}
103104

104105
[Fact]
@@ -192,6 +193,7 @@ public async Task Denies_JsonApi_ContentType_header_with_profile()
192193
error.StatusCode.Should().Be(HttpStatusCode.UnsupportedMediaType);
193194
error.Title.Should().Be("The specified Content-Type header value is not supported.");
194195
error.Detail.Should().Be($"Please specify 'application/vnd.api+json' instead of '{contentType}' for the Content-Type header value.");
196+
error.Source.Header.Should().Be("Content-Type");
195197
}
196198

197199
[Fact]
@@ -225,6 +227,7 @@ public async Task Denies_JsonApi_ContentType_header_with_extension()
225227
error.StatusCode.Should().Be(HttpStatusCode.UnsupportedMediaType);
226228
error.Title.Should().Be("The specified Content-Type header value is not supported.");
227229
error.Detail.Should().Be($"Please specify 'application/vnd.api+json' instead of '{contentType}' for the Content-Type header value.");
230+
error.Source.Header.Should().Be("Content-Type");
228231
}
229232

230233
[Fact]
@@ -258,6 +261,7 @@ public async Task Denies_JsonApi_ContentType_header_with_AtomicOperations_extens
258261
error.StatusCode.Should().Be(HttpStatusCode.UnsupportedMediaType);
259262
error.Title.Should().Be("The specified Content-Type header value is not supported.");
260263
error.Detail.Should().Be($"Please specify 'application/vnd.api+json' instead of '{contentType}' for the Content-Type header value.");
264+
error.Source.Header.Should().Be("Content-Type");
261265
}
262266

263267
[Fact]
@@ -291,6 +295,7 @@ public async Task Denies_JsonApi_ContentType_header_with_CharSet()
291295
error.StatusCode.Should().Be(HttpStatusCode.UnsupportedMediaType);
292296
error.Title.Should().Be("The specified Content-Type header value is not supported.");
293297
error.Detail.Should().Be($"Please specify 'application/vnd.api+json' instead of '{contentType}' for the Content-Type header value.");
298+
error.Source.Header.Should().Be("Content-Type");
294299
}
295300

296301
[Fact]
@@ -324,6 +329,7 @@ public async Task Denies_JsonApi_ContentType_header_with_unknown_parameter()
324329
error.StatusCode.Should().Be(HttpStatusCode.UnsupportedMediaType);
325330
error.Title.Should().Be("The specified Content-Type header value is not supported.");
326331
error.Detail.Should().Be($"Please specify 'application/vnd.api+json' instead of '{contentType}' for the Content-Type header value.");
332+
error.Source.Header.Should().Be("Content-Type");
327333
}
328334

329335
[Fact]
@@ -367,6 +373,7 @@ public async Task Denies_JsonApi_ContentType_header_at_operations_endpoint()
367373
error.StatusCode.Should().Be(HttpStatusCode.UnsupportedMediaType);
368374
error.Title.Should().Be("The specified Content-Type header value is not supported.");
369375
error.Detail.Should().Be(detail);
376+
error.Source.Header.Should().Be("Content-Type");
370377
}
371378
}
372379
}

test/JsonApiDotNetCoreTests/IntegrationTests/Serialization/ETagTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
175175
error.StatusCode.Should().Be(HttpStatusCode.PreconditionFailed);
176176
error.Title.Should().Be("Detection of mid-air edit collisions using ETags is not supported.");
177177
error.Detail.Should().BeNull();
178+
error.Source.Header.Should().Be("If-Match");
178179
}
179180

180181
[Fact]

0 commit comments

Comments
 (0)