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 6 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
73 changes: 59 additions & 14 deletions dotnet/src/webdriver/SessionId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,34 @@
// under the License.
// </copyright>

using System;

#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>
/// <exception cref="ArgumentNullException">If <paramref name="opaqueKey"/> is <see langword="null"/>.</exception>
public SessionId(string opaqueKey)
{
this.sessionOpaqueKey = opaqueKey;
this.sessionOpaqueKey = opaqueKey ?? throw new ArgumentNullException(nameof(opaqueKey));
}

/// <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 +55,64 @@ public override string ToString()
/// <returns>The hash code of the key</returns>
public override int GetHashCode()
{
return this.sessionOpaqueKey.GetHashCode();
return StringComparer.Ordinal.GetHashCode(this.sessionOpaqueKey);
}

/// <summary>
/// Indicates whether the current session ID value is the same as <paramref name="obj"/>.
/// </summary>
/// <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)
{
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 two Sessions
/// Compares the two values to determine equality.
/// </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="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)
{
bool objectsAreEqual = false;
SessionId other = obj as SessionId;
if (other != null)
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 = response.SessionId is null ? null : new SessionId(response.SessionId);
nvborisenko marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>
Expand Down