Skip to content

Commit e2b99f1

Browse files
authored
Merge pull request #470 from iceljc/bugfix/fix-controller-json-serilizer
fix api controller json serilizer
2 parents d9fd652 + c49fcc5 commit e2b99f1

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

src/Infrastructure/BotSharp.Core/Conversations/ConversationPlugin.cs

+7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BotSharp.Abstraction.Files;
2+
using BotSharp.Abstraction.Google.Settings;
23
using BotSharp.Abstraction.Instructs;
34
using BotSharp.Abstraction.Messaging;
45
using BotSharp.Abstraction.Plugins.Models;
@@ -34,6 +35,12 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
3435
return settingService.Bind<ConversationSetting>("Conversation");
3536
});
3637

38+
services.AddScoped(provider =>
39+
{
40+
var settingService = provider.GetRequiredService<ISettingService>();
41+
return settingService.Bind<GoogleApiSettings>("GoogleApi");
42+
});
43+
3744
services.AddScoped<IConversationStorage, ConversationStorage>();
3845
services.AddScoped<IConversationService, ConversationService>();
3946
services.AddScoped<IConversationStateService, ConversationStateService>();

src/Infrastructure/BotSharp.OpenAPI/BotSharpOpenApiExtensions.cs

-8
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
using Microsoft.OpenApi.Models;
1212
using Microsoft.IdentityModel.JsonWebTokens;
1313
using BotSharp.OpenAPI.BackgroundServices;
14-
using BotSharp.Abstraction.Settings;
15-
using BotSharp.Abstraction.Google.Settings;
1614

1715
namespace BotSharp.OpenAPI;
1816

@@ -34,12 +32,6 @@ public static IServiceCollection AddBotSharpOpenAPI(this IServiceCollection serv
3432
services.AddScoped<IUserIdentity, UserIdentity>();
3533
services.AddHostedService<ConversationTimeoutService>();
3634

37-
services.AddScoped(provider =>
38-
{
39-
var settingService = provider.GetRequiredService<ISettingService>();
40-
return settingService.Bind<GoogleApiSettings>("GoogleApi");
41-
});
42-
4335
// Add bearer authentication
4436
var schema = "MIXED_SCHEME";
4537
var builder = services.AddAuthentication(options =>

src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs

+27-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using BotSharp.Abstraction.Options;
12
using BotSharp.Abstraction.Routing;
23

34
namespace BotSharp.OpenAPI.Controllers;
@@ -8,12 +9,16 @@ public class ConversationController : ControllerBase
89
{
910
private readonly IServiceProvider _services;
1011
private readonly IUserIdentity _user;
12+
private readonly JsonSerializerOptions _jsonOptions;
1113

1214
public ConversationController(IServiceProvider services,
13-
IUserIdentity user)
15+
IUserIdentity user,
16+
BotSharpOptions options)
1417
{
1518
_services = services;
1619
_user = user;
20+
_jsonOptions = InitJsonOptions(options);
21+
1722
}
1823

1924
[HttpPost("/conversation/{agentId}")]
@@ -312,10 +317,7 @@ await conv.SendMessage(agentId, inputMsg,
312317

313318
private async Task OnChunkReceived(HttpResponse response, ChatResponseModel message)
314319
{
315-
var json = JsonSerializer.Serialize(message, new JsonSerializerOptions
316-
{
317-
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
318-
});
320+
var json = JsonSerializer.Serialize(message, _jsonOptions);
319321

320322
var buffer = Encoding.UTF8.GetBytes($"data:{json}\n");
321323
await response.Body.WriteAsync(buffer, 0, buffer.Length);
@@ -333,4 +335,24 @@ private async Task OnEventCompleted(HttpResponse response)
333335
buffer = Encoding.UTF8.GetBytes("\n");
334336
await response.Body.WriteAsync(buffer, 0, buffer.Length);
335337
}
338+
339+
private JsonSerializerOptions InitJsonOptions(BotSharpOptions options)
340+
{
341+
var jsonOption = new JsonSerializerOptions
342+
{
343+
PropertyNameCaseInsensitive = true,
344+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
345+
AllowTrailingCommas = true
346+
};
347+
348+
if (options?.JsonSerializerOptions != null)
349+
{
350+
foreach (var option in options.JsonSerializerOptions.Converters)
351+
{
352+
jsonOption.Converters.Add(option);
353+
}
354+
}
355+
356+
return jsonOption;
357+
}
336358
}

0 commit comments

Comments
 (0)