Skip to content

Commit

Permalink
[dotnet] Add nullability to Command type (#15147)
Browse files Browse the repository at this point in the history
* [dotnet] Add nullability to `Command` type

* Simplify initialization logic of `Command.Parameters`
  • Loading branch information
RenderMichael authored Jan 24, 2025
1 parent 286c139 commit 2383fac
Showing 1 changed file with 18 additions and 31 deletions.
49 changes: 18 additions & 31 deletions dotnet/src/webdriver/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,21 @@
// </copyright>

using OpenQA.Selenium.Internal;
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;

#nullable enable

namespace OpenQA.Selenium
{
/// <summary>
/// Provides a way to send commands to the remote server
/// </summary>
public class Command
{
private SessionId commandSessionId;
private string commandName;
private Dictionary<string, object> commandParameters = new Dictionary<string, object>();

private readonly static JsonSerializerOptions s_jsonSerializerOptions = new()
{
TypeInfoResolver = JsonTypeInfoResolver.Combine(CommandJsonSerializerContext.Default, new DefaultJsonTypeInfoResolver()),
Expand All @@ -56,43 +55,30 @@ public Command(string name, string jsonParameters)
/// <param name="sessionId">Session ID the driver is using</param>
/// <param name="name">Name of the command</param>
/// <param name="parameters">Parameters for that command</param>
public Command(SessionId sessionId, string name, Dictionary<string, object> parameters)
public Command(SessionId? sessionId, string name, Dictionary<string, object>? parameters)
{
this.commandSessionId = sessionId;
if (parameters != null)
{
this.commandParameters = parameters;
}

this.commandName = name;
this.SessionId = sessionId;
this.Parameters = parameters ?? new Dictionary<string, object>();
this.Name = name;
}

/// <summary>
/// Gets the SessionID of the command
/// </summary>
[JsonPropertyName("sessionId")]
public SessionId SessionId
{
get { return this.commandSessionId; }
}
public SessionId? SessionId { get; }

/// <summary>
/// Gets the command name
/// </summary>
[JsonPropertyName("name")]
public string Name
{
get { return this.commandName; }
}
public string Name { get; }

/// <summary>
/// Gets the parameters of the command
/// </summary>
[JsonPropertyName("parameters")]
public Dictionary<string, object> Parameters
{
get { return this.commandParameters; }
}
public Dictionary<string, object> Parameters { get; }

/// <summary>
/// Gets the parameters of the command as a JSON-encoded string.
Expand All @@ -101,13 +87,12 @@ public string ParametersAsJsonString
{
get
{
string parametersString = string.Empty;
if (this.commandParameters != null && this.commandParameters.Count > 0)
string parametersString;
if (this.Parameters != null && this.Parameters.Count > 0)
{
parametersString = JsonSerializer.Serialize(this.commandParameters, s_jsonSerializerOptions);
parametersString = JsonSerializer.Serialize(this.Parameters, s_jsonSerializerOptions);
}

if (string.IsNullOrEmpty(parametersString))
else
{
parametersString = "{}";
}
Expand All @@ -130,9 +115,11 @@ public override string ToString()
/// </summary>
/// <param name="value">The JSON-encoded string representing the command parameters.</param>
/// <returns>A <see cref="Dictionary{K, V}"/> with a string keys, and an object value. </returns>
private static Dictionary<string, object> ConvertParametersFromJson(string value)
/// <exception cref="JsonException">If <paramref name="value"/> is not a JSON object.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="value"/> is <see langword="null"/>.</exception>
private static Dictionary<string, object>? ConvertParametersFromJson(string value)
{
Dictionary<string, object> parameters = JsonSerializer.Deserialize<Dictionary<string, object>>(value, s_jsonSerializerOptions);
Dictionary<string, object>? parameters = JsonSerializer.Deserialize<Dictionary<string, object>>(value, s_jsonSerializerOptions);
return parameters;
}
}
Expand Down

0 comments on commit 2383fac

Please sign in to comment.