diff --git a/dotnet/src/webdriver/Response.cs b/dotnet/src/webdriver/Response.cs
index 39962a0484a1f..0af9b181779b3 100644
--- a/dotnet/src/webdriver/Response.cs
+++ b/dotnet/src/webdriver/Response.cs
@@ -31,16 +31,12 @@ namespace OpenQA.Selenium
///
public class Response
{
- private readonly static JsonSerializerOptions s_jsonSerializerOptions = new()
+ private static readonly JsonSerializerOptions s_jsonSerializerOptions = new()
{
TypeInfoResolver = ResponseJsonSerializerContext.Default,
Converters = { new ResponseValueJsonConverter() } // we still need it to make `Object` as `Dictionary`
};
- private object responseValue;
- private string responseSessionId;
- private WebDriverResult responseStatus;
-
///
/// Initializes a new instance of the class
///
@@ -56,7 +52,7 @@ public Response(SessionId sessionId)
{
if (sessionId != null)
{
- this.responseSessionId = sessionId.ToString();
+ this.SessionId = sessionId.ToString();
}
}
@@ -66,13 +62,13 @@ private Response(Dictionary rawResponse)
{
if (rawResponse["sessionId"] != null)
{
- this.responseSessionId = rawResponse["sessionId"].ToString();
+ this.SessionId = rawResponse["sessionId"].ToString();
}
}
- if (rawResponse.ContainsKey("value"))
+ if (rawResponse.TryGetValue("value", out object value))
{
- this.responseValue = rawResponse["value"];
+ this.Value = value;
}
// If the returned object does *not* have a "value" property
@@ -80,35 +76,34 @@ private Response(Dictionary rawResponse)
// TODO: Remove this if statement altogether; there should
// never be a spec-compliant response that does not contain a
// value property.
- if (!rawResponse.ContainsKey("value") && this.responseValue == null)
+ if (!rawResponse.ContainsKey("value") && this.Value == null)
{
// Special-case for the new session command, where the "capabilities"
// property of the response is the actual value we're interested in.
if (rawResponse.ContainsKey("capabilities"))
{
- this.responseValue = rawResponse["capabilities"];
+ this.Value = rawResponse["capabilities"];
}
else
{
- this.responseValue = rawResponse;
+ this.Value = rawResponse;
}
}
- Dictionary valueDictionary = this.responseValue as Dictionary;
- if (valueDictionary != null)
+ if (this.Value is Dictionary valueDictionary)
{
// Special case code for the new session command. If the response contains
// sessionId and capabilities properties, fix up the session ID and value members.
if (valueDictionary.ContainsKey("sessionId"))
{
- this.responseSessionId = valueDictionary["sessionId"].ToString();
- if (valueDictionary.ContainsKey("capabilities"))
+ this.SessionId = valueDictionary["sessionId"].ToString();
+ if (valueDictionary.TryGetValue("capabilities", out object capabilities))
{
- this.responseValue = valueDictionary["capabilities"];
+ this.Value = capabilities;
}
else
{
- this.responseValue = valueDictionary["value"];
+ this.Value = valueDictionary["value"];
}
}
}
@@ -117,29 +112,17 @@ private Response(Dictionary rawResponse)
///
/// Gets or sets the value from JSON.
///
- public object Value
- {
- get { return this.responseValue; }
- set { this.responseValue = value; }
- }
+ public object Value { get; set; }
///
/// Gets or sets the session ID.
///
- public string SessionId
- {
- get { return this.responseSessionId; }
- set { this.responseSessionId = value; }
- }
+ public string SessionId { get; set; }
///
/// Gets or sets the status value of the response.
///
- public WebDriverResult Status
- {
- get { return this.responseStatus; }
- set { this.responseStatus = value; }
- }
+ public WebDriverResult Status { get; set; }
///
/// Returns a new from a JSON-encoded string.
@@ -148,9 +131,10 @@ public WebDriverResult Status
/// A object described by the JSON string.
public static Response FromJson(string value)
{
- Dictionary deserializedResponse = JsonSerializer.Deserialize>(value, s_jsonSerializerOptions);
- Response response = new Response(deserializedResponse);
- return response;
+ Dictionary deserializedResponse = JsonSerializer.Deserialize>(value, s_jsonSerializerOptions)
+ ?? throw new WebDriverException("JSON success response returned \"null\" value");
+
+ return new Response(deserializedResponse);
}
///
@@ -160,7 +144,8 @@ public static Response FromJson(string value)
/// A object described by the JSON string.
public static Response FromErrorJson(string value)
{
- var deserializedResponse = JsonSerializer.Deserialize>(value, s_jsonSerializerOptions);
+ var deserializedResponse = JsonSerializer.Deserialize>(value, s_jsonSerializerOptions)
+ ?? throw new WebDriverException("JSON error response returned \"null\" value");
var response = new Response();
@@ -181,14 +166,14 @@ public static Response FromErrorJson(string value)
throw new WebDriverException($"The 'value > error' property was not found in the response:{Environment.NewLine}{value}");
}
- if (errorObject is not string)
+ if (errorObject is not string errorString)
{
throw new WebDriverException($"The 'value > error' property is not a string{Environment.NewLine}{value}");
}
response.Value = deserializedResponse["value"];
- response.Status = WebDriverError.ResultFromError(errorObject.ToString());
+ response.Status = WebDriverError.ResultFromError(errorString);
return response;
}
@@ -213,8 +198,5 @@ public override string ToString()
}
[JsonSerializable(typeof(Dictionary))]
- internal partial class ResponseJsonSerializerContext : JsonSerializerContext
- {
-
- }
+ internal sealed partial class ResponseJsonSerializerContext : JsonSerializerContext;
}