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 on ChromiumOptions #15173

Merged
merged 1 commit into from
Jan 28, 2025

Conversation

RenderMichael
Copy link
Contributor

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

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


Description

  • Annotated nullability for properties and methods in ChromiumOptions.

  • Replaced mutable fields with readonly where applicable.

  • Added exception handling for invalid arguments in public methods.

  • Refactored helper methods to use static and nullable parameters.


Changes walkthrough 📝

Relevant files
Enhancement
ChromiumOptions.cs
Added nullability annotations and refactored `ChromiumOptions`.

dotnet/src/webdriver/Chromium/ChromiumOptions.cs

  • Enabled nullable reference types with #nullable enable.
  • Annotated properties and methods with nullable types.
  • Replaced mutable fields with readonly for immutability.
  • Added exception handling for invalid arguments in public methods.
  • Refactored helper methods to use static and nullable parameters.
  • +93/-102

    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: 3 🔵🔵🔵⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Possible Issue

    The AddArguments() and AddEncodedExtensions() methods still create a new List unnecessarily when calling the overloaded method

    this.AddEncodedExtensions((IEnumerable<string>)extensions);
    Validation

    The EnableMobileEmulation method accepts null deviceSettings but does not validate other properties like Width, Height, PixelRatio for valid values

    public void EnableMobileEmulation(ChromiumMobileEmulationDeviceSettings? deviceSettings)
    {

    Copy link
    Contributor

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Validate individual collection elements

    The AddArguments method should validate each individual argument in the collection
    for null/empty values, not just the collection itself, to prevent invalid arguments
    from being added.

    dotnet/src/webdriver/Chromium/ChromiumOptions.cs [182-190]

     public void AddArguments(IEnumerable<string> argumentsToAdd)
     {
         if (argumentsToAdd == null)
         {
             throw new ArgumentNullException(nameof(argumentsToAdd), "Arguments to add cannot be null");
         }
     
    +    foreach (string arg in argumentsToAdd)
    +    {
    +        if (string.IsNullOrEmpty(arg))
    +        {
    +            throw new ArgumentException("Individual arguments cannot be null or empty");
    +        }
    +    }
         this.arguments.AddRange(argumentsToAdd);
     }
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: The suggestion adds important input validation to prevent invalid arguments from being added to the collection, which could cause issues later when the arguments are used. This improves robustness and error handling.

    7
    Add null check before access

    The EnableMobileEmulation method should validate that the device settings object is
    not null before accessing its properties to avoid potential null reference
    exceptions.

    dotnet/src/webdriver/Chromium/ChromiumOptions.cs [407-413]

     public void EnableMobileEmulation(ChromiumMobileEmulationDeviceSettings? deviceSettings)
     {
         this.mobileEmulationDeviceName = null;
    -    if (deviceSettings != null && string.IsNullOrEmpty(deviceSettings.UserAgent))
    +    if (deviceSettings == null)
    +    {
    +        this.mobileEmulationDeviceSettings = null;
    +        return;
    +    }
    +    
    +    if (string.IsNullOrEmpty(deviceSettings.UserAgent))
         {
             throw new ArgumentException("Device settings must include user agent string", nameof(deviceSettings));
         }
     
         this.mobileEmulationDeviceSettings = deviceSettings;
     }
    • Apply this suggestion
    Suggestion importance[1-10]: 5

    Why: The suggestion improves null handling by separating the null check from the UserAgent validation, making the code more maintainable and clearer in its intent. However, the original code already handled nulls safely.

    5

    @RenderMichael
    Copy link
    Contributor Author

    I am not adding null checks to:

    • Each string element of AddArguments
    • Each string element of AddExcludedArgument
    • Each string element of AddExtensions
    • Each string element of AddEncodedExtensions
    • Value preferenceValue parameter of AddUserProfilePreference
    • Value preferenceValue parameter of AddLocalStatePreference
    • Each string element of AddWindowTypes
    • Value optionValue parameter of AddAdditionalChromiumOption

    This is to avoid breaking users. There is no downstream issue on the C# side of passing in null (it will just get serialized normally). If there are exceptions on the backend, let users see it. We will add a null warning, but not throw an exception on our side here.

    @RenderMichael RenderMichael merged commit c882e14 into SeleniumHQ:trunk Jan 28, 2025
    10 checks passed
    @RenderMichael RenderMichael deleted the chromium-options branch January 28, 2025 19:02
    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