Skip to content

Commit 94b4a44

Browse files
stephentoubCopilot
andcommitted
Fix JsonRpc serialization of args and formatting
SerializeArgs was calling GetTypeInfo(typeof(object?[])), which is not provided by any of the source-generated JsonSerializerContexts and therefore threw NotSupportedException at runtime on macOS/Ubuntu (Windows happened to skip these code paths in earlier failures). Build the params JSON array manually, looking up TypeInfo by each argument's runtime type, which is what the merged source-gen resolver actually contains. Also insert the missing space before ':' in the PendingRequest primary-constructor base list to satisfy 'dotnet format --verify-no-changes'. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent cafa0f5 commit 94b4a44

1 file changed

Lines changed: 26 additions & 2 deletions

File tree

dotnet/src/JsonRpc.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,31 @@ private async Task HandleIncomingMethodAsync(string methodName, JsonElement mess
565565
return null;
566566
}
567567

568-
return JsonSerializer.SerializeToElement(args, _serializerOptions.GetTypeInfo(typeof(object?[])));
568+
// Source-generated JsonSerializerOptions do not provide metadata for object[],
569+
// so build the JSON array manually, serializing each element with a TypeInfo
570+
// looked up by its runtime type from the merged resolver.
571+
var buffer = new ArrayBufferWriter<byte>();
572+
using (var writer = new Utf8JsonWriter(buffer))
573+
{
574+
writer.WriteStartArray();
575+
foreach (var arg in args)
576+
{
577+
if (arg is null)
578+
{
579+
writer.WriteNullValue();
580+
}
581+
else
582+
{
583+
var typeInfo = _serializerOptions.GetTypeInfo(arg.GetType());
584+
JsonSerializer.Serialize(writer, arg, typeInfo);
585+
}
586+
}
587+
588+
writer.WriteEndArray();
589+
}
590+
591+
using var doc = JsonDocument.Parse(buffer.WrittenMemory);
592+
return doc.RootElement.Clone();
569593
}
570594

571595
private async Task SendResultResponseAsync(JsonElement id, object? result, CancellationToken cancellationToken)
@@ -618,7 +642,7 @@ await SendMessageAsync(new JsonRpcNotification
618642
}
619643
}
620644

621-
private sealed class PendingRequest(): TaskCompletionSource<JsonElement>(TaskCreationOptions.RunContinuationsAsynchronously);
645+
private sealed class PendingRequest() : TaskCompletionSource<JsonElement>(TaskCreationOptions.RunContinuationsAsynchronously);
622646

623647
private sealed class MethodRegistration
624648
{

0 commit comments

Comments
 (0)