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 JavaScript protocol #15238

Merged
merged 1 commit into from
Feb 6, 2025

Conversation

RenderMichael
Copy link
Contributor

@RenderMichael RenderMichael commented Feb 5, 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.

Annotate nullability on JavaScript protocol

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

  • Enabled nullable reference types in multiple JavaScript protocol classes.

  • Added nullability annotations to event handlers and method parameters.

  • Introduced ArgumentNullException checks for constructor parameters.

  • Improved type safety for collections and nullable values.


Changes walkthrough 📝

Relevant files
Enhancement
JavaScript.cs
Annotated nullability in JavaScript base class                     

dotnet/src/webdriver/DevTools/JavaScript.cs

  • Enabled nullable reference types.
  • Annotated event handlers with nullable types.
  • +5/-3     
    V130JavaScript.cs
    Annotated nullability in V130 JavaScript implementation   

    dotnet/src/webdriver/DevTools/v130/V130JavaScript.cs

  • Enabled nullable reference types.
  • Added ArgumentNullException for constructor parameters.
  • Updated event handlers and collections with nullable annotations.
  • +12/-9   
    V131JavaScript.cs
    Annotated nullability in V131 JavaScript implementation   

    dotnet/src/webdriver/DevTools/v131/V131JavaScript.cs

  • Enabled nullable reference types.
  • Added ArgumentNullException for constructor parameters.
  • Updated event handlers and collections with nullable annotations.
  • +12/-9   
    V132JavaScript.cs
    Annotated nullability in V132 JavaScript implementation   

    dotnet/src/webdriver/DevTools/v132/V132JavaScript.cs

  • Enabled nullable reference types.
  • Added ArgumentNullException for constructor parameters.
  • Updated event handlers and collections with nullable annotations.
  • +12/-9   
    V85JavaScript.cs
    Annotated nullability in V85 JavaScript implementation     

    dotnet/src/webdriver/DevTools/v85/V85JavaScript.cs

  • Enabled nullable reference types.
  • Added ArgumentNullException for constructor parameters.
  • Updated event handlers and collections with nullable annotations.
  • +12/-9   

    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 Feb 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

    Event Handler Safety

    The nullable event handlers could lead to null reference exceptions if not properly checked before invocation. Consider adding null checks when raising events.

    public event EventHandler<BindingCalledEventArgs>? BindingCalled;
    
    /// <summary>
    /// Occurs when the browser's JavaScript console API is called.
    /// </summary>
    public event EventHandler<ConsoleApiCalledEventArgs>? ConsoleApiCalled;
    
    /// <summary>
    /// Occurs when a JavaScript exception is thrown.
    /// </summary>
    public event EventHandler<ExceptionThrownEventArgs>? ExceptionThrown;

    Copy link
    Contributor

    qodo-merge-pro bot commented Feb 5, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    Add null check for event args

    Initialize the args list before the foreach loop to avoid potential null
    reference exceptions. Move the list declaration outside the loop scope.

    dotnet/src/webdriver/DevTools/v130/V130JavaScript.cs [168-175]

     private void OnRuntimeConsoleApiCalled(object? sender, ConsoleAPICalledEventArgs e)
     {
    +    if (e?.Args == null)
    +        return;
    +        
         List<ConsoleApiArgument> args = new List<ConsoleApiArgument>(e.Args.Length);
         foreach (var arg in e.Args)
         {
             string? argValue = arg.Value?.ToString();
             args.Add(new ConsoleApiArgument(arg.Type.ToString(), argValue));
         }
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    __

    Why: The suggestion adds important null checks for the event args and its Args property, preventing potential NullReferenceException in the event handler. This is a critical defensive programming practice when dealing with event handlers.

    Medium

    @RenderMichael
    Copy link
    Contributor Author

    Failures are unrelated to this PR

    //java/test/org/openqa/selenium/mobile:NetworkConnectionTest
    //java/test/org/openqa/selenium/remote:RemoteWebDriverScreenshotTest
    //java/test/org/openqa/selenium/remote:RemoteWebDriverScreenshotTest-firefox-beta
    //rb/spec/integration/selenium/webdriver:network-firefox-beta-bidi
    //rb/spec/integration/selenium/webdriver:network-firefox-bidi

    @RenderMichael RenderMichael merged commit 572b087 into SeleniumHQ:trunk Feb 6, 2025
    9 of 10 checks passed
    @RenderMichael RenderMichael deleted the nullable-javascript branch February 6, 2025 05:18
    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