Skip to content
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 Http(Request/Response) extension methods overloads accepting non-generic JsonTypeInfo #45568

Closed
brunolins16 opened this issue Dec 13, 2022 · 4 comments · Fixed by #45593
Labels
api-approved API was approved in API review, it can be implemented area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels
Milestone

Comments

@brunolins16
Copy link
Member

Background and Motivation

STJ introduced new overloads (dotnet/runtime#77051) with untyped JsonTypeInfo, however, HttpResponse and HttpRequest extension methods only support JsonTypeInfo<T>.

As part of the work to make ASP.NET Core AOT-friendly, we have a need of those methods available. As an example, RDF needs to write the JSON response based on the runtime type and cannot create a typed JsonTypeInfo<T>.

return HttpResponseJsonExtensions.WriteAsJsonAsync(response, value, value is null ? typeof(object) : value.GetType(), options, default);

Same for the uController source generator rely on the HttpResponse extensions to write a JSON content and might not be able to have a generic JsonTypeInfo.

https://github.com/davidfowl/uController/blob/86fe16e634bdc34e641c3d983a3f26bba80ccebe/GeneratedOutput/RouteBuilderExtensions.g.cs#L504

Proposed API

namespace Microsoft.AspNetCore.Http;

public static class HttpResponseJsonExtensions
{
+        public static Task WriteAsJsonAsync(
+                          this HttpResponse response, 
+                          object? value, 
+                          JsonTypeInfo jsonTypeInfo, 
+                          string? contentType = default, 
+                          CancellationToken cancellationToken = default);
}
namespace Microsoft.AspNetCore.Http;

public static class HttpRequestJsonExtensions
{
+        public static ValueTask<object?> ReadFromJsonAsync(
+                          this HttpRequest request,
+                          JsonTypeInfo jsonTypeInfo, 
+                          CancellationToken cancellationToken = default);
}

Usage Examples

ValueTask<object?>  ReadBody(HttpContext context, Type bodyType,  JsonSerializerOptions  jsonSerializerOptions)
{
     var bodyJsonTypeInfo = jsonSerializerOptions.GetTypeInfo(bodyType);
     return httpContext.Request.ReadFromJsonAsync(bodyJsonTypeInfo);
}

Task WriteResponse(HttpContext context, object? value, JsonSerializerOptions  jsonSerializerOptions)
{
     var runtimeType = value is null ? typeof(object) : value.GetType();
     var runtimeTypeInfo = jsonSerializerOptions.GetTypeInfo(runtimeType);
     return httpContext.Response.WriteAsJsonAsync(value, runtimeTypeInfo );
}

Alternative Designs

No response

Risks

No response

cc @eiriktsarpalis

@brunolins16 brunolins16 added api-suggestion Early API idea and discussion, it is NOT ready for implementation area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels api-ready-for-review API is ready for formal API review - https://github.com/dotnet/apireviews and removed api-suggestion Early API idea and discussion, it is NOT ready for implementation labels Dec 13, 2022
@ghost
Copy link

ghost commented Dec 13, 2022

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:

  • The PR contains changes to the reference-assembly that describe the API change. Or, you have included a snippet of reference-assembly-style code that illustrates the API change.
  • The PR describes the impact to users, both positive (useful new APIs) and negative (breaking changes).
  • Someone is assigned to "champion" this change in the meeting, and they understand the impact and design of the change.

@captainsafia captainsafia added this to the .NET 8 Planning milestone Dec 13, 2022
@ghost
Copy link

ghost commented Dec 13, 2022

Thanks for contacting us.

We're moving this issue to the .NET 8 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s).
If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues.
To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

@halter73
Copy link
Member

halter73 commented Jan 5, 2023

API Review Notes:

  1. This matches the pattern used by JsonSerializer.DeserializeAsync/SerializeAsync
  2. We're curious how JsonSerializer handles null, but we figure it just treats the type as object.

API Approved as proposed!

@halter73 halter73 added api-approved API was approved in API review, it can be implemented and removed api-ready-for-review API is ready for formal API review - https://github.com/dotnet/apireviews labels Jan 5, 2023
@eiriktsarpalis
Copy link
Member

We're curious how JsonSerializer handles null, but we figure it just treats the type as object

It throws, which is consistent with the generic overloads and how null arguments are treated in non-nullable parameter types. Generally speaking, a source generated context might not include metadata for object serialization.

@ghost ghost locked as resolved and limited conversation to collaborators Feb 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-approved API was approved in API review, it can be implemented area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants