Skip to content

Commit 6325827

Browse files
committed
Add nullable reference type annotations to Response
1 parent 3331a86 commit 6325827

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

dotnet/src/webdriver/Response.cs

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

27+
#nullable enable
28+
2729
namespace OpenQA.Selenium
2830
{
2931
/// <summary>
@@ -48,7 +50,7 @@ public Response()
4850
/// Initializes a new instance of the <see cref="Response"/> class
4951
/// </summary>
5052
/// <param name="sessionId">Session ID in use</param>
51-
public Response(SessionId sessionId)
53+
public Response(SessionId? sessionId)
5254
{
5355
this.SessionId = sessionId?.ToString();
5456
}
@@ -59,7 +61,7 @@ public Response(SessionId sessionId)
5961
/// <param name="sessionId">The Session ID in use, if any.</param>
6062
/// <param name="value">The JSON payload of the response.</param>
6163
/// <param name="status">The WebDriver result status of the response.</param>
62-
public Response(string sessionId, object value, WebDriverResult status)
64+
public Response(string? sessionId, object? value, WebDriverResult status)
6365
{
6466
this.SessionId = sessionId;
6567
this.Value = value;
@@ -71,20 +73,22 @@ public Response(string sessionId, object value, WebDriverResult status)
7173
/// </summary>
7274
/// <param name="value">The JSON string to deserialize into a <see cref="Response"/>.</param>
7375
/// <returns>A <see cref="Response"/> object described by the JSON string.</returns>
76+
/// <exception cref="ArgumentNullException">If <paramref name="value"/> is <see langword="null"/>.</exception>
77+
/// <exception cref="JsonException">If <paramref name="value"/> is not a valid JSON object.</exception>
7478
public static Response FromJson(string value)
7579
{
76-
Dictionary<string, object> rawResponse = JsonSerializer.Deserialize<Dictionary<string, object>>(value, s_jsonSerializerOptions)
80+
Dictionary<string, object?> rawResponse = JsonSerializer.Deserialize<Dictionary<string, object?>>(value, s_jsonSerializerOptions)
7781
?? throw new WebDriverException("JSON success response returned \"null\" value");
7882

79-
object contents;
80-
string sessionId = null;
83+
object? contents;
84+
string? sessionId = null;
8185

82-
if (rawResponse.TryGetValue("sessionId", out var s) && s is not null)
86+
if (rawResponse.TryGetValue("sessionId", out object? s) && s is not null)
8387
{
8488
sessionId = s.ToString();
8589
}
8690

87-
if (rawResponse.TryGetValue("value", out object valueObj))
91+
if (rawResponse.TryGetValue("value", out object? valueObj))
8892
{
8993
contents = valueObj;
9094
}
@@ -98,7 +102,7 @@ public static Response FromJson(string value)
98102

99103
// Special-case for the new session command, where the "capabilities"
100104
// property of the response is the actual value we're interested in.
101-
if (rawResponse.TryGetValue("capabilities", out var capabilities))
105+
if (rawResponse.TryGetValue("capabilities", out object? capabilities))
102106
{
103107
contents = capabilities;
104108
}
@@ -112,10 +116,10 @@ public static Response FromJson(string value)
112116
{
113117
// Special case code for the new session command. If the response contains
114118
// sessionId and capabilities properties, fix up the session ID and value members.
115-
if (valueDictionary.TryGetValue("sessionId", out var session))
119+
if (valueDictionary.TryGetValue("sessionId", out object? session))
116120
{
117121
sessionId = session.ToString();
118-
if (valueDictionary.TryGetValue("capabilities", out object capabilities))
122+
if (valueDictionary.TryGetValue("capabilities", out object? capabilities))
119123
{
120124
contents = capabilities;
121125
}
@@ -132,12 +136,12 @@ public static Response FromJson(string value)
132136
/// <summary>
133137
/// Gets or sets the value from JSON.
134138
/// </summary>
135-
public object Value { get; set; }
139+
public object? Value { get; set; }
136140

137141
/// <summary>
138142
/// Gets or sets the session ID.
139143
/// </summary>
140-
public string SessionId { get; set; }
144+
public string? SessionId { get; set; }
141145

142146
/// <summary>
143147
/// Gets or sets the status value of the response.
@@ -150,12 +154,15 @@ public static Response FromJson(string value)
150154
/// </summary>
151155
/// <param name="value">The JSON string to deserialize into a <see cref="Response"/>.</param>
152156
/// <returns>A <see cref="Response"/> object described by the JSON string.</returns>
157+
/// <exception cref="ArgumentNullException">If <paramref name="value"/> is <see langword="null"/>.</exception>
158+
/// <exception cref="JsonException">If <paramref name="value"/> is not a valid JSON object.</exception>
159+
/// <exception cref="WebDriverException">If the JSON dictionary is not in the expected state, per spec.</exception>
153160
public static Response FromErrorJson(string value)
154161
{
155-
var deserializedResponse = JsonSerializer.Deserialize<Dictionary<string, object>>(value, s_jsonSerializerOptions)
162+
Dictionary<string, object?> deserializedResponse = JsonSerializer.Deserialize<Dictionary<string, object?>>(value, s_jsonSerializerOptions)
156163
?? throw new WebDriverException("JSON error response returned \"null\" value");
157164

158-
if (!deserializedResponse.TryGetValue("value", out var valueObject))
165+
if (!deserializedResponse.TryGetValue("value", out object? valueObject))
159166
{
160167
throw new WebDriverException($"The 'value' property was not found in the response:{Environment.NewLine}{value}");
161168
}
@@ -165,7 +172,7 @@ public static Response FromErrorJson(string value)
165172
throw new WebDriverException($"The 'value' property is not a dictionary of <string, object>{Environment.NewLine}{value}");
166173
}
167174

168-
if (!valueDictionary.TryGetValue("error", out var errorObject))
175+
if (!valueDictionary.TryGetValue("error", out object? errorObject))
169176
{
170177
throw new WebDriverException($"The 'value > error' property was not found in the response:{Environment.NewLine}{value}");
171178
}

0 commit comments

Comments
 (0)