Skip to content

Commit 583f76f

Browse files
author
Bart Koelman
committed
Fixed: exception logged in Kestrel on Delete request
When an ActionResult returns null, we wrap that in a json:api response, causing an error from Kestrel because it does not allow a response body to be sent in some cases. See https://github.com/aspnet/KestrelHttpServer/blob/50bb0b3bc9c45e5c89617b9769959aa32e12278b/src/Kestrel.Core/Internal/Http/HttpProtocol.cs#L1270-L1275 for details.
1 parent ac5b100 commit 583f76f

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/JsonApiDotNetCore/Formatters/JsonApiWriter.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,19 @@ private string SerializeResponse(object contextObject, HttpStatusCode statusCode
7777
throw new UnsuccessfulActionResultException(problemDetails);
7878
}
7979

80-
if (contextObject == null && !IsSuccessStatusCode(statusCode))
80+
if (contextObject == null)
8181
{
82-
throw new UnsuccessfulActionResultException(statusCode);
82+
if (!IsSuccessStatusCode(statusCode))
83+
{
84+
throw new UnsuccessfulActionResultException(statusCode);
85+
}
86+
87+
if (statusCode == HttpStatusCode.NoContent || statusCode == HttpStatusCode.ResetContent ||
88+
statusCode == HttpStatusCode.NotModified)
89+
{
90+
// Prevent exception from Kestrel server, caused by writing data:null json response.
91+
return null;
92+
}
8393
}
8494

8595
contextObject = WrapErrors(contextObject);

test/NoEntityFrameworkTests/WorkItemTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ await ExecuteOnDbContextAsync(async dbContext =>
146146
string responseBody = await response.Content.ReadAsStringAsync();
147147
var document = JsonConvert.DeserializeObject<Document>(responseBody);
148148

149-
Assert.Null(document.Data);
149+
Assert.Null(document);
150150
}
151151

152152
private async Task ExecuteOnDbContextAsync(Func<AppDbContext, Task> asyncAction)

0 commit comments

Comments
 (0)