Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dotnet] Annotate nullable reference types on CDP-generated code #15255

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions dotnet/src/webdriver/DevTools/DevToolsEventData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class DevToolsEventData
/// <param name="eventArgsType">The type of the event args for the event to be raised.</param>
/// <param name="invoker">The method that will be used to invoke the event.</param>
/// <exception cref="ArgumentNullException">If<paramref name="eventArgsType"/> or <paramref name="invoker"/> is <see langword="null"/>.</exception>
public DevToolsEventData(Type eventArgsType, Action<object> invoker)
public DevToolsEventData(Type eventArgsType, Action<object?> invoker)
{
EventArgsType = eventArgsType ?? throw new ArgumentNullException(nameof(eventArgsType));
EventInvoker = invoker ?? throw new ArgumentNullException(nameof(invoker));
Expand All @@ -48,6 +48,6 @@ public DevToolsEventData(Type eventArgsType, Action<object> invoker)
/// <summary>
/// The method to called to raise the event.
/// </summary>
public Action<object> EventInvoker { get; }
public Action<object?> EventInvoker { get; }
}
}
20 changes: 13 additions & 7 deletions third_party/dotnet/devtools/src/generator/CodeGen/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static string GetTypeMappingForType(TypeDefinition typeDefinition, Domain
{
var primitiveType = typeInfo.TypeName;

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

return primitiveType;
}

mappedType = $"{typeInfo.Namespace}.{typeInfo.TypeName}";
if (typeDefinition.Optional && typeInfo.ByRef)
if (typeDefinition.Optional)
{
mappedType += "?";
}
}
else if (knownTypes.TryGetValue($"{domainDefinition.Name}.{type}", out typeInfo))
{
mappedType = typeInfo.TypeName;
if (typeInfo.ByRef && typeDefinition.Optional)
if (typeDefinition.Optional)
{
mappedType += "?";
}
Expand All @@ -89,15 +90,15 @@ public static string GetTypeMappingForType(TypeDefinition typeDefinition, Domain
switch (type)
{
case "number":
mappedType = typeDefinition.Optional ? "double?" : "double";
mappedType = "double";
break;

case "integer":
mappedType = typeDefinition.Optional ? "long?" : "long";
mappedType = "long";
break;

case "boolean":
mappedType = typeDefinition.Optional ? "bool?" : "bool";
mappedType = "bool";
break;

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

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

default:
throw new InvalidOperationException($"Unmapped data type: {type}");
}

if (typeDefinition.Optional)
{
mappedType += "?";
}
}

if (isArray)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@ public sealed class CommandDefinition : ProtocolDefinitionItem

[JsonIgnore]
public bool NoParameters => Parameters == null || Parameters.Count == 0;

[JsonIgnore]
public bool NoReturn => Returns == null || Returns.Count == 0;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// <auto-generated />

#nullable enable

namespace {{rootNamespace}}.{{domain.Name}}
{
using System.Text.Json.Serialization;
Expand Down Expand Up @@ -28,7 +31,7 @@ namespace {{rootNamespace}}.{{domain.Name}}
{{/if}}
[JsonPropertyName("{{Name}}")]
{{#if Optional}}[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]{{/if}}
public {{typemap ../context}} {{dehumanize Name}} { get; set; }
public {{typemap ../context}} {{dehumanize Name}} { get; set; }{{#unless Optional}} = default!;{{/unless}}

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

{{/each}}
}
Expand Down
13 changes: 9 additions & 4 deletions third_party/dotnet/devtools/src/generator/Templates/domain.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// <auto-generated />

#nullable enable

namespace {{rootNamespace}}.{{domain.Name}}
{
using System;
Expand Down Expand Up @@ -38,22 +41,24 @@ namespace {{rootNamespace}}.{{domain.Name}}
/// <summary>
/// {{xml-code-comment Description 2}}
/// </summary>
public event EventHandler<{{dehumanize Name}}EventArgs> {{dehumanize Name}};
public event EventHandler<{{dehumanize Name}}EventArgs>? {{dehumanize Name}};

{{/each}}
#nullable disable warnings

{{#each domain.Commands}}
/// <summary>
/// {{xml-code-comment Description 2}}
/// </summary>
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 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)
{
return Session.SendCommand<{{dehumanize Name}}CommandSettings, {{dehumanize Name}}CommandResponse>(command{{#if NoParameters}} ?? new {{dehumanize Name}}CommandSettings(){{/if}}, cancellationToken, millisecondsTimeout, throwExceptionIfResponseNotReceived);
}

{{/each}}
#nullable enable warnings

private void OnDevToolsEventReceived(object sender, DevToolsEventReceivedEventArgs e)
private void OnDevToolsEventReceived(object? sender, DevToolsEventReceivedEventArgs e)
{
if (e.DomainName == m_domainName)
{
Expand All @@ -67,7 +72,7 @@ namespace {{rootNamespace}}.{{domain.Name}}
}

{{#each domain.Events}}
private void On{{dehumanize Name}}(object rawEventArgs)
private void On{{dehumanize Name}}(object? rawEventArgs)
{
if (rawEventArgs is {{dehumanize Name}}EventArgs e)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// <auto-generated />

#nullable enable

namespace {{rootNamespace}}.{{domain.Name}}
{
using System;
Expand All @@ -21,7 +24,7 @@ namespace {{rootNamespace}}.{{domain.Name}}
{{/if}}
[JsonPropertyName("{{Name}}")]
{{#if Optional}}[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]{{/if}}
public {{typemap ../context}} {{dehumanize Name}} { get; set; }
public {{typemap ../context}} {{dehumanize Name}} { get; set; }{{#unless Optional}} = default!;{{/unless}}

{{/each}}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// <auto-generated />

#nullable enable

namespace {{rootNamespace}}.{{domain.Name}}
{
using System.Runtime.Serialization;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// <auto-generated />

#nullable enable

namespace {{rootNamespace}}.{{domain.Name}}
{
using System.Collections.Generic;
Expand All @@ -14,7 +17,7 @@ namespace {{rootNamespace}}.{{domain.Name}}
///</summary>
[JsonPropertyName("{{Name}}")]
{{#if Optional}}[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]{{/if}}
public {{typemap ../context}} {{dehumanize Name}} { get; set; }
public {{typemap ../context}} {{dehumanize Name}} { get; set; }{{#unless Optional}} = default!;{{/unless}}

{{/each}}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// <auto-generated />

#nullable enable

namespace {{rootNamespace}}.{{domain.Name}}
{
using System.Text.Json.Serialization;
Expand All @@ -14,7 +17,7 @@ namespace {{rootNamespace}}.{{domain.Name}}
///</summary>
[JsonPropertyName("{{Name}}")]
{{#if Optional}}[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]{{/if}}
public {{typemap ../context}} {{dehumanize Name}} { get; set; }
public {{typemap ../context}} {{dehumanize Name}} { get; set; }{{#unless Optional}} = default!;{{/unless}}

{{/each}}
}
Expand Down
Loading