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

Commit

Permalink
ProducesResponseTypeAttribute(int statusCode) ctor added
Browse files Browse the repository at this point in the history
Addresses #4863
  • Loading branch information
jbagga authored Oct 20, 2016
1 parent d5b0ebd commit 79e576b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ namespace Microsoft.AspNetCore.Mvc
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class ProducesResponseTypeAttribute : Attribute, IApiResponseMetadataProvider
{
/// <summary>
/// Initializes an instance of <see cref="ProducesResponseTypeAttribute"/>.
/// </summary>
/// <param name="statusCode">The HTTP response status code.</param>
public ProducesResponseTypeAttribute(int statusCode)
: this(typeof(void), statusCode)
{
}

/// <summary>
/// Initializes an instance of <see cref="ProducesResponseTypeAttribute"/>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,12 +423,15 @@ public static TheoryData ReturnsActionResultWithProducesAndProducesContentTypeDa
new FilterDescriptor(
new ProducesAttribute("text/json", "application/json") { Type = typeof(Customer) },
FilterScope.Action),
new FilterDescriptor(
new ProducesResponseTypeAttribute(304),
FilterScope.Action),
new FilterDescriptor(
new ProducesResponseTypeAttribute(typeof(BadData), 400),
FilterScope.Action),
new FilterDescriptor(
new ProducesResponseTypeAttribute(typeof(ErrorDetails), 500),
FilterScope.Action)
FilterScope.Action),
};

return new TheoryData<Type, string, List<FilterDescriptor>>
Expand All @@ -443,11 +446,16 @@ public static TheoryData ReturnsActionResultWithProducesAndProducesContentTypeDa
nameof(DefaultApiDescriptionProviderTest.ReturnsActionResult),
filterDescriptors
},
{
typeof(DefaultApiDescriptionProviderTest),
nameof(DefaultApiDescriptionProviderTest.ReturnsActionResult),
filterDescriptors
},
{
typeof(DerivedProducesController),
nameof(DerivedProducesController.ReturnsActionResult),
filterDescriptors
},
}
};
}
}
Expand All @@ -469,7 +477,7 @@ public void GetApiDescription_ReturnsActionResultWithProduces_And_ProducesConten

// Assert
var description = Assert.Single(descriptions);
Assert.Equal(3, description.SupportedResponseTypes.Count);
Assert.Equal(4, description.SupportedResponseTypes.Count);

Assert.Collection(
description.SupportedResponseTypes.OrderBy(responseType => responseType.StatusCode),
Expand All @@ -481,6 +489,13 @@ public void GetApiDescription_ReturnsActionResultWithProduces_And_ProducesConten
Assert.Equal(expectedMediaTypes, GetSortedMediaTypes(responseType));
},
responseType =>
{
Assert.Equal(304, responseType.StatusCode);
Assert.Equal(typeof(void), responseType.Type);
Assert.Null(responseType.ModelMetadata);
Assert.Empty(responseType.ApiResponseFormats);
},
responseType =>
{
Assert.Equal(400, responseType.StatusCode);
Assert.Equal(typeof(BadData), responseType.Type);
Expand Down Expand Up @@ -509,7 +524,7 @@ public static TheoryData<Type, string, List<FilterDescriptor>> ReturnsVoidOrTask
new ProducesAttribute("text/json", "application/json"),
FilterScope.Action),
new FilterDescriptor(
new ProducesResponseTypeAttribute(typeof(void), 200),
new ProducesResponseTypeAttribute(200),
FilterScope.Action),
new FilterDescriptor(
new ProducesResponseTypeAttribute(typeof(BadData), 400),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void ProducesAttribute_InvalidContentType_Throws(string content, string i
() => new ProducesAttribute(contentTypes[0], contentTypes.Skip(1).ToArray()));

Assert.Equal(
string.Format("The argument '{0}' is invalid. "+
string.Format("The argument '{0}' is invalid. " +
"Media types which match all types or match all subtypes are not supported.",
invalidContentType),
ex.Message);
Expand Down

0 comments on commit 79e576b

Please sign in to comment.