Skip to content

Commit 745990b

Browse files
committed
Bidi serializer context
1 parent 77796f4 commit 745990b

File tree

4 files changed

+115
-13
lines changed

4 files changed

+115
-13
lines changed

dotnet/src/webdriver/BiDi/Communication/Broker.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
// under the License.
1818
// </copyright>
1919

20+
using OpenQA.Selenium.BiDi.Communication.Json;
2021
using OpenQA.Selenium.BiDi.Communication.Json.Converters;
2122
using OpenQA.Selenium.BiDi.Communication.Transport;
2223
using OpenQA.Selenium.Internal.Logging;
@@ -53,14 +54,14 @@ public class Broker : IAsyncDisposable
5354
private Task? _eventEmitterTask;
5455
private CancellationTokenSource? _receiveMessagesCancellationTokenSource;
5556

56-
private readonly JsonSerializerOptions _jsonSerializerOptions;
57+
private readonly BiDiSerializerContext _jsonSerializerContext;
5758

5859
internal Broker(BiDi bidi, ITransport transport)
5960
{
6061
_bidi = bidi;
6162
_transport = transport;
6263

63-
_jsonSerializerOptions = new JsonSerializerOptions
64+
var jsonSerializerOptions = new JsonSerializerOptions
6465
{
6566
PropertyNameCaseInsensitive = true,
6667
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
@@ -99,6 +100,8 @@ internal Broker(BiDi bidi, ITransport transport)
99100
new Json.Converters.Enumerable.GetRealmsResultConverter(),
100101
}
101102
};
103+
104+
_jsonSerializerContext = new BiDiSerializerContext(jsonSerializerOptions);
102105
}
103106

104107
public async Task ConnectAsync(CancellationToken cancellationToken)
@@ -114,7 +117,7 @@ private async Task ReceiveMessagesAsync(CancellationToken cancellationToken)
114117
{
115118
while (!cancellationToken.IsCancellationRequested)
116119
{
117-
var message = await _transport.ReceiveAsJsonAsync<Message>(_jsonSerializerOptions, cancellationToken);
120+
var message = await _transport.ReceiveAsJsonAsync<Message>(_jsonSerializerContext, cancellationToken);
118121

119122
switch (message)
120123
{
@@ -145,7 +148,7 @@ private async Task ProcessEventsAwaiterAsync()
145148
{
146149
foreach (var handler in eventHandlers.ToArray()) // copy handlers avoiding modified collection while iterating
147150
{
148-
var args = (EventArgs)result.Params.Deserialize(handler.EventArgsType, _jsonSerializerOptions)!;
151+
var args = (EventArgs)result.Params.Deserialize(handler.EventArgsType, _jsonSerializerContext)!;
149152

150153
args.BiDi = _bidi;
151154

@@ -177,7 +180,7 @@ public async Task<TResult> ExecuteCommandAsync<TResult>(Command command, Command
177180
{
178181
var result = await ExecuteCommandCoreAsync(command, options).ConfigureAwait(false);
179182

180-
return (TResult)((JsonElement)result).Deserialize(typeof(TResult), _jsonSerializerOptions)!;
183+
return (TResult)((JsonElement)result).Deserialize(typeof(TResult), _jsonSerializerContext)!;
181184
}
182185

183186
public async Task ExecuteCommandAsync(Command command, CommandOptions? options)
@@ -199,7 +202,7 @@ private async Task<object> ExecuteCommandCoreAsync(Command command, CommandOptio
199202

200203
_pendingCommands[command.Id] = tcs;
201204

202-
await _transport.SendAsJsonAsync(command, _jsonSerializerOptions, cts.Token).ConfigureAwait(false);
205+
await _transport.SendAsJsonAsync(command, _jsonSerializerContext, cts.Token).ConfigureAwait(false);
203206

204207
return await tcs.Task.ConfigureAwait(false);
205208
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace OpenQA.Selenium.BiDi.Communication.Json;
4+
5+
// https://github.com/dotnet/runtime/issues/72604
6+
[JsonSerializable(typeof(MessageSuccess))]
7+
[JsonSerializable(typeof(MessageError))]
8+
[JsonSerializable(typeof(MessageEvent))]
9+
10+
[JsonSerializable(typeof(Modules.Script.EvaluateResult.Success))]
11+
[JsonSerializable(typeof(Modules.Script.EvaluateResult.Exception))]
12+
13+
[JsonSerializable(typeof(Modules.Script.RemoteValue.Number), TypeInfoPropertyName = "Script_RemoteValue_Number")]
14+
[JsonSerializable(typeof(Modules.Script.RemoteValue.String), TypeInfoPropertyName = "Script_RemoteValue_String")]
15+
[JsonSerializable(typeof(Modules.Script.RemoteValue.Null), TypeInfoPropertyName = "Script_RemoteValue_Null")]
16+
[JsonSerializable(typeof(Modules.Script.RemoteValue.Undefined), TypeInfoPropertyName = "Script_RemoteValue_Undefined")]
17+
[JsonSerializable(typeof(Modules.Script.RemoteValue.Symbol))]
18+
[JsonSerializable(typeof(Modules.Script.RemoteValue.Object), TypeInfoPropertyName = "Script_RemoteValue_Object")]
19+
[JsonSerializable(typeof(Modules.Script.RemoteValue.Function))]
20+
[JsonSerializable(typeof(Modules.Script.RemoteValue.RegExp), TypeInfoPropertyName = "Script_RemoteValue_RegExp")]
21+
[JsonSerializable(typeof(Modules.Script.RemoteValue.RegExp.RegExpValue), TypeInfoPropertyName = "Script_RemoteValue_RegExp_RegExpValue")]
22+
[JsonSerializable(typeof(Modules.Script.RemoteValue.Date), TypeInfoPropertyName = "Script_RemoteValue_Date")]
23+
[JsonSerializable(typeof(Modules.Script.RemoteValue.Map), TypeInfoPropertyName = "Script_RemoteValue_Map")]
24+
[JsonSerializable(typeof(Modules.Script.RemoteValue.Set), TypeInfoPropertyName = "Script_RemoteValue_Set")]
25+
[JsonSerializable(typeof(Modules.Script.RemoteValue.WeakMap))]
26+
[JsonSerializable(typeof(Modules.Script.RemoteValue.WeakSet))]
27+
[JsonSerializable(typeof(Modules.Script.RemoteValue.Generator))]
28+
[JsonSerializable(typeof(Modules.Script.RemoteValue.Error))]
29+
[JsonSerializable(typeof(Modules.Script.RemoteValue.Proxy))]
30+
[JsonSerializable(typeof(Modules.Script.RemoteValue.Promise))]
31+
[JsonSerializable(typeof(Modules.Script.RemoteValue.TypedArray))]
32+
[JsonSerializable(typeof(Modules.Script.RemoteValue.ArrayBuffer))]
33+
[JsonSerializable(typeof(Modules.Script.RemoteValue.NodeList))]
34+
[JsonSerializable(typeof(Modules.Script.RemoteValue.HtmlCollection))]
35+
[JsonSerializable(typeof(Modules.Script.RemoteValue.Node))]
36+
[JsonSerializable(typeof(Modules.Script.RemoteValue.WindowProxy))]
37+
38+
[JsonSerializable(typeof(Modules.Script.LocalValue.String), TypeInfoPropertyName = "Script_LocalValue_String")]
39+
40+
[JsonSerializable(typeof(Modules.Script.Target.Realm), TypeInfoPropertyName = "Script_Target_Realm")]
41+
[JsonSerializable(typeof(Modules.Script.Target.Context), TypeInfoPropertyName = "Script_Target_Context")]
42+
43+
[JsonSerializable(typeof(Modules.Script.RealmInfo.Window))]
44+
[JsonSerializable(typeof(Modules.Script.RealmInfo.DedicatedWorker))]
45+
[JsonSerializable(typeof(Modules.Script.RealmInfo.SharedWorker))]
46+
[JsonSerializable(typeof(Modules.Script.RealmInfo.ServiceWorker))]
47+
[JsonSerializable(typeof(Modules.Script.RealmInfo.Worker))]
48+
[JsonSerializable(typeof(Modules.Script.RealmInfo.PaintWorklet))]
49+
[JsonSerializable(typeof(Modules.Script.RealmInfo.AudioWorklet))]
50+
[JsonSerializable(typeof(Modules.Script.RealmInfo.Worklet))]
51+
52+
[JsonSerializable(typeof(Modules.Log.Entry.Console))]
53+
[JsonSerializable(typeof(Modules.Log.Entry.Javascript))]
54+
//
55+
56+
[JsonSerializable(typeof(Command))]
57+
[JsonSerializable(typeof(Message))]
58+
59+
[JsonSerializable(typeof(Modules.Session.StatusResult))]
60+
[JsonSerializable(typeof(Modules.Session.NewResult))]
61+
62+
[JsonSerializable(typeof(Modules.Browser.CloseCommand), TypeInfoPropertyName = "Browser_CloseCommand")]
63+
[JsonSerializable(typeof(Modules.Browser.UserContextInfo))]
64+
[JsonSerializable(typeof(Modules.Browser.GetUserContextsResult))]
65+
66+
[JsonSerializable(typeof(Modules.BrowsingContext.CloseCommand), TypeInfoPropertyName = "BrowsingContext_CloseCommand")]
67+
[JsonSerializable(typeof(Modules.BrowsingContext.CreateResult))]
68+
[JsonSerializable(typeof(Modules.BrowsingContext.BrowsingContextInfo))]
69+
[JsonSerializable(typeof(Modules.BrowsingContext.NavigateResult))]
70+
[JsonSerializable(typeof(Modules.BrowsingContext.NavigationInfo))]
71+
[JsonSerializable(typeof(Modules.BrowsingContext.TraverseHistoryResult))]
72+
[JsonSerializable(typeof(Modules.BrowsingContext.LocateNodesResult))]
73+
[JsonSerializable(typeof(Modules.BrowsingContext.CaptureScreenshotResult))]
74+
[JsonSerializable(typeof(Modules.BrowsingContext.GetTreeResult))]
75+
[JsonSerializable(typeof(Modules.BrowsingContext.PrintResult))]
76+
[JsonSerializable(typeof(Modules.BrowsingContext.UserPromptOpenedEventArgs))]
77+
[JsonSerializable(typeof(Modules.BrowsingContext.UserPromptClosedEventArgs))]
78+
79+
[JsonSerializable(typeof(Modules.Network.BytesValue.String), TypeInfoPropertyName = "Network_BytesValue_String")]
80+
[JsonSerializable(typeof(Modules.Network.UrlPattern.String), TypeInfoPropertyName = "Network_UrlPattern_String")]
81+
[JsonSerializable(typeof(Modules.Network.ContinueWithAuthParameters.Default), TypeInfoPropertyName = "Network_ContinueWithAuthParameters_Default")]
82+
[JsonSerializable(typeof(Modules.Network.AddInterceptResult))]
83+
[JsonSerializable(typeof(Modules.Network.BeforeRequestSentEventArgs))]
84+
[JsonSerializable(typeof(Modules.Network.ResponseStartedEventArgs))]
85+
[JsonSerializable(typeof(Modules.Network.ResponseCompletedEventArgs))]
86+
[JsonSerializable(typeof(Modules.Network.FetchErrorEventArgs))]
87+
88+
[JsonSerializable(typeof(Modules.Script.Channel), TypeInfoPropertyName = "Script_Channel")]
89+
[JsonSerializable(typeof(Modules.Script.AddPreloadScriptResult))]
90+
[JsonSerializable(typeof(Modules.Script.EvaluateResult))]
91+
[JsonSerializable(typeof(Modules.Script.GetRealmsResult))]
92+
93+
[JsonSerializable(typeof(Modules.Log.Entry))]
94+
95+
[JsonSerializable(typeof(Modules.Storage.GetCookiesResult))]
96+
[JsonSerializable(typeof(Modules.Storage.DeleteCookiesResult))]
97+
[JsonSerializable(typeof(Modules.Storage.SetCookieResult))]
98+
internal partial class BiDiSerializerContext: JsonSerializerContext;

dotnet/src/webdriver/BiDi/Communication/Transport/ITransport.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
// under the License.
1818
// </copyright>
1919

20-
using System.Text.Json;
2120
using System.Threading.Tasks;
2221
using System.Threading;
2322
using System;
23+
using System.Text.Json.Serialization;
2424

2525
#nullable enable
2626

@@ -30,7 +30,7 @@ interface ITransport : IDisposable
3030
{
3131
Task ConnectAsync(CancellationToken cancellationToken);
3232

33-
Task<T> ReceiveAsJsonAsync<T>(JsonSerializerOptions jsonSerializerOptions, CancellationToken cancellationToken);
33+
Task<T> ReceiveAsJsonAsync<T>(JsonSerializerContext jsonSerializerContext, CancellationToken cancellationToken);
3434

35-
Task SendAsJsonAsync(Command command, JsonSerializerOptions jsonSerializerOptions, CancellationToken cancellationToken);
35+
Task SendAsJsonAsync(Command command, JsonSerializerContext jsonSerializerContext, CancellationToken cancellationToken);
3636
}

dotnet/src/webdriver/BiDi/Communication/Transport/WebSocketTransport.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
using System.Text.Json;
2626
using System.Text;
2727
using OpenQA.Selenium.Internal.Logging;
28+
using System.Text.Json.Serialization;
2829

2930
#nullable enable
3031

@@ -44,7 +45,7 @@ public async Task ConnectAsync(CancellationToken cancellationToken)
4445
await _webSocket.ConnectAsync(_uri, cancellationToken).ConfigureAwait(false);
4546
}
4647

47-
public async Task<T> ReceiveAsJsonAsync<T>(JsonSerializerOptions jsonSerializerOptions, CancellationToken cancellationToken)
48+
public async Task<T> ReceiveAsJsonAsync<T>(JsonSerializerContext jsonSerializerContext, CancellationToken cancellationToken)
4849
{
4950
using var ms = new MemoryStream();
5051

@@ -65,14 +66,14 @@ public async Task<T> ReceiveAsJsonAsync<T>(JsonSerializerOptions jsonSerializerO
6566
_logger.Trace($"BiDi RCV << {Encoding.UTF8.GetString(ms.ToArray())}");
6667
}
6768

68-
var res = await JsonSerializer.DeserializeAsync(ms, typeof(T), jsonSerializerOptions, cancellationToken).ConfigureAwait(false);
69+
var res = await JsonSerializer.DeserializeAsync(ms, typeof(T), jsonSerializerContext, cancellationToken).ConfigureAwait(false);
6970

7071
return (T)res!;
7172
}
7273

73-
public async Task SendAsJsonAsync(Command command, JsonSerializerOptions jsonSerializerOptions, CancellationToken cancellationToken)
74+
public async Task SendAsJsonAsync(Command command, JsonSerializerContext jsonSerializerContext, CancellationToken cancellationToken)
7475
{
75-
var buffer = JsonSerializer.SerializeToUtf8Bytes(command, typeof(Command), jsonSerializerOptions);
76+
var buffer = JsonSerializer.SerializeToUtf8Bytes(command, typeof(Command), jsonSerializerContext);
7677

7778
await _socketSendSemaphoreSlim.WaitAsync(cancellationToken);
7879

0 commit comments

Comments
 (0)