Don't set StatusCode in WriteAsJsonAsync (#23583)
This commit is contained in:
parent
7f6147d50f
commit
01de355289
|
|
@ -77,7 +77,6 @@ namespace Microsoft.AspNetCore.Http.Json
|
||||||
options ??= ResolveSerializerOptions(response.HttpContext);
|
options ??= ResolveSerializerOptions(response.HttpContext);
|
||||||
|
|
||||||
response.ContentType = contentType ?? JsonConstants.JsonContentTypeWithCharset;
|
response.ContentType = contentType ?? JsonConstants.JsonContentTypeWithCharset;
|
||||||
response.StatusCode = StatusCodes.Status200OK;
|
|
||||||
return JsonSerializer.SerializeAsync<TValue>(response.Body, value!, options, cancellationToken);
|
return JsonSerializer.SerializeAsync<TValue>(response.Body, value!, options, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -150,7 +149,6 @@ namespace Microsoft.AspNetCore.Http.Json
|
||||||
options ??= ResolveSerializerOptions(response.HttpContext);
|
options ??= ResolveSerializerOptions(response.HttpContext);
|
||||||
|
|
||||||
response.ContentType = contentType ?? JsonConstants.JsonContentTypeWithCharset;
|
response.ContentType = contentType ?? JsonConstants.JsonContentTypeWithCharset;
|
||||||
response.StatusCode = StatusCodes.Status200OK;
|
|
||||||
return JsonSerializer.SerializeAsync(response.Body, value, type, options, cancellationToken);
|
return JsonSerializer.SerializeAsync(response.Body, value, type, options, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -88,6 +88,23 @@ namespace Microsoft.AspNetCore.Http.Extensions.Tests
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task WriteAsJsonAsyncGeneric_CustomStatusCode_StatusCodeUnchanged()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var body = new MemoryStream();
|
||||||
|
var context = new DefaultHttpContext();
|
||||||
|
context.Response.Body = body;
|
||||||
|
|
||||||
|
// Act
|
||||||
|
context.Response.StatusCode = StatusCodes.Status418ImATeapot;
|
||||||
|
await context.Response.WriteAsJsonAsync(1);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(JsonConstants.JsonContentTypeWithCharset, context.Response.ContentType);
|
||||||
|
Assert.Equal(StatusCodes.Status418ImATeapot, context.Response.StatusCode);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task WriteAsJsonAsyncGeneric_WithContentType_JsonResponseWithCustomContentType()
|
public async Task WriteAsJsonAsyncGeneric_WithContentType_JsonResponseWithCustomContentType()
|
||||||
{
|
{
|
||||||
|
|
@ -223,6 +240,23 @@ namespace Microsoft.AspNetCore.Http.Extensions.Tests
|
||||||
Assert.Equal(@"{""stringProperty"":""激光這兩個字是甚麼意思""}", data);
|
Assert.Equal(@"{""stringProperty"":""激光這兩個字是甚麼意思""}", data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task WriteAsJsonAsync_CustomStatusCode_StatusCodeUnchanged()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var body = new MemoryStream();
|
||||||
|
var context = new DefaultHttpContext();
|
||||||
|
context.Response.Body = body;
|
||||||
|
|
||||||
|
// Act
|
||||||
|
context.Response.StatusCode = StatusCodes.Status418ImATeapot;
|
||||||
|
await context.Response.WriteAsJsonAsync(1, typeof(int));
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(JsonConstants.JsonContentTypeWithCharset, context.Response.ContentType);
|
||||||
|
Assert.Equal(StatusCodes.Status418ImATeapot, context.Response.StatusCode);
|
||||||
|
}
|
||||||
|
|
||||||
public class TestObject
|
public class TestObject
|
||||||
{
|
{
|
||||||
public string? StringProperty { get; set; }
|
public string? StringProperty { get; set; }
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue