Default Status Code for void types changed to 200

Fixes #4838
This commit is contained in:
jbagga 2016-10-06 14:39:04 -07:00
parent 5820854940
commit 41f00eea23
5 changed files with 58 additions and 9 deletions

View File

@ -377,8 +377,7 @@ namespace Microsoft.AspNetCore.Mvc.ApiExplorer
// Set the default status only when no status has already been set explicitly // Set the default status only when no status has already been set explicitly
if (objectTypes.Count == 0 if (objectTypes.Count == 0
&& type != null && type != null)
&& type != typeof(void))
{ {
objectTypes[StatusCodes.Status200OK] = type; objectTypes[StatusCodes.Status200OK] = type;
} }

View File

@ -509,7 +509,7 @@ namespace Microsoft.AspNetCore.Mvc.Description
new ProducesAttribute("text/json", "application/json"), new ProducesAttribute("text/json", "application/json"),
FilterScope.Action), FilterScope.Action),
new FilterDescriptor( new FilterDescriptor(
new ProducesResponseTypeAttribute(typeof(void), 204), new ProducesResponseTypeAttribute(typeof(void), 200),
FilterScope.Action), FilterScope.Action),
new FilterDescriptor( new FilterDescriptor(
new ProducesResponseTypeAttribute(typeof(BadData), 400), new ProducesResponseTypeAttribute(typeof(BadData), 400),
@ -569,7 +569,7 @@ namespace Microsoft.AspNetCore.Mvc.Description
responseType => responseType =>
{ {
Assert.Equal(typeof(void), responseType.Type); Assert.Equal(typeof(void), responseType.Type);
Assert.Equal(204, responseType.StatusCode); Assert.Equal(200, responseType.StatusCode);
Assert.Null(responseType.ModelMetadata); Assert.Null(responseType.ModelMetadata);
Assert.Empty(responseType.ApiResponseFormats); Assert.Empty(responseType.ApiResponseFormats);
}, },
@ -592,7 +592,26 @@ namespace Microsoft.AspNetCore.Mvc.Description
[Theory] [Theory]
[InlineData(nameof(ReturnsVoid))] [InlineData(nameof(ReturnsVoid))]
[InlineData(nameof(ReturnsTask))] [InlineData(nameof(ReturnsTask))]
public void GetApiDescription_DoesNotPopulatesResponseInformation_WhenVoid(string methodName) public void GetApiDescription_DefaultVoidStatus(string methodName)
{
// Arrange
var action = CreateActionDescriptor(methodName);
// Act
var descriptions = GetApiDescriptions(action);
// Assert
var description = Assert.Single(descriptions);
var responseType = Assert.Single(description.SupportedResponseTypes);
Assert.Equal(typeof(void), responseType.Type);
Assert.Equal(200, responseType.StatusCode);
Assert.Null(responseType.ModelMetadata);
}
[Theory]
[InlineData(nameof(ReturnsVoid))]
[InlineData(nameof(ReturnsTask))]
public void GetApiDescription_VoidWithResponseTypeAttributeStatus(string methodName)
{ {
// Arrange // Arrange
var action = CreateActionDescriptor(methodName); var action = CreateActionDescriptor(methodName);

View File

@ -393,10 +393,30 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
Assert.Single(result, d => d.HttpMethod == "POST"); Assert.Single(result, d => d.HttpMethod == "POST");
} }
[Theory]
[InlineData("GetVoidWithExplicitResponseTypeStatusCode")]
[InlineData("GetTaskWithExplicitResponseTypeStatusCode")]
public async Task ApiExplorer_ResponseType_VoidWithResponseTypeAttributeStatusCode(string action)
{
// Arrange & Act
var response = await Client.GetAsync(
"http://localhost/ApiExplorerResponseTypeWithAttribute/" + action);
var body = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<List<ApiExplorerData>>(body);
// Assert
var description = Assert.Single(result);
var responseType = Assert.Single(description.SupportedResponseTypes);
Assert.Equal(typeof(void).FullName, responseType.ResponseType);
Assert.Equal(204, responseType.StatusCode);
Assert.Empty(responseType.ResponseFormats);
}
[Theory] [Theory]
[InlineData("GetVoid")] [InlineData("GetVoid")]
[InlineData("GetTask")] [InlineData("GetTask")]
public async Task ApiExplorer_ResponseType_VoidWithoutAttribute(string action) public async Task ApiExplorer_ResponseType_VoidWithoutAttributeDefaultStatusCode(string action)
{ {
// Arrange & Act // Arrange & Act
var response = await Client.GetAsync( var response = await Client.GetAsync(
@ -409,7 +429,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
var description = Assert.Single(result); var description = Assert.Single(result);
var responseType = Assert.Single(description.SupportedResponseTypes); var responseType = Assert.Single(description.SupportedResponseTypes);
Assert.Equal(typeof(void).FullName, responseType.ResponseType); Assert.Equal(typeof(void).FullName, responseType.ResponseType);
Assert.Equal(204, responseType.StatusCode); Assert.Equal(200, responseType.StatusCode);
Assert.Empty(responseType.ResponseFormats); Assert.Empty(responseType.ResponseFormats);
} }

View File

@ -10,6 +10,12 @@ namespace ApiExplorerWebSite
[Route("[controller]/[Action]")] [Route("[controller]/[Action]")]
public class ApiExplorerResponseTypeWithAttributeController : Controller public class ApiExplorerResponseTypeWithAttributeController : Controller
{ {
[HttpGet]
[ProducesResponseType(typeof(void), 204)]
public void GetVoidWithExplicitResponseTypeStatusCode()
{
}
[HttpGet] [HttpGet]
[Produces(typeof(Customer))] [Produces(typeof(Customer))]
public void GetVoid() public void GetVoid()
@ -30,6 +36,13 @@ namespace ApiExplorerWebSite
return new EmptyResult(); return new EmptyResult();
} }
[HttpGet]
[ProducesResponseType(typeof(void), 204)]
public Task GetTaskWithExplicitResponseTypeStatusCode()
{
return Task.FromResult(true);
}
[HttpGet] [HttpGet]
[Produces("application/json", Type = typeof(int))] [Produces("application/json", Type = typeof(int))]
public Task GetTask() public Task GetTask()

View File

@ -10,7 +10,6 @@ namespace ApiExplorerWebSite
public class ApiExplorerResponseTypeWithoutAttributeController : Controller public class ApiExplorerResponseTypeWithoutAttributeController : Controller
{ {
[HttpGet] [HttpGet]
[ProducesResponseType(typeof(void), 204)]
public void GetVoid() public void GetVoid()
{ {
} }
@ -46,7 +45,6 @@ namespace ApiExplorerWebSite
} }
[HttpGet] [HttpGet]
[ProducesResponseType(typeof(void), 204)]
public Task GetTask() public Task GetTask()
{ {
return Task.FromResult(true); return Task.FromResult(true);