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

[API Proposal]: STJ SystemTextJsonOutputFormatter output type selector #55629

Open
angelaki opened this issue Apr 29, 2024 · 2 comments
Open
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates untriaged

Comments

@angelaki
Copy link

Background and motivation

Right now the default SystemTextJsonOutputFormatter internally decides what type to serialize (in WriteResponseBodyAsync). In my case I'd like to keep the controlers response type (an Interface) that now gets broken down to it's actual class.

Right now the type to serialize gets chosen here:

and checked here:

It'd be great to have an overrideable method where the jsonTypeInfo is selected.

API Proposal

    protected JsonTypeInfo? GetTypeInfo(OutputFormatterWriteContext context)
    {
        // context.ObjectType reflects the declared model type when specified.
        // For polymorphic scenarios where the user declares a return type, but returns a derived type,
        // we want to serialize all the properties on the derived type. This keeps parity with
        // the behavior you get when the user does not declare the return type.
        // To enable this our best option is to check if the JsonTypeInfo for the declared type is valid,
        // if it is use it. If it isn't, serialize the value as 'object' and let JsonSerializer serialize it as necessary.
        JsonTypeInfo? jsonTypeInfo = null;
        if (context.ObjectType is not null)
        {
            var declaredTypeJsonInfo = SerializerOptions.GetTypeInfo(context.ObjectType);

            var runtimeType = context.Object?.GetType();
            if (declaredTypeJsonInfo.ShouldUseWith(runtimeType))
            {
                jsonTypeInfo = declaredTypeJsonInfo;
            }
        }
        return jsonTypeInfo;
    }

API Usage

    protected override JsonTypeInfo? GetTypeInfo(OutputFormatterWriteContext context)
    {
        if (context.ObjectType?.IsInterface == true) { return SerializerOptions.GetTypeInfo(context.ObjectType); }
        return base.GetTypeInfo(context);
    }

Alternative Designs

No response

Risks

No response

@angelaki angelaki added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Apr 29, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

@eiriktsarpalis
Copy link
Member

cc @captainsafia

@eiriktsarpalis eiriktsarpalis transferred this issue from dotnet/runtime May 9, 2024
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label May 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates untriaged
Projects
None yet
Development

No branches or pull requests

2 participants