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] Add nullability annotations to SessionId #14841

Merged
merged 18 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
97 changes: 83 additions & 14 deletions dotnet/src/webdriver/SessionId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,53 @@
// under the License.
// </copyright>

using System;
using System.Diagnostics.CodeAnalysis;

#nullable enable

namespace OpenQA.Selenium
{
/// <summary>
/// Provides a mechanism for maintaining a session for a test
/// </summary>
public class SessionId
public class SessionId : IEquatable<SessionId>, IEquatable<string>
{
private string sessionOpaqueKey;
private readonly string? sessionOpaqueKey;

/// <summary>
/// Initializes a new instance of the <see cref="SessionId"/> class
/// </summary>
/// <param name="opaqueKey">Key for the session in use</param>
public SessionId(string opaqueKey)
[Obsolete("Call SessionId.Create")]
public SessionId(string? opaqueKey)
nvborisenko marked this conversation as resolved.
Show resolved Hide resolved
{
this.sessionOpaqueKey = opaqueKey;
}

/// <summary>
/// Creates a new <see cref="SessionId"/> instance, or <see langword="null"/> if <paramref name="opaqueKey"/> is <see langword="null"/>.
/// </summary>
/// <param name="opaqueKey">The session ID value.</param>
/// <returns>A <see cref="SessionId"/>, or <see langword="null"/>.</returns>
[return: NotNullIfNotNull(nameof(opaqueKey))]
public static SessionId? Create(string? opaqueKey)
{
if (opaqueKey is null)
{
return null;
}

#pragma warning disable CS0618 // Type or member is obsolete
return new SessionId(opaqueKey);
#pragma warning restore CS0618 // Type or member is obsolete
}

/// <summary>
/// Get the value of the key
/// </summary>
/// <returns>The key in use</returns>
public override string ToString()
public override string? ToString()
RenderMichael marked this conversation as resolved.
Show resolved Hide resolved
{
return this.sessionOpaqueKey;
}
Expand All @@ -50,24 +74,69 @@ public override string ToString()
/// <returns>The hash code of the key</returns>
public override int GetHashCode()
{
return this.sessionOpaqueKey.GetHashCode();
if (this.sessionOpaqueKey is { } key)
{
return StringComparer.Ordinal.GetHashCode(key);
}

return 0;
}

/// <summary>
/// Compares two Sessions
/// Indicates whether the current session ID value is the same as <paramref name="obj"/>.
/// </summary>
/// <param name="obj">Session to compare</param>
/// <returns>True if they are equal or False if they are not</returns>
public override bool Equals(object obj)
/// <param name="obj">The session to compare to.</param>
/// <returns><see langword="true"/> if the values are equal; otherwise, <see langword="false"/>.</returns>
public override bool Equals(object? obj)
{
bool objectsAreEqual = false;
SessionId other = obj as SessionId;
if (other != null)
return Equals(obj as SessionId);
}

/// <summary>
/// Indicates whether the current session ID value is the same as <paramref name="other"/>.
/// </summary>
/// <param name="other">A value to compare with this session ID.</param>
/// <returns><see langword="true"/> if the current session ID is equal to the <paramref name="other"/> parameter; otherwise, <see langword="false"/>.</returns>
public bool Equals(SessionId? other)
{
return other is not null && Equals(other.sessionOpaqueKey);
}

/// <summary>
/// Indicates whether the current session ID value is the same as <paramref name="other"/>.
/// </summary>
/// <param name="other">A value to compare with this session ID></param>
/// <returns><see langword="true"/> if the current session ID is equal to the <paramref name="other"/> parameter; otherwise, <see langword="false"/>.</returns>
public bool Equals(string? other)
{
return string.Equals(this.sessionOpaqueKey, other, StringComparison.Ordinal);
}

/// <summary>
/// Compares the two values to determine equality.
/// </summary>
/// <param name="left">The value to compare with <paramref name="right"/>.</param>
/// <param name="right">The value to compare with <paramref name="left"/>.</param>
/// <returns><see langword="true"/> if <paramref name="left"/> is equal to <paramref name="right"/>; otherwise, <see langword="false"/>.</returns>
public static bool operator ==(SessionId? left, SessionId? right)
{
if (left is null)
{
objectsAreEqual = this.sessionOpaqueKey.Equals(other.sessionOpaqueKey);
return right is null;
}

return objectsAreEqual;
return left.Equals(right);
}

/// <summary>
/// Compares the two values to determine inequality.
/// </summary>
/// <param name="left">The value to compare with <paramref name="right"/>.</param>
/// <param name="right">The value to compare with <paramref name="left"/>.</param>
/// <returns><see langword="true"/> if <paramref name="left"/> is not equal to <paramref name="right"/>; otherwise, <see langword="false"/>.</returns>
public static bool operator !=(SessionId? left, SessionId? right)
{
return !(left == right);
}
}
}
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/WebDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ protected void StartSession(ICapabilities capabilities)

ReturnedCapabilities returnedCapabilities = new ReturnedCapabilities(rawCapabilities);
this.capabilities = returnedCapabilities;
this.sessionId = new SessionId(response.SessionId);
this.sessionId = SessionId.Create(response.SessionId);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion dotnet/test/common/CommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void CommandSerializesAnonymousType()
["arg"] = new { param1 = true, param2 = false },
};

var command = new Command(new SessionId("session"), "test command", parameters);
var command = new Command(SessionId.Create("session"), "test command", parameters);

Assert.That(command.ParametersAsJsonString, Is.EqualTo("""{"arg":{"param1":true,"param2":false}}"""));
}
Expand Down