Skip to content

Commit 7f9b45e

Browse files
authored
Add overloads for problem and validation problem results that accept IEnumerable<KeyValuePair<,>> (#56368)
1 parent c6de659 commit 7f9b45e

File tree

8 files changed

+264
-17
lines changed

8 files changed

+264
-17
lines changed

src/Http/Http.Abstractions/src/ProblemDetails/HttpValidationProblemDetails.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ public HttpValidationProblemDetails()
2424
/// </summary>
2525
/// <param name="errors">The validation errors.</param>
2626
public HttpValidationProblemDetails(IDictionary<string, string[]> errors)
27+
: this((IEnumerable<KeyValuePair<string, string[]>>)errors)
28+
{
29+
}
30+
31+
/// <summary>
32+
/// Initializes a new instance of <see cref="HttpValidationProblemDetails"/> using the specified <paramref name="errors"/>.
33+
/// </summary>
34+
/// <param name="errors">The validation errors.</param>
35+
public HttpValidationProblemDetails(IEnumerable<KeyValuePair<string, string[]>> errors)
2736
: this(new Dictionary<string, string[]>(errors ?? throw new ArgumentNullException(nameof(errors)), StringComparer.Ordinal))
2837
{
2938
}

src/Http/Http.Abstractions/src/PublicAPI.Unshipped.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
Microsoft.AspNetCore.Http.HostString.HostString(string? value) -> void
44
*REMOVED*Microsoft.AspNetCore.Http.HostString.Value.get -> string!
55
Microsoft.AspNetCore.Http.HostString.Value.get -> string?
6+
Microsoft.AspNetCore.Http.HttpValidationProblemDetails.HttpValidationProblemDetails(System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string!, string![]!>>! errors) -> void
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
#nullable enable
2+
Microsoft.AspNetCore.Http.HttpValidationProblemDetails.HttpValidationProblemDetails(System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string!, string![]!>>! errors) -> void (forwarded, contained in Microsoft.AspNetCore.Http.Abstractions)

src/Http/Http.Results/src/PublicAPI.Unshipped.txt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,17 @@ Microsoft.AspNetCore.Http.HttpResults.InternalServerError<TValue>.StatusCode.get
88
Microsoft.AspNetCore.Http.HttpResults.InternalServerError<TValue>.Value.get -> TValue?
99
static Microsoft.AspNetCore.Http.Results.InternalServerError() -> Microsoft.AspNetCore.Http.IResult!
1010
static Microsoft.AspNetCore.Http.Results.InternalServerError<TValue>(TValue? error) -> Microsoft.AspNetCore.Http.IResult!
11+
static Microsoft.AspNetCore.Http.Results.Problem(string? detail = null, string? instance = null, int? statusCode = null, string? title = null, string? type = null, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string!, object?>>? extensions = null) -> Microsoft.AspNetCore.Http.IResult!
12+
*REMOVED*static Microsoft.AspNetCore.Http.Results.Problem(string? detail = null, string? instance = null, int? statusCode = null, string? title = null, string? type = null, System.Collections.Generic.IDictionary<string!, object?>? extensions = null) -> Microsoft.AspNetCore.Http.IResult!
13+
static Microsoft.AspNetCore.Http.Results.Problem(string? detail, string? instance, int? statusCode, string? title, string? type, System.Collections.Generic.IDictionary<string!, object?>? extensions) -> Microsoft.AspNetCore.Http.IResult!
14+
*REMOVED*static Microsoft.AspNetCore.Http.Results.ValidationProblem(System.Collections.Generic.IDictionary<string!, string![]!>! errors, string? detail = null, string? instance = null, int? statusCode = null, string? title = null, string? type = null, System.Collections.Generic.IDictionary<string!, object?>? extensions = null) -> Microsoft.AspNetCore.Http.IResult!
15+
static Microsoft.AspNetCore.Http.Results.ValidationProblem(System.Collections.Generic.IDictionary<string!, string![]!>! errors, string? detail, string? instance, int? statusCode, string? title, string? type, System.Collections.Generic.IDictionary<string!, object?>? extensions) -> Microsoft.AspNetCore.Http.IResult!
16+
static Microsoft.AspNetCore.Http.Results.ValidationProblem(System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string!, string![]!>>! errors, string? detail = null, string? instance = null, int? statusCode = null, string? title = null, string? type = null, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string!, object?>>? extensions = null) -> Microsoft.AspNetCore.Http.IResult!
1117
static Microsoft.AspNetCore.Http.TypedResults.InternalServerError() -> Microsoft.AspNetCore.Http.HttpResults.InternalServerError!
12-
static Microsoft.AspNetCore.Http.TypedResults.InternalServerError<TValue>(TValue? error) -> Microsoft.AspNetCore.Http.HttpResults.InternalServerError<TValue>!
18+
static Microsoft.AspNetCore.Http.TypedResults.InternalServerError<TValue>(TValue? error) -> Microsoft.AspNetCore.Http.HttpResults.InternalServerError<TValue>!
19+
static Microsoft.AspNetCore.Http.TypedResults.Problem(string? detail = null, string? instance = null, int? statusCode = null, string? title = null, string? type = null, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string!, object?>>? extensions = null) -> Microsoft.AspNetCore.Http.HttpResults.ProblemHttpResult!
20+
*REMOVED*static Microsoft.AspNetCore.Http.TypedResults.Problem(string? detail = null, string? instance = null, int? statusCode = null, string? title = null, string? type = null, System.Collections.Generic.IDictionary<string!, object?>? extensions = null) -> Microsoft.AspNetCore.Http.HttpResults.ProblemHttpResult!
21+
static Microsoft.AspNetCore.Http.TypedResults.Problem(string? detail, string? instance, int? statusCode, string? title, string? type, System.Collections.Generic.IDictionary<string!, object?>? extensions) -> Microsoft.AspNetCore.Http.HttpResults.ProblemHttpResult!
22+
*REMOVED*static Microsoft.AspNetCore.Http.TypedResults.ValidationProblem(System.Collections.Generic.IDictionary<string!, string![]!>! errors, string? detail = null, string? instance = null, string? title = null, string? type = null, System.Collections.Generic.IDictionary<string!, object?>? extensions = null) -> Microsoft.AspNetCore.Http.HttpResults.ValidationProblem!
23+
static Microsoft.AspNetCore.Http.TypedResults.ValidationProblem(System.Collections.Generic.IDictionary<string!, string![]!>! errors, string? detail, string? instance, string? title, string? type, System.Collections.Generic.IDictionary<string!, object?>? extensions) -> Microsoft.AspNetCore.Http.HttpResults.ValidationProblem!
24+
static Microsoft.AspNetCore.Http.TypedResults.ValidationProblem(System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string!, string![]!>>! errors, string? detail = null, string? instance = null, string? title = null, string? type = null, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string!, object?>>? extensions = null) -> Microsoft.AspNetCore.Http.HttpResults.ValidationProblem!

src/Http/Http.Results/src/Results.cs

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -712,12 +712,33 @@ public static IResult InternalServerError<TValue>(TValue? error)
712712
/// <param name="extensions">The value for <see cref="ProblemDetails.Extensions" />.</param>
713713
/// <returns>The created <see cref="IResult"/> for the response.</returns>
714714
public static IResult Problem(
715+
string? detail,
716+
string? instance,
717+
int? statusCode,
718+
string? title,
719+
string? type,
720+
IDictionary<string, object?>? extensions)
721+
=> TypedResults.Problem(detail, instance, statusCode, title, type, extensions);
722+
723+
/// <summary>
724+
/// Produces a <see cref="ProblemDetails"/> response.
725+
/// </summary>
726+
/// <param name="statusCode">The value for <see cref="ProblemDetails.Status" />.</param>
727+
/// <param name="detail">The value for <see cref="ProblemDetails.Detail" />.</param>
728+
/// <param name="instance">The value for <see cref="ProblemDetails.Instance" />.</param>
729+
/// <param name="title">The value for <see cref="ProblemDetails.Title" />.</param>
730+
/// <param name="type">The value for <see cref="ProblemDetails.Type" />.</param>
731+
/// <param name="extensions">The value for <see cref="ProblemDetails.Extensions" />.</param>
732+
/// <returns>The created <see cref="IResult"/> for the response.</returns>
733+
#pragma warning disable RS0027 // Public API with optional parameter(s) should have the most parameters amongst its public overloads
734+
public static IResult Problem(
735+
#pragma warning restore RS0027 // Public API with optional parameter(s) should have the most parameters amongst its public overloads
715736
string? detail = null,
716737
string? instance = null,
717738
int? statusCode = null,
718739
string? title = null,
719740
string? type = null,
720-
IDictionary<string, object?>? extensions = null)
741+
IEnumerable<KeyValuePair<string, object?>>? extensions = null)
721742
=> TypedResults.Problem(detail, instance, statusCode, title, type, extensions);
722743

723744
/// <summary>
@@ -742,13 +763,41 @@ public static IResult Problem(ProblemDetails problemDetails)
742763
/// <returns>The created <see cref="IResult"/> for the response.</returns>
743764
public static IResult ValidationProblem(
744765
IDictionary<string, string[]> errors,
766+
string? detail,
767+
string? instance,
768+
int? statusCode,
769+
string? title,
770+
string? type,
771+
IDictionary<string, object?>? extensions)
772+
{
773+
return ValidationProblem(errors, detail, instance, statusCode, title, type, (IEnumerable<KeyValuePair<string, object?>>?)extensions);
774+
}
775+
776+
/// <summary>
777+
/// Produces a <see cref="StatusCodes.Status400BadRequest"/> response
778+
/// with a <see cref="HttpValidationProblemDetails"/> value.
779+
/// </summary>
780+
/// <param name="errors">One or more validation errors.</param>
781+
/// <param name="detail">The value for <see cref="ProblemDetails.Detail" />.</param>
782+
/// <param name="instance">The value for <see cref="ProblemDetails.Instance" />.</param>
783+
/// <param name="statusCode">The status code.</param>
784+
/// <param name="title">The value for <see cref="ProblemDetails.Title" />. Defaults to "One or more validation errors occurred."</param>
785+
/// <param name="type">The value for <see cref="ProblemDetails.Type" />.</param>
786+
/// <param name="extensions">The value for <see cref="ProblemDetails.Extensions" />.</param>
787+
/// <returns>The created <see cref="IResult"/> for the response.</returns>
788+
#pragma warning disable RS0027 // Public API with optional parameter(s) should have the most parameters amongst its public overloads
789+
public static IResult ValidationProblem(
790+
#pragma warning restore RS0027 // Public API with optional parameter(s) should have the most parameters amongst its public overloads
791+
IEnumerable<KeyValuePair<string, string[]>> errors,
745792
string? detail = null,
746793
string? instance = null,
747794
int? statusCode = null,
748795
string? title = null,
749796
string? type = null,
750-
IDictionary<string, object?>? extensions = null)
797+
IEnumerable<KeyValuePair<string, object?>>? extensions = null)
751798
{
799+
ArgumentNullException.ThrowIfNull(errors);
800+
752801
// TypedResults.ValidationProblem() does not allow setting the statusCode so we do this manually here
753802
var problemDetails = new HttpValidationProblemDetails(errors)
754803
{
@@ -765,7 +814,7 @@ public static IResult ValidationProblem(
765814
return TypedResults.Problem(problemDetails);
766815
}
767816

768-
private static void CopyExtensions(IDictionary<string, object?>? extensions, HttpValidationProblemDetails problemDetails)
817+
private static void CopyExtensions(IEnumerable<KeyValuePair<string, object?>>? extensions, HttpValidationProblemDetails problemDetails)
769818
{
770819
if (extensions is not null)
771820
{

src/Http/Http.Results/src/TypedResults.cs

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -755,12 +755,35 @@ public static StatusCodeHttpResult StatusCode(int statusCode)
755755
/// <param name="extensions">The value for <see cref="ProblemDetails.Extensions" />.</param>
756756
/// <returns>The created <see cref="ProblemHttpResult"/> for the response.</returns>
757757
public static ProblemHttpResult Problem(
758+
string? detail,
759+
string? instance,
760+
int? statusCode,
761+
string? title,
762+
string? type,
763+
IDictionary<string, object?>? extensions)
764+
{
765+
return Problem(detail, instance, statusCode, title, type, (IEnumerable<KeyValuePair<string, object?>>?)extensions);
766+
}
767+
768+
/// <summary>
769+
/// Produces a <see cref="ProblemDetails"/> response.
770+
/// </summary>
771+
/// <param name="statusCode">The value for <see cref="ProblemDetails.Status" />.</param>
772+
/// <param name="detail">The value for <see cref="ProblemDetails.Detail" />.</param>
773+
/// <param name="instance">The value for <see cref="ProblemDetails.Instance" />.</param>
774+
/// <param name="title">The value for <see cref="ProblemDetails.Title" />.</param>
775+
/// <param name="type">The value for <see cref="ProblemDetails.Type" />.</param>
776+
/// <param name="extensions">The value for <see cref="ProblemDetails.Extensions" />.</param>
777+
/// <returns>The created <see cref="ProblemHttpResult"/> for the response.</returns>
778+
#pragma warning disable RS0027 // Public API with optional parameter(s) should have the most parameters amongst its public overloads
779+
public static ProblemHttpResult Problem(
780+
#pragma warning restore RS0027 // Public API with optional parameter(s) should have the most parameters amongst its public overloads
758781
string? detail = null,
759782
string? instance = null,
760783
int? statusCode = null,
761784
string? title = null,
762785
string? type = null,
763-
IDictionary<string, object?>? extensions = null)
786+
IEnumerable<KeyValuePair<string, object?>>? extensions = null)
764787
{
765788
var problemDetails = new ProblemDetails
766789
{
@@ -776,17 +799,6 @@ public static ProblemHttpResult Problem(
776799
return new(problemDetails);
777800
}
778801

779-
private static void CopyExtensions(IDictionary<string, object?>? extensions, ProblemDetails problemDetails)
780-
{
781-
if (extensions is not null)
782-
{
783-
foreach (var extension in extensions)
784-
{
785-
problemDetails.Extensions.Add(extension);
786-
}
787-
}
788-
}
789-
790802
/// <summary>
791803
/// Produces a <see cref="ProblemDetails"/> response.
792804
/// </summary>
@@ -811,11 +823,34 @@ public static ProblemHttpResult Problem(ProblemDetails problemDetails)
811823
/// <returns>The created <see cref="HttpResults.ValidationProblem"/> for the response.</returns>
812824
public static ValidationProblem ValidationProblem(
813825
IDictionary<string, string[]> errors,
826+
string? detail,
827+
string? instance,
828+
string? title,
829+
string? type,
830+
IDictionary<string, object?>? extensions)
831+
{
832+
return ValidationProblem(errors, detail, instance, title, type, (IEnumerable<KeyValuePair<string, object?>>?)extensions);
833+
}
834+
835+
/// <summary>
836+
/// Produces a <see cref="StatusCodes.Status400BadRequest"/> response with an <see cref="HttpValidationProblemDetails"/> value.
837+
/// </summary>
838+
/// <param name="errors">One or more validation errors.</param>
839+
/// <param name="detail">The value for <see cref="ProblemDetails.Detail" />.</param>
840+
/// <param name="instance">The value for <see cref="ProblemDetails.Instance" />.</param>
841+
/// <param name="title">The value for <see cref="ProblemDetails.Title" />. Defaults to "One or more validation errors occurred."</param>
842+
/// <param name="type">The value for <see cref="ProblemDetails.Type" />.</param>
843+
/// <param name="extensions">The value for <see cref="ProblemDetails.Extensions" />.</param>
844+
/// <returns>The created <see cref="HttpResults.ValidationProblem"/> for the response.</returns>
845+
#pragma warning disable RS0027 // Public API with optional parameter(s) should have the most parameters amongst its public overloads
846+
public static ValidationProblem ValidationProblem(
847+
#pragma warning restore RS0027 // Public API with optional parameter(s) should have the most parameters amongst its public overloads
848+
IEnumerable<KeyValuePair<string, string[]>> errors,
814849
string? detail = null,
815850
string? instance = null,
816851
string? title = null,
817852
string? type = null,
818-
IDictionary<string, object?>? extensions = null)
853+
IEnumerable<KeyValuePair<string, object?>>? extensions = null)
819854
{
820855
ArgumentNullException.ThrowIfNull(errors);
821856

@@ -833,6 +868,17 @@ public static ValidationProblem ValidationProblem(
833868
return new(problemDetails);
834869
}
835870

871+
private static void CopyExtensions(IEnumerable<KeyValuePair<string, object?>>? extensions, ProblemDetails problemDetails)
872+
{
873+
if (extensions is not null)
874+
{
875+
foreach (var extension in extensions)
876+
{
877+
problemDetails.Extensions.Add(extension);
878+
}
879+
}
880+
}
881+
836882
/// <summary>
837883
/// Produces a <see cref="StatusCodes.Status201Created"/> response.
838884
/// </summary>

0 commit comments

Comments
 (0)