Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
kichalla committed Mar 29, 2016
1 parent bef2a4f commit 80b0069
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 115 deletions.
7 changes: 1 addition & 6 deletions src/Microsoft.AspNetCore.Mvc.ApiExplorer/ApiResponseType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,10 @@ namespace Microsoft.AspNetCore.Mvc.ApiExplorer
/// </summary>
public class ApiResponseType
{
public ApiResponseType()
{
ApiResponseFormats = new List<ApiResponseFormat>();
}

/// <summary>
/// Gets or sets the response formats supported by this type.
/// </summary>
public IList<ApiResponseFormat> ApiResponseFormats { get; set; }
public IList<ApiResponseFormat> ApiResponseFormats { get; set; } = new List<ApiResponseFormat>();

/// <summary>
/// Gets or sets <see cref="ModelBinding.ModelMetadata"/> for the <see cref="Type"/> or null.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Reflection;
#endif
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Formatters;
Expand Down Expand Up @@ -361,7 +362,7 @@ private IReadOnlyList<ApiResponseType> GetApiResponseTypes(
{
// This return type can be overriden by any response metadata
// attributes later if the user wishes to.
objectTypes[200] = type;
objectTypes[StatusCodes.Status200OK] = type;
}

// Get the content type that the action explicitly set to support.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public ProducesResponseTypeAttribute(Type type, int statusCode)
public int StatusCode { get; set; }

/// <inheritdoc />
public void SetContentTypes(MediaTypeCollection contentTypes)
void IApiResponseMetadataProvider.SetContentTypes(MediaTypeCollection contentTypes)
{
// Users are supposed to use the 'Produces' attribute to set the content types that an action can support.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,7 @@ public void GetApiDescription_DoesNotPopulatesResponseInformation_WhenUnknown(st
Assert.Empty(description.SupportedResponseTypes);
}

public static TheoryData<Type, string, List<FilterDescriptor>>
ReturnsActionResultWithProducesAndProducesContentTypeData
public static TheoryData ReturnsActionResultWithProducesAndProducesContentTypeData
{
get
{
Expand Down Expand Up @@ -471,29 +470,29 @@ public void GetApiDescription_ReturnsActionResultWithProduces_And_ProducesConten
var description = Assert.Single(descriptions);
Assert.Equal(3, description.SupportedResponseTypes.Count);

var responseType = Assert.Single(
description.SupportedResponseTypes.Where(respType => respType.Type == typeof(Customer)));
Assert.Equal(200, responseType.StatusCode);
Assert.NotNull(responseType.ModelMetadata);
Assert.Equal(
expectedMediaTypes,
responseType.ApiResponseFormats.OrderBy(format => format.MediaType).Select(format => format.MediaType));

responseType = Assert.Single(
description.SupportedResponseTypes.Where(respType => respType.Type == typeof(BadData)));
Assert.Equal(400, responseType.StatusCode);
Assert.NotNull(responseType.ModelMetadata);
Assert.Equal(
expectedMediaTypes,
responseType.ApiResponseFormats.OrderBy(format => format.MediaType).Select(format => format.MediaType));

responseType = Assert.Single(
description.SupportedResponseTypes.Where(respFormat => respFormat.Type == typeof(ErrorDetails)));
Assert.Equal(500, responseType.StatusCode);
Assert.NotNull(responseType.ModelMetadata);
Assert.Equal(
expectedMediaTypes,
responseType.ApiResponseFormats.OrderBy(format => format.MediaType).Select(format => format.MediaType));
Assert.Collection(
description.SupportedResponseTypes.OrderBy(responseType => responseType.StatusCode),
responseType =>
{
Assert.Equal(200, responseType.StatusCode);
Assert.Equal(typeof(Customer), responseType.Type);
Assert.NotNull(responseType.ModelMetadata);
Assert.Equal(expectedMediaTypes, GetSortedMediaTypes(responseType));
},
responseType =>
{
Assert.Equal(400, responseType.StatusCode);
Assert.Equal(typeof(BadData), responseType.Type);
Assert.NotNull(responseType.ModelMetadata);
Assert.Equal(expectedMediaTypes, GetSortedMediaTypes(responseType));
},
responseType =>
{
Assert.Equal(500, responseType.StatusCode);
Assert.Equal(typeof(ErrorDetails), responseType.Type);
Assert.NotNull(responseType.ModelMetadata);
Assert.Equal(expectedMediaTypes, GetSortedMediaTypes(responseType));
});
}

public static TheoryData<Type, string, List<FilterDescriptor>> ReturnsVoidOrTaskWithProducesContentTypeData
Expand Down Expand Up @@ -555,6 +554,7 @@ public void GetApiDescription_ReturnsVoidWithProducesContentType(
// Arrange
var action = CreateActionDescriptor(methodName, controllerType);
action.FilterDescriptors = filterDescriptors;
var expectedMediaTypes = new[] { "application/json", "text/json" };

// Act
var descriptions = GetApiDescriptions(action);
Expand All @@ -563,28 +563,29 @@ public void GetApiDescription_ReturnsVoidWithProducesContentType(
var description = Assert.Single(descriptions);
Assert.Equal(3, description.SupportedResponseTypes.Count);

var supportedResponseTypes = description.SupportedResponseTypes;
var responseType = Assert.Single(supportedResponseTypes.Where(respType => respType.Type == typeof(void)));
Assert.Equal(typeof(void), responseType.Type);
Assert.Equal(204, responseType.StatusCode);
Assert.Null(responseType.ModelMetadata);
Assert.Empty(responseType.ApiResponseFormats);

responseType = Assert.Single(
description.SupportedResponseTypes.Where(respType => respType.Type == typeof(BadData)));
Assert.Equal(400, responseType.StatusCode);
Assert.NotNull(responseType.ModelMetadata);
var apiResponseFormats = responseType.ApiResponseFormats;
Assert.Single(apiResponseFormats.Where(responseFormat => responseFormat.MediaType == "text/json"));
Assert.Single(apiResponseFormats.Where(responseFormat => responseFormat.MediaType == "application/json"));

responseType = Assert.Single(
supportedResponseTypes.Where(responseFormat => responseFormat.Type == typeof(ErrorDetails)));
Assert.Equal(500, responseType.StatusCode);
Assert.NotNull(responseType.ModelMetadata);
apiResponseFormats = responseType.ApiResponseFormats;
Assert.Single(apiResponseFormats.Where(frmt => frmt.MediaType == "text/json"));
Assert.Single(apiResponseFormats.Where(frmt => frmt.MediaType == "application/json"));
Assert.Collection(
description.SupportedResponseTypes.OrderBy(responseType => responseType.StatusCode),
responseType =>
{
Assert.Equal(typeof(void), responseType.Type);
Assert.Equal(204, responseType.StatusCode);
Assert.Null(responseType.ModelMetadata);
Assert.Empty(responseType.ApiResponseFormats);
},
responseType =>
{
Assert.Equal(typeof(BadData), responseType.Type);
Assert.Equal(400, responseType.StatusCode);
Assert.NotNull(responseType.ModelMetadata);
Assert.Equal(expectedMediaTypes, GetSortedMediaTypes(responseType));
},
responseType =>
{
Assert.Equal(typeof(ErrorDetails), responseType.Type);
Assert.Equal(500, responseType.StatusCode);
Assert.NotNull(responseType.ModelMetadata);
Assert.Equal(expectedMediaTypes, GetSortedMediaTypes(responseType));
});
}

[Theory]
Expand Down Expand Up @@ -651,27 +652,23 @@ public void GetApiDescription_IncludesResponseFormats()
{
// Arrange
var action = CreateActionDescriptor(nameof(ReturnsProduct));
var expectedMediaTypes = new[] { "application/json", "application/xml", "text/json", "text/xml" };

// Act
var descriptions = GetApiDescriptions(action);

// Assert
var description = Assert.Single(descriptions);
var responseType = Assert.Single(description.SupportedResponseTypes);
Assert.Collection(
responseType.ApiResponseFormats.OrderBy(responseFormat => responseFormat.MediaType.ToString()),
responseFormat => Assert.Equal("application/json", responseFormat.MediaType.ToString()),
responseFormat => Assert.Equal("application/xml", responseFormat.MediaType.ToString()),
responseFormat => Assert.Equal("text/json", responseFormat.MediaType.ToString()),
responseFormat => Assert.Equal("text/xml", responseFormat.MediaType.ToString()));
Assert.Equal(expectedMediaTypes, GetSortedMediaTypes(responseType));
}

[Fact]
public void GetApiDescription_IncludesResponseFormats_FilteredByAttribute()
{
// Arrange
var action = CreateActionDescriptor(nameof(ReturnsProduct));

var expectedMediaTypes = new[] { "text/json", "text/xml" };
action.FilterDescriptors = new List<FilterDescriptor>();
action.FilterDescriptors.Add(new FilterDescriptor(new ContentTypeAttribute("text/*"), FilterScope.Action));

Expand All @@ -681,10 +678,7 @@ public void GetApiDescription_IncludesResponseFormats_FilteredByAttribute()
// Assert
var description = Assert.Single(descriptions);
var responseType = Assert.Single(description.SupportedResponseTypes);
Assert.Collection(
responseType.ApiResponseFormats.OrderBy(responseFormat => responseFormat.MediaType.ToString()),
responseFormat => Assert.Equal("text/json", responseFormat.MediaType.ToString()),
responseFormat => Assert.Equal("text/xml", responseFormat.MediaType.ToString()));
Assert.Equal(expectedMediaTypes, GetSortedMediaTypes(responseType));
}

[Fact]
Expand Down Expand Up @@ -1376,6 +1370,13 @@ private ControllerActionDescriptor CreateActionDescriptor(string methodName = nu
return action;
}

private IEnumerable<string> GetSortedMediaTypes(ApiResponseType apiResponseType)
{
return apiResponseType.ApiResponseFormats
.OrderBy(responseType => responseType.MediaType)
.Select(responseType => responseType.MediaType);
}

private object ReturnsObject()
{
return null;
Expand Down
Loading

0 comments on commit 80b0069

Please sign in to comment.