Skip to content

Commit

Permalink
[dotnet] Refactor away private constructor from Response (#14877)
Browse files Browse the repository at this point in the history
  • Loading branch information
RenderMichael authored Dec 11, 2024
1 parent 28e9a3a commit 0174bce
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions dotnet/src/webdriver/Response.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,57 +56,69 @@ public Response(SessionId sessionId)
}
}

private Response(Dictionary<string, object> rawResponse)
/// <summary>
/// Returns a new <see cref="Response"/> from a JSON-encoded string.
/// </summary>
/// <param name="value">The JSON string to deserialize into a <see cref="Response"/>.</param>
/// <returns>A <see cref="Response"/> object described by the JSON string.</returns>
public static Response FromJson(string value)
{
Dictionary<string, object> rawResponse = JsonSerializer.Deserialize<Dictionary<string, object>>(value, s_jsonSerializerOptions)
?? throw new WebDriverException("JSON success response returned \"null\" value");

var response = new Response();

if (rawResponse.ContainsKey("sessionId"))
{
if (rawResponse["sessionId"] != null)
{
this.SessionId = rawResponse["sessionId"].ToString();
response.SessionId = rawResponse["sessionId"].ToString();
}
}

if (rawResponse.TryGetValue("value", out object value))
if (rawResponse.TryGetValue("value", out object valueObj))
{
this.Value = value;
response.Value = valueObj;
}

// If the returned object does *not* have a "value" property
// the response value should be the entirety of the response.
// 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.Value == null)
if (!rawResponse.ContainsKey("value") && response.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.Value = rawResponse["capabilities"];
response.Value = rawResponse["capabilities"];
}
else
{
this.Value = rawResponse;
response.Value = rawResponse;
}
}

if (this.Value is Dictionary<string, object> valueDictionary)
if (response.Value is Dictionary<string, object> 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.SessionId = valueDictionary["sessionId"].ToString();
response.SessionId = valueDictionary["sessionId"].ToString();
if (valueDictionary.TryGetValue("capabilities", out object capabilities))
{
this.Value = capabilities;
response.Value = capabilities;
}
else
{
this.Value = valueDictionary["value"];
response.Value = valueDictionary["value"];
}
}
}

return response;
}

/// <summary>
Expand All @@ -124,18 +136,6 @@ private Response(Dictionary<string, object> rawResponse)
/// </summary>
public WebDriverResult Status { get; set; }

/// <summary>
/// Returns a new <see cref="Response"/> from a JSON-encoded string.
/// </summary>
/// <param name="value">The JSON string to deserialize into a <see cref="Response"/>.</param>
/// <returns>A <see cref="Response"/> object described by the JSON string.</returns>
public static Response FromJson(string value)
{
Dictionary<string, object> deserializedResponse = JsonSerializer.Deserialize<Dictionary<string, object>>(value, s_jsonSerializerOptions)
?? throw new WebDriverException("JSON success response returned \"null\" value");

return new Response(deserializedResponse);
}

/// <summary>
/// Returns a new <see cref="Response"/> from a JSON-encoded string.
Expand Down

0 comments on commit 0174bce

Please sign in to comment.