This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add UnprocessableEntityResult, UnprocessableEntityObjectResult and ControllerBase.UnprocessableEntity methods #6851
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/Microsoft.AspNetCore.Mvc.Core/UnprocessableEntityObjectResult.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/Microsoft.AspNetCore.Mvc.Core/UnprocessableEntityResult.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
test/Microsoft.AspNetCore.Mvc.Core.Test/UnprocessableEntityObjectResultTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe toss in an error in to the MSD so we know it's the type that's being serialized. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no serialization in the test, and taht's fine. We don't need to overthink it |
||
|
||
// Assert | ||
Assert.Equal(StatusCodes.Status422UnprocessableEntity, result.StatusCode); | ||
var errors = Assert.IsType<SerializableError>(result.Value); | ||
Assert.Empty(errors); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
test/Microsoft.AspNetCore.Mvc.Core.Test/UnprocessableEntityResultTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public virtual UnprocessableEntityResult UnprocessableEntity() => new UnprocessableEntityResult()
for 💯 pointsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are many other methods like this in the same class, it it worth the inconsistency?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's leave this for #6864 😄