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

Consider a nicer API for configuring JsonOptions for minimal actions / route-to-code #39226

Closed
pranavkm opened this issue Dec 29, 2021 · 2 comments · Fixed by #39502
Closed
Assignees
Labels
api-approved API was approved in API review, it can be implemented area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels Docs This issue tracks updating documentation feature-minimal-actions Controller-like actions for endpoint routing
Milestone

Comments

@pranavkm
Copy link
Contributor

pranavkm commented Dec 29, 2021

Background and Motivation

The current way to configure JsonOptions for minimal is by configuring the Microsoft.AspNetCore.Http.Json.JsonOptions instance. From our docs, here's what this looks like:

using Microsoft.AspNetCore.Http.Json;

...
builder.Services.Configure<JsonOptions>(options =>
{
    options.SerializerOptions.IncludeFields = true;
});

Note that

a) You have to include a not too well-known namespace
b) There's also a Microsoft.AspNetCore.Mvc.JsonOptions type in the framework, so if you had previously imported the Mvc namespace, you'd be configuring the wrong JsonOptions type.

This makes it ripe for failure. We could consider adding a first class helper API to allow configuring this

Proposed API

namespace Microsoft.AspNetCore.Http;
public static HttpJsonServiceCollectionExtensions
{
    /// <summary>Configures options used for reading and writing JSON by route handlers, <c>HttpRequest.GetFromJsonAsync</c> and <c>HttpResponse.WriteJsonAsync</c>. </summary>
    public static IServiceCollection ConfigureHttpJsonOptions(this IServiceCollection serviceCollection, Action<JsonOptions> configureAction);
}

Usage Examples

var builder = WebApplication.CreateBuilder(args);
builder.Services.ConfigureHttpJsonOptions(options =>
{
    options.SerializerOptions.IncludeFields = true;
});

Alternative Designs

  • We could try and unify the HTTP and MVC options, but that seems like a much trickier proposition since both options can be configured independently today.

Risks

Users might be confused why configuring this option does not affect controller actions since this is API is much more accessible. I feel a documentation page that describes all the different JSON things that can be configured in ASP.NET Core might help mitigate this.

@pranavkm pranavkm 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 labels Dec 29, 2021
@rafikiassumani-msft rafikiassumani-msft added this to the 17.1-Preview1 milestone Jan 4, 2022
@halter73 halter73 added 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 Jan 14, 2022
@ghost
Copy link

ghost commented Jan 14, 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.

@rafikiassumani-msft rafikiassumani-msft added the feature-minimal-actions Controller-like actions for endpoint routing label Jan 20, 2022
@captainsafia captainsafia 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 24, 2022
@captainsafia
Copy link
Member

The API was approved with slight modifications including:

  • Changed ConfigureHttpJsonOptions to ConfigureRouteHandlerJsonOptions. Will follow-up on this naming to see if it need to be re-evaluated based on user feedback.
  • Added API under Microsoft.Extensions.DependencyInjection namespace

@BrennanConroy BrennanConroy added the Docs This issue tracks updating documentation label Feb 3, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Mar 5, 2022
@amcasey amcasey added the area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc label Jun 2, 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-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels Docs This issue tracks updating documentation feature-minimal-actions Controller-like actions for endpoint routing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants