Skip to content

Commit d5d6877

Browse files
committed
Move ownership of json options to bidi
1 parent a832596 commit d5d6877

File tree

3 files changed

+37
-36
lines changed

3 files changed

+37
-36
lines changed

dotnet/src/webdriver/BiDi/BiDi.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,51 @@
1919

2020
using System;
2121
using System.Collections.Concurrent;
22+
using System.Text.Json;
23+
using System.Text.Json.Serialization;
2224
using System.Threading;
2325
using System.Threading.Tasks;
2426
using OpenQA.Selenium.BiDi.Communication;
27+
using OpenQA.Selenium.BiDi.Communication.Json.Converters;
2528

2629
namespace OpenQA.Selenium.BiDi;
2730

2831
public sealed class BiDi : IAsyncDisposable
2932
{
3033
private readonly Broker _broker;
34+
private readonly JsonSerializerOptions _jsonOptions;
3135

3236
private readonly ConcurrentDictionary<Type, Module> _modules = [];
3337

3438
private BiDi(string url)
3539
{
3640
var uri = new Uri(url);
3741

38-
_broker = new Broker(this, uri);
42+
_jsonOptions = new JsonSerializerOptions
43+
{
44+
PropertyNameCaseInsensitive = true,
45+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
46+
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
47+
48+
// BiDi returns special numbers such as "NaN" as strings
49+
// Additionally, -0 is returned as a string "-0"
50+
NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals | JsonNumberHandling.AllowReadingFromString,
51+
Converters =
52+
{
53+
new BrowsingContextConverter(this),
54+
new BrowserUserContextConverter(this),
55+
new CollectorConverter(this),
56+
new InterceptConverter(this),
57+
new HandleConverter(this),
58+
new InternalIdConverter(this),
59+
new PreloadScriptConverter(this),
60+
new RealmConverter(this),
61+
new DateTimeOffsetConverter(),
62+
new WebExtensionConverter(this),
63+
}
64+
};
65+
66+
_broker = new Broker(this, uri, _jsonOptions);
3967
}
4068

4169
internal Session.SessionModule SessionModule => AsModule<Session.SessionModule>();
@@ -60,7 +88,7 @@ private BiDi(string url)
6088

6189
public TModule AsModule<TModule>() where TModule : Module, new()
6290
{
63-
return (TModule)_modules.GetOrAdd(typeof(TModule), _ => Module.Create<TModule>(_broker));
91+
return (TModule)_modules.GetOrAdd(typeof(TModule), _ => Module.Create<TModule>(this, _broker, _jsonOptions));
6492
}
6593

6694
public Task<Session.StatusResult> StatusAsync()

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

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
// under the License.
1818
// </copyright>
1919

20-
using OpenQA.Selenium.BiDi.Communication.Json;
21-
using OpenQA.Selenium.BiDi.Communication.Json.Converters;
2220
using OpenQA.Selenium.BiDi.Communication.Transport;
2321
using OpenQA.Selenium.Internal.Logging;
2422
using System;
@@ -53,39 +51,12 @@ public sealed class Broker : IAsyncDisposable
5351
private Task? _eventEmitterTask;
5452
private CancellationTokenSource? _receiveMessagesCancellationTokenSource;
5553

56-
internal Broker(BiDi bidi, Uri url)
54+
internal Broker(BiDi bidi, Uri url, JsonSerializerOptions jsonOptions)
5755
{
5856
_bidi = bidi;
5957
_transport = new WebSocketTransport(url);
6058
}
6159

62-
public JsonSerializerOptions CreateOptions()
63-
{
64-
return new JsonSerializerOptions
65-
{
66-
PropertyNameCaseInsensitive = true,
67-
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
68-
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
69-
70-
// BiDi returns special numbers such as "NaN" as strings
71-
// Additionally, -0 is returned as a string "-0"
72-
NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals | JsonNumberHandling.AllowReadingFromString,
73-
Converters =
74-
{
75-
new BrowsingContextConverter(_bidi),
76-
new BrowserUserContextConverter(_bidi),
77-
new CollectorConverter(_bidi),
78-
new InterceptConverter(_bidi),
79-
new HandleConverter(_bidi),
80-
new InternalIdConverter(_bidi),
81-
new PreloadScriptConverter(_bidi),
82-
new RealmConverter(_bidi),
83-
new DateTimeOffsetConverter(),
84-
new WebExtensionConverter(_bidi),
85-
}
86-
};
87-
}
88-
8960
public async Task ConnectAsync(CancellationToken cancellationToken)
9061
{
9162
await _transport.ConnectAsync(cancellationToken).ConfigureAwait(false);

dotnet/src/webdriver/BiDi/Module.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@ public abstract class Module
3131

3232
protected internal abstract JsonSerializerContext ConfigureJson(JsonSerializerOptions options);
3333

34-
public static TModule Create<TModule>(Broker broker) where TModule : Module, new()
34+
public static TModule Create<TModule>(BiDi bidi, Broker broker, JsonSerializerOptions jsonOptions) where TModule : Module, new()
3535
{
36-
TModule module = new();
37-
module.Broker = broker;
38-
module.JsonContext = module.ConfigureJson(broker.CreateOptions());
36+
TModule module = new()
37+
{
38+
Broker = broker
39+
};
40+
module.JsonContext = module.ConfigureJson(jsonOptions);
3941
return module;
4042
}
4143
}

0 commit comments

Comments
 (0)