Skip to content

Commit 4e51853

Browse files
RenderMichaelsandeepsuryaprasad
authored andcommitted
[dotnet] Add nullability annotations to Proxy (SeleniumHQ#14861)
1 parent a0b9225 commit 4e51853

File tree

1 file changed

+65
-98
lines changed

1 file changed

+65
-98
lines changed

dotnet/src/webdriver/Proxy.cs

Lines changed: 65 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
using System.Globalization;
2424
using System.Text.Json.Serialization;
2525

26+
#nullable enable
27+
2628
namespace OpenQA.Selenium
2729
{
2830
/// <summary>
@@ -72,13 +74,13 @@ public class Proxy
7274
{
7375
private ProxyKind proxyKind = ProxyKind.Unspecified;
7476
private bool isAutoDetect;
75-
private string ftpProxyLocation;
76-
private string httpProxyLocation;
77-
private string proxyAutoConfigUrl;
78-
private string sslProxyLocation;
79-
private string socksProxyLocation;
80-
private string socksUserName;
81-
private string socksPassword;
77+
private string? ftpProxyLocation;
78+
private string? httpProxyLocation;
79+
private string? proxyAutoConfigUrl;
80+
private string? sslProxyLocation;
81+
private string? socksProxyLocation;
82+
private string? socksUserName;
83+
private string? socksPassword;
8284
private int? socksVersion;
8385
private List<string> noProxyAddresses = new List<string>();
8486

@@ -93,93 +95,93 @@ public Proxy()
9395
/// Initializes a new instance of the <see cref="Proxy"/> class with the given proxy settings.
9496
/// </summary>
9597
/// <param name="settings">A dictionary of settings to use with the proxy.</param>
98+
/// <exception cref="ArgumentNullException">If <paramref name="settings"/> is <see langword="null"/>.</exception>
99+
/// <exception cref="ArgumentException">If The "noProxy" value is a list with a <see langword="null"/> element.</exception>
96100
public Proxy(Dictionary<string, object> settings)
97101
{
98102
if (settings == null)
99103
{
100104
throw new ArgumentNullException(nameof(settings), "settings dictionary cannot be null");
101105
}
102106

103-
if (settings.ContainsKey("proxyType") && settings["proxyType"] != null)
107+
if (settings.TryGetValue("proxyType", out object? proxyTypeObj) && proxyTypeObj?.ToString() is string proxyType)
104108
{
105109
// Special-case "PAC" since that is the correct serialization.
106-
if (settings["proxyType"].ToString().ToLowerInvariant() == "pac")
110+
if (proxyType.Equals("pac", StringComparison.InvariantCultureIgnoreCase))
107111
{
108112
this.Kind = ProxyKind.ProxyAutoConfigure;
109113
}
110114
else
111115
{
112-
ProxyKind rawType = (ProxyKind)Enum.Parse(typeof(ProxyKind), settings["proxyType"].ToString(), true);
116+
ProxyKind rawType = (ProxyKind)Enum.Parse(typeof(ProxyKind), proxyType, ignoreCase: true);
113117
this.Kind = rawType;
114118
}
115119
}
116120

117-
if (settings.ContainsKey("ftpProxy") && settings["ftpProxy"] != null)
121+
if (settings.TryGetValue("ftpProxy", out object? ftpProxyObj) && ftpProxyObj?.ToString() is string ftpProxy)
118122
{
119-
this.FtpProxy = settings["ftpProxy"].ToString();
123+
this.FtpProxy = ftpProxy;
120124
}
121125

122-
if (settings.ContainsKey("httpProxy") && settings["httpProxy"] != null)
126+
if (settings.TryGetValue("httpProxy", out object? httpProxyObj) && httpProxyObj?.ToString() is string httpProxy)
123127
{
124-
this.HttpProxy = settings["httpProxy"].ToString();
128+
this.HttpProxy = httpProxy;
125129
}
126130

127-
if (settings.ContainsKey("noProxy") && settings["noProxy"] != null)
131+
if (settings.TryGetValue("noProxy", out object? noProxy) && noProxy != null)
128132
{
129133
List<string> bypassAddresses = new List<string>();
130-
string addressesAsString = settings["noProxy"] as string;
131-
if (addressesAsString != null)
134+
if (noProxy is string addressesAsString)
132135
{
133136
bypassAddresses.AddRange(addressesAsString.Split(';'));
134137
}
135138
else
136139
{
137-
object[] addressesAsArray = settings["noProxy"] as object[];
138-
if (addressesAsArray != null)
140+
if (noProxy is object?[] addressesAsArray)
139141
{
140-
foreach (object address in addressesAsArray)
142+
foreach (object? address in addressesAsArray)
141143
{
142-
bypassAddresses.Add(address.ToString());
144+
bypassAddresses.Add(address?.ToString() ?? throw new ArgumentException("Proxy bypass address list \"noProxy\" contained a null element", nameof(settings)));
143145
}
144146
}
145147
}
146148

147149
this.AddBypassAddresses(bypassAddresses);
148150
}
149151

150-
if (settings.ContainsKey("proxyAutoconfigUrl") && settings["proxyAutoconfigUrl"] != null)
152+
if (settings.TryGetValue("proxyAutoconfigUrl", out object? proxyAutoconfigUrlObj) && proxyAutoconfigUrlObj?.ToString() is string proxyAutoconfigUrl)
151153
{
152-
this.ProxyAutoConfigUrl = settings["proxyAutoconfigUrl"].ToString();
154+
this.ProxyAutoConfigUrl = proxyAutoconfigUrl;
153155
}
154156

155-
if (settings.ContainsKey("sslProxy") && settings["sslProxy"] != null)
157+
if (settings.TryGetValue("sslProxy", out object? sslProxyObj) && sslProxyObj?.ToString() is string sslProxy)
156158
{
157-
this.SslProxy = settings["sslProxy"].ToString();
159+
this.SslProxy = sslProxy;
158160
}
159161

160-
if (settings.ContainsKey("socksProxy") && settings["socksProxy"] != null)
162+
if (settings.TryGetValue("socksProxy", out object? socksProxyObj) && socksProxyObj?.ToString() is string socksProxy)
161163
{
162-
this.SocksProxy = settings["socksProxy"].ToString();
164+
this.SocksProxy = socksProxy;
163165
}
164166

165-
if (settings.ContainsKey("socksUsername") && settings["socksUsername"] != null)
167+
if (settings.TryGetValue("socksUsername", out object? socksUsernameObj) && socksUsernameObj?.ToString() is string socksUsername)
166168
{
167-
this.SocksUserName = settings["socksUsername"].ToString();
169+
this.SocksUserName = socksUsername;
168170
}
169171

170-
if (settings.ContainsKey("socksPassword") && settings["socksPassword"] != null)
172+
if (settings.TryGetValue("socksPassword", out object? socksPasswordObj) && socksPasswordObj?.ToString() is string socksPassword)
171173
{
172-
this.SocksPassword = settings["socksPassword"].ToString();
174+
this.SocksPassword = socksPassword;
173175
}
174176

175-
if (settings.ContainsKey("socksVersion") && settings["socksVersion"] != null)
177+
if (settings.TryGetValue("socksVersion", out object? socksVersion) && socksVersion != null)
176178
{
177-
this.SocksVersion = Convert.ToInt32(settings["socksVersion"]);
179+
this.SocksVersion = Convert.ToInt32(socksVersion);
178180
}
179181

180-
if (settings.ContainsKey("autodetect") && settings["autodetect"] != null)
182+
if (settings.TryGetValue("autodetect", out object? autodetect) && autodetect != null)
181183
{
182-
this.IsAutoDetect = (bool)settings["autodetect"];
184+
this.IsAutoDetect = Convert.ToBoolean(autodetect);
183185
}
184186
}
185187

@@ -189,10 +191,7 @@ public Proxy(Dictionary<string, object> settings)
189191
[JsonIgnore]
190192
public ProxyKind Kind
191193
{
192-
get
193-
{
194-
return this.proxyKind;
195-
}
194+
get => this.proxyKind;
196195

197196
set
198197
{
@@ -224,10 +223,7 @@ public string SerializableProxyKind
224223
[JsonIgnore]
225224
public bool IsAutoDetect
226225
{
227-
get
228-
{
229-
return this.isAutoDetect;
230-
}
226+
get => this.isAutoDetect;
231227

232228
set
233229
{
@@ -247,12 +243,9 @@ public bool IsAutoDetect
247243
/// </summary>
248244
[JsonPropertyName("ftpProxy")]
249245
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
250-
public string FtpProxy
246+
public string? FtpProxy
251247
{
252-
get
253-
{
254-
return this.ftpProxyLocation;
255-
}
248+
get => this.ftpProxyLocation;
256249

257250
set
258251
{
@@ -267,12 +260,9 @@ public string FtpProxy
267260
/// </summary>
268261
[JsonPropertyName("httpProxy")]
269262
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
270-
public string HttpProxy
263+
public string? HttpProxy
271264
{
272-
get
273-
{
274-
return this.httpProxyLocation;
275-
}
265+
get => this.httpProxyLocation;
276266

277267
set
278268
{
@@ -287,7 +277,7 @@ public string HttpProxy
287277
/// </summary>
288278
[JsonPropertyName("noProxy")]
289279
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
290-
public ReadOnlyCollection<string> BypassProxyAddresses
280+
public ReadOnlyCollection<string>? BypassProxyAddresses
291281
{
292282
get
293283
{
@@ -305,12 +295,9 @@ public ReadOnlyCollection<string> BypassProxyAddresses
305295
/// </summary>
306296
[JsonPropertyName("proxyAutoconfigUrl")]
307297
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
308-
public string ProxyAutoConfigUrl
298+
public string? ProxyAutoConfigUrl
309299
{
310-
get
311-
{
312-
return this.proxyAutoConfigUrl;
313-
}
300+
get => this.proxyAutoConfigUrl;
314301

315302
set
316303
{
@@ -325,12 +312,9 @@ public string ProxyAutoConfigUrl
325312
/// </summary>
326313
[JsonPropertyName("sslProxy")]
327314
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
328-
public string SslProxy
315+
public string? SslProxy
329316
{
330-
get
331-
{
332-
return this.sslProxyLocation;
333-
}
317+
get => this.sslProxyLocation;
334318

335319
set
336320
{
@@ -345,12 +329,9 @@ public string SslProxy
345329
/// </summary>
346330
[JsonPropertyName("socksProxy")]
347331
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
348-
public string SocksProxy
332+
public string? SocksProxy
349333
{
350-
get
351-
{
352-
return this.socksProxyLocation;
353-
}
334+
get => this.socksProxyLocation;
354335

355336
set
356337
{
@@ -365,12 +346,9 @@ public string SocksProxy
365346
/// </summary>
366347
[JsonPropertyName("socksUsername")]
367348
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
368-
public string SocksUserName
349+
public string? SocksUserName
369350
{
370-
get
371-
{
372-
return this.socksUserName;
373-
}
351+
get => this.socksUserName;
374352

375353
set
376354
{
@@ -388,10 +366,7 @@ public string SocksUserName
388366
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
389367
public int? SocksVersion
390368
{
391-
get
392-
{
393-
return this.socksVersion;
394-
}
369+
get => this.socksVersion;
395370

396371
set
397372
{
@@ -403,7 +378,7 @@ public int? SocksVersion
403378
{
404379
if (value.Value <= 0)
405380
{
406-
throw new ArgumentException("SocksVersion must be a positive integer");
381+
throw new ArgumentOutOfRangeException(nameof(value), "SocksVersion must be a positive integer");
407382
}
408383

409384
this.VerifyProxyTypeCompatilibily(ProxyKind.Manual);
@@ -418,12 +393,9 @@ public int? SocksVersion
418393
/// </summary>
419394
[JsonPropertyName("socksPassword")]
420395
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
421-
public string SocksPassword
396+
public string? SocksPassword
422397
{
423-
get
424-
{
425-
return this.socksPassword;
426-
}
398+
get => this.socksPassword;
427399

428400
set
429401
{
@@ -453,7 +425,7 @@ public void AddBypassAddress(string address)
453425
/// <param name="addressesToAdd">An array of addresses to add.</param>
454426
public void AddBypassAddresses(params string[] addressesToAdd)
455427
{
456-
this.AddBypassAddresses(new List<string>(addressesToAdd));
428+
this.AddBypassAddresses((IEnumerable<string>)addressesToAdd);
457429
}
458430

459431
/// <summary>
@@ -478,7 +450,7 @@ public void AddBypassAddresses(IEnumerable<string> addressesToAdd)
478450
/// </summary>
479451
/// <returns>A dictionary suitable for serializing to the W3C Specification
480452
/// dialect of the wire protocol.</returns>
481-
internal Dictionary<string, object> ToCapability()
453+
internal Dictionary<string, object?>? ToCapability()
482454
{
483455
return this.AsDictionary(true);
484456
}
@@ -489,17 +461,17 @@ internal Dictionary<string, object> ToCapability()
489461
/// </summary>
490462
/// <returns>A dictionary suitable for serializing to the OSS dialect of the
491463
/// wire protocol.</returns>
492-
internal Dictionary<string, object> ToLegacyCapability()
464+
internal Dictionary<string, object?>? ToLegacyCapability()
493465
{
494466
return this.AsDictionary(false);
495467
}
496468

497-
private Dictionary<string, object> AsDictionary(bool isSpecCompliant)
469+
private Dictionary<string, object?>? AsDictionary(bool isSpecCompliant)
498470
{
499-
Dictionary<string, object> serializedDictionary = null;
471+
Dictionary<string, object?>? serializedDictionary = null;
500472
if (this.proxyKind != ProxyKind.Unspecified)
501473
{
502-
serializedDictionary = new Dictionary<string, object>();
474+
serializedDictionary = new Dictionary<string, object?>();
503475
if (this.proxyKind == ProxyKind.ProxyAutoConfigure)
504476
{
505477
serializedDictionary["proxyType"] = "pac";
@@ -556,17 +528,12 @@ private Dictionary<string, object> AsDictionary(bool isSpecCompliant)
556528
return serializedDictionary;
557529
}
558530

559-
private object GetNoProxyAddressList(bool isSpecCompliant)
531+
private object? GetNoProxyAddressList(bool isSpecCompliant)
560532
{
561-
object addresses = null;
533+
object? addresses = null;
562534
if (isSpecCompliant)
563535
{
564-
List<object> addressList = new List<object>();
565-
foreach (string address in this.noProxyAddresses)
566-
{
567-
addressList.Add(address);
568-
}
569-
536+
List<object> addressList = [.. this.noProxyAddresses];
570537
addresses = addressList;
571538
}
572539
else

0 commit comments

Comments
 (0)