-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Add ProblemDetails helpers to Results #34206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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
45 changes: 45 additions & 0 deletions
45
src/Http/Http.Extensions/src/HttpValidationProblemDetails.cs
This file contains hidden or 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,45 @@ | ||
| // 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 System; | ||
| using System.Collections.Generic; | ||
| using System.Text.Json.Serialization; | ||
| using Microsoft.AspNetCore.Mvc; | ||
|
|
||
| namespace Microsoft.AspNetCore.Http.Extensions | ||
| { | ||
| /// <summary> | ||
| /// A <see cref="ProblemDetails"/> for validation errors. | ||
| /// </summary> | ||
| [JsonConverter(typeof(HttpValidationProblemDetailsJsonConverter))] | ||
| public class HttpValidationProblemDetails : ProblemDetails | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of <see cref="HttpValidationProblemDetails"/>. | ||
| /// </summary> | ||
| public HttpValidationProblemDetails() | ||
| : this(new Dictionary<string, string[]>(StringComparer.Ordinal)) | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of <see cref="HttpValidationProblemDetails"/> using the specified <paramref name="errors"/>. | ||
| /// </summary> | ||
| /// <param name="errors">The validation errors.</param> | ||
| public HttpValidationProblemDetails(IDictionary<string, string[]> errors) | ||
| : this(new Dictionary<string, string[]>(errors, StringComparer.Ordinal)) | ||
| { | ||
| } | ||
|
|
||
| private HttpValidationProblemDetails(Dictionary<string, string[]> errors) | ||
| { | ||
| Title = "One or more validation errors occurred."; | ||
| Errors = errors; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the validation errors associated with this instance of <see cref="HttpValidationProblemDetails"/>. | ||
| /// </summary> | ||
| public IDictionary<string, string[]> Errors { get; } = new Dictionary<string, string[]>(StringComparer.Ordinal); | ||
| } | ||
| } | ||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
140 changes: 140 additions & 0 deletions
140
src/Http/Http.Extensions/test/HttpValidationProblemDetailsJsonConverterTest.cs
This file contains hidden or 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,140 @@ | ||
| // 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 System.Linq; | ||
| using System.Text; | ||
| using System.Text.Json; | ||
| using Microsoft.AspNetCore.Http.Json; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.AspNetCore.Http.Extensions | ||
| { | ||
| public class HttpValidationProblemDetailsJsonConverterTest | ||
| { | ||
| private static JsonSerializerOptions JsonSerializerOptions => new JsonOptions().SerializerOptions; | ||
|
|
||
| [Fact] | ||
| public void Read_Works() | ||
| { | ||
| // Arrange | ||
| var type = "https://tools.ietf.org/html/rfc7231#section-6.5.4"; | ||
| var title = "Not found"; | ||
| var status = 404; | ||
| var detail = "Product not found"; | ||
| var instance = "http://example.com/products/14"; | ||
| var traceId = "|37dd3dd5-4a9619f953c40a16."; | ||
| var json = $"{{\"type\":\"{type}\",\"title\":\"{title}\",\"status\":{status},\"detail\":\"{detail}\", \"instance\":\"{instance}\",\"traceId\":\"{traceId}\"," + | ||
| "\"errors\":{\"key0\":[\"error0\"],\"key1\":[\"error1\",\"error2\"]}}"; | ||
| var converter = new HttpValidationProblemDetailsJsonConverter(); | ||
| var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(json)); | ||
| reader.Read(); | ||
|
|
||
| // Act | ||
| var problemDetails = converter.Read(ref reader, typeof(HttpValidationProblemDetails), JsonSerializerOptions); | ||
|
|
||
| Assert.Equal(type, problemDetails.Type); | ||
| Assert.Equal(title, problemDetails.Title); | ||
| Assert.Equal(status, problemDetails.Status); | ||
| Assert.Equal(instance, problemDetails.Instance); | ||
| Assert.Equal(detail, problemDetails.Detail); | ||
| Assert.Collection( | ||
| problemDetails.Extensions, | ||
| kvp => | ||
| { | ||
| Assert.Equal("traceId", kvp.Key); | ||
| Assert.Equal(traceId, kvp.Value.ToString()); | ||
| }); | ||
| Assert.Collection( | ||
| problemDetails.Errors.OrderBy(kvp => kvp.Key), | ||
| kvp => | ||
| { | ||
| Assert.Equal("key0", kvp.Key); | ||
| Assert.Equal(new[] { "error0" }, kvp.Value); | ||
| }, | ||
| kvp => | ||
| { | ||
| Assert.Equal("key1", kvp.Key); | ||
| Assert.Equal(new[] { "error1", "error2" }, kvp.Value); | ||
| }); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Read_WithSomeMissingValues_Works() | ||
| { | ||
| // Arrange | ||
| var type = "https://tools.ietf.org/html/rfc7231#section-6.5.4"; | ||
| var title = "Not found"; | ||
| var status = 404; | ||
| var traceId = "|37dd3dd5-4a9619f953c40a16."; | ||
| var json = $"{{\"type\":\"{type}\",\"title\":\"{title}\",\"status\":{status},\"traceId\":\"{traceId}\"," + | ||
| "\"errors\":{\"key0\":[\"error0\"],\"key1\":[\"error1\",\"error2\"]}}"; | ||
| var converter = new HttpValidationProblemDetailsJsonConverter(); | ||
| var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(json)); | ||
| reader.Read(); | ||
|
|
||
| // Act | ||
| var problemDetails = converter.Read(ref reader, typeof(HttpValidationProblemDetails), JsonSerializerOptions); | ||
|
|
||
| Assert.Equal(type, problemDetails.Type); | ||
| Assert.Equal(title, problemDetails.Title); | ||
| Assert.Equal(status, problemDetails.Status); | ||
| Assert.Collection( | ||
| problemDetails.Extensions, | ||
| kvp => | ||
| { | ||
| Assert.Equal("traceId", kvp.Key); | ||
| Assert.Equal(traceId, kvp.Value.ToString()); | ||
| }); | ||
| Assert.Collection( | ||
| problemDetails.Errors.OrderBy(kvp => kvp.Key), | ||
| kvp => | ||
| { | ||
| Assert.Equal("key0", kvp.Key); | ||
| Assert.Equal(new[] { "error0" }, kvp.Value); | ||
| }, | ||
| kvp => | ||
| { | ||
| Assert.Equal("key1", kvp.Key); | ||
| Assert.Equal(new[] { "error1", "error2" }, kvp.Value); | ||
| }); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ReadUsingJsonSerializerWorks() | ||
| { | ||
| // Arrange | ||
| var type = "https://tools.ietf.org/html/rfc7231#section-6.5.4"; | ||
| var title = "Not found"; | ||
| var status = 404; | ||
| var traceId = "|37dd3dd5-4a9619f953c40a16."; | ||
| var json = $"{{\"type\":\"{type}\",\"title\":\"{title}\",\"status\":{status},\"traceId\":\"{traceId}\"," + | ||
| "\"errors\":{\"key0\":[\"error0\"],\"key1\":[\"error1\",\"error2\"]}}"; | ||
|
|
||
| // Act | ||
| var problemDetails = JsonSerializer.Deserialize<HttpValidationProblemDetails>(json, JsonSerializerOptions); | ||
|
|
||
| Assert.Equal(type, problemDetails.Type); | ||
| Assert.Equal(title, problemDetails.Title); | ||
| Assert.Equal(status, problemDetails.Status); | ||
| Assert.Collection( | ||
| problemDetails.Extensions, | ||
| kvp => | ||
| { | ||
| Assert.Equal("traceId", kvp.Key); | ||
| Assert.Equal(traceId, kvp.Value.ToString()); | ||
| }); | ||
| Assert.Collection( | ||
| problemDetails.Errors.OrderBy(kvp => kvp.Key), | ||
| kvp => | ||
| { | ||
| Assert.Equal("key0", kvp.Key); | ||
| Assert.Equal(new[] { "error0" }, kvp.Value); | ||
| }, | ||
| kvp => | ||
| { | ||
| Assert.Equal("key1", kvp.Key); | ||
| Assert.Equal(new[] { "error1", "error2" }, kvp.Value); | ||
| }); | ||
| } | ||
| } | ||
| } |
8 changes: 5 additions & 3 deletions
8
...ucture/ProblemDetailsJsonConverterTest.cs → ...s/test/ProblemDetailsJsonConverterTest.cs
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
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.
sealed?
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.
namespace Microsoft.AspNetCore.Mvc { - public class ValidationProblemDetails : ProblemDetails + public class ValidationProblemDetails : HttpValidationProblemDetails { } }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.
I'm open to not doing this - it just felt like a good way to help move the feature over to Http.Extensions.
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.
I was originally thinking we would have ProblemDetails derive from something in the core ProblemDetailsBase.