@@ -31,16 +31,12 @@ namespace OpenQA.Selenium
31
31
/// </summary>
32
32
public class Response
33
33
{
34
- private readonly static JsonSerializerOptions s_jsonSerializerOptions = new ( )
34
+ private static readonly JsonSerializerOptions s_jsonSerializerOptions = new ( )
35
35
{
36
36
TypeInfoResolver = ResponseJsonSerializerContext . Default ,
37
37
Converters = { new ResponseValueJsonConverter ( ) } // we still need it to make `Object` as `Dictionary`
38
38
} ;
39
39
40
- private object responseValue ;
41
- private string responseSessionId ;
42
- private WebDriverResult responseStatus ;
43
-
44
40
/// <summary>
45
41
/// Initializes a new instance of the <see cref="Response"/> class
46
42
/// </summary>
@@ -56,7 +52,7 @@ public Response(SessionId sessionId)
56
52
{
57
53
if ( sessionId != null )
58
54
{
59
- this . responseSessionId = sessionId . ToString ( ) ;
55
+ this . SessionId = sessionId . ToString ( ) ;
60
56
}
61
57
}
62
58
@@ -66,49 +62,48 @@ private Response(Dictionary<string, object> rawResponse)
66
62
{
67
63
if ( rawResponse [ "sessionId" ] != null )
68
64
{
69
- this . responseSessionId = rawResponse [ "sessionId" ] . ToString ( ) ;
65
+ this . SessionId = rawResponse [ "sessionId" ] . ToString ( ) ;
70
66
}
71
67
}
72
68
73
- if ( rawResponse . ContainsKey ( "value" ) )
69
+ if ( rawResponse . TryGetValue ( "value" , out object value ) )
74
70
{
75
- this . responseValue = rawResponse [ " value" ] ;
71
+ this . Value = value ;
76
72
}
77
73
78
74
// If the returned object does *not* have a "value" property
79
75
// the response value should be the entirety of the response.
80
76
// TODO: Remove this if statement altogether; there should
81
77
// never be a spec-compliant response that does not contain a
82
78
// value property.
83
- if ( ! rawResponse . ContainsKey ( "value" ) && this . responseValue == null )
79
+ if ( ! rawResponse . ContainsKey ( "value" ) && this . Value == null )
84
80
{
85
81
// Special-case for the new session command, where the "capabilities"
86
82
// property of the response is the actual value we're interested in.
87
83
if ( rawResponse . ContainsKey ( "capabilities" ) )
88
84
{
89
- this . responseValue = rawResponse [ "capabilities" ] ;
85
+ this . Value = rawResponse [ "capabilities" ] ;
90
86
}
91
87
else
92
88
{
93
- this . responseValue = rawResponse ;
89
+ this . Value = rawResponse ;
94
90
}
95
91
}
96
92
97
- Dictionary < string , object > valueDictionary = this . responseValue as Dictionary < string , object > ;
98
- if ( valueDictionary != null )
93
+ if ( this . Value is Dictionary < string , object > valueDictionary )
99
94
{
100
95
// Special case code for the new session command. If the response contains
101
96
// sessionId and capabilities properties, fix up the session ID and value members.
102
97
if ( valueDictionary . ContainsKey ( "sessionId" ) )
103
98
{
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 ) )
106
101
{
107
- this . responseValue = valueDictionary [ " capabilities" ] ;
102
+ this . Value = capabilities ;
108
103
}
109
104
else
110
105
{
111
- this . responseValue = valueDictionary [ "value" ] ;
106
+ this . Value = valueDictionary [ "value" ] ;
112
107
}
113
108
}
114
109
}
@@ -117,29 +112,17 @@ private Response(Dictionary<string, object> rawResponse)
117
112
/// <summary>
118
113
/// Gets or sets the value from JSON.
119
114
/// </summary>
120
- public object Value
121
- {
122
- get { return this . responseValue ; }
123
- set { this . responseValue = value ; }
124
- }
115
+ public object Value { get ; set ; }
125
116
126
117
/// <summary>
127
118
/// Gets or sets the session ID.
128
119
/// </summary>
129
- public string SessionId
130
- {
131
- get { return this . responseSessionId ; }
132
- set { this . responseSessionId = value ; }
133
- }
120
+ public string SessionId { get ; set ; }
134
121
135
122
/// <summary>
136
123
/// Gets or sets the status value of the response.
137
124
/// </summary>
138
- public WebDriverResult Status
139
- {
140
- get { return this . responseStatus ; }
141
- set { this . responseStatus = value ; }
142
- }
125
+ public WebDriverResult Status { get ; set ; }
143
126
144
127
/// <summary>
145
128
/// Returns a new <see cref="Response"/> from a JSON-encoded string.
@@ -148,9 +131,10 @@ public WebDriverResult Status
148
131
/// <returns>A <see cref="Response"/> object described by the JSON string.</returns>
149
132
public static Response FromJson ( string value )
150
133
{
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 ) ;
154
138
}
155
139
156
140
/// <summary>
@@ -160,7 +144,8 @@ public static Response FromJson(string value)
160
144
/// <returns>A <see cref="Response"/> object described by the JSON string.</returns>
161
145
public static Response FromErrorJson ( string value )
162
146
{
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" ) ;
164
149
165
150
var response = new Response ( ) ;
166
151
@@ -181,14 +166,14 @@ public static Response FromErrorJson(string value)
181
166
throw new WebDriverException ( $ "The 'value > error' property was not found in the response:{ Environment . NewLine } { value } ") ;
182
167
}
183
168
184
- if ( errorObject is not string )
169
+ if ( errorObject is not string errorString )
185
170
{
186
171
throw new WebDriverException ( $ "The 'value > error' property is not a string{ Environment . NewLine } { value } ") ;
187
172
}
188
173
189
174
response . Value = deserializedResponse [ "value" ] ;
190
175
191
- response . Status = WebDriverError . ResultFromError ( errorObject . ToString ( ) ) ;
176
+ response . Status = WebDriverError . ResultFromError ( errorString ) ;
192
177
193
178
return response ;
194
179
}
@@ -213,8 +198,5 @@ public override string ToString()
213
198
}
214
199
215
200
[ JsonSerializable ( typeof ( Dictionary < string , object > ) ) ]
216
- internal partial class ResponseJsonSerializerContext : JsonSerializerContext
217
- {
218
-
219
- }
201
+ internal sealed partial class ResponseJsonSerializerContext : JsonSerializerContext ;
220
202
}
0 commit comments