JsonSerializerContext not working with minimalApi
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 281
Description
### Is there an existing issue for this?
- [X] I have searched the existing issues
### Describe the bug
After configure Web API service to use generated version of `JsonSerializerContext` for serializing JSON, still using default serialization
### Expected Behavior
Object returned by endpoint should be serialized using configured class derived from `JsonSerializerContext` with his configuration.
### Steps To Reproduce
```C#
using System.Text.Json;
using System.Text.Json.Serialization;
var builder = WebApplication.CreateSlimBuilder(args);
builder.Services.ConfigureHttpJsonOptions(options =>
options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default));
var app = builder.Build();
app.MapGet("/json", () => new Person("Alex"));
app.MapGet("/string", () => JsonSerializer.Serialize(new Person("Alex"),AppJsonSerializerContext.Default.Person));
app.Run();
record Person(string FirstName)
{
}
[JsonSerializable(typeof(Person))]
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.KebabCaseUpper)]
internal partial class AppJsonSerializerContext : JsonSerializerContext
{
}
```
endpoint `/string` (manually using AppJsonSerializerContext) returns:
```JSON
{"FIRST-NAME":"Alex"}
```
but endpoint `/json` returns default serialized JSON, without `AppJsonSerializerContext`:
```JSON
{
"firstName": "Alex"
}
```
### Exceptions (if any)
_No response_
### .NET Version
8.0.100
### Anything else?
Project configuration .csproj
```XML
net8.0
enable
enable
true
```
I've tried debugging generated code, endpoint `/string` goes into generated implementation, but `/json` not
Contributor guide
Assessment
This issue has not been assessed yet.