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] Annotate nullability for DriverService and chromium/safari services #15101

Merged
merged 3 commits into from
Jan 24, 2025

Conversation

RenderMichael
Copy link
Contributor

@RenderMichael RenderMichael commented Jan 16, 2025

User description

Thanks for contributing to Selenium!
A PR well described will help maintainers to quickly review and merge it

Before submitting your PR, please check our contributing guidelines.
Avoid large PRs, help reviewers by making them as simple and short as possible.

Description

Adds nullability annotations to DriverService, as well as some child types SafariDriverService and ChromiumDriverService.

Motivation and Context

Contributes to #14640

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • I have read the contributing document.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

PR Type

Enhancement


Description

  • Added nullability annotations to DriverService and derived classes.

  • Refactored properties in ChromiumDriverService to use auto-properties.

  • Improved code readability and safety with nullable reference types.

  • Updated event handling and initialization logic in DriverService.


Changes walkthrough 📝

Relevant files
Enhancement
ChromiumDriverService.cs
Refactored `ChromiumDriverService` with nullability annotations

dotnet/src/webdriver/Chromium/ChromiumDriverService.cs

  • Enabled nullable reference types for the file.
  • Refactored properties to use auto-properties with default values.
  • Updated command-line argument construction for null-safe properties.
  • Simplified obsolete property handling and improved readability.
  • +33/-68 
    DriverService.cs
    Enhanced `DriverService` with nullability and refactoring

    dotnet/src/webdriver/DriverService.cs

  • Enabled nullable reference types for the file.
  • Refactored fields to properties with default values.
  • Updated event handling with null-safe invocation.
  • Improved initialization and process management logic.
  • +40/-80 
    SafariDriverService.cs
    Updated `SafariDriverService` with nullability and refactoring

    dotnet/src/webdriver/Safari/SafariDriverService.cs

  • Enabled nullable reference types for the file.
  • Updated constructor parameters to nullable types.
  • Refactored properties to use expression-bodied members.
  • Improved default service creation logic with null safety.
  • +7/-5     

    Need help?
  • Type /help how to ... in the comments thread for any question about Qodo Merge usage.
  • Check out the documentation for more information.
  • Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Null Reference

    The ServiceUrl property constructs a new Uri without checking if HostName is null, which could lead to a NullReferenceException.

        get
        {
            string url = string.Format(CultureInfo.InvariantCulture, "http://{0}:{1}", this.HostName, this.Port);
            return new Uri(url);
        }
    }
    Error Handling

    The Start() method throws a generic WebDriverException without including the original exception details when service initialization fails.

        throw new WebDriverException($"Cannot start the driver service on {this.ServiceUrl}");
    }

    Copy link
    Contributor

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Add proper null checking when processing file paths to prevent runtime errors

    Add null check for driverPath before using Path.GetDirectoryName() since it could
    return null for invalid paths. Consider throwing an ArgumentException if path is
    invalid.

    dotnet/src/webdriver/Safari/SafariDriverService.cs [112]

    -driverPath = Path.GetDirectoryName(driverPath)!;
    +var dirPath = Path.GetDirectoryName(driverPath);
    +if (dirPath == null)
    +{
    +    throw new ArgumentException("Invalid driver path specified", nameof(driverPath));
    +}
    +driverPath = dirPath;
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: The suggestion addresses a potential runtime error by adding proper null checks for directory paths. This is important for robustness and error handling, preventing crashes when invalid paths are provided.

    8
    Add null checks when accessing nested properties to prevent null reference exceptions

    Add null check for response.Content.Headers.ContentType before accessing its
    MediaType property to prevent potential NullReferenceException.

    dotnet/src/webdriver/DriverService.cs [195]

    -isInitialized = response.StatusCode == HttpStatusCode.OK && response.Content.Headers.ContentType is { MediaType: string mediaType } && mediaType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase);
    +isInitialized = response.StatusCode == HttpStatusCode.OK && 
    +                response.Content?.Headers?.ContentType?.MediaType is string mediaType && 
    +                mediaType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase);
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: The suggestion improves code safety by adding proper null checks for nested properties in the HTTP response handling. This prevents potential NullReferenceExceptions in production environments.

    7

    @nvborisenko
    Copy link
    Member

    In general any field here can be null. Later all fields go to executable as command parameters (if not null or empty).

    The principle question:

    driverService.PortServerAddress = null; // am I doing right as an user? or exception?

    I guess any parameter in this class can be nullable.

    TIP: AndroidDebugBridgePort also can be nullable, previously -1 indicated it is not set (as I understand the logic).

    @RenderMichael
    Copy link
    Contributor Author

    Good points! Let me update the code.

    @RenderMichael RenderMichael merged commit 286c139 into SeleniumHQ:trunk Jan 24, 2025
    10 checks passed
    @RenderMichael RenderMichael deleted the driver-service-nulable branch January 24, 2025 17:04
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    2 participants