[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"/>.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
|
||||
public class ProducesAttribute : ResultFilterAttribute, IApiResponseMetadataProvider
|
||||
public class ProducesAttribute : Attribute, IResultFilter, IOrderedFilter, IApiResponseMetadataProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes an instance of <see cref="ProducesAttribute"/>.
|
||||
|
|
@ -72,14 +72,16 @@ namespace Microsoft.AspNetCore.Mvc
|
|||
public int StatusCode => StatusCodes.Status200OK;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void OnResultExecuting(ResultExecutingContext context)
|
||||
public int Order { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual void OnResultExecuting(ResultExecutingContext context)
|
||||
{
|
||||
if (context == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(context));
|
||||
}
|
||||
|
||||
base.OnResultExecuting(context);
|
||||
var objectResult = context.Result as ObjectResult;
|
||||
|
||||
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)
|
||||
{
|
||||
var completeArgs = new List<string>();
|
||||
|
|
@ -121,15 +138,5 @@ namespace Microsoft.AspNetCore.Mvc
|
|||
|
||||
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
|
||||
{
|
||||
[Fact]
|
||||
public async Task ProducesAttribute_SetsContentType()
|
||||
public void ProducesAttribute_SetsContentType()
|
||||
{
|
||||
// Arrange
|
||||
var mediaType1 = new StringSegment("application/json");
|
||||
|
|
@ -30,7 +30,7 @@ namespace Microsoft.AspNetCore.Mvc.Test
|
|||
() => Task.FromResult(CreateResultExecutedContext(resultExecutingContext)));
|
||||
|
||||
// Act
|
||||
await producesContentAttribute.OnResultExecutionAsync(resultExecutingContext, next);
|
||||
producesContentAttribute.OnResultExecuting(resultExecutingContext);
|
||||
|
||||
// Assert
|
||||
var objectResult = resultExecutingContext.Result as ObjectResult;
|
||||
|
|
@ -40,7 +40,7 @@ namespace Microsoft.AspNetCore.Mvc.Test
|
|||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ProducesContentAttribute_FormatFilterAttribute_NotActive()
|
||||
public void ProducesContentAttribute_FormatFilterAttribute_NotActive()
|
||||
{
|
||||
// Arrange
|
||||
var producesContentAttribute = new ProducesAttribute("application/xml");
|
||||
|
|
@ -57,7 +57,7 @@ namespace Microsoft.AspNetCore.Mvc.Test
|
|||
() => Task.FromResult(CreateResultExecutedContext(resultExecutingContext)));
|
||||
|
||||
// Act
|
||||
await producesContentAttribute.OnResultExecutionAsync(resultExecutingContext, next);
|
||||
producesContentAttribute.OnResultExecuting(resultExecutingContext);
|
||||
|
||||
// Assert
|
||||
var objectResult = Assert.IsType<ObjectResult>(resultExecutingContext.Result);
|
||||
|
|
@ -65,7 +65,7 @@ namespace Microsoft.AspNetCore.Mvc.Test
|
|||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ProducesContentAttribute_FormatFilterAttribute_Active()
|
||||
public void ProducesContentAttribute_FormatFilterAttribute_Active()
|
||||
{
|
||||
// Arrange
|
||||
var producesContentAttribute = new ProducesAttribute("application/xml");
|
||||
|
|
@ -82,7 +82,7 @@ namespace Microsoft.AspNetCore.Mvc.Test
|
|||
() => Task.FromResult(CreateResultExecutedContext(resultExecutingContext)));
|
||||
|
||||
// Act
|
||||
await producesContentAttribute.OnResultExecutionAsync(resultExecutingContext, next);
|
||||
producesContentAttribute.OnResultExecuting(resultExecutingContext);
|
||||
|
||||
// Assert
|
||||
var objectResult = Assert.IsType<ObjectResult>(resultExecutingContext.Result);
|
||||
|
|
|
|||
Loading…
Reference in New Issue