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 to Command type #15147

Merged
merged 4 commits into from
Jan 24, 2025

Conversation

RenderMichael
Copy link
Contributor

@RenderMichael RenderMichael commented Jan 24, 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

Add nullability to Command type

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, Bug fix


Description

  • Added nullability annotations to the Command class.

  • Refactored properties to use auto-implemented properties.

  • Updated method ConvertParametersFromJson to handle null and exceptions.

  • Improved code readability and consistency in Command class.


Changes walkthrough 📝

Relevant files
Enhancement
Command.cs
Refactored and added nullability to `Command` class           

dotnet/src/webdriver/Command.cs

  • Enabled nullability annotations in the file.
  • Refactored fields to auto-implemented properties.
  • Updated ConvertParametersFromJson to handle null and exceptions.
  • Improved code structure and readability.
  • +18/-27 

    Need help?
  • Type /help how to ... in the comments thread for any questions 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 Check

    The constructor assigns parameters to Parameters property only when parameters is not null, but Parameters is always initialized with new Dictionary. This logic seems inconsistent and may lead to confusion.

    if (parameters != null)
    {
        this.Parameters = parameters;
    }
    Parameter Validation

    The constructor does not validate if the name parameter is null or empty, which could lead to invalid command state.

    public Command(SessionId? sessionId, string name, Dictionary<string, object>? parameters)
    {
        this.SessionId = sessionId;
        if (parameters != null)
        {
            this.Parameters = parameters;
        }
    
        this.Name = name;
    }

    Copy link
    Contributor

    qodo-merge-pro bot commented Jan 24, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Validate required command name parameter

    Add null check for the 'name' parameter in the constructor to prevent creating
    commands with invalid names.

    dotnet/src/webdriver/Command.cs [58-67]

     public Command(SessionId? sessionId, string name, Dictionary<string, object>? parameters)
     {
         this.SessionId = sessionId;
    +    if (string.IsNullOrEmpty(name))
    +    {
    +        throw new ArgumentNullException(nameof(name));
    +    }
    +    this.Name = name;
    +
         if (parameters != null)
         {
             this.Parameters = parameters;
         }
    -
    -    this.Name = name;
     }
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Adding null validation for the required 'name' parameter is crucial for preventing runtime errors and ensuring command integrity. This is a significant improvement for robustness and error handling.

    8
    ✅ Fix parameters initialization logic
    Suggestion Impact:The commit addresses the same issue but implements a different solution - instead of making Parameters settable with private set, it removes the default initialization and uses null coalescing in the constructor

    code diff:

    -        public Dictionary<string, object> Parameters { get; } = new Dictionary<string, object>();
    +        public Dictionary<string, object> Parameters { get; }

    The constructor assigns parameters directly to Parameters property only when not
    null, but the property already initializes a new dictionary. This can lead to
    inconsistent state. Consider removing the default initialization.

    dotnet/src/webdriver/Command.cs [85]

    -public Dictionary<string, object> Parameters { get; } = new Dictionary<string, object>();
    +public Dictionary<string, object> Parameters { get; private set; } = new();
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: The suggestion correctly identifies a potential state inconsistency issue where the Parameters property has both an initializer and conditional assignment in the constructor, which could lead to confusion and bugs.

    7

    @RenderMichael RenderMichael merged commit 2383fac into SeleniumHQ:trunk Jan 24, 2025
    10 checks passed
    @RenderMichael RenderMichael deleted the dotnet-command branch January 24, 2025 17:23
    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.

    1 participant