8000 fix json serializer w/controller · devel0/example-webapp-with-auth@f559129 · GitHub
[go: up one dir, main page]

Skip to content

Commit f559129

Browse files
committed
fix json serializer w/controller
1 parent bbfdab6 commit f559129

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

src/backend/webapi/Extensions/SetupControllers.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,23 @@ namespace ExampleWebApp.Backend.WebApi;
33
public static partial class Extensions
44
{
55

6-
public static IMvcBuilder SetupControllers(this IServiceCollection serviceCollection)
6+
public static IMvcBuilder SetupControllers(this WebApplicationBuilder builder)
77
{
8-
var res = serviceCollection.AddControllers(options =>
8+
var sp = builder.Services.BuildServiceProvider();
9+
10+
var res = builder.Services.AddControllers(options =>
11+
{
12+
// options.Filters.Add<JWTFilter>();
13+
})
14+
15+
.AddJsonOptions(options =>
916
{
10-
options.Filters.Add<JWTFilter>();
17+
var util = sp.GetRequiredService<IUtilService>();
18+
19+
util.ConfigureJsonSerializerOptions(options.JsonSerializerOptions);
1120
});
1221

13-
return res;
22+
return res;
1423
}
1524

1625
}

src/backend/webapi/Implementations/UtilService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ IConfiguration configuration
2424
public JsonSerializerOptions ConfigureJsonSerializerOptions(JsonSerializerOptions options)
2525
{
2626
options.Converters.Add(new JsonStringEnumConverter());
27-
options.ReferenceHandler = ReferenceHandler.IgnoreCycles; // TOOD: verify if can use All
27+
options.ReferenceHandler = ReferenceHandler.Preserve;
2828

2929
options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
3030

src/backend/webapi/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
builder.Services.SetupRoles();
3939

4040
// add controllers
41-
builder.Services.SetupControllers();
41+
builder.SetupControllers();
4242

4343
builder.Services.AddProblemDetails();
4444

0 commit comments

Comments
 (0)
0