Skip to content

Commit b7f283c

Browse files
[dotnet] Annotate nullable reference types on CDP-generated code (#15255)
1 parent be61980 commit b7f283c

File tree

9 files changed

+47
-18
lines changed

9 files changed

+47
-18
lines changed

dotnet/src/webdriver/DevTools/DevToolsEventData.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class DevToolsEventData
3434
/// <param name="eventArgsType">The type of the event args for the event to be raised.</param>
3535
/// <param name="invoker">The method that will be used to invoke the event.</param>
3636
/// <exception cref="ArgumentNullException">If<paramref name="eventArgsType"/> or <paramref name="invoker"/> is <see langword="null"/>.</exception>
37-
public DevToolsEventData(Type eventArgsType, Action<object> invoker)
37+
public DevToolsEventData(Type eventArgsType, Action<object?> invoker)
3838
{
3939
EventArgsType = eventArgsType ?? throw new ArgumentNullException(nameof(eventArgsType));
4040
EventInvoker = invoker ?? throw new ArgumentNullException(nameof(invoker));
@@ -48,6 +48,6 @@ public DevToolsEventData(Type eventArgsType, Action<object> invoker)
4848
/// <summary>
4949
/// The method to called to raise the event.
5050
/// </summary>
51-
public Action<object> EventInvoker { get; }
51+
public Action<object?> EventInvoker { get; }
5252
}
5353
}

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static string GetTypeMappingForType(TypeDefinition typeDefinition, Domain
5858
{
5959
var primitiveType = typeInfo.TypeName;
6060

61-
if (typeDefinition.Optional && typeInfo.ByRef)
61+
if (typeDefinition.Optional)
6262
{
6363
primitiveType += "?";
6464
}
@@ -70,16 +70,17 @@ public static string GetTypeMappingForType(TypeDefinition typeDefinition, Domain
7070

7171
return primitiveType;
7272
}
73+
7374
mappedType = $"{typeInfo.Namespace}.{typeInfo.TypeName}";
74-
if (typeDefinition.Optional && typeInfo.ByRef)
75+
if (typeDefinition.Optional)
7576
{
7677
mappedType += "?";
7778
}
7879
}
7980
else if (knownTypes.TryGetValue($"{domainDefinition.Name}.{type}", out typeInfo))
8081
{
8182
mappedType = typeInfo.TypeName;
82-
if (typeInfo.ByRef && typeDefinition.Optional)
83+
if (typeDefinition.Optional)
8384
{
8485
mappedType += "?";
8586
}
@@ -89,15 +90,15 @@ public static string GetTypeMappingForType(TypeDefinition typeDefinition, Domain
8990
switch (type)
9091
{
9192
case "number":
92-
mappedType = typeDefinition.Optional ? "double?" : "double";
93+
mappedType = "double";
9394
break;
9495

9596
case "integer":
96-
mappedType = typeDefinition.Optional ? "long?" : "long";
97+
mappedType = "long";
9798
break;
9899

99100
case "boolean":
100-
mappedType = typeDefinition.Optional ? "bool?" : "bool";
101+
mappedType = "bool";
101102
break;
102103

103104
case "string":
@@ -115,12 +116,17 @@ public static string GetTypeMappingForType(TypeDefinition typeDefinition, Domain
115116

116117
case "array":
117118
var items = typeDefinition.Items ?? throw new InvalidOperationException("Type definition was type array but has no Items");
118-
mappedType = GetTypeMappingForType(items, domainDefinition, knownTypes, true);
119+
mappedType = GetTypeMappingForType(items, domainDefinition, knownTypes, isArray: true);
119120
break;
120121

121122
default:
122123
throw new InvalidOperationException($"Unmapped data type: {type}");
123124
}
125+
126+
if (typeDefinition.Optional)
127+
{
128+
mappedType += "?";
129+
}
124130
}
125131

126132
if (isArray)

third_party/dotnet/devtools/src/generator/ProtocolDefinition/CommandDefinition.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,8 @@ public sealed class CommandDefinition : ProtocolDefinitionItem
2020

2121
[JsonIgnore]
2222
public bool NoParameters => Parameters == null || Parameters.Count == 0;
23+
24+
[JsonIgnore]
25+
public bool NoReturn => Returns == null || Returns.Count == 0;
2326
}
2427
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
// <auto-generated />
2+
3+
#nullable enable
4+
25
namespace {{rootNamespace}}.{{domain.Name}}
36
{
47
using System.Text.Json.Serialization;
@@ -28,7 +31,7 @@ namespace {{rootNamespace}}.{{domain.Name}}
2831
{{/if}}
2932
[JsonPropertyName("{{Name}}")]
3033
{{#if Optional}}[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]{{/if}}
31-
public {{typemap ../context}} {{dehumanize Name}} { get; set; }
34+
public {{typemap ../context}} {{dehumanize Name}} { get; set; }{{#unless Optional}} = default!;{{/unless}}
3235

3336
{{/each}}
3437
}
@@ -50,7 +53,7 @@ namespace {{rootNamespace}}.{{domain.Name}}
5053
{{/if}}
5154
[JsonPropertyName("{{Name}}")]
5255
{{#if Optional}}[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]{{/if}}
53-
public {{typemap ../context}} {{dehumanize Name}} { get; set; }
56+
public {{typemap ../context}} {{dehumanize Name}} { get; set; }{{#unless Optional}} = default!;{{/unless}}
5457

5558
{{/each}}
5659
}

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
// <auto-generated />
2+
3+
#nullable enable
4+
25
namespace {{rootNamespace}}.{{domain.Name}}
36
{
47
using System;
@@ -38,22 +41,24 @@ namespace {{rootNamespace}}.{{domain.Name}}
3841
/// <summary>
3942
/// {{xml-code-comment Description 2}}
4043
/// </summary>
41-
public event EventHandler<{{dehumanize Name}}EventArgs> {{dehumanize Name}};
44+
public event EventHandler<{{dehumanize Name}}EventArgs>? {{dehumanize Name}};
4245

4346
{{/each}}
47+
#nullable disable warnings
4448

4549
{{#each domain.Commands}}
4650
/// <summary>
4751
/// {{xml-code-comment Description 2}}
4852
/// </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)
53+
public Task<{{dehumanize Name}}CommandResponse{{#if NoReturn}}?{{/if}}> {{dehumanize Name}}({{dehumanize Name}}CommandSettings{{#if NoParameters}}?{{/if}} command{{#if NoParameters}} = null{{/if}}, CancellationToken cancellationToken = default(CancellationToken), int? millisecondsTimeout = null, bool throwExceptionIfResponseNotReceived = true)
5054
{
5155
return Session.SendCommand<{{dehumanize Name}}CommandSettings, {{dehumanize Name}}CommandResponse>(command{{#if NoParameters}} ?? new {{dehumanize Name}}CommandSettings(){{/if}}, cancellationToken, millisecondsTimeout, throwExceptionIfResponseNotReceived);
5256
}
5357

5458
{{/each}}
59+
#nullable enable warnings
5560

56-
private void OnDevToolsEventReceived(object sender, DevToolsEventReceivedEventArgs e)
61+
private void OnDevToolsEventReceived(object? sender, DevToolsEventReceivedEventArgs e)
5762
{
5863
if (e.DomainName == m_domainName)
5964
{
@@ -67,7 +72,7 @@ namespace {{rootNamespace}}.{{domain.Name}}
6772
}
6873

6974
{{#each domain.Events}}
70-
private void On{{dehumanize Name}}(object rawEventArgs)
75+
private void On{{dehumanize Name}}(object? rawEventArgs)
7176
{
7277
if (rawEventArgs is {{dehumanize Name}}EventArgs e)
7378
{

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
// <auto-generated />
2+
3+
#nullable enable
4+
25
namespace {{rootNamespace}}.{{domain.Name}}
36
{
47
using System;
@@ -21,7 +24,7 @@ namespace {{rootNamespace}}.{{domain.Name}}
2124
{{/if}}
2225
[JsonPropertyName("{{Name}}")]
2326
{{#if Optional}}[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]{{/if}}
24-
public {{typemap ../context}} {{dehumanize Name}} { get; set; }
27+
public {{typemap ../context}} {{dehumanize Name}} { get; set; }{{#unless Optional}} = default!;{{/unless}}
2528

2629
{{/each}}
2730
}

third_party/dotnet/devtools/src/generator/Templates/type-enum.hbs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
// <auto-generated />
2+
3+
#nullable enable
4+
25
namespace {{rootNamespace}}.{{domain.Name}}
36
{
47
using System.Runtime.Serialization;

third_party/dotnet/devtools/src/generator/Templates/type-hash.hbs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
// <auto-generated />
2+
3+
#nullable enable
4+
25
namespace {{rootNamespace}}.{{domain.Name}}
36
{
47
using System.Collections.Generic;
@@ -14,7 +17,7 @@ namespace {{rootNamespace}}.{{domain.Name}}
1417
///</summary>
1518
[JsonPropertyName("{{Name}}")]
1619
{{#if Optional}}[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]{{/if}}
17-
public {{typemap ../context}} {{dehumanize Name}} { get; set; }
20+
public {{typemap ../context}} {{dehumanize Name}} { get; set; }{{#unless Optional}} = default!;{{/unless}}
1821

1922
{{/each}}
2023
}

third_party/dotnet/devtools/src/generator/Templates/type-object.hbs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
// <auto-generated />
2+
3+
#nullable enable
4+
25
namespace {{rootNamespace}}.{{domain.Name}}
36
{
47
using System.Text.Json.Serialization;
@@ -14,7 +17,7 @@ namespace {{rootNamespace}}.{{domain.Name}}
1417
///</summary>
1518
[JsonPropertyName("{{Name}}")]
1619
{{#if Optional}}[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]{{/if}}
17-
public {{typemap ../context}} {{dehumanize Name}} { get; set; }
20+
public {{typemap ../context}} {{dehumanize Name}} { get; set; }{{#unless Optional}} = default!;{{/unless}}
1821

1922
{{/each}}
2023
}

0 commit comments

Comments
 (0)