Skip to content

Commit e92ac14

Browse files
committed
Move DevTools JSON context to each adapter
1 parent c17accc commit e92ac14

File tree

5 files changed

+65
-18
lines changed

5 files changed

+65
-18
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// <copyright file="IAdapter.cs" company="Selenium Committers">
2+
// Licensed to the Software Freedom Conservancy (SFC) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The SFC licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
// </copyright>
19+
20+
using System.Text.Json.Nodes;
21+
22+
namespace OpenQA.Selenium.DevTools;
23+
24+
internal interface IAdapter
25+
{
26+
JsonNode SerializeToNode<TCommand>(TCommand command)
27+
where TCommand : ICommand;
28+
}

third_party/dotnet/devtools/src/generator/CodeGen/CommandGenerator.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using OpenQA.Selenium.DevToolsGenerator.ProtocolDefinition;
33
using System;
44
using System.Collections.Generic;
5-
using System.Linq;
65

76
namespace OpenQA.Selenium.DevToolsGenerator.CodeGen
87
{
@@ -34,8 +33,7 @@ public override IDictionary<string, string> GenerateCode(CommandDefinition comma
3433
className = className,
3534
domain = context.Domain,
3635
rootNamespace = Settings.RootNamespace,
37-
context = context,
38-
protocolVersion = Settings.RootNamespace.Split('.').Last()
36+
context = context
3937
});
4038

4139
var outputPath = Utility.ReplaceTokensInPath(Settings.DefinitionTemplates.CommandTemplate.OutputPath, className, context, Settings);

third_party/dotnet/devtools/src/generator/CodeGen/DomainGenerator.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using OpenQA.Selenium.DevToolsGenerator.ProtocolDefinition;
44
using System;
55
using System.Collections.Generic;
6+
using System.Linq;
67

78
namespace OpenQA.Selenium.DevToolsGenerator.CodeGen
89
{
@@ -61,7 +62,8 @@ public override IDictionary<string, string> GenerateCode(DomainDefinition domain
6162
domain = domainDefinition,
6263
className = className,
6364
rootNamespace = Settings.RootNamespace,
64-
context = context
65+
context = context,
66+
protocolVersion = Settings.RootNamespace.Split('.').Last()
6567
});
6668

6769
var outputPath = Utility.ReplaceTokensInPath(Settings.DefinitionTemplates.DomainTemplate.OutputPath, className, context, Settings);

third_party/dotnet/devtools/src/generator/Templates/command.hbs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,3 @@ namespace {{rootNamespace}}.{{domain.Name}}
5555
{{/each}}
5656
}
5757
}
58-
59-
namespace {{rootNamespace}}
60-
{
61-
[global::System.Text.Json.Serialization.JsonSerializable(typeof(global::{{rootNamespace}}.{{domain.Name}}.{{className}}CommandSettings))]
62-
internal sealed partial class {{protocolVersion}}RequestSerializationContext;
63-
64-
[global::System.Text.Json.Serialization.JsonSerializable(typeof(global::{{rootNamespace}}.{{domain.Name}}.{{className}}CommandResponse))]
65-
internal sealed partial class {{protocolVersion}}ResponseSerializationContext;
66-
}

third_party/dotnet/devtools/src/generator/Templates/domain.hbs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)