ProducesResponseTypeAttribute(int statusCode) ctor added
Addresses #4863
This commit is contained in:
parent
d5b0ebd10c
commit
79e576b86c
|
|
@ -13,6 +13,15 @@ namespace Microsoft.AspNetCore.Mvc
|
||||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
|
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
|
||||||
public class ProducesResponseTypeAttribute : Attribute, IApiResponseMetadataProvider
|
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>
|
/// <summary>
|
||||||
/// Initializes an instance of <see cref="ProducesResponseTypeAttribute"/>.
|
/// Initializes an instance of <see cref="ProducesResponseTypeAttribute"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
||||||
|
|
@ -423,12 +423,15 @@ namespace Microsoft.AspNetCore.Mvc.Description
|
||||||
new FilterDescriptor(
|
new FilterDescriptor(
|
||||||
new ProducesAttribute("text/json", "application/json") { Type = typeof(Customer) },
|
new ProducesAttribute("text/json", "application/json") { Type = typeof(Customer) },
|
||||||
FilterScope.Action),
|
FilterScope.Action),
|
||||||
|
new FilterDescriptor(
|
||||||
|
new ProducesResponseTypeAttribute(304),
|
||||||
|
FilterScope.Action),
|
||||||
new FilterDescriptor(
|
new FilterDescriptor(
|
||||||
new ProducesResponseTypeAttribute(typeof(BadData), 400),
|
new ProducesResponseTypeAttribute(typeof(BadData), 400),
|
||||||
FilterScope.Action),
|
FilterScope.Action),
|
||||||
new FilterDescriptor(
|
new FilterDescriptor(
|
||||||
new ProducesResponseTypeAttribute(typeof(ErrorDetails), 500),
|
new ProducesResponseTypeAttribute(typeof(ErrorDetails), 500),
|
||||||
FilterScope.Action)
|
FilterScope.Action),
|
||||||
};
|
};
|
||||||
|
|
||||||
return new TheoryData<Type, string, List<FilterDescriptor>>
|
return new TheoryData<Type, string, List<FilterDescriptor>>
|
||||||
|
|
@ -443,11 +446,16 @@ namespace Microsoft.AspNetCore.Mvc.Description
|
||||||
nameof(DefaultApiDescriptionProviderTest.ReturnsActionResult),
|
nameof(DefaultApiDescriptionProviderTest.ReturnsActionResult),
|
||||||
filterDescriptors
|
filterDescriptors
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
typeof(DefaultApiDescriptionProviderTest),
|
||||||
|
nameof(DefaultApiDescriptionProviderTest.ReturnsActionResult),
|
||||||
|
filterDescriptors
|
||||||
|
},
|
||||||
{
|
{
|
||||||
typeof(DerivedProducesController),
|
typeof(DerivedProducesController),
|
||||||
nameof(DerivedProducesController.ReturnsActionResult),
|
nameof(DerivedProducesController.ReturnsActionResult),
|
||||||
filterDescriptors
|
filterDescriptors
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -469,7 +477,7 @@ namespace Microsoft.AspNetCore.Mvc.Description
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
var description = Assert.Single(descriptions);
|
var description = Assert.Single(descriptions);
|
||||||
Assert.Equal(3, description.SupportedResponseTypes.Count);
|
Assert.Equal(4, description.SupportedResponseTypes.Count);
|
||||||
|
|
||||||
Assert.Collection(
|
Assert.Collection(
|
||||||
description.SupportedResponseTypes.OrderBy(responseType => responseType.StatusCode),
|
description.SupportedResponseTypes.OrderBy(responseType => responseType.StatusCode),
|
||||||
|
|
@ -481,6 +489,13 @@ namespace Microsoft.AspNetCore.Mvc.Description
|
||||||
Assert.Equal(expectedMediaTypes, GetSortedMediaTypes(responseType));
|
Assert.Equal(expectedMediaTypes, GetSortedMediaTypes(responseType));
|
||||||
},
|
},
|
||||||
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(400, responseType.StatusCode);
|
||||||
Assert.Equal(typeof(BadData), responseType.Type);
|
Assert.Equal(typeof(BadData), responseType.Type);
|
||||||
|
|
@ -509,7 +524,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), 200),
|
new ProducesResponseTypeAttribute(200),
|
||||||
FilterScope.Action),
|
FilterScope.Action),
|
||||||
new FilterDescriptor(
|
new FilterDescriptor(
|
||||||
new ProducesResponseTypeAttribute(typeof(BadData), 400),
|
new ProducesResponseTypeAttribute(typeof(BadData), 400),
|
||||||
|
|
|
||||||
|
|
@ -126,7 +126,7 @@ namespace Microsoft.AspNetCore.Mvc.Test
|
||||||
() => new ProducesAttribute(contentTypes[0], contentTypes.Skip(1).ToArray()));
|
() => new ProducesAttribute(contentTypes[0], contentTypes.Skip(1).ToArray()));
|
||||||
|
|
||||||
Assert.Equal(
|
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.",
|
"Media types which match all types or match all subtypes are not supported.",
|
||||||
invalidContentType),
|
invalidContentType),
|
||||||
ex.Message);
|
ex.Message);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue