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 DriverServiceCommandExecutor for nullability #14942

Merged

Conversation

RenderMichael
Copy link
Contributor

@RenderMichael RenderMichael commented Dec 26, 2024

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

Motivation and Context

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

  • Enhanced type safety by enabling nullable reference types in command executor classes
  • Improved DriverServiceCommandExecutor implementation:
    • Added proper null checks in constructors with documented exceptions
    • Changed internalExecutor field to auto-implemented property HttpExecutor
    • Fixed disposal pattern by adding GC.SuppressFinalize
  • Added XML documentation for constructor exceptions

Changes walkthrough 📝

Relevant files
Enhancement
DriverServiceCommandExecutor.cs
Enhance nullability and improve command executor implementation

dotnet/src/webdriver/Remote/DriverServiceCommandExecutor.cs

  • Added nullable reference type annotations
  • Changed internalExecutor field to property HttpExecutor
  • Added null checks in constructors with proper exception documentation
  • Improved disposal pattern with GC.SuppressFinalize
  • +17/-15 
    ICommandServer.cs
    Enable nullable reference types                                                   

    dotnet/src/webdriver/Remote/ICommandServer.cs

    • Added nullable reference type annotations
    +2/-0     

    💡 PR-Agent usage: Comment /help "your question" on any pull request to receive relevant 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

    Uninitialized Variable

    The toReturn variable is declared but may not be initialized if an exception occurs in the try block before assignment. Consider initializing it to null or a default value.

    Response toReturn;
    if (commandToExecute.Name == DriverCommand.NewSession)
    {
        this.service.Start();
    }
    
    // Use a try-catch block to catch exceptions for the Quit
    // command, so that we can get the finally block.
    try
    {
        toReturn = await this.HttpExecutor.ExecuteAsync(commandToExecute).ConfigureAwait(false);

    Copy link
    Contributor

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Prevent execution of commands on disposed objects to maintain proper object lifecycle

    Add a check for isDisposed in ExecuteAsync to prevent executing commands on a
    disposed instance, which could lead to unexpected behavior.

    dotnet/src/webdriver/Remote/DriverServiceCommandExecutor.cs [107-112]

     public async Task<Response> ExecuteAsync(Command commandToExecute)
     {
    +    if (isDisposed)
    +    {
    +        throw new ObjectDisposedException(nameof(DriverServiceCommandExecutor));
    +    }
         if (commandToExecute == null)
         {
             throw new ArgumentNullException(nameof(commandToExecute), "Command to execute cannot be null");
         }
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: This is a critical safety check that prevents potential bugs and resource-related issues by ensuring disposed objects cannot be used, following proper .NET disposal patterns.

    8
    General
    Initialize local variables with default values to prevent potential uninitialized variable usage

    Initialize toReturn with a default value to ensure it's always assigned before use.
    The current code could lead to a compiler warning about uninitialized variable
    usage.

    dotnet/src/webdriver/Remote/DriverServiceCommandExecutor.cs [114-118]

    -Response toReturn;
    +Response toReturn = null!;
     if (commandToExecute.Name == DriverCommand.NewSession)
     {
         this.service.Start();
     }
    • Apply this suggestion
    Suggestion importance[1-10]: 3

    Why: While the suggestion is technically correct for avoiding compiler warnings, the variable is guaranteed to be assigned in the try block before use, making this a minor improvement with minimal impact on code reliability.

    3

    Copy link
    Member

    @nvborisenko nvborisenko left a comment

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Thanks @RenderMichael !

    @nvborisenko nvborisenko merged commit 0fbc0bf into SeleniumHQ:trunk Dec 27, 2024
    10 checks passed
    @RenderMichael RenderMichael deleted the DriverServiceCommandExecutor branch December 27, 2024 22:10
    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