Skip to content

JsonSerializerOptions.AddContext should allow context composition #80527

Closed

Description

@brunolins16

Description

In ASP.NET Core, JsonSerializerOptions are resolved from DI and is possible to configure JsonTypeResolver/Context by:

builder.Services.ConfigureHttpJsonOptions(
    options => options.SerializerOptions.AddContext<CustomAppContext>());

or

builder.Services.ConfigureHttpJsonOptions(
    options => options.SerializerOptions.TypeInfoResolver = CustomAppContext.Default);

Also, users and internally can combine additional JsonTypeInfoResolvers using Options [post-]configuration.

Eg.:

        services.PostConfigure<JsonOptions>(options =>
        {
            if (options.SerializerOptions.TypeInfoResolver is not null)
            {
                _serializerOptions.TypeInfoResolver = JsonTypeInfoResolver.Combine(_serializerOptions.TypeInfoResolver, ProblemDetailsJsonContext.Default);
            }

        });

The biggest challenge is how JsonTypeResolver/Context composition works, since a call to AddContext makes the JsonSerializerOptions read only and locks it to any further modification related to the Resolver.

Related to: dotnet/aspnetcore#45906

cc @davidfowl @eiriktsarpalis

Reproduction Steps

using Microsoft.AspNetCore.Http.Json;
using System.Text.Json.Serialization;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.ConfigureHttpJsonOptions(
    options => options.SerializerOptions.AddContext<CustomAppContext>());

// Additional JsonSerializerOptions configuration
builder.Services.PostConfigure<JsonOptions>(options =>
{
    options.SerializerOptions.AddContext<AdditionalAppContext>();
});

var app = builder.Build();

app.Run();

public class TypeA { }

[JsonSerializable(typeof(TypeA))]
public partial class CustomAppContext : JsonSerializerContext {}

public class TypeB { }

[JsonSerializable(typeof(TypeB))]
public partial class AdditionalAppContext : JsonSerializerContext { }

Expected behavior

The expected behavior is multiple calls to AddContext or a new API (eg.: AppendContext) combine JsonSerializerContext in the order it is called while keeping the possibility to keep the fast path performance.

Actual behavior

The second call to AddContext throws an InvalidOperationException:

System.InvalidOperationException: JsonSerializerOptions instances cannot be modified once encapsulated by a JsonSerializerContext. Such encapsulation can happen either when calling 'JsonSerializerOptions.AddContext' or when passing the options instance to a JsonSerializerContext constructor.
   at System.Text.Json.ThrowHelper.ThrowInvalidOperationException_SerializerOptionsReadOnly(JsonSerializerContext context)
   at System.Text.Json.JsonSerializerOptions.VerifyMutable()
   at System.Text.Json.JsonSerializerOptions.AddContext[TContext]()
   at Program.<>c.<<Main>$>b__0_1(JsonOptions options) in C:\Users\brolivei\source\repos\WebApplication24\Program.cs:line 11
   at Microsoft.Extensions.Options.OptionsFactory`1.Create(String name)
   at Microsoft.Extensions.Options.UnnamedOptionsManager`1.get_Value()
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl..ctor(RequestDelegate next, IOptions`1 options, ILoggerFactory loggerFactory, IWebHostEnvironment hostingEnvironment, DiagnosticSource diagnosticSource, IEnumerable`1 filters, IOptions`1 jsonOptions, IProblemDetailsService problemDetailsService)
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.ConstructorInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)
   at System.Reflection.RuntimeConstructorInfo.InvokeWithManyArguments(RuntimeConstructorInfo ci, Int32 argCount, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Microsoft.Extensions.Internal.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
   at Microsoft.Extensions.Internal.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
   at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.<>c__DisplayClass5_0.<UseMiddleware>b__0(RequestDelegate next)
   at Microsoft.AspNetCore.Builder.ApplicationBuilder.Build()
   at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken)
   at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host)
   at Program.<Main>$(String[] args) in C:\Users\brolivei\source\repos\WebApplication24\Program.cs:line 16

Regression?

No response

Known Workarounds

If you have control of the JsonSerializerOptions (not possible in ASP.NET Core right now) you could create a new JsonSerializerOptions using the copy constructor and combine the additional context:

    var serializerOptions = options.SerializerOptions;
    if (serializerOptions.TypeInfoResolver != null)
    {
        serializerOptions = new(options.SerializerOptions);
        serializerOptions.TypeInfoResolver = JsonTypeInfoResolver.Combine(serializerOptions.TypeInfoResolver!, AdditionalAppContext.Default);
    }

Configuration

.NET 7

Other information

No response

Activity

  1. ghost added
    untriaged
    on Jan 11, 2023
  2. krwq commented on Jan 11, 2023

    @krwq
    Member

    Note: the workaround also has perf hit from fast path not being used currently

  3. eiriktsarpalis commented on Jan 12, 2023

    @eiriktsarpalis
    Member

    Note: the workaround also has perf hit from fast path not being used currently

    cc #71933

  4. added
    partner-impact
    and removed
    untriaged
    on Jan 12, 2023
  5. added this to the 8.0.0 milestone on Jan 12, 2023
  6. added
    breaking-change
    on Jan 12, 2023
  7. eiriktsarpalis commented on Jan 12, 2023

    @eiriktsarpalis
    Member

    Following offline conversation, there is desire to introduce the following (breaking change) to the AddContext method:

    public void AddContext<TContext>() where TContext : JsonSerializerContext, new()
    {
        VerifyMutable();
        TContext context = new();
    -    // create bidirectional link between this options instance and the context, 
    -    // making options read-only and overwriting any existing resolver configuration.
    -    context.Options = this;
    -    TypeInfoResolver = context;
    +    // Do not associate the context with the current options and 
    +    // chain the current context after any pre-existing resolvers.
    +    TypeInfoResolver = TypeInfoResolver is null ? context : JsonTypeInfoResolver.Combine(TypeInfoResolver, context);
    }

    This would introduce the following breaking change:

    options.TypeInfoResolver = FooContext.Default;
    options.AddContext<BarContext>();
    
    Assert.IsType<BarContext>(options.TypeInforesolver); // passes in .NET 7, would start to fail in .NET 8

    I feel that this type of breaking change is acceptable, since more likely than not the user is doing something wrong here. We should document the breaking change regardless.

  8. layomia commented on Jan 12, 2023

    @layomia
    Contributor

    there is desire to introduce the following (breaking change) to the AddContext method

    👍. Also we should be able to just combine the contexts even if one exists already; still a breaking change. Also noting that we discussed keeping track of the originating context to enable using fast-path for combined contexts.

  9. ghost added
    in-pr
    on Jan 16, 2023
  10. ghost removed
    in-pr
    on Jan 17, 2023
  11. eiriktsarpalis commented on Jan 17, 2023

    @eiriktsarpalis
    Member

    Fixed in #80698. I'll refrain from creating a breaking change document until we have built confidence that this is the behavior we're shipping in .NET 8.

  12. ghost locked as resolved and limited conversation to collaborators on Feb 16, 2023
  13. removed
    breaking-change
    on Mar 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-System.Text.Jsonpartner-impact

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions