Skip to content

Commit abb8440

Browse files
[dotnet] Modernize Response type (#14839)
1 parent be90e2f commit abb8440

File tree

1 file changed

+25
-43
lines changed

1 file changed

+25
-43
lines changed

dotnet/src/webdriver/Response.cs

+25-43
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,12 @@ namespace OpenQA.Selenium
3131
/// </summary>
3232
public class Response
3333
{
34-
private readonly static JsonSerializerOptions s_jsonSerializerOptions = new()
34+
private static readonly JsonSerializerOptions s_jsonSerializerOptions = new()
3535
{
3636
TypeInfoResolver = ResponseJsonSerializerContext.Default,
3737
Converters = { new ResponseValueJsonConverter() } // we still need it to make `Object` as `Dictionary`
3838
};
3939

40-
private object responseValue;
41-
private string responseSessionId;
42-
private WebDriverResult responseStatus;
43-
4440
/// <summary>
4541
/// Initializes a new instance of the <see cref="Response"/> class
4642
/// </summary>
@@ -56,7 +52,7 @@ public Response(SessionId sessionId)
5652
{
5753
if (sessionId != null)
5854
{
59-
this.responseSessionId = sessionId.ToString();
55+
this.SessionId = sessionId.ToString();
6056
}
6157
}
6258

@@ -66,49 +62,48 @@ private Response(Dictionary<string, object> rawResponse)
6662
{
6763
if (rawResponse["sessionId"] != null)
6864
{
69-
this.responseSessionId = rawResponse["sessionId"].ToString();
65+
this.SessionId = rawResponse["sessionId"].ToString();
7066
}
7167
}
7268

73-
if (rawResponse.ContainsKey("value"))
69+
if (rawResponse.TryGetValue("value", out object value))
7470
{
75-
this.responseValue = rawResponse["value"];
71+
this.Value = value;
7672
}
7773

7874
// If the returned object does *not* have a "value" property
7975
// the response value should be the entirety of the response.
8076
// TODO: Remove this if statement altogether; there should
8177
// never be a spec-compliant response that does not contain a
8278
// value property.
83-
if (!rawResponse.ContainsKey("value") && this.responseValue == null)
79+
if (!rawResponse.ContainsKey("value") && this.Value == null)
8480
{
8581
// Special-case for the new session command, where the "capabilities"
8682
// property of the response is the actual value we're interested in.
8783
if (rawResponse.ContainsKey("capabilities"))
8884
{
89-
this.responseValue = rawResponse["capabilities"];
85+
this.Value = rawResponse["capabilities"];
9086
}
9187
else
9288
{
93-
this.responseValue = rawResponse;
89+
this.Value = rawResponse;
9490
}
9591
}
9692

97-
Dictionary<string, object> valueDictionary = this.responseValue as Dictionary<string, object>;
98-
if (valueDictionary != null)
93+
if (this.Value is Dictionary<string, object> valueDictionary)
9994
{
10095
// Special case code for the new session command. If the response contains
10196
// sessionId and capabilities properties, fix up the session ID and value members.
10297
if (valueDictionary.ContainsKey("sessionId"))
10398
{
104-
this.responseSessionId = valueDictionary["sessionId"].ToString();
105-
if (valueDictionary.ContainsKey("capabilities"))
99+
this.SessionId = valueDictionary["sessionId"].ToString();
100+
if (valueDictionary.TryGetValue("capabilities", out object capabilities))
106101
{
107-
this.responseValue = valueDictionary["capabilities"];
102+
this.Value = capabilities;
108103
}
109104
else
110105
{
111-
this.responseValue = valueDictionary["value"];
106+
this.Value = valueDictionary["value"];
112107
}
113108
}
114109
}
@@ -117,29 +112,17 @@ private Response(Dictionary<string, object> rawResponse)
117112
/// <summary>
118113
/// Gets or sets the value from JSON.
119114
/// </summary>
120-
public object Value
121-
{
122-
get { return this.responseValue; }
123-
set { this.responseValue = value; }
124-
}
115+
public object Value { get; set; }
125116

126117
/// <summary>
127118
/// Gets or sets the session ID.
128119
/// </summary>
129-
public string SessionId
130-
{
131-
get { return this.responseSessionId; }
132-
set { this.responseSessionId = value; }
133-
}
120+
public string SessionId { get; set; }
134121

135122
/// <summary>
136123
/// Gets or sets the status value of the response.
137124
/// </summary>
138-
public WebDriverResult Status
139-
{
140-
get { return this.responseStatus; }
141-
set { this.responseStatus = value; }
142-
}
125+
public WebDriverResult Status { get; set; }
143126

144127
/// <summary>
145128
/// Returns a new <see cref="Response"/> from a JSON-encoded string.
@@ -148,9 +131,10 @@ public WebDriverResult Status
148131
/// <returns>A <see cref="Response"/> object described by the JSON string.</returns>
149132
public static Response FromJson(string value)
150133
{
151-
Dictionary<string, object> deserializedResponse = JsonSerializer.Deserialize<Dictionary<string, object>>(value, s_jsonSerializerOptions);
152-
Response response = new Response(deserializedResponse);
153-
return response;
134+
Dictionary<string, object> deserializedResponse = JsonSerializer.Deserialize<Dictionary<string, object>>(value, s_jsonSerializerOptions)
135+
?? throw new WebDriverException("JSON success response returned \"null\" value");
136+
137+
return new Response(deserializedResponse);
154138
}
155139

156140
/// <summary>
@@ -160,7 +144,8 @@ public static Response FromJson(string value)
160144
/// <returns>A <see cref="Response"/> object described by the JSON string.</returns>
161145
public static Response FromErrorJson(string value)
162146
{
163-
var deserializedResponse = JsonSerializer.Deserialize<Dictionary<string, object>>(value, s_jsonSerializerOptions);
147+
var deserializedResponse = JsonSerializer.Deserialize<Dictionary<string, object>>(value, s_jsonSerializerOptions)
148+
?? throw new WebDriverException("JSON error response returned \"null\" value");
164149

165150
var response = new Response();
166151

@@ -181,14 +166,14 @@ public static Response FromErrorJson(string value)
181166
throw new WebDriverException($"The 'value > error' property was not found in the response:{Environment.NewLine}{value}");
182167
}
183168

184-
if (errorObject is not string)
169+
if (errorObject is not string errorString)
185170
{
186171
throw new WebDriverException($"The 'value > error' property is not a string{Environment.NewLine}{value}");
187172
}
188173

189174
response.Value = deserializedResponse["value"];
190175

191-
response.Status = WebDriverError.ResultFromError(errorObject.ToString());
176+
response.Status = WebDriverError.ResultFromError(errorString);
192177

193178
return response;
194179
}
@@ -213,8 +198,5 @@ public override string ToString()
213198
}
214199

215200
[JsonSerializable(typeof(Dictionary<string, object>))]
216-
internal partial class ResponseJsonSerializerContext : JsonSerializerContext
217-
{
218-
219-
}
201+
internal sealed partial class ResponseJsonSerializerContext : JsonSerializerContext;
220202
}

0 commit comments

Comments
 (0)