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 devtools event args #15134

Merged
merged 1 commit into from
Jan 23, 2025

Conversation

RenderMichael
Copy link
Contributor

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

Annotates nullability on devtools event args types, and adds explicit constructors that ensures non-nullable reference types are initialized.

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

  • Annotated nullability for DevTools event arguments and related classes.

  • Added explicit constructors to ensure proper initialization of non-nullable fields.

  • Improved type safety with null checks and exception handling for invalid arguments.

  • Refactored event handling logic for better readability and maintainability.


Changes walkthrough 📝

Relevant files
Enhancement
20 files
AuthRequiredEventArgs.cs
Added nullability annotations and explicit constructor.   
+15/-2   
BindingCalledEventArgs.cs
Added nullability annotations and explicit constructor.   
+18/-3   
CommandResponseExtensions.cs
Updated method to return nullable type.                                   
+4/-2     
CommandResponseTypeMap.cs
Added null checks and annotations for type safety.             
+16/-2   
ConsoleApiArgument.cs
Added nullability annotations and explicit constructor.   
+18/-2   
ConsoleApiCalledEventArgs.cs
Added nullability annotations and explicit constructor.   
+19/-3   
DevToolsCommandData.cs
Added null checks and annotations for constructor arguments.
+8/-3     
DevToolsEventData.cs
Added null checks and annotations for constructor arguments.
+5/-2     
EntryAddedEventArgs.cs
Added nullability annotations and explicit constructor.   
+13/-1   
LogEntry.cs
Added nullability annotations and explicit constructor.   
+15/-2   
RequestPausedEventArgs.cs
Added nullability annotations and explicit constructor.   
+15/-2   
ResponsePausedEventArgs.cs
Added nullability annotations and explicit constructor.   
+11/-1   
TargetAttachedEventArgs.cs
Added nullability annotations and explicit constructor.   
+21/-4   
TargetDetachedEventArgs.cs
Added nullability annotations and explicit constructor.   
+15/-2   
WebSocketConnectionDataReceivedEventArgs.cs
Added nullability annotations and explicit constructor.   
+5/-4     
V130JavaScript.cs
Refactored event handling for better readability.               
+14/-18 
V130Log.cs
Refactored log entry handling with improved initialization.
+5/-4     
V130Network.cs
Refactored network event handling with improved initialization.
+9/-9     
V130Target.cs
Refactored target event handling with improved initialization.
+10/-8   
V131JavaScript.cs
Refactored event handling for better readability.               
+14/-18 
Additional files
11 files
V131Log.cs +5/-4     
V131Network.cs +9/-9     
V131Target.cs +10/-8   
V132JavaScript.cs +14/-18 
V132Log.cs +5/-4     
V132Network.cs +9/-9     
V132Target.cs +10/-8   
V85JavaScript.cs +14/-18 
V85Log.cs +5/-4     
V85Network.cs +9/-9     
V85Target.cs +10/-8   

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

    Null Safety

    The TryGetCommandResponseType methods return null for commandResponseType but the XML docs don't indicate this possibility. Consider documenting the null return case.

        /// <summary>
        /// Gets the command response type corresponding to the specified command type.
        /// </summary>
        /// <typeparam name="T">The type of command for which to retrieve the response type.</typeparam>
        /// <param name="commandResponseType">The returned response type.</param>
        /// <returns><see langword="true"/> if the specified command type has a mapped response type; otherwise, <see langword="false"/>.</returns>
        public bool TryGetCommandResponseType<T>([NotNullWhen(true)] out Type? commandResponseType)
            where T : ICommand
        {
            return commandResponseTypeDictionary.TryGetValue(typeof(T), out commandResponseType);
        }
    
        /// <summary>
        /// Gets the command response type corresponding to the specified command type.
        /// </summary>
        /// <param name="command">The type of command for which to retrieve the response type.</param>
        /// <param name="commandResponseType">The returned response type.</param>
        /// <returns><see langword="true"/> if the specified command type has a mapped response type; otherwise, <see langword="false"/>.</returns>
        public bool TryGetCommandResponseType(ICommand command, [NotNullWhen(true)] out Type? commandResponseType)
        {
            return commandResponseTypeDictionary.TryGetValue(command.GetType(), out commandResponseType);
        }
    }
    Constructor Validation

    The constructor allows null value but doesn't validate type parameter. Consider adding validation or documenting that type must not be null.

    public ConsoleApiArgument(string type, string? value)
    {
        Type = type ?? throw new ArgumentNullException(nameof(type));
        Value = value;
    }

    Copy link
    Contributor

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Add null check for requestData

    The OnFetchRequestPaused method should check if requestData is not null before using
    it in the constructor to prevent potential null reference exceptions.

    dotnet/src/webdriver/DevTools/v130/V130Network.cs [305]

    -RequestPausedEventArgs wrapped = new RequestPausedEventArgs(null, requestData);
    +if (requestData != null) {
    +    RequestPausedEventArgs wrapped = new RequestPausedEventArgs(null, requestData);
    +    this.OnRequestPaused(wrapped);
    +}
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: The suggestion addresses a potential null reference exception by adding a null check before using requestData in the constructor, which is a valid defensive programming practice.

    7
    Add null check for responseData

    The OnFetchRequestPaused method should check if responseData is not null before
    using it in the constructor to prevent potential null reference exceptions.

    dotnet/src/webdriver/DevTools/v130/V130Network.cs [316]

    -ResponsePausedEventArgs wrappedResponse = new ResponsePausedEventArgs(responseData);
    +if (responseData != null) {
    +    ResponsePausedEventArgs wrappedResponse = new ResponsePausedEventArgs(responseData);
    +    if (e.ResponseStatusCode.HasValue) {
    +}
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: The suggestion adds important null validation before using responseData in the constructor, preventing potential runtime exceptions and improving code robustness.

    7

    @nvborisenko
    Copy link
    Member

    DevTools will be deprecated, I am afraid it is risky to make any "minor" changes in this area. Except you are sure these minor changes are absolutely safe.

    @RenderMichael
    Copy link
    Contributor Author

    RenderMichael commented Jan 22, 2025

    These changes are completely safe. The event args are never serialized or expected to be initialized by users. They just have an event associated with each one, which users can subscribe to. I avoided null checks from properties which are returned by cdp, just in case.

    99% of the code changes just add an explicit constructor.

    @RenderMichael RenderMichael merged commit 0983522 into SeleniumHQ:trunk Jan 23, 2025
    10 checks passed
    @RenderMichael RenderMichael deleted the devtools-nullability branch January 23, 2025 15:35
    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