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

Dotnet 8 JsonSerializerOptions Breaking change #55692

Open
MoishyS opened this issue May 9, 2024 · 10 comments
Open

Dotnet 8 JsonSerializerOptions Breaking change #55692

MoishyS opened this issue May 9, 2024 · 10 comments
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates untriaged

Comments

@MoishyS
Copy link

MoishyS commented May 9, 2024

I have a Attribute to change JSON serialization from camelCase to PascalCase, which worked fine in dotnet 7, but in dotnet 8 i am getting an error System.InvalidOperationException: JsonSerializerOptions instance must specify a TypeInfoResolver setting before being marked as read-only

public class PascalCaseJson : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext context)
    {
        if (context.Result is ObjectResult objectResult)
            objectResult.Formatters.Add(new SystemTextJsonOutputFormatter(new JsonSerializerOptions()));
    }
}

Adding TypeInfoResolver = new DefaultJsonTypeInfoResolver() fixes the issue, but i couldn't find any documentation on this breaking change.

@pinkfloydx33
Copy link

Note that you shouldn't create a new instance of the options each time either.

@eiriktsarpalis
Copy link
Member

@MoishyS presumably this is using trimming or Native AOT? You appear to be impacted by this intentional breaking change: https://learn.microsoft.com/en-us/dotnet/core/compatibility/serialization/8.0/publishtrimmed.

If you are using trimming or Native AOT, then you should consider using the source generator instead of the reflection-based DefaultJsonTypeInfoResolver type which can lead to unexpected runtime errors related to trimming and Native AOT.

@MoishyS
Copy link
Author

MoishyS commented May 10, 2024

@eiriktsarpalis i am having this issue for a regular aspnet app, without trimming or Native AOT

@eiriktsarpalis
Copy link
Member

@MoishyS can you share a minimal reproduction that demonstrates the issue?

@eiriktsarpalis
Copy link
Member

That repo is not public, it would help if you could extract a self contained console app or a ZIP file that everyone reading the issue can access.

@MoishyS
Copy link
Author

MoishyS commented May 10, 2024

@eiriktsarpalis i made it public

@eiriktsarpalis
Copy link
Member

Thanks. This looks like a moderately sized project that I won't be able to take a look at right now. It would help immensely if you could reduce the bug to the smallest possible console app (one that fits in a github comment). Thanks.

@MoishyS
Copy link
Author

MoishyS commented May 13, 2024

@eiriktsarpalis i used dotnet new webapi -o SerializerAttribute -controllers to create a new app. the only change i made was to add the [PascalCaseJson] to the controller and create the PascalCaseJson Attribute

public class PascalCaseJson : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext context)
    {
        if (context.Result is ObjectResult objectResult)
            objectResult.Formatters.Add(new SystemTextJsonOutputFormatter(new JsonSerializerOptions()));
    }
}

@eiriktsarpalis eiriktsarpalis transferred this issue from dotnet/runtime May 13, 2024
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label May 13, 2024
@eiriktsarpalis
Copy link
Member

Moving to aspnetcore for further triage. The error should be expected with Native AOT or PublishTrimmed=true however the OP is claiming that this is not the case here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates untriaged
Projects
None yet
Development

No branches or pull requests

3 participants