Skip to content

Add overloads for problem and validation problem results that accept IEnumerable<KeyValuePair<,>> #56368

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 14 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ public HttpValidationProblemDetails()
/// </summary>
/// <param name="errors">The validation errors.</param>
public HttpValidationProblemDetails(IDictionary<string, string[]> errors)
: this((IEnumerable<KeyValuePair<string, string[]>>)errors)
{
}

/// <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(IEnumerable<KeyValuePair<string, string[]>> errors)
: this(new Dictionary<string, string[]>(errors ?? throw new ArgumentNullException(nameof(errors)), StringComparer.Ordinal))
{
}
Expand Down
1 change: 1 addition & 0 deletions src/Http/Http.Abstractions/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
Microsoft.AspNetCore.Http.HostString.HostString(string? value) -> void
*REMOVED*Microsoft.AspNetCore.Http.HostString.Value.get -> string!
Microsoft.AspNetCore.Http.HostString.Value.get -> string?
Microsoft.AspNetCore.Http.HttpValidationProblemDetails.HttpValidationProblemDetails(System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string!, string![]!>>! errors) -> void
1 change: 1 addition & 0 deletions src/Http/Http.Extensions/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#nullable enable
Microsoft.AspNetCore.Http.HttpValidationProblemDetails.HttpValidationProblemDetails(System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string!, string![]!>>! errors) -> void (forwarded, contained in Microsoft.AspNetCore.Http.Abstractions)
14 changes: 13 additions & 1 deletion src/Http/Http.Results/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,17 @@ Microsoft.AspNetCore.Http.HttpResults.InternalServerError<TValue>.StatusCode.get
Microsoft.AspNetCore.Http.HttpResults.InternalServerError<TValue>.Value.get -> TValue?
static Microsoft.AspNetCore.Http.Results.InternalServerError() -> Microsoft.AspNetCore.Http.IResult!
static Microsoft.AspNetCore.Http.Results.InternalServerError<TValue>(TValue? error) -> Microsoft.AspNetCore.Http.IResult!
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!
*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!
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!
*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!
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!
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!
static Microsoft.AspNetCore.Http.TypedResults.InternalServerError() -> Microsoft.AspNetCore.Http.HttpResults.InternalServerError!
static Microsoft.AspNetCore.Http.TypedResults.InternalServerError<TValue>(TValue? error) -> Microsoft.AspNetCore.Http.HttpResults.InternalServerError<TValue>!
static Microsoft.AspNetCore.Http.TypedResults.InternalServerError<TValue>(TValue? error) -> Microsoft.AspNetCore.Http.HttpResults.InternalServerError<TValue>!
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!
*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!
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!
*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!
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!
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!
55 changes: 52 additions & 3 deletions src/Http/Http.Results/src/Results.cs
Original file line number Diff line number Diff line change
Expand Up @@ -712,12 +712,33 @@ public static IResult InternalServerError<TValue>(TValue? error)
/// <param name="extensions">The value for <see cref="ProblemDetails.Extensions" />.</param>
/// <returns>The created <see cref="IResult"/> for the response.</returns>
public static IResult Problem(
string? detail,
string? instance,
int? statusCode,
string? title,
string? type,
IDictionary<string, object?>? extensions)
=> TypedResults.Problem(detail, instance, statusCode, title, type, extensions);

/// <summary>
/// Produces a <see cref="ProblemDetails"/> response.
/// </summary>
/// <param name="statusCode">The value for <see cref="ProblemDetails.Status" />.</param>
/// <param name="detail">The value for <see cref="ProblemDetails.Detail" />.</param>
/// <param name="instance">The value for <see cref="ProblemDetails.Instance" />.</param>
/// <param name="title">The value for <see cref="ProblemDetails.Title" />.</param>
/// <param name="type">The value for <see cref="ProblemDetails.Type" />.</param>
/// <param name="extensions">The value for <see cref="ProblemDetails.Extensions" />.</param>
/// <returns>The created <see cref="IResult"/> for the response.</returns>
#pragma warning disable RS0027 // Public API with optional parameter(s) should have the most parameters amongst its public overloads
public static IResult Problem(
#pragma warning restore RS0027 // Public API with optional parameter(s) should have the most parameters amongst its public overloads
string? detail = null,
string? instance = null,
int? statusCode = null,
string? title = null,
string? type = null,
IDictionary<string, object?>? extensions = null)
IEnumerable<KeyValuePair<string, object?>>? extensions = null)
=> TypedResults.Problem(detail, instance, statusCode, title, type, extensions);

/// <summary>
Expand All @@ -742,13 +763,41 @@ public static IResult Problem(ProblemDetails problemDetails)
/// <returns>The created <see cref="IResult"/> for the response.</returns>
public static IResult ValidationProblem(
IDictionary<string, string[]> errors,
string? detail,
string? instance,
int? statusCode,
string? title,
string? type,
IDictionary<string, object?>? extensions)
{
return ValidationProblem(errors, detail, instance, statusCode, title, type, (IEnumerable<KeyValuePair<string, object?>>?)extensions);
}

/// <summary>
/// Produces a <see cref="StatusCodes.Status400BadRequest"/> response
/// with a <see cref="HttpValidationProblemDetails"/> value.
/// </summary>
/// <param name="errors">One or more validation errors.</param>
/// <param name="detail">The value for <see cref="ProblemDetails.Detail" />.</param>
/// <param name="instance">The value for <see cref="ProblemDetails.Instance" />.</param>
/// <param name="statusCode">The status code.</param>
/// <param name="title">The value for <see cref="ProblemDetails.Title" />. Defaults to "One or more validation errors occurred."</param>
/// <param name="type">The value for <see cref="ProblemDetails.Type" />.</param>
/// <param name="extensions">The value for <see cref="ProblemDetails.Extensions" />.</param>
/// <returns>The created <see cref="IResult"/> for the response.</returns>
#pragma warning disable RS0027 // Public API with optional parameter(s) should have the most parameters amongst its public overloads
public static IResult ValidationProblem(
#pragma warning restore RS0027 // Public API with optional parameter(s) should have the most parameters amongst its public overloads
IEnumerable<KeyValuePair<string, string[]>> errors,
string? detail = null,
string? instance = null,
int? statusCode = null,
string? title = null,
string? type = null,
IDictionary<string, object?>? extensions = null)
IEnumerable<KeyValuePair<string, object?>>? extensions = null)
{
ArgumentNullException.ThrowIfNull(errors);

// TypedResults.ValidationProblem() does not allow setting the statusCode so we do this manually here
var problemDetails = new HttpValidationProblemDetails(errors)
{
Expand All @@ -765,7 +814,7 @@ public static IResult ValidationProblem(
return TypedResults.Problem(problemDetails);
}

private static void CopyExtensions(IDictionary<string, object?>? extensions, HttpValidationProblemDetails problemDetails)
private static void CopyExtensions(IEnumerable<KeyValuePair<string, object?>>? extensions, HttpValidationProblemDetails problemDetails)
{
if (extensions is not null)
{
Expand Down
72 changes: 59 additions & 13 deletions src/Http/Http.Results/src/TypedResults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -755,12 +755,35 @@ public static StatusCodeHttpResult StatusCode(int statusCode)
/// <param name="extensions">The value for <see cref="ProblemDetails.Extensions" />.</param>
/// <returns>The created <see cref="ProblemHttpResult"/> for the response.</returns>
public static ProblemHttpResult Problem(
string? detail,
string? instance,
int? statusCode,
string? title,
string? type,
IDictionary<string, object?>? extensions)
{
return Problem(detail, instance, statusCode, title, type, (IEnumerable<KeyValuePair<string, object?>>?)extensions);
}

/// <summary>
/// Produces a <see cref="ProblemDetails"/> response.
/// </summary>
/// <param name="statusCode">The value for <see cref="ProblemDetails.Status" />.</param>
/// <param name="detail">The value for <see cref="ProblemDetails.Detail" />.</param>
/// <param name="instance">The value for <see cref="ProblemDetails.Instance" />.</param>
/// <param name="title">The value for <see cref="ProblemDetails.Title" />.</param>
/// <param name="type">The value for <see cref="ProblemDetails.Type" />.</param>
/// <param name="extensions">The value for <see cref="ProblemDetails.Extensions" />.</param>
/// <returns>The created <see cref="ProblemHttpResult"/> for the response.</returns>
#pragma warning disable RS0027 // Public API with optional parameter(s) should have the most parameters amongst its public overloads
public static ProblemHttpResult Problem(
#pragma warning restore RS0027 // Public API with optional parameter(s) should have the most parameters amongst its public overloads
string? detail = null,
string? instance = null,
int? statusCode = null,
string? title = null,
string? type = null,
IDictionary<string, object?>? extensions = null)
IEnumerable<KeyValuePair<string, object?>>? extensions = null)
{
var problemDetails = new ProblemDetails
{
Expand All @@ -776,17 +799,6 @@ public static ProblemHttpResult Problem(
return new(problemDetails);
}

private static void CopyExtensions(IDictionary<string, object?>? extensions, ProblemDetails problemDetails)
{
if (extensions is not null)
{
foreach (var extension in extensions)
{
problemDetails.Extensions.Add(extension);
}
}
}

/// <summary>
/// Produces a <see cref="ProblemDetails"/> response.
/// </summary>
Expand All @@ -811,11 +823,34 @@ public static ProblemHttpResult Problem(ProblemDetails problemDetails)
/// <returns>The created <see cref="HttpResults.ValidationProblem"/> for the response.</returns>
public static ValidationProblem ValidationProblem(
IDictionary<string, string[]> errors,
string? detail,
string? instance,
string? title,
string? type,
IDictionary<string, object?>? extensions)
{
return ValidationProblem(errors, detail, instance, title, type, (IEnumerable<KeyValuePair<string, object?>>?)extensions);
}

/// <summary>
/// Produces a <see cref="StatusCodes.Status400BadRequest"/> response with an <see cref="HttpValidationProblemDetails"/> value.
/// </summary>
/// <param name="errors">One or more validation errors.</param>
/// <param name="detail">The value for <see cref="ProblemDetails.Detail" />.</param>
/// <param name="instance">The value for <see cref="ProblemDetails.Instance" />.</param>
/// <param name="title">The value for <see cref="ProblemDetails.Title" />. Defaults to "One or more validation errors occurred."</param>
/// <param name="type">The value for <see cref="ProblemDetails.Type" />.</param>
/// <param name="extensions">The value for <see cref="ProblemDetails.Extensions" />.</param>
/// <returns>The created <see cref="HttpResults.ValidationProblem"/> for the response.</returns>
#pragma warning disable RS0027 // Public API with optional parameter(s) should have the most parameters amongst its public overloads
public static ValidationProblem ValidationProblem(
#pragma warning restore RS0027 // Public API with optional parameter(s) should have the most parameters amongst its public overloads
IEnumerable<KeyValuePair<string, string[]>> errors,
string? detail = null,
string? instance = null,
string? title = null,
string? type = null,
IDictionary<string, object?>? extensions = null)
IEnumerable<KeyValuePair<string, object?>>? extensions = null)
{
ArgumentNullException.ThrowIfNull(errors);

Expand All @@ -833,6 +868,17 @@ public static ValidationProblem ValidationProblem(
return new(problemDetails);
}

private static void CopyExtensions(IEnumerable<KeyValuePair<string, object?>>? extensions, ProblemDetails problemDetails)
{
if (extensions is not null)
{
foreach (var extension in extensions)
{
problemDetails.Extensions.Add(extension);
}
}
}

/// <summary>
/// Produces a <see cref="StatusCodes.Status201Created"/> response.
/// </summary>
Expand Down
Loading
Loading