Skip to content

Commit 04fddba

Browse files
RenderMichaelsandeepsuryaprasad
authored andcommitted
[dotnet] Annotate nullability on capabilities types (SeleniumHQ#15353)
1 parent b120bea commit 04fddba

File tree

5 files changed

+42
-55
lines changed

5 files changed

+42
-55
lines changed

dotnet/src/webdriver/IHasCapabilities.cs

+2
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
2123
{
2224
/// <summary>

dotnet/src/webdriver/IWritableCapabilities.cs

+2
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
2123
{
2224
/// <summary>

dotnet/src/webdriver/Remote/DesiredCapabilities.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public DesiredCapabilities()
6363
/// DesiredCapabilities capabilities = new DesiredCapabilities(new Dictionary<![CDATA[<string,object>]]>(){["browserName","firefox"],["version",string.Empty],["javaScript",true]});
6464
/// </code>
6565
/// </example>
66-
public DesiredCapabilities(Dictionary<string, object> rawMap)
66+
public DesiredCapabilities(Dictionary<string, object>? rawMap)
6767
{
6868
if (rawMap != null)
6969
{

dotnet/src/webdriver/Remote/ReadOnlyDesiredCapabilities.cs

+20-40
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
using System.Collections.ObjectModel;
2424
using System.Globalization;
2525

26+
#nullable enable
27+
2628
namespace OpenQA.Selenium.Remote
2729
{
2830
/// <summary>
@@ -56,14 +58,7 @@ public string BrowserName
5658
{
5759
get
5860
{
59-
string name = string.Empty;
60-
object capabilityValue = this.GetCapability(CapabilityType.BrowserName);
61-
if (capabilityValue != null)
62-
{
63-
name = capabilityValue.ToString();
64-
}
65-
66-
return name;
61+
return this.GetCapability(CapabilityType.BrowserName)?.ToString() ?? string.Empty;
6762
}
6863
}
6964

@@ -85,14 +80,7 @@ public string Version
8580
{
8681
get
8782
{
88-
string browserVersion = string.Empty;
89-
object capabilityValue = this.GetCapability(CapabilityType.Version);
90-
if (capabilityValue != null)
91-
{
92-
browserVersion = capabilityValue.ToString();
93-
}
94-
95-
return browserVersion;
83+
return this.GetCapability(CapabilityType.Version)?.ToString() ?? string.Empty;
9684
}
9785
}
9886

@@ -104,7 +92,7 @@ public bool AcceptInsecureCerts
10492
get
10593
{
10694
bool acceptSSLCerts = false;
107-
object capabilityValue = this.GetCapability(CapabilityType.AcceptInsecureCertificates);
95+
object? capabilityValue = this.GetCapability(CapabilityType.AcceptInsecureCertificates);
10896
if (capabilityValue != null)
10997
{
11098
acceptSSLCerts = (bool)capabilityValue;
@@ -117,18 +105,12 @@ public bool AcceptInsecureCerts
117105
/// <summary>
118106
/// Gets the underlying Dictionary for a given set of capabilities.
119107
/// </summary>
120-
IDictionary<string, object> IHasCapabilitiesDictionary.CapabilitiesDictionary
121-
{
122-
get { return this.CapabilitiesDictionary; }
123-
}
108+
IDictionary<string, object> IHasCapabilitiesDictionary.CapabilitiesDictionary => this.CapabilitiesDictionary;
124109

125110
/// <summary>
126111
/// Gets the underlying Dictionary for a given set of capabilities.
127112
/// </summary>
128-
internal IDictionary<string, object> CapabilitiesDictionary
129-
{
130-
get { return new ReadOnlyDictionary<string, object>(this.capabilities); }
131-
}
113+
internal IDictionary<string, object> CapabilitiesDictionary => new ReadOnlyDictionary<string, object>(this.capabilities);
132114

133115
/// <summary>
134116
/// Gets the capability value with the specified name.
@@ -142,12 +124,12 @@ public object this[string capabilityName]
142124
{
143125
get
144126
{
145-
if (!this.capabilities.ContainsKey(capabilityName))
127+
if (!this.capabilities.TryGetValue(capabilityName, out object? capabilityValue))
146128
{
147129
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "The capability {0} is not present in this set of capabilities", capabilityName));
148130
}
149131

150-
return this.capabilities[capabilityName];
132+
return capabilityValue;
151133
}
152134
}
153135

@@ -167,20 +149,19 @@ public bool HasCapability(string capability)
167149
/// <param name="capability">The capability to get.</param>
168150
/// <returns>An object associated with the capability, or <see langword="null"/>
169151
/// if the capability is not set on the browser.</returns>
170-
public object GetCapability(string capability)
152+
public object? GetCapability(string capability)
171153
{
172-
object capabilityValue = null;
173-
if (this.capabilities.ContainsKey(capability))
154+
if (this.capabilities.TryGetValue(capability, out object? capabilityValue))
174155
{
175-
capabilityValue = this.capabilities[capability];
176-
string capabilityValueString = capabilityValue as string;
177-
if (capability == CapabilityType.Platform && capabilityValueString != null)
156+
if (capability == CapabilityType.Platform && capabilityValue is string capabilityValueString)
178157
{
179-
capabilityValue = Platform.FromString(capabilityValue.ToString());
158+
capabilityValue = Platform.FromString(capabilityValueString);
180159
}
160+
161+
return capabilityValue;
181162
}
182163

183-
return capabilityValue;
164+
return null;
184165
}
185166

186167
/// <summary>
@@ -219,20 +200,19 @@ public override string ToString()
219200
/// </summary>
220201
/// <param name="obj">DesiredCapabilities you wish to compare</param>
221202
/// <returns>true if they are the same or false if they are not</returns>
222-
public override bool Equals(object obj)
203+
public override bool Equals(object? obj)
223204
{
224205
if (this == obj)
225206
{
226207
return true;
227208
}
228209

229-
DesiredCapabilities other = obj as DesiredCapabilities;
230-
if (other == null)
210+
if (obj is not DesiredCapabilities other)
231211
{
232212
return false;
233213
}
234214

235-
if (this.BrowserName != null ? this.BrowserName != other.BrowserName : other.BrowserName != null)
215+
if (this.BrowserName != other.BrowserName)
236216
{
237217
return false;
238218
}
@@ -242,7 +222,7 @@ public override bool Equals(object obj)
242222
return false;
243223
}
244224

245-
if (this.Version != null ? this.Version != other.Version : other.Version != null)
225+
if (this.Version != other.Version)
246226
{
247227
return false;
248228
}

dotnet/src/webdriver/Remote/RemoteSessionSettings.cs

+17-14
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
using System.Collections.Generic;
2323
using System.Globalization;
2424

25+
#nullable enable
26+
2527
namespace OpenQA.Selenium
2628
{
2729
/// <summary>
@@ -32,10 +34,10 @@ public class RemoteSessionSettings : ICapabilities
3234
private const string FirstMatchCapabilityName = "firstMatch";
3335
private const string AlwaysMatchCapabilityName = "alwaysMatch";
3436

35-
private readonly List<string> reservedSettingNames = new List<string>() { FirstMatchCapabilityName, AlwaysMatchCapabilityName };
36-
private DriverOptions mustMatchDriverOptions;
37-
private List<DriverOptions> firstMatchOptions = new List<DriverOptions>();
38-
private Dictionary<string, object> remoteMetadataSettings = new Dictionary<string, object>();
37+
private readonly HashSet<string> reservedSettingNames = new HashSet<string>() { FirstMatchCapabilityName, AlwaysMatchCapabilityName };
38+
private DriverOptions? mustMatchDriverOptions;
39+
private readonly List<DriverOptions> firstMatchOptions = new List<DriverOptions>();
40+
private readonly Dictionary<string, object> remoteMetadataSettings = new Dictionary<string, object>();
3941

4042
/// <summary>
4143
/// Creates a new instance of the <see cref="RemoteSessionSettings"/> class.
@@ -69,7 +71,7 @@ public RemoteSessionSettings(DriverOptions mustMatchDriverOptions, params Driver
6971
/// <summary>
7072
/// Gets a value indicating the options that must be matched by the remote end to create a session.
7173
/// </summary>
72-
internal DriverOptions MustMatchDriverOptions => this.mustMatchDriverOptions;
74+
internal DriverOptions? MustMatchDriverOptions => this.mustMatchDriverOptions;
7375

7476
/// <summary>
7577
/// Gets a value indicating the number of options that may be matched by the remote end to create a session.
@@ -91,7 +93,8 @@ public object this[string capabilityName]
9193
{
9294
if (capabilityName == AlwaysMatchCapabilityName)
9395
{
94-
return this.GetAlwaysMatchOptionsAsSerializableDictionary();
96+
return this.GetAlwaysMatchOptionsAsSerializableDictionary()
97+
?? throw new ArgumentException("The \"alwaysMatch\" value has not been set", nameof(capabilityName));
9598
}
9699

97100
if (capabilityName == FirstMatchCapabilityName)
@@ -203,7 +206,7 @@ public bool HasCapability(string capability)
203206
/// <param name="capability">The capability to get.</param>
204207
/// <returns>An object associated with the capability, or <see langword="null"/>
205208
/// if the capability is not set in this set of capabilities.</returns>
206-
public object GetCapability(string capability)
209+
public object? GetCapability(string capability)
207210
{
208211
if (capability == AlwaysMatchCapabilityName)
209212
{
@@ -227,9 +230,9 @@ public object GetCapability(string capability)
227230
/// Return a dictionary representation of this <see cref="RemoteSessionSettings"/>.
228231
/// </summary>
229232
/// <returns>A <see cref="Dictionary{TKey, TValue}"/> representation of this <see cref="RemoteSessionSettings"/>.</returns>
230-
public Dictionary<string, object> ToDictionary()
233+
public Dictionary<string, object?> ToDictionary()
231234
{
232-
Dictionary<string, object> capabilitiesDictionary = new Dictionary<string, object>();
235+
Dictionary<string, object?> capabilitiesDictionary = new Dictionary<string, object?>();
233236

234237
foreach (KeyValuePair<string, object> remoteMetadataSetting in this.remoteMetadataSettings)
235238
{
@@ -243,7 +246,7 @@ public Dictionary<string, object> ToDictionary()
243246

244247
if (this.firstMatchOptions.Count > 0)
245248
{
246-
List<object> optionsMatches = GetFirstMatchOptionsAsSerializableList();
249+
List<object?> optionsMatches = GetFirstMatchOptionsAsSerializableList();
247250

248251
capabilitiesDictionary["firstMatch"] = optionsMatches;
249252
}
@@ -261,14 +264,14 @@ internal DriverOptions GetFirstMatchDriverOptions(int firstMatchIndex)
261264
return this.firstMatchOptions[firstMatchIndex];
262265
}
263266

264-
private IDictionary<string, object> GetAlwaysMatchOptionsAsSerializableDictionary()
267+
private IDictionary<string, object>? GetAlwaysMatchOptionsAsSerializableDictionary()
265268
{
266-
return this.mustMatchDriverOptions.ToDictionary();
269+
return this.mustMatchDriverOptions?.ToDictionary();
267270
}
268271

269-
private List<object> GetFirstMatchOptionsAsSerializableList()
272+
private List<object?> GetFirstMatchOptionsAsSerializableList()
270273
{
271-
List<object> optionsMatches = new List<object>(this.firstMatchOptions.Count);
274+
List<object?> optionsMatches = new List<object?>(this.firstMatchOptions.Count);
272275
foreach (DriverOptions options in this.firstMatchOptions)
273276
{
274277
optionsMatches.Add(options.ToDictionary());

0 commit comments

Comments
 (0)