-
Notifications
You must be signed in to change notification settings - Fork 10.1k
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
Adding Json aot/trimmer-safe overloads to [Typed]Results #46252
Comments
Thanks for contacting us. We're moving this issue to the |
Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:
|
API Review Notes:
API Approved! namespace Microsoft.AspNetCore.Http;
public static class TypedResults
{
public static JsonHttpResult<TValue> Json<TValue>(TValue? data, JsonSerializerOptions? options = null, string? contentType = null, int? statusCode = null)
+ public static JsonHttpResult<TValue> Json<TValue>(TValue? data, JsonSerializerContext jsonContext, string? contentType = null, int? statusCode = null) {}
+ public static JsonHttpResult<TValue> Json<TValue>(TValue? data, JsonTypeInfo<TValue> jsonTypeInfo, string? contentType = null, int? statusCode = null)
}
public static class Results
{
public static IResult Json<TValue>(TValue? data, JsonSerializerOptions? options = null, string? contentType = null, int? statusCode = null)
+ public static IResult Json<TValue>(TValue? data, JsonSerializerContext jsonSerializerContext, string? contentType = null, int? statusCode = null) {}
+ public static IResult Json<TValue>(TValue? data, JsonTypeInfo<TValue> jsonTypeInfo, string? contentType = null, int? statusCode = null){}
public static IResult Json(object? data, JsonSerializerOptions? options = null, string? contentType = null, int? statusCode = null)
// EDIT: The commented API was originally approved. The line below with the Type type argument now replaces it.
// + public static IResult Json(object? data, JsonSerializerContext jsonSerializerContext, string? contentType = null, int? statusCode = null){}
+ public static IResult Json(object? data, Type type, JsonSerializerContext jsonSerializerContext, string? contentType = null, int? statusCode = null){}
+ public static IResult Json(object? data, JsonTypeInfo jsonTypeInfo, string? contentType = null, int? statusCode = null){}
} |
I was updating the PR with the approved API and I noticed that we have one inconsistency between this API and JsonSerializer (or HttpReponseExtensions) where when the non-generic method takes a JsonSerializerContext we always take a type as well while in the proposed api it is not there. I believe we have two options:
- public static IResult Json(object? data, JsonSerializerContext jsonSerializerContext, string? contentType = null, int? statusCode = null){}
+ public static IResult Json(object? data, Type type, JsonSerializerContext jsonSerializerContext, string? contentType = null, int? statusCode = null){}
- public static IResult Json(object? data, JsonSerializerContext jsonSerializerContext, string? contentType = null, int? statusCode = null){} I would say option 1 is more consistent with what we have today, however, |
Just to clarify. today we create a |
I have updated my PR to: namespace Microsoft.AspNetCore.Http;
public static class TypedResults
{
public static JsonHttpResult<TValue> Json<TValue>(TValue? data, JsonSerializerOptions? options = null, string? contentType = null, int? statusCode = null)
+ public static JsonHttpResult<TValue> Json<TValue>(TValue? data, JsonSerializerContext context, string? contentType = null, int? statusCode = null) {}
+ public static JsonHttpResult<TValue> Json<TValue>(TValue? data, JsonTypeInfo<TValue> jsonTypeInfo, string? contentType = null, int? statusCode = null)
}
public static class Results
{
public static IResult Json<TValue>(TValue? data, JsonSerializerOptions? options = null, string? contentType = null, int? statusCode = null)
+ public static IResult Json<TValue>(TValue? data, JsonSerializerContext context, string? contentType = null, int? statusCode = null) {}
+ public static IResult Json<TValue>(TValue? data, JsonTypeInfo<TValue> jsonTypeInfo, string? contentType = null, int? statusCode = null){}
public static IResult Json(object? data, JsonSerializerOptions? options = null, string? contentType = null, int? statusCode = null)
+ public static IResult Json(object? data, Type type, JsonSerializerContext context, string? contentType = null, int? statusCode = null){}
+ public static IResult Json(object? data, JsonTypeInfo jsonTypeInfo, string? contentType = null, int? statusCode = null){}
} |
Nice catch @brunolins16. The goal here was to align with The updated API is approved! I'll make a note of it in the meeting email. Thanks. |
Background and Motivation
Both
JsonSerializer
andHttp[Request/Response]Extensions
provide AOT/Trimmer-safe overloads, withJsonTypeInfo
orJsonSerializerContext
, however, when producing a Json response using the static[Typed]Results
class only overloads that take JsonSerializerOptions are available.My suggestion is introduced new overload, following the same available in the
JsonSerializer
andHttp[Request/Response]Extensions
and mark the current overload withRUC/RDC
attributes.Proposed API
Usage Examples
Alternative Designs
Should MVC controller base be updated as well?
Risks
[Type]Results
already have few overloads for theJson
methods, that introducing those new overloads will cause ambiguity when calling something likeJson(new Sample(), null, contentType, statusCode)
but it is similar to whatJsonSerializer
method have todayThe text was updated successfully, but these errors were encountered: