Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dotnet] Refactor away private constructor from Response #14877

Merged
merged 5 commits into from
Dec 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading