@@ -10,8 +10,13 @@ namespace {{rootNamespace}}.{{domain.Name}}
1010 /// <summary >
1111 /// Represents an adapter for the {{ domain.Name }} domain to simplify the command interface.
1212 /// </summary >
13- public class {{ dehumanize domain.Name }} Adapter
13+ public class {{ dehumanize domain.Name }} Adapter : global::OpenQA.Selenium.DevTools.IAdapter
1414 {
15+ private static readonly JsonSerializerOptions jsonSerializerOptions = new()
16+ {
17+ TypeInfoResolver = {{ protocolVersion }}{{ dehumanize domain.Name }} SerializerContext.Default
18+ };
19+
1520 private readonly string m_domainName = "{{ dehumanize domain.Name }} ";
1621 private Dictionary<string , DevToolsEventData> m_eventMap = new Dictionary<string , DevToolsEventData>();
1722
@@ -46,9 +51,18 @@ namespace {{rootNamespace}}.{{domain.Name}}
4651 /// <summary >
4752 /// {{ xml-code-comment Description 2 }}
4853 /// </summary >
49- public Task<{{dehumanize Name}}CommandResponse> {{ dehumanize Name }} ({{ dehumanize Name }} CommandSettings command{{ #if NoParameters }} = null{{ /if }} , CancellationToken cancellationToken = default(CancellationToken), int? millisecondsTimeout = null, bool throwExceptionIfResponseNotReceived = true)
54+ public async Task<{{dehumanize Name}}CommandResponse> {{ dehumanize Name }} ({{ dehumanize Name }} CommandSettings command{{ #if NoParameters }} = null{{ /if }} , CancellationToken cancellationToken = default(CancellationToken), int? millisecondsTimeout = null, bool throwExceptionIfResponseNotReceived = true)
5055 {
51- return Session.SendCommand<{{dehumanize Name}}CommandSettings, {{ dehumanize Name }} CommandResponse>(command{{ #if NoParameters }} ?? new {{ dehumanize Name }} CommandSettings(){{ /if }} , cancellationToken, millisecondsTimeout, throwExceptionIfResponseNotReceived);
56+ {{ #if NoParameters }} command ??= new {{ dehumanize Name }} CommandSettings();{{ /if }}
57+ global::System.Text.Json.Nodes.JsonNode serializedCommand = JsonSerializer.SerializeToNode(command, jsonSerializerOptions);
58+ global::System.Text.Json.JsonElement? response = await Session.SendCommand(command.CommandName, serializedCommand, cancellationToken, millisecondsTimeout, throwExceptionIfResponseNotReceived).ConfigureAwait(false);
59+
60+ if (response == null)
61+ {
62+ return default({{ dehumanize Name }} CommandResponse);
63+ }
64+
65+ return response.Value.Deserialize<{{dehumanize Name}}CommandResponse>(jsonSerializerOptions);
5266 }
5367
5468 {{ /each }}
@@ -57,9 +71,8 @@ namespace {{rootNamespace}}.{{domain.Name}}
5771 {
5872 if (e.DomainName == m_domainName)
5973 {
60- if (m_eventMap.ContainsKey (e.EventName))
74+ if (m_eventMap.TryGetValue (e.EventName, out var eventData ))
6175 {
62- var eventData = m_eventMap[e.EventName];
6376 var eventArgs = e.EventData.Deserialize(eventData.EventArgsType, global::OpenQA.Selenium.DevTools.Json.DevToolsJsonOptions.Default);
6477 eventData.EventInvoker(eventArgs);
6578 }
@@ -76,5 +89,20 @@ namespace {{rootNamespace}}.{{domain.Name}}
7689 }
7790
7891 {{ /each }}
92+
93+ global::System.Text.Json.Nodes.JsonNode IAdapter.SerializeToNode<TCommand >(TCommand command)
94+ {
95+ return JsonSerializer.SerializeToNode(command, jsonSerializerOptions);
96+ }
7997 }
8098}
99+
100+ namespace {{ rootNamespace }}
101+ {
102+ [global::System.Text.Json.Serialization.JsonSerializable(typeof(string))] // Placeholder in case nothing gets generated
103+ {{ #each domain.Commands }}
104+ [global::System.Text.Json.Serialization.JsonSerializable(typeof(global::{{ ../rootNamespace }} .{{ dehumanize ../domain.Name }} .{{ dehumanize Name }} CommandSettings))]
105+ [global::System.Text.Json.Serialization.JsonSerializable(typeof(global::{{ ../rootNamespace }} .{{ dehumanize ../domain.Name }} .{{ dehumanize Name }} CommandResponse))]
106+ {{ /each }}
107+ internal sealed partial class {{ protocolVersion }}{{ dehumanize domain.Name }} SerializerContext : global::System.Text.Json.Serialization.JsonSerializerContext;
108+ }
0 commit comments