diff --git a/third_party/dotnet/devtools/src/generator/CodeGen/DomainGenerator.cs b/third_party/dotnet/devtools/src/generator/CodeGen/DomainGenerator.cs index 84c2c1988f579..b76a8929ab43d 100644 --- a/third_party/dotnet/devtools/src/generator/CodeGen/DomainGenerator.cs +++ b/third_party/dotnet/devtools/src/generator/CodeGen/DomainGenerator.cs @@ -3,6 +3,7 @@ using OpenQA.Selenium.DevToolsGenerator.ProtocolDefinition; using System; using System.Collections.Generic; +using System.Linq; namespace OpenQA.Selenium.DevToolsGenerator.CodeGen { @@ -61,7 +62,8 @@ public override IDictionary GenerateCode(DomainDefinition domain domain = domainDefinition, className = className, rootNamespace = Settings.RootNamespace, - context = context + context = context, + protocolVersion = Settings.RootNamespace.Split('.').Last() }); var outputPath = Utility.ReplaceTokensInPath(Settings.DefinitionTemplates.DomainTemplate.OutputPath, className, context, Settings); diff --git a/third_party/dotnet/devtools/src/generator/Templates/domain.hbs b/third_party/dotnet/devtools/src/generator/Templates/domain.hbs index 27c483f19d06e..13b4b47e451f6 100644 --- a/third_party/dotnet/devtools/src/generator/Templates/domain.hbs +++ b/third_party/dotnet/devtools/src/generator/Templates/domain.hbs @@ -12,6 +12,11 @@ namespace {{rootNamespace}}.{{domain.Name}} /// public class {{dehumanize domain.Name}}Adapter { + private static readonly JsonSerializerOptions jsonSerializerOptions = new() + { + TypeInfoResolver = {{protocolVersion}}{{dehumanize domain.Name}}SerializerContext.Default + }; + private readonly string m_domainName = "{{dehumanize domain.Name}}"; private Dictionary m_eventMap = new Dictionary(); @@ -46,9 +51,18 @@ namespace {{rootNamespace}}.{{domain.Name}} /// /// {{xml-code-comment Description 2}} /// - 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) + 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) { - return Session.SendCommand<{{dehumanize Name}}CommandSettings, {{dehumanize Name}}CommandResponse>(command{{#if NoParameters}} ?? new {{dehumanize Name}}CommandSettings(){{/if}}, cancellationToken, millisecondsTimeout, throwExceptionIfResponseNotReceived); + {{#if NoParameters}}command ??= new {{dehumanize Name}}CommandSettings();{{/if}} + global::System.Text.Json.Nodes.JsonNode serializedCommand = JsonSerializer.SerializeToNode(command, jsonSerializerOptions); + global::System.Text.Json.JsonElement? response = await Session.SendCommand(command.CommandName, serializedCommand, cancellationToken, millisecondsTimeout, throwExceptionIfResponseNotReceived).ConfigureAwait(false); + + if (response == null) + { + return default({{dehumanize Name}}CommandResponse); + } + + return response.Value.Deserialize<{{dehumanize Name}}CommandResponse>(jsonSerializerOptions); } {{/each}} @@ -57,9 +71,8 @@ namespace {{rootNamespace}}.{{domain.Name}} { if (e.DomainName == m_domainName) { - if (m_eventMap.ContainsKey(e.EventName)) + if (m_eventMap.TryGetValue(e.EventName, out var eventData)) { - var eventData = m_eventMap[e.EventName]; var eventArgs = e.EventData.Deserialize(eventData.EventArgsType, global::OpenQA.Selenium.DevTools.Json.DevToolsJsonOptions.Default); eventData.EventInvoker(eventArgs); } @@ -78,3 +91,13 @@ namespace {{rootNamespace}}.{{domain.Name}} {{/each}} } } + +namespace {{rootNamespace}} +{ + [global::System.Text.Json.Serialization.JsonSerializable(typeof(string))] // Placeholder in case nothing gets generated + {{#each domain.Commands}} + [global::System.Text.Json.Serialization.JsonSerializable(typeof(global::{{../rootNamespace}}.{{dehumanize ../domain.Name}}.{{dehumanize Name}}CommandSettings))] + [global::System.Text.Json.Serialization.JsonSerializable(typeof(global::{{../rootNamespace}}.{{dehumanize ../domain.Name}}.{{dehumanize Name}}CommandResponse))] + {{/each}} + internal sealed partial class {{protocolVersion}}{{dehumanize domain.Name}}SerializerContext : global::System.Text.Json.Serialization.JsonSerializerContext; +}