diff --git a/sidecar/src/Eryx.AgentHost/Services/LspToolSession.cs b/sidecar/src/Eryx.AgentHost/Services/LspToolSession.cs index 188020b..db0640a 100644 --- a/sidecar/src/Eryx.AgentHost/Services/LspToolSession.cs +++ b/sidecar/src/Eryx.AgentHost/Services/LspToolSession.cs @@ -4,6 +4,7 @@ using System.Reflection; using System.Text; using System.Text.Json; using System.Text.Json.Serialization; +using System.Text.Json.Serialization.Metadata; using Eryx.AgentHost.Contracts; using Microsoft.Extensions.AI; @@ -11,11 +12,7 @@ namespace Eryx.AgentHost.Services; internal sealed class LspToolSession : IAsyncDisposable { - private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web) - { - DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, - WriteIndented = true, - }; + private static readonly JsonSerializerOptions JsonOptions = CreateJsonSerializerOptions(); private readonly RunTurnLspProfileConfigDto _profile; private readonly string _projectPath; @@ -71,6 +68,19 @@ internal sealed class LspToolSession : IAsyncDisposable public IReadOnlyList Tools { get; } + internal static JsonSerializerOptions CreateJsonSerializerOptions() + { + JsonSerializerOptions options = new(JsonSerializerDefaults.Web) + { + TypeInfoResolver = new DefaultJsonTypeInfoResolver(), + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + WriteIndented = true, + }; + + options.MakeReadOnly(); + return options; + } + public static async Task StartAsync( RunTurnLspProfileConfigDto profile, string projectPath, diff --git a/sidecar/tests/Eryx.AgentHost.Tests/LspToolSessionTests.cs b/sidecar/tests/Eryx.AgentHost.Tests/LspToolSessionTests.cs new file mode 100644 index 0000000..1572086 --- /dev/null +++ b/sidecar/tests/Eryx.AgentHost.Tests/LspToolSessionTests.cs @@ -0,0 +1,27 @@ +using System.Text.Json; +using Eryx.AgentHost.Services; + +namespace Eryx.AgentHost.Tests; + +public sealed class LspToolSessionTests +{ + [Fact] + public void CreateJsonSerializerOptions_ReturnsReadOnlyResolverBackedOptions() + { + JsonSerializerOptions options = LspToolSession.CreateJsonSerializerOptions(); + + Assert.True(options.IsReadOnly); + Assert.NotNull(options.TypeInfoResolver); + + string json = JsonSerializer.Serialize( + new + { + RelativePath = "src/file.ts", + Line = 12, + Character = 4, + }, + options); + + Assert.Contains("relativePath", json); + } +}