Skip to content

Add generic method overloads to Microsoft.AspNetCore.Http.Results static class #42176

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 5 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion src/Http/Http.Results/src/HttpResultsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static Task WriteResultAsJsonAsync<T>(

Log.WritingResultAsJson(logger, typeof(T).Name);

return httpContext.Response.WriteAsJsonAsync(
return httpContext.Response.WriteAsJsonAsync<object?>(
value,
options: jsonSerializerOptions,
contentType: contentType);
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Http.Results/src/ProblemHttpResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Task ExecuteAsync(HttpContext httpContext)
httpContext.Response.StatusCode = code;
}

return HttpResultsHelper.WriteResultAsJsonAsync<object?>(
return HttpResultsHelper.WriteResultAsJsonAsync(
httpContext,
logger,
value: ProblemDetails,
Expand Down
11 changes: 11 additions & 0 deletions src/Http/Http.Results/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,23 @@ Microsoft.AspNetCore.Http.HttpResults.RedirectToRouteHttpResult.RouteName.get ->
Microsoft.AspNetCore.Http.HttpResults.RedirectToRouteHttpResult.RouteValues.get -> Microsoft.AspNetCore.Routing.RouteValueDictionary?
*REMOVED*static Microsoft.AspNetCore.Http.Results.Content(string! content, string? contentType = null, System.Text.Encoding? contentEncoding = null) -> Microsoft.AspNetCore.Http.IResult!
*REMOVED*static Microsoft.AspNetCore.Http.Results.Content(string! content, Microsoft.Net.Http.Headers.MediaTypeHeaderValue! contentType) -> Microsoft.AspNetCore.Http.IResult!
static Microsoft.AspNetCore.Http.Results.Accepted<TValue>(string? uri = null, TValue? value = default(TValue?)) -> Microsoft.AspNetCore.Http.IResult!
static Microsoft.AspNetCore.Http.Results.AcceptedAtRoute<TValue>(string? routeName = null, object? routeValues = null, TValue? value = default(TValue?)) -> Microsoft.AspNetCore.Http.IResult!
static Microsoft.AspNetCore.Http.Results.BadRequest<TValue>(TValue? error) -> Microsoft.AspNetCore.Http.IResult!
static Microsoft.AspNetCore.Http.Results.Conflict<TValue>(TValue? error) -> Microsoft.AspNetCore.Http.IResult!
static Microsoft.AspNetCore.Http.Results.Content(string? content, Microsoft.Net.Http.Headers.MediaTypeHeaderValue! contentType) -> Microsoft.AspNetCore.Http.IResult!
static Microsoft.AspNetCore.Http.Results.Content(string? content, string? contentType = null, System.Text.Encoding? contentEncoding = null, int? statusCode = null) -> Microsoft.AspNetCore.Http.IResult!
static Microsoft.AspNetCore.Http.Results.Content(string? content, string? contentType, System.Text.Encoding? contentEncoding) -> Microsoft.AspNetCore.Http.IResult!
*REMOVED*static Microsoft.AspNetCore.Http.Results.Text(string! content, string? contentType = null, System.Text.Encoding? contentEncoding = null) -> Microsoft.AspNetCore.Http.IResult!
static Microsoft.AspNetCore.Http.Results.Created<TValue>(System.Uri! uri, TValue? value) -> Microsoft.AspNetCore.Http.IResult!
static Microsoft.AspNetCore.Http.Results.Created<TValue>(string! uri, TValue? value) -> Microsoft.AspNetCore.Http.IResult!
static Microsoft.AspNetCore.Http.Results.CreatedAtRoute<TValue>(string? routeName = null, object? routeValues = null, TValue? value = default(TValue?)) -> Microsoft.AspNetCore.Http.IResult!
static Microsoft.AspNetCore.Http.Results.Json<TValue>(TValue? data, System.Text.Json.JsonSerializerOptions? options = null, string? contentType = null, int? statusCode = null) -> Microsoft.AspNetCore.Http.IResult!
static Microsoft.AspNetCore.Http.Results.NotFound<TValue>(TValue? value) -> Microsoft.AspNetCore.Http.IResult!
static Microsoft.AspNetCore.Http.Results.Ok<TValue>(TValue? value) -> Microsoft.AspNetCore.Http.IResult!
static Microsoft.AspNetCore.Http.Results.Text(string? content, string? contentType = null, System.Text.Encoding? contentEncoding = null, int? statusCode = null) -> Microsoft.AspNetCore.Http.IResult!
static Microsoft.AspNetCore.Http.Results.Text(string? content, string? contentType, System.Text.Encoding? contentEncoding) -> Microsoft.AspNetCore.Http.IResult!
static Microsoft.AspNetCore.Http.Results.UnprocessableEntity<TValue>(TValue? error) -> Microsoft.AspNetCore.Http.IResult!
static Microsoft.AspNetCore.Http.TypedResults.Content(string? content, string? contentType = null, System.Text.Encoding? contentEncoding = null, int? statusCode = null) -> Microsoft.AspNetCore.Http.HttpResults.ContentHttpResult!
static Microsoft.AspNetCore.Http.TypedResults.Content(string? content, string? contentType, System.Text.Encoding? contentEncoding) -> Microsoft.AspNetCore.Http.HttpResults.ContentHttpResult!
static Microsoft.AspNetCore.Http.TypedResults.Text(string? content, string? contentType = null, System.Text.Encoding? contentEncoding = null, int? statusCode = null) -> Microsoft.AspNetCore.Http.HttpResults.ContentHttpResult!
Expand Down
121 changes: 121 additions & 0 deletions src/Http/Http.Results/src/Results.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,22 @@ public static IResult Content(string? content, MediaTypeHeaderValue contentType)
/// <remarks>Callers should cache an instance of serializer settings to avoid
/// recreating cached data with each call.</remarks>
public static IResult Json(object? data, JsonSerializerOptions? options = null, string? contentType = null, int? statusCode = null)
=> Json<object>(data, options, contentType, statusCode);

/// <summary>
/// Creates a <see cref="IResult"/> that serializes the specified <paramref name="data"/> object to JSON.
/// </summary>
/// <param name="data">The object to write as JSON.</param>
/// <param name="options">The serializer options to use when serializing the value.</param>
/// <param name="contentType">The content-type to set on the response.</param>
/// <param name="statusCode">The status code to set on the response.</param>
/// <returns>The created <see cref="JsonHttpResult{TValue}"/> that serializes the specified <paramref name="data"/>
/// as JSON format for the response.</returns>
/// <remarks>Callers should cache an instance of serializer settings to avoid
/// recreating cached data with each call.</remarks>
#pragma warning disable RS0026 // Do not add multiple public overloads with optional parameters
public static IResult Json<TValue>(TValue? data, JsonSerializerOptions? options = null, string? contentType = null, int? statusCode = null)
#pragma warning restore RS0026 // Do not add multiple public overloads with optional parameters
=> TypedResults.Json(data, options, contentType, statusCode);

/// <summary>
Expand Down Expand Up @@ -454,7 +470,17 @@ public static IResult StatusCode(int statusCode)
/// </summary>
/// <param name="value">The value to be included in the HTTP response body.</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 NotFound(object? value = null)
#pragma warning restore RS0027 // Public API with optional parameter(s) should have the most parameters amongst its public overloads.
=> NotFound<object>(value);

/// <summary>
/// Produces a <see cref="StatusCodes.Status404NotFound"/> response.
/// </summary>
/// <param name="value">The value to be included in the HTTP response body.</param>
/// <returns>The created <see cref="IResult"/> for the response.</returns>
public static IResult NotFound<TValue>(TValue? value)
=> value is null ? TypedResults.NotFound() : TypedResults.NotFound(value);

/// <summary>
Expand All @@ -469,15 +495,35 @@ public static IResult Unauthorized()
/// </summary>
/// <param name="error">An error object to be included in the HTTP response body.</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 BadRequest(object? error = null)
#pragma warning restore RS0027 // Public API with optional parameter(s) should have the most parameters amongst its public overloads.
=> BadRequest<object>(error);

/// <summary>
/// Produces a <see cref="StatusCodes.Status400BadRequest"/> response.
/// </summary>
/// <param name="error">An error object to be included in the HTTP response body.</param>
/// <returns>The created <see cref="IResult"/> for the response.</returns>
public static IResult BadRequest<TValue>(TValue? error)
=> error is null ? TypedResults.BadRequest() : TypedResults.BadRequest(error);

/// <summary>
/// Produces a <see cref="StatusCodes.Status409Conflict"/> response.
/// </summary>
/// <param name="error">An error object to be included in the HTTP response body.</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 Conflict(object? error = null)
#pragma warning restore RS0027 // Public API with optional parameter(s) should have the most parameters amongst its public overloads.
=> Conflict<object>(error);

/// <summary>
/// Produces a <see cref="StatusCodes.Status409Conflict"/> response.
/// </summary>
/// <param name="error">An error object to be included in the HTTP response body.</param>
/// <returns>The created <see cref="IResult"/> for the response.</returns>
public static IResult Conflict<TValue>(TValue? error)
=> error is null ? TypedResults.Conflict() : TypedResults.Conflict(error);

/// <summary>
Expand All @@ -492,15 +538,35 @@ public static IResult NoContent()
/// </summary>
/// <param name="value">The value to be included in the HTTP response body.</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 Ok(object? value = null)
#pragma warning restore RS0027 // Public API with optional parameter(s) should have the most parameters amongst its public overloads.
=> Ok<object>(value);

/// <summary>
/// Produces a <see cref="StatusCodes.Status200OK"/> response.
/// </summary>
/// <param name="value">The value to be included in the HTTP response body.</param>
/// <returns>The created <see cref="IResult"/> for the response.</returns>
public static IResult Ok<TValue>(TValue? value)
=> value is null ? TypedResults.Ok() : TypedResults.Ok(value);

/// <summary>
/// Produces a <see cref="StatusCodes.Status422UnprocessableEntity"/> response.
/// </summary>
/// <param name="error">An error object to be included in the HTTP response body.</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 UnprocessableEntity(object? error = null)
#pragma warning restore RS0027 // Public API with optional parameter(s) should have the most parameters amongst its public overloads.
=> UnprocessableEntity<object>(error);

/// <summary>
/// Produces a <see cref="StatusCodes.Status422UnprocessableEntity"/> response.
/// </summary>
/// <param name="error">An error object to be included in the HTTP response body.</param>
/// <returns>The created <see cref="IResult"/> for the response.</returns>
public static IResult UnprocessableEntity<TValue>(TValue? error)
=> error is null ? TypedResults.UnprocessableEntity() : TypedResults.UnprocessableEntity(error);

/// <summary>
Expand Down Expand Up @@ -580,6 +646,15 @@ public static IResult ValidationProblem(
/// <param name="value">The value to be included in the HTTP response body.</param>
/// <returns>The created <see cref="IResult"/> for the response.</returns>
public static IResult Created(string uri, object? value)
=> Created<object>(uri, value);

/// <summary>
/// Produces a <see cref="StatusCodes.Status201Created"/> response.
/// </summary>
/// <param name="uri">The URI at which the content has been created.</param>
/// <param name="value">The value to be included in the HTTP response body.</param>
/// <returns>The created <see cref="IResult"/> for the response.</returns>
public static IResult Created<TValue>(string uri, TValue? value)
=> value is null ? TypedResults.Created(uri) : TypedResults.Created(uri, value);

/// <summary>
Expand All @@ -589,6 +664,15 @@ public static IResult Created(string uri, object? value)
/// <param name="value">The value to be included in the HTTP response body.</param>
/// <returns>The created <see cref="IResult"/> for the response.</returns>
public static IResult Created(Uri uri, object? value)
=> Created<object>(uri, value);

/// <summary>
/// Produces a <see cref="StatusCodes.Status201Created"/> response.
/// </summary>
/// <param name="uri">The URI at which the content has been created.</param>
/// <param name="value">The value to be included in the HTTP response body.</param>
/// <returns>The created <see cref="IResult"/> for the response.</returns>
public static IResult Created<TValue>(Uri uri, TValue? value)
=> value is null ? TypedResults.Created(uri) : TypedResults.Created(uri, value);

/// <summary>
Expand All @@ -599,6 +683,18 @@ public static IResult Created(Uri uri, object? value)
/// <param name="value">The value to be included in the HTTP response body.</param>
/// <returns>The created <see cref="IResult"/> for the response.</returns>
public static IResult CreatedAtRoute(string? routeName = null, object? routeValues = null, object? value = null)
=> CreatedAtRoute<object>(routeName, routeValues, value);

/// <summary>
/// Produces a <see cref="StatusCodes.Status201Created"/> response.
/// </summary>
/// <param name="routeName">The name of the route to use for generating the URL.</param>
/// <param name="routeValues">The route data to use for generating the URL.</param>
/// <param name="value">The value to be included in the HTTP response body.</param>
/// <returns>The created <see cref="IResult"/> for the response.</returns>
#pragma warning disable RS0026 // Do not add multiple public overloads with optional parameters
public static IResult CreatedAtRoute<TValue>(string? routeName = null, object? routeValues = null, TValue? value = default)
#pragma warning restore RS0026 // Do not add multiple public overloads with optional parameters
=> value is null ? TypedResults.CreatedAtRoute(routeName, routeValues) : TypedResults.CreatedAtRoute(value, routeName, routeValues);

/// <summary>
Expand All @@ -608,6 +704,17 @@ public static IResult CreatedAtRoute(string? routeName = null, object? routeValu
/// <param name="value">The optional content value to format in the response body.</param>
/// <returns>The created <see cref="IResult"/> for the response.</returns>
public static IResult Accepted(string? uri = null, object? value = null)
=> Accepted<object>(uri, value);

/// <summary>
/// Produces a <see cref="StatusCodes.Status202Accepted"/> response.
/// </summary>
/// <param name="uri">The URI with the location at which the status of requested content can be monitored.</param>
/// <param name="value">The optional content value to format in the response body.</param>
/// <returns>The created <see cref="IResult"/> for the response.</returns>
#pragma warning disable RS0026 // Do not add multiple public overloads with optional parameters
public static IResult Accepted<TValue>(string? uri = null, TValue? value = default)
#pragma warning restore RS0026 // Do not add multiple public overloads with optional parameters
=> value is null ? TypedResults.Accepted(uri) : TypedResults.Accepted(uri, value);

/// <summary>
Expand All @@ -617,7 +724,21 @@ public static IResult Accepted(string? uri = null, object? value = null)
/// <param name="routeValues">The route data to use for generating the URL.</param>
/// <param name="value">The optional content value to format in the response body.</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 AcceptedAtRoute(string? routeName = null, object? routeValues = null, object? value = null)
#pragma warning restore RS0027 // Public API with optional parameter(s) should have the most parameters amongst its public overloads.
=> AcceptedAtRoute<object>(routeName, routeValues, value);

/// <summary>
/// Produces a <see cref="StatusCodes.Status202Accepted"/> response.
/// </summary>
/// <param name="routeName">The name of the route to use for generating the URL.</param>
/// <param name="routeValues">The route data to use for generating the URL.</param>
/// <param name="value">The optional content value to format in the response body.</param>
/// <returns>The created <see cref="IResult"/> for the response.</returns>
#pragma warning disable RS0026 // Do not add multiple public overloads with optional parameters
public static IResult AcceptedAtRoute<TValue>(string? routeName = null, object? routeValues = null, TValue? value = default)
#pragma warning restore RS0026 // Do not add multiple public overloads with optional parameters
=> value is null ? TypedResults.AcceptedAtRoute(routeName, routeValues) : TypedResults.AcceptedAtRoute(value, routeName, routeValues);

/// <summary>
Expand Down
Loading