Skip to content

Commit

Permalink
Default Status Code for void types changed to 200
Browse files Browse the repository at this point in the history
  • Loading branch information
jbagga committed Oct 6, 2016
1 parent 5820854 commit 41f00ee
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,7 @@ private IReadOnlyList<ApiResponseType> GetApiResponseTypes(

// Set the default status only when no status has already been set explicitly
if (objectTypes.Count == 0
&& type != null
&& type != typeof(void))
&& type != null)
{
objectTypes[StatusCodes.Status200OK] = type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ public static TheoryData<Type, string, List<FilterDescriptor>> ReturnsVoidOrTask
new ProducesAttribute("text/json", "application/json"),
FilterScope.Action),
new FilterDescriptor(
new ProducesResponseTypeAttribute(typeof(void), 204),
new ProducesResponseTypeAttribute(typeof(void), 200),
FilterScope.Action),
new FilterDescriptor(
new ProducesResponseTypeAttribute(typeof(BadData), 400),
Expand Down Expand Up @@ -569,7 +569,7 @@ public void GetApiDescription_ReturnsVoidWithProducesContentType(
responseType =>
{
Assert.Equal(typeof(void), responseType.Type);
Assert.Equal(204, responseType.StatusCode);
Assert.Equal(200, responseType.StatusCode);
Assert.Null(responseType.ModelMetadata);
Assert.Empty(responseType.ApiResponseFormats);
},
Expand All @@ -592,7 +592,26 @@ public void GetApiDescription_ReturnsVoidWithProducesContentType(
[Theory]
[InlineData(nameof(ReturnsVoid))]
[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
var action = CreateActionDescriptor(methodName);
Expand Down
24 changes: 22 additions & 2 deletions test/Microsoft.AspNetCore.Mvc.FunctionalTests/ApiExplorerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,10 +393,30 @@ public async Task ApiExplorer_HttpMethod_Single(string httpMethod)
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]
[InlineData("GetVoid")]
[InlineData("GetTask")]
public async Task ApiExplorer_ResponseType_VoidWithoutAttribute(string action)
public async Task ApiExplorer_ResponseType_VoidWithoutAttributeDefaultStatusCode(string action)
{
// Arrange & Act
var response = await Client.GetAsync(
Expand All @@ -409,7 +429,7 @@ public async Task ApiExplorer_ResponseType_VoidWithoutAttribute(string action)
var description = Assert.Single(result);
var responseType = Assert.Single(description.SupportedResponseTypes);
Assert.Equal(typeof(void).FullName, responseType.ResponseType);
Assert.Equal(204, responseType.StatusCode);
Assert.Equal(200, responseType.StatusCode);
Assert.Empty(responseType.ResponseFormats);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ namespace ApiExplorerWebSite
[Route("[controller]/[Action]")]
public class ApiExplorerResponseTypeWithAttributeController : Controller
{
[HttpGet]
[ProducesResponseType(typeof(void), 204)]
public void GetVoidWithExplicitResponseTypeStatusCode()
{
}

[HttpGet]
[Produces(typeof(Customer))]
public void GetVoid()
Expand All @@ -30,6 +36,13 @@ public IActionResult GetIActionResult()
return new EmptyResult();
}

[HttpGet]
[ProducesResponseType(typeof(void), 204)]
public Task GetTaskWithExplicitResponseTypeStatusCode()
{
return Task.FromResult(true);
}

[HttpGet]
[Produces("application/json", Type = typeof(int))]
public Task GetTask()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace ApiExplorerWebSite
public class ApiExplorerResponseTypeWithoutAttributeController : Controller
{
[HttpGet]
[ProducesResponseType(typeof(void), 204)]
public void GetVoid()
{
}
Expand Down Expand Up @@ -46,7 +45,6 @@ public int GetInt()
}

[HttpGet]
[ProducesResponseType(typeof(void), 204)]
public Task GetTask()
{
return Task.FromResult(true);
Expand Down

0 comments on commit 41f00ee

Please sign in to comment.