Skip to content

Commit 595408b

Browse files
[dotnet] Annotate nullability on most remaining types (#15257)
* [dotnet] Annotate nullability on most remaining types * fix bool check * Improve experience with `By` * Remove additional validation * Remove complexity from `By` * Update dotnet/src/webdriver/DomMutationData.cs * Simplify null warnings to show where exactly we are suppressing nulls
1 parent 5ca1cc0 commit 595408b

20 files changed

+343
-442
lines changed

dotnet/src/webdriver/By.cs

Lines changed: 87 additions & 81 deletions
Large diffs are not rendered by default.

dotnet/src/webdriver/Chromium/ChromiumDriver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ public DevToolsSession GetDevToolsSession(DevToolsOptions options)
330330

331331
try
332332
{
333-
DevToolsSession session = new DevToolsSession(debuggerAddress?.ToString(), options);
333+
DevToolsSession session = new DevToolsSession(debuggerAddress?.ToString()!, options);
334334
Task.Run(async () => await session.StartSession()).GetAwaiter().GetResult();
335335
this.devToolsSession = session;
336336
}

dotnet/src/webdriver/Cookie.cs

Lines changed: 50 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
using System.Linq;
2525
using System.Text.Json.Serialization;
2626

27+
#nullable enable
28+
2729
namespace OpenQA.Selenium
2830
{
2931
/// <summary>
@@ -32,15 +34,20 @@ namespace OpenQA.Selenium
3234
[Serializable]
3335
public class Cookie
3436
{
35-
private string cookieName;
36-
private string cookieValue;
37-
private string cookiePath;
38-
private string cookieDomain;
39-
private string sameSite;
40-
private bool isHttpOnly;
41-
private bool secure;
42-
private DateTime? cookieExpiry;
43-
private readonly string[] sameSiteValues = { "Strict", "Lax", "None" };
37+
private readonly string cookieName;
38+
private readonly string cookieValue;
39+
private readonly string? cookiePath;
40+
private readonly string? cookieDomain;
41+
private readonly string? sameSite;
42+
private readonly bool isHttpOnly;
43+
private readonly bool secure;
44+
private readonly DateTime? cookieExpiry;
45+
private readonly HashSet<string?> sameSiteValues = new HashSet<string?>()
46+
{
47+
"Strict",
48+
"Lax",
49+
"None"
50+
};
4451

4552
/// <summary>
4653
/// Initializes a new instance of the <see cref="Cookie"/> class with a specific name and value.
@@ -65,7 +72,7 @@ public Cookie(string name, string value)
6572
/// <exception cref="ArgumentException">If the name is <see langword="null"/> or an empty string,
6673
/// or if it contains a semi-colon.</exception>
6774
/// <exception cref="ArgumentNullException">If the value is <see langword="null"/>.</exception>
68-
public Cookie(string name, string value, string path)
75+
public Cookie(string name, string value, string? path)
6976
: this(name, value, path, null)
7077
{
7178
}
@@ -81,7 +88,7 @@ public Cookie(string name, string value, string path)
8188
/// <exception cref="ArgumentException">If the name is <see langword="null"/> or an empty string,
8289
/// or if it contains a semi-colon.</exception>
8390
/// <exception cref="ArgumentNullException">If the value is <see langword="null"/>.</exception>
84-
public Cookie(string name, string value, string path, DateTime? expiry)
91+
public Cookie(string name, string value, string? path, DateTime? expiry)
8592
: this(name, value, null, path, expiry)
8693
{
8794
}
@@ -98,7 +105,7 @@ public Cookie(string name, string value, string path, DateTime? expiry)
98105
/// <exception cref="ArgumentException">If the name is <see langword="null"/> or an empty string,
99106
/// or if it contains a semi-colon.</exception>
100107
/// <exception cref="ArgumentNullException">If the value is <see langword="null"/>.</exception>
101-
public Cookie(string name, string value, string domain, string path, DateTime? expiry)
108+
public Cookie(string name, string value, string? domain, string? path, DateTime? expiry)
102109
: this(name, value, domain, path, expiry, false, false, null)
103110
{
104111
}
@@ -118,7 +125,7 @@ public Cookie(string name, string value, string domain, string path, DateTime? e
118125
/// <exception cref="ArgumentException">If the name and value are both an empty string,
119126
/// if the name contains a semi-colon, or if same site value is not valid.</exception>
120127
/// <exception cref="ArgumentNullException">If the name, value or currentUrl is <see langword="null"/>.</exception>
121-
public Cookie(string name, string value, string domain, string path, DateTime? expiry, bool secure, bool isHttpOnly, string sameSite)
128+
public Cookie(string name, string value, string? domain, string? path, DateTime? expiry, bool secure, bool isHttpOnly, string? sameSite)
122129
{
123130
if (name == null)
124131
{
@@ -135,7 +142,7 @@ public Cookie(string name, string value, string domain, string path, DateTime? e
135142
throw new ArgumentException("Cookie name and value cannot both be empty string");
136143
}
137144

138-
if (name.IndexOf(';') != -1)
145+
if (name.Contains(';'))
139146
{
140147
throw new ArgumentException("Cookie names cannot contain a ';': " + name, nameof(name));
141148
}
@@ -172,77 +179,52 @@ public Cookie(string name, string value, string domain, string path, DateTime? e
172179
/// Gets the name of the cookie.
173180
/// </summary>
174181
[JsonPropertyName("name")]
175-
public string Name
176-
{
177-
get { return this.cookieName; }
178-
}
182+
public string Name => this.cookieName;
179183

180184
/// <summary>
181185
/// Gets the value of the cookie.
182186
/// </summary>
183187
[JsonPropertyName("value")]
184-
public string Value
185-
{
186-
get { return this.cookieValue; }
187-
}
188+
public string Value => this.cookieValue;
188189

189190
/// <summary>
190191
/// Gets the domain of the cookie.
191192
/// </summary>
192193
[JsonPropertyName("domain")]
193194
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
194-
public string Domain
195-
{
196-
get { return this.cookieDomain; }
197-
}
195+
public string? Domain => this.cookieDomain;
198196

199197
/// <summary>
200198
/// Gets the path of the cookie.
201199
/// </summary>
202200
[JsonPropertyName("path")]
203201
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
204-
public virtual string Path
205-
{
206-
get { return this.cookiePath; }
207-
}
202+
public virtual string? Path => this.cookiePath;
208203

209204
/// <summary>
210205
/// Gets a value indicating whether the cookie is secure.
211206
/// </summary>
212207
[JsonPropertyName("secure")]
213-
public virtual bool Secure
214-
{
215-
get { return this.secure; }
216-
}
208+
public virtual bool Secure => this.secure;
217209

218210
/// <summary>
219211
/// Gets a value indicating whether the cookie is an HTTP-only cookie.
220212
/// </summary>
221213
[JsonPropertyName("httpOnly")]
222-
public virtual bool IsHttpOnly
223-
{
224-
get { return this.isHttpOnly; }
225-
226-
}
214+
public virtual bool IsHttpOnly => this.isHttpOnly;
227215

228216
/// <summary>
229217
/// Gets the SameSite setting for the cookie.
230218
/// </summary>
231219
[JsonPropertyName("sameSite")]
232220
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
233-
public virtual string SameSite
234-
{
235-
get { return this.sameSite; }
236-
}
221+
public virtual string? SameSite => this.sameSite;
237222

238223
/// <summary>
239224
/// Gets the expiration date of the cookie.
240225
/// </summary>
241226
[JsonIgnore]
242-
public DateTime? Expiry
243-
{
244-
get { return this.cookieExpiry; }
245-
}
227+
public DateTime? Expiry => this.cookieExpiry;
246228

247229
/// <summary>
248230
/// Gets the cookie expiration date in seconds from the defined zero date (01 January 1970 00:00:00 UTC).
@@ -273,54 +255,54 @@ internal long? ExpirySeconds
273255
/// </summary>
274256
/// <param name="rawCookie">The Dictionary object containing the cookie parameters.</param>
275257
/// <returns>A <see cref="Cookie"/> object with the proper parameters set.</returns>
276-
public static Cookie FromDictionary(Dictionary<string, object> rawCookie)
258+
public static Cookie FromDictionary(Dictionary<string, object?> rawCookie)
277259
{
278260
if (rawCookie == null)
279261
{
280262
throw new ArgumentNullException(nameof(rawCookie));
281263
}
282264

283-
string name = rawCookie["name"].ToString();
265+
string name = rawCookie["name"]!.ToString()!;
284266
string value = string.Empty;
285-
if (rawCookie["value"] != null)
267+
if (rawCookie.TryGetValue("value", out object? valueObj))
286268
{
287-
value = rawCookie["value"].ToString();
269+
value = valueObj!.ToString()!;
288270
}
289271

290272
string path = "/";
291-
if (rawCookie.ContainsKey("path") && rawCookie["path"] != null)
273+
if (rawCookie.TryGetValue("path", out object? pathObj) && pathObj != null)
292274
{
293-
path = rawCookie["path"].ToString();
275+
path = pathObj.ToString()!;
294276
}
295277

296278
string domain = string.Empty;
297-
if (rawCookie.ContainsKey("domain") && rawCookie["domain"] != null)
279+
if (rawCookie.TryGetValue("domain", out object? domainObj) && domainObj != null)
298280
{
299-
domain = rawCookie["domain"].ToString();
281+
domain = domainObj.ToString()!;
300282
}
301283

302284
DateTime? expires = null;
303-
if (rawCookie.ContainsKey("expiry") && rawCookie["expiry"] != null)
285+
if (rawCookie.TryGetValue("expiry", out object? expiryObj) && expiryObj != null)
304286
{
305-
expires = ConvertExpirationTime(rawCookie["expiry"].ToString());
287+
expires = ConvertExpirationTime(expiryObj.ToString()!);
306288
}
307289

308290
bool secure = false;
309-
if (rawCookie.ContainsKey("secure") && rawCookie["secure"] != null)
291+
if (rawCookie.TryGetValue("secure", out object? secureObj) && secureObj != null)
310292
{
311-
secure = bool.Parse(rawCookie["secure"].ToString());
293+
secure = bool.Parse(secureObj.ToString()!);
312294
}
313295

314296
bool isHttpOnly = false;
315-
if (rawCookie.ContainsKey("httpOnly") && rawCookie["httpOnly"] != null)
297+
if (rawCookie.TryGetValue("httpOnly", out object? httpOnlyObj) && httpOnlyObj != null)
316298
{
317-
isHttpOnly = bool.Parse(rawCookie["httpOnly"].ToString());
299+
isHttpOnly = bool.Parse(httpOnlyObj.ToString()!);
318300
}
319301

320-
string sameSite = null;
321-
if (rawCookie.ContainsKey("sameSite") && rawCookie["sameSite"] != null)
302+
string? sameSite = null;
303+
if (rawCookie.TryGetValue("sameSite", out object? sameSiteObj))
322304
{
323-
sameSite = rawCookie["sameSite"].ToString();
305+
sameSite = sameSiteObj?.ToString();
324306
}
325307

326308
return new ReturnedCookie(name, value, domain, path, expires, secure, isHttpOnly, sameSite);
@@ -348,7 +330,7 @@ public override string ToString()
348330
/// <returns><see langword="true"/> if the specified <see cref="object">Object</see>
349331
/// is equal to the current <see cref="object">Object</see>; otherwise,
350332
/// <see langword="false"/>.</returns>
351-
public override bool Equals(object obj)
333+
public override bool Equals(object? obj)
352334
{
353335
// Two cookies are equal if the name and value match
354336
if (this == obj)
@@ -378,16 +360,15 @@ public override int GetHashCode()
378360
return this.cookieName.GetHashCode();
379361
}
380362

381-
private static string StripPort(string domain)
363+
private static string? StripPort(string? domain)
382364
{
383-
return string.IsNullOrEmpty(domain) ? null : domain.Split(':')[0];
365+
return string.IsNullOrEmpty(domain) ? null : domain!.Split(':')[0];
384366
}
385367

386368
private static DateTime? ConvertExpirationTime(string expirationTime)
387369
{
388370
DateTime? expires = null;
389-
double seconds = 0;
390-
if (double.TryParse(expirationTime, NumberStyles.Number, CultureInfo.InvariantCulture, out seconds))
371+
if (double.TryParse(expirationTime, NumberStyles.Number, CultureInfo.InvariantCulture, out double seconds))
391372
{
392373
try
393374
{

dotnet/src/webdriver/DevTools/DevToolsSession.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ private async Task InitializeSocketConnection()
496496
LogTrace("Creating WebSocket");
497497
this.connection = new WebSocketConnection(this.openConnectionWaitTimeSpan, this.closeConnectionWaitTimeSpan);
498498
connection.DataReceived += OnConnectionDataReceived;
499-
await connection.Start(this.EndpointAddress).ConfigureAwait(false);
499+
await connection.Start(this.EndpointAddress!).ConfigureAwait(false);
500500
LogTrace("WebSocket created");
501501
}
502502

dotnet/src/webdriver/DomMutationData.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ public class DomMutationData
5757
public string AttributeOriginalValue { get; internal set; } = null!;
5858

5959
/// <summary>
60-
/// Stores the element associated with the target ID
60+
/// Stores the element associated with the target ID, if any.
6161
/// </summary>
6262
[JsonIgnore]
63-
public IWebElement Element { get; internal set; } = null!; // Set internally
63+
public IWebElement? Element { get; internal set; }
6464

6565
/// <summary>
6666
/// Returns a string that represents the current object.

dotnet/src/webdriver/IE/InternetExplorerDriverLogLevel.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
// under the License.
1818
// </copyright>
1919

20+
#nullable enable
21+
2022
namespace OpenQA.Selenium.IE
2123
{
2224
/// <summary>

0 commit comments

Comments
 (0)