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] Fix null warnings in RelativeBy by sealing the type #15379

Merged
merged 5 commits into from
Mar 25, 2025

Conversation

RenderMichael
Copy link
Contributor

@RenderMichael RenderMichael commented Mar 5, 2025

User description

Description

Removed the parameterless constructor from RelativeBy, because it does not provide a value for root and this value is crucial to RelativeBy's functionality.

Motivation and Context

Fixes null warning on RelativeBy.root and prevents extending the RelativeBy type.

Currently, if you try to extend the type, you get an error:

var elements = driver.FindElements(new MyRelativeBy());

class MyRelativeBy : RelativeBy
{
}
System.ArgumentNullException : object to serialize must not be null (Parameter 'root')
    RelativeBy.GetSerializableObject(Object root) line 379
    RelativeBy.FindElements(ISearchContext context) line 106
    WebDriver.FindElements(By by) line 377

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

Bug fix, Enhancement


Description

  • Sealed the RelativeBy class to prevent extension.

  • Removed the parameterless constructor from RelativeBy.

  • Refactored filters initialization to handle null values.

  • Moved wrappedAtom initialization to a static method.


Changes walkthrough 📝

Relevant files
Enhancement
RelativeBy.cs
Refactor and seal `RelativeBy` class                                         

dotnet/src/webdriver/RelativeBy.cs

  • Sealed the RelativeBy class to prevent extension.
  • Removed the parameterless constructor.
  • Refactored filters initialization to handle null values.
  • Moved wrappedAtom initialization to a static helper method.
  • +6/-12   

    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

    qodo-merge-pro bot commented Mar 5, 2025

    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

    Empty List Initialization

    The new code initializes filters with an empty list literal [] which is a C# 12 feature. Ensure this is compatible with the project's target framework and compiler version.

    this.filters = filters is null ? [] : [.. filters]; // Clone filters
    Collection Expression

    The code uses collection expressions with [.. filters] which is a C# 12 feature. Verify this syntax is supported in the project's environment.

    this.filters = filters is null ? [] : [.. filters]; // Clone filters

    Copy link
    Contributor

    qodo-merge-pro bot commented Mar 5, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    General
    Add missing sealed modifier
    Suggestion Impact:The commit directly implemented the suggestion by adding the 'sealed' modifier to the RelativeBy class, which was appropriate since the implementation prevents inheritance.

    code diff:

    -    public class RelativeBy : By
    +    public sealed class RelativeBy : By

    The class should be explicitly marked as sealed since the implementation now
    prevents inheritance by removing the protected constructor and making all
    constructors private.

    dotnet/src/webdriver/RelativeBy.cs [32-36]

    -public class RelativeBy : By
    +public sealed class RelativeBy : By
     {
         private readonly string wrappedAtom;
         private readonly object root;
         private readonly List<object> filters;

    [Suggestion has been applied]

    Suggestion importance[1-10]: 7

    __

    Why: The suggestion correctly identifies that the class should be marked as sealed since the implementation prevents inheritance by removing the protected constructor and making all constructors private. This improves code clarity and prevents potential misuse.

    Medium
    Learned
    best practice
    Use explicit collection initialization with new List<object>() instead of collection expressions for better readability and consistency with .NET conventions
    Suggestion Impact:The commit addressed the same issue as the suggestion by replacing the collection expression syntax with explicit initialization. Instead of using 'new List()' as suggested, the implementation used field initialization with 'new List()' and then 'AddRange' for populating the list when filters are not null.

    code diff:

    -        private readonly List<object> filters;
    +        private readonly List<object> filters = new List<object>();
     
             private static string GetWrappedAtom()
             {
    @@ -53,7 +53,10 @@
             {
                 this.wrappedAtom = GetWrappedAtom();
                 this.root = GetSerializableRoot(root);
    -            this.filters = filters is null ? [] : [.. filters]; // Clone filters
    +            if (filters != null)
    +            {
    +                this.filters.AddRange(filters);
    +            }

    The current implementation uses collection expressions [] to initialize an empty
    list when filters is null. While this works, it's better to use new List() or
    the collection initializer syntax new() for consistency with .NET conventions
    and to make the code more explicit about the type being created.

    dotnet/src/webdriver/RelativeBy.cs [56]

    -this.filters = filters is null ? [] : [.. filters]; // Clone filters
    +this.filters = filters is null ? new List<object>() : new List<object>(filters); // Clone filters

    [Suggestion has been applied]

    Suggestion importance[1-10]: 6

    Low
    • Update

    @nvborisenko nvborisenko self-requested a review March 15, 2025 15:02
    @RenderMichael RenderMichael merged commit 86156cb into SeleniumHQ:trunk Mar 25, 2025
    10 checks passed
    @RenderMichael RenderMichael deleted the relative-by branch March 25, 2025 06: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.

    1 participant