Add UnprocessableEntityResult, UnprocessableEntityObjectResult and ControllerBase.UnprocessableEntity methods (#6851)
* Added UnprocessableEntityResult * Added UnprocessableEntityObjectResult * Added UnprocessableEntity overloads to ControllerBase Fixes https://github.com/aspnet/Mvc/issues/6795
This commit is contained in:
parent
197ef139d6
commit
97fab8711a
|
|
@ -1400,6 +1400,7 @@ namespace Microsoft.AspNetCore.Mvc
|
|||
/// <summary>
|
||||
/// Creates an <see cref="BadRequestObjectResult"/> that produces a <see cref="StatusCodes.Status400BadRequest"/> response.
|
||||
/// </summary>
|
||||
/// <param name="error">An error object to be returned to the client.</param>
|
||||
/// <returns>The created <see cref="BadRequestObjectResult"/> for the response.</returns>
|
||||
[NonAction]
|
||||
public virtual BadRequestObjectResult BadRequest(object error)
|
||||
|
|
@ -1408,6 +1409,7 @@ namespace Microsoft.AspNetCore.Mvc
|
|||
/// <summary>
|
||||
/// Creates an <see cref="BadRequestObjectResult"/> that produces a <see cref="StatusCodes.Status400BadRequest"/> response.
|
||||
/// </summary>
|
||||
/// <param name="modelState">The model state dictionary containing errors to be returned to the client.</param>
|
||||
/// <returns>The created <see cref="BadRequestObjectResult"/> for the response.</returns>
|
||||
[NonAction]
|
||||
public virtual BadRequestObjectResult BadRequest(ModelStateDictionary modelState)
|
||||
|
|
@ -1420,6 +1422,43 @@ namespace Microsoft.AspNetCore.Mvc
|
|||
return new BadRequestObjectResult(modelState);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates an <see cref="UnprocessableEntityResult"/> that produces a <see cref="StatusCodes.Status422UnprocessableEntity"/> response.
|
||||
/// </summary>
|
||||
/// <returns>The created <see cref="UnprocessableEntityResult"/> for the response.</returns>
|
||||
[NonAction]
|
||||
public virtual UnprocessableEntityResult UnprocessableEntity()
|
||||
{
|
||||
return new UnprocessableEntityResult();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates an <see cref="UnprocessableEntityObjectResult"/> that produces a <see cref="StatusCodes.Status422UnprocessableEntity"/> response.
|
||||
/// </summary>
|
||||
/// <param name="error">An error object to be returned to the client.</param>
|
||||
/// <returns>The created <see cref="UnprocessableEntityObjectResult"/> for the response.</returns>
|
||||
[NonAction]
|
||||
public virtual UnprocessableEntityObjectResult UnprocessableEntity(object error)
|
||||
{
|
||||
return new UnprocessableEntityObjectResult(error);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates an <see cref="UnprocessableEntityObjectResult"/> that produces a <see cref="StatusCodes.Status422UnprocessableEntity"/> response.
|
||||
/// </summary>
|
||||
/// <param name="modelState">The model state dictionary containing errors to be returned to the client.</param>
|
||||
/// <returns>The created <see cref="UnprocessableEntityObjectResult"/> for the response.</returns>
|
||||
[NonAction]
|
||||
public virtual UnprocessableEntityObjectResult UnprocessableEntity(ModelStateDictionary modelState)
|
||||
{
|
||||
if (modelState == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(modelState));
|
||||
}
|
||||
|
||||
return new UnprocessableEntityObjectResult(modelState);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates an <see cref="BadRequestObjectResult"/> that produces a <see cref="StatusCodes.Status400BadRequest"/> response.
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,33 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
|
||||
namespace Microsoft.AspNetCore.Mvc
|
||||
{
|
||||
/// <summary>
|
||||
/// An <see cref="ObjectResult"/> that when executed will produce a Unprocessable Entity (422) response.
|
||||
/// </summary>
|
||||
public class UnprocessableEntityObjectResult : ObjectResult
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="UnprocessableEntityObjectResult"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="modelState"><see cref="ModelStateDictionary"/> containing the validation errors.</param>
|
||||
public UnprocessableEntityObjectResult(ModelStateDictionary modelState)
|
||||
: this(new SerializableError(modelState))
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="UnprocessableEntityObjectResult"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="error">Contains errors to be returned to the client.</param>
|
||||
public UnprocessableEntityObjectResult(object error)
|
||||
: base(error)
|
||||
{
|
||||
StatusCode = StatusCodes.Status422UnprocessableEntity;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace Microsoft.AspNetCore.Mvc
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see cref="StatusCodeResult"/> that when
|
||||
/// executed will produce a Unprocessable Entity (422) response.
|
||||
/// </summary>
|
||||
public class UnprocessableEntityResult : StatusCodeResult
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="UnprocessableEntityResult"/> instance.
|
||||
/// </summary>
|
||||
public UnprocessableEntityResult()
|
||||
: base(StatusCodes.Status422UnprocessableEntity)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1990,6 +1990,52 @@ namespace Microsoft.AspNetCore.Mvc.Core.Test
|
|||
Assert.Empty(errors);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UnprocessableEntity_SetsStatusCode()
|
||||
{
|
||||
// Arrange
|
||||
var controller = new TestableController();
|
||||
|
||||
// Act
|
||||
var result = controller.UnprocessableEntity();
|
||||
|
||||
// Assert
|
||||
Assert.IsType<UnprocessableEntityResult>(result);
|
||||
Assert.Equal(StatusCodes.Status422UnprocessableEntity, result.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UnprocessableEntity_SetsStatusCodeAndValue_Object()
|
||||
{
|
||||
// Arrange
|
||||
var controller = new TestableController();
|
||||
var obj = new object();
|
||||
|
||||
// Act
|
||||
var result = controller.UnprocessableEntity(obj);
|
||||
|
||||
// Assert
|
||||
Assert.IsType<UnprocessableEntityObjectResult>(result);
|
||||
Assert.Equal(StatusCodes.Status422UnprocessableEntity, result.StatusCode);
|
||||
Assert.Equal(obj, result.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UnprocessableEntity_SetsStatusCodeAndValue_ModelState()
|
||||
{
|
||||
// Arrange
|
||||
var controller = new TestableController();
|
||||
|
||||
// Act
|
||||
var result = controller.UnprocessableEntity(new ModelStateDictionary());
|
||||
|
||||
// Assert
|
||||
Assert.IsType<UnprocessableEntityObjectResult>(result);
|
||||
Assert.Equal(StatusCodes.Status422UnprocessableEntity, result.StatusCode);
|
||||
var errors = Assert.IsType<SerializableError>(result.Value);
|
||||
Assert.Empty(errors);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(PublicNormalMethodsFromControllerBase))]
|
||||
public void NonActionAttribute_IsOnEveryPublicNormalMethodFromControllerBase(MethodInfo method)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,36 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNetCore.Mvc
|
||||
{
|
||||
public class UnprocessableEntityObjectResultTests
|
||||
{
|
||||
[Fact]
|
||||
public void UnprocessableEntityObjectResult_SetsStatusCodeAndValue()
|
||||
{
|
||||
// Arrange & Act
|
||||
var obj = new object();
|
||||
var result = new UnprocessableEntityObjectResult(obj);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(StatusCodes.Status422UnprocessableEntity, result.StatusCode);
|
||||
Assert.Equal(obj, result.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UnprocessableEntityObjectResult_ModelState_SetsStatusCodeAndValue()
|
||||
{
|
||||
// Arrange & Act
|
||||
var result = new UnprocessableEntityObjectResult(new ModelStateDictionary());
|
||||
|
||||
// Assert
|
||||
Assert.Equal(StatusCodes.Status422UnprocessableEntity, result.StatusCode);
|
||||
var errors = Assert.IsType<SerializableError>(result.Value);
|
||||
Assert.Empty(errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNetCore.Mvc
|
||||
{
|
||||
public class UnprocessableEntityResultTests
|
||||
{
|
||||
[Fact]
|
||||
public void UnprocessableEntityResult_InitializesStatusCode()
|
||||
{
|
||||
// Arrange & act
|
||||
var result = new UnprocessableEntityResult();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(StatusCodes.Status422UnprocessableEntity, result.StatusCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue