Skip to content

Commit

Permalink
[dotnet] Add nullability annotations to Proxy (SeleniumHQ#14861)
Browse files Browse the repository at this point in the history
  • Loading branch information
RenderMichael authored and sandeepsuryaprasad committed Dec 7, 2024
1 parent a0b9225 commit 4e51853
Showing 1 changed file with 65 additions and 98 deletions.
163 changes: 65 additions & 98 deletions dotnet/src/webdriver/Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
using System.Globalization;
using System.Text.Json.Serialization;

#nullable enable

namespace OpenQA.Selenium
{
/// <summary>
Expand Down Expand Up @@ -72,13 +74,13 @@ public class Proxy
{
private ProxyKind proxyKind = ProxyKind.Unspecified;
private bool isAutoDetect;
private string ftpProxyLocation;
private string httpProxyLocation;
private string proxyAutoConfigUrl;
private string sslProxyLocation;
private string socksProxyLocation;
private string socksUserName;
private string socksPassword;
private string? ftpProxyLocation;
private string? httpProxyLocation;
private string? proxyAutoConfigUrl;
private string? sslProxyLocation;
private string? socksProxyLocation;
private string? socksUserName;
private string? socksPassword;
private int? socksVersion;
private List<string> noProxyAddresses = new List<string>();

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

if (settings.ContainsKey("proxyType") && settings["proxyType"] != null)
if (settings.TryGetValue("proxyType", out object? proxyTypeObj) && proxyTypeObj?.ToString() is string proxyType)
{
// Special-case "PAC" since that is the correct serialization.
if (settings["proxyType"].ToString().ToLowerInvariant() == "pac")
if (proxyType.Equals("pac", StringComparison.InvariantCultureIgnoreCase))
{
this.Kind = ProxyKind.ProxyAutoConfigure;
}
else
{
ProxyKind rawType = (ProxyKind)Enum.Parse(typeof(ProxyKind), settings["proxyType"].ToString(), true);
ProxyKind rawType = (ProxyKind)Enum.Parse(typeof(ProxyKind), proxyType, ignoreCase: true);
this.Kind = rawType;
}
}

if (settings.ContainsKey("ftpProxy") && settings["ftpProxy"] != null)
if (settings.TryGetValue("ftpProxy", out object? ftpProxyObj) && ftpProxyObj?.ToString() is string ftpProxy)
{
this.FtpProxy = settings["ftpProxy"].ToString();
this.FtpProxy = ftpProxy;
}

if (settings.ContainsKey("httpProxy") && settings["httpProxy"] != null)
if (settings.TryGetValue("httpProxy", out object? httpProxyObj) && httpProxyObj?.ToString() is string httpProxy)
{
this.HttpProxy = settings["httpProxy"].ToString();
this.HttpProxy = httpProxy;
}

if (settings.ContainsKey("noProxy") && settings["noProxy"] != null)
if (settings.TryGetValue("noProxy", out object? noProxy) && noProxy != null)
{
List<string> bypassAddresses = new List<string>();
string addressesAsString = settings["noProxy"] as string;
if (addressesAsString != null)
if (noProxy is string addressesAsString)
{
bypassAddresses.AddRange(addressesAsString.Split(';'));
}
else
{
object[] addressesAsArray = settings["noProxy"] as object[];
if (addressesAsArray != null)
if (noProxy is object?[] addressesAsArray)
{
foreach (object address in addressesAsArray)
foreach (object? address in addressesAsArray)
{
bypassAddresses.Add(address.ToString());
bypassAddresses.Add(address?.ToString() ?? throw new ArgumentException("Proxy bypass address list \"noProxy\" contained a null element", nameof(settings)));
}
}
}

this.AddBypassAddresses(bypassAddresses);
}

if (settings.ContainsKey("proxyAutoconfigUrl") && settings["proxyAutoconfigUrl"] != null)
if (settings.TryGetValue("proxyAutoconfigUrl", out object? proxyAutoconfigUrlObj) && proxyAutoconfigUrlObj?.ToString() is string proxyAutoconfigUrl)
{
this.ProxyAutoConfigUrl = settings["proxyAutoconfigUrl"].ToString();
this.ProxyAutoConfigUrl = proxyAutoconfigUrl;
}

if (settings.ContainsKey("sslProxy") && settings["sslProxy"] != null)
if (settings.TryGetValue("sslProxy", out object? sslProxyObj) && sslProxyObj?.ToString() is string sslProxy)
{
this.SslProxy = settings["sslProxy"].ToString();
this.SslProxy = sslProxy;
}

if (settings.ContainsKey("socksProxy") && settings["socksProxy"] != null)
if (settings.TryGetValue("socksProxy", out object? socksProxyObj) && socksProxyObj?.ToString() is string socksProxy)
{
this.SocksProxy = settings["socksProxy"].ToString();
this.SocksProxy = socksProxy;
}

if (settings.ContainsKey("socksUsername") && settings["socksUsername"] != null)
if (settings.TryGetValue("socksUsername", out object? socksUsernameObj) && socksUsernameObj?.ToString() is string socksUsername)
{
this.SocksUserName = settings["socksUsername"].ToString();
this.SocksUserName = socksUsername;
}

if (settings.ContainsKey("socksPassword") && settings["socksPassword"] != null)
if (settings.TryGetValue("socksPassword", out object? socksPasswordObj) && socksPasswordObj?.ToString() is string socksPassword)
{
this.SocksPassword = settings["socksPassword"].ToString();
this.SocksPassword = socksPassword;
}

if (settings.ContainsKey("socksVersion") && settings["socksVersion"] != null)
if (settings.TryGetValue("socksVersion", out object? socksVersion) && socksVersion != null)
{
this.SocksVersion = Convert.ToInt32(settings["socksVersion"]);
this.SocksVersion = Convert.ToInt32(socksVersion);
}

if (settings.ContainsKey("autodetect") && settings["autodetect"] != null)
if (settings.TryGetValue("autodetect", out object? autodetect) && autodetect != null)
{
this.IsAutoDetect = (bool)settings["autodetect"];
this.IsAutoDetect = Convert.ToBoolean(autodetect);
}
}

Expand All @@ -189,10 +191,7 @@ public Proxy(Dictionary<string, object> settings)
[JsonIgnore]
public ProxyKind Kind
{
get
{
return this.proxyKind;
}
get => this.proxyKind;

set
{
Expand Down Expand Up @@ -224,10 +223,7 @@ public string SerializableProxyKind
[JsonIgnore]
public bool IsAutoDetect
{
get
{
return this.isAutoDetect;
}
get => this.isAutoDetect;

set
{
Expand All @@ -247,12 +243,9 @@ public bool IsAutoDetect
/// </summary>
[JsonPropertyName("ftpProxy")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string FtpProxy
public string? FtpProxy
{
get
{
return this.ftpProxyLocation;
}
get => this.ftpProxyLocation;

set
{
Expand All @@ -267,12 +260,9 @@ public string FtpProxy
/// </summary>
[JsonPropertyName("httpProxy")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string HttpProxy
public string? HttpProxy
{
get
{
return this.httpProxyLocation;
}
get => this.httpProxyLocation;

set
{
Expand All @@ -287,7 +277,7 @@ public string HttpProxy
/// </summary>
[JsonPropertyName("noProxy")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public ReadOnlyCollection<string> BypassProxyAddresses
public ReadOnlyCollection<string>? BypassProxyAddresses
{
get
{
Expand All @@ -305,12 +295,9 @@ public ReadOnlyCollection<string> BypassProxyAddresses
/// </summary>
[JsonPropertyName("proxyAutoconfigUrl")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string ProxyAutoConfigUrl
public string? ProxyAutoConfigUrl
{
get
{
return this.proxyAutoConfigUrl;
}
get => this.proxyAutoConfigUrl;

set
{
Expand All @@ -325,12 +312,9 @@ public string ProxyAutoConfigUrl
/// </summary>
[JsonPropertyName("sslProxy")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string SslProxy
public string? SslProxy
{
get
{
return this.sslProxyLocation;
}
get => this.sslProxyLocation;

set
{
Expand All @@ -345,12 +329,9 @@ public string SslProxy
/// </summary>
[JsonPropertyName("socksProxy")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string SocksProxy
public string? SocksProxy
{
get
{
return this.socksProxyLocation;
}
get => this.socksProxyLocation;

set
{
Expand All @@ -365,12 +346,9 @@ public string SocksProxy
/// </summary>
[JsonPropertyName("socksUsername")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string SocksUserName
public string? SocksUserName
{
get
{
return this.socksUserName;
}
get => this.socksUserName;

set
{
Expand All @@ -388,10 +366,7 @@ public string SocksUserName
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public int? SocksVersion
{
get
{
return this.socksVersion;
}
get => this.socksVersion;

set
{
Expand All @@ -403,7 +378,7 @@ public int? SocksVersion
{
if (value.Value <= 0)
{
throw new ArgumentException("SocksVersion must be a positive integer");
throw new ArgumentOutOfRangeException(nameof(value), "SocksVersion must be a positive integer");
}

this.VerifyProxyTypeCompatilibily(ProxyKind.Manual);
Expand All @@ -418,12 +393,9 @@ public int? SocksVersion
/// </summary>
[JsonPropertyName("socksPassword")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string SocksPassword
public string? SocksPassword
{
get
{
return this.socksPassword;
}
get => this.socksPassword;

set
{
Expand Down Expand Up @@ -453,7 +425,7 @@ public void AddBypassAddress(string address)
/// <param name="addressesToAdd">An array of addresses to add.</param>
public void AddBypassAddresses(params string[] addressesToAdd)
{
this.AddBypassAddresses(new List<string>(addressesToAdd));
this.AddBypassAddresses((IEnumerable<string>)addressesToAdd);
}

/// <summary>
Expand All @@ -478,7 +450,7 @@ public void AddBypassAddresses(IEnumerable<string> addressesToAdd)
/// </summary>
/// <returns>A dictionary suitable for serializing to the W3C Specification
/// dialect of the wire protocol.</returns>
internal Dictionary<string, object> ToCapability()
internal Dictionary<string, object?>? ToCapability()
{
return this.AsDictionary(true);
}
Expand All @@ -489,17 +461,17 @@ internal Dictionary<string, object> ToCapability()
/// </summary>
/// <returns>A dictionary suitable for serializing to the OSS dialect of the
/// wire protocol.</returns>
internal Dictionary<string, object> ToLegacyCapability()
internal Dictionary<string, object?>? ToLegacyCapability()
{
return this.AsDictionary(false);
}

private Dictionary<string, object> AsDictionary(bool isSpecCompliant)
private Dictionary<string, object?>? AsDictionary(bool isSpecCompliant)
{
Dictionary<string, object> serializedDictionary = null;
Dictionary<string, object?>? serializedDictionary = null;
if (this.proxyKind != ProxyKind.Unspecified)
{
serializedDictionary = new Dictionary<string, object>();
serializedDictionary = new Dictionary<string, object?>();
if (this.proxyKind == ProxyKind.ProxyAutoConfigure)
{
serializedDictionary["proxyType"] = "pac";
Expand Down Expand Up @@ -556,17 +528,12 @@ private Dictionary<string, object> AsDictionary(bool isSpecCompliant)
return serializedDictionary;
}

private object GetNoProxyAddressList(bool isSpecCompliant)
private object? GetNoProxyAddressList(bool isSpecCompliant)
{
object addresses = null;
object? addresses = null;
if (isSpecCompliant)
{
List<object> addressList = new List<object>();
foreach (string address in this.noProxyAddresses)
{
addressList.Add(address);
}

List<object> addressList = [.. this.noProxyAddresses];
addresses = addressList;
}
else
Expand Down

0 comments on commit 4e51853

Please sign in to comment.