[Fixes #5012] ProducesAttribute should not inherit from ResultFilterAttribute
This commit is contained in:
parent
8ac6b6699f
commit
42ae78a360
|
|
@ -19,7 +19,7 @@ namespace Microsoft.AspNetCore.Mvc
|
||||||
/// <see cref="ObjectResult.ContentTypes"/>.
|
/// <see cref="ObjectResult.ContentTypes"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
|
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
|
||||||
public class ProducesAttribute : ResultFilterAttribute, IApiResponseMetadataProvider
|
public class ProducesAttribute : Attribute, IResultFilter, IOrderedFilter, IApiResponseMetadataProvider
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes an instance of <see cref="ProducesAttribute"/>.
|
/// Initializes an instance of <see cref="ProducesAttribute"/>.
|
||||||
|
|
@ -72,14 +72,16 @@ namespace Microsoft.AspNetCore.Mvc
|
||||||
public int StatusCode => StatusCodes.Status200OK;
|
public int StatusCode => StatusCodes.Status200OK;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override void OnResultExecuting(ResultExecutingContext context)
|
public int Order { get; set; }
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public virtual void OnResultExecuting(ResultExecutingContext context)
|
||||||
{
|
{
|
||||||
if (context == null)
|
if (context == null)
|
||||||
{
|
{
|
||||||
throw new ArgumentNullException(nameof(context));
|
throw new ArgumentNullException(nameof(context));
|
||||||
}
|
}
|
||||||
|
|
||||||
base.OnResultExecuting(context);
|
|
||||||
var objectResult = context.Result as ObjectResult;
|
var objectResult = context.Result as ObjectResult;
|
||||||
|
|
||||||
if (objectResult != null)
|
if (objectResult != null)
|
||||||
|
|
@ -100,6 +102,21 @@ namespace Microsoft.AspNetCore.Mvc
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public virtual void OnResultExecuted(ResultExecutedContext context)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public void SetContentTypes(MediaTypeCollection contentTypes)
|
||||||
|
{
|
||||||
|
contentTypes.Clear();
|
||||||
|
foreach (var contentType in ContentTypes)
|
||||||
|
{
|
||||||
|
contentTypes.Add(contentType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private MediaTypeCollection GetContentTypes(string firstArg, string[] args)
|
private MediaTypeCollection GetContentTypes(string firstArg, string[] args)
|
||||||
{
|
{
|
||||||
var completeArgs = new List<string>();
|
var completeArgs = new List<string>();
|
||||||
|
|
@ -121,15 +138,5 @@ namespace Microsoft.AspNetCore.Mvc
|
||||||
|
|
||||||
return contentTypes;
|
return contentTypes;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public void SetContentTypes(MediaTypeCollection contentTypes)
|
|
||||||
{
|
|
||||||
contentTypes.Clear();
|
|
||||||
foreach (var contentType in ContentTypes)
|
|
||||||
{
|
|
||||||
contentTypes.Add(contentType);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"OldTypeId": "public class Microsoft.AspNetCore.Mvc.ProducesAttribute : Microsoft.AspNetCore.Mvc.Filters.ResultFilterAttribute, Microsoft.AspNetCore.Mvc.ApiExplorer.IApiResponseMetadataProvider",
|
||||||
|
"Kind": "Removal"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"OldTypeId": "public class Microsoft.AspNetCore.Mvc.ProducesAttribute : Microsoft.AspNetCore.Mvc.Filters.ResultFilterAttribute, Microsoft.AspNetCore.Mvc.ApiExplorer.IApiResponseMetadataProvider",
|
||||||
|
"Kind": "Removal"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
@ -19,7 +19,7 @@ namespace Microsoft.AspNetCore.Mvc.Test
|
||||||
public class ProducesAttributeTests
|
public class ProducesAttributeTests
|
||||||
{
|
{
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task ProducesAttribute_SetsContentType()
|
public void ProducesAttribute_SetsContentType()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var mediaType1 = new StringSegment("application/json");
|
var mediaType1 = new StringSegment("application/json");
|
||||||
|
|
@ -30,7 +30,7 @@ namespace Microsoft.AspNetCore.Mvc.Test
|
||||||
() => Task.FromResult(CreateResultExecutedContext(resultExecutingContext)));
|
() => Task.FromResult(CreateResultExecutedContext(resultExecutingContext)));
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
await producesContentAttribute.OnResultExecutionAsync(resultExecutingContext, next);
|
producesContentAttribute.OnResultExecuting(resultExecutingContext);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
var objectResult = resultExecutingContext.Result as ObjectResult;
|
var objectResult = resultExecutingContext.Result as ObjectResult;
|
||||||
|
|
@ -40,7 +40,7 @@ namespace Microsoft.AspNetCore.Mvc.Test
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task ProducesContentAttribute_FormatFilterAttribute_NotActive()
|
public void ProducesContentAttribute_FormatFilterAttribute_NotActive()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var producesContentAttribute = new ProducesAttribute("application/xml");
|
var producesContentAttribute = new ProducesAttribute("application/xml");
|
||||||
|
|
@ -57,7 +57,7 @@ namespace Microsoft.AspNetCore.Mvc.Test
|
||||||
() => Task.FromResult(CreateResultExecutedContext(resultExecutingContext)));
|
() => Task.FromResult(CreateResultExecutedContext(resultExecutingContext)));
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
await producesContentAttribute.OnResultExecutionAsync(resultExecutingContext, next);
|
producesContentAttribute.OnResultExecuting(resultExecutingContext);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
var objectResult = Assert.IsType<ObjectResult>(resultExecutingContext.Result);
|
var objectResult = Assert.IsType<ObjectResult>(resultExecutingContext.Result);
|
||||||
|
|
@ -65,7 +65,7 @@ namespace Microsoft.AspNetCore.Mvc.Test
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task ProducesContentAttribute_FormatFilterAttribute_Active()
|
public void ProducesContentAttribute_FormatFilterAttribute_Active()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var producesContentAttribute = new ProducesAttribute("application/xml");
|
var producesContentAttribute = new ProducesAttribute("application/xml");
|
||||||
|
|
@ -82,7 +82,7 @@ namespace Microsoft.AspNetCore.Mvc.Test
|
||||||
() => Task.FromResult(CreateResultExecutedContext(resultExecutingContext)));
|
() => Task.FromResult(CreateResultExecutedContext(resultExecutingContext)));
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
await producesContentAttribute.OnResultExecutionAsync(resultExecutingContext, next);
|
producesContentAttribute.OnResultExecuting(resultExecutingContext);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
var objectResult = Assert.IsType<ObjectResult>(resultExecutingContext.Result);
|
var objectResult = Assert.IsType<ObjectResult>(resultExecutingContext.Result);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue