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 annotations to ShadowRoot #14812

Merged
merged 1 commit into from
Nov 27, 2024

Conversation

RenderMichael
Copy link
Contributor

@RenderMichael RenderMichael commented Nov 26, 2024

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

Adds nullable reference type annotations to ShadowRoot and dependent types.

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


Description

  • Enabled nullable reference types in several interfaces and classes, improving code safety and clarity.
  • Added null checks and exceptions in the ShadowRoot constructor to prevent null reference issues.
  • Refactored ShadowRoot methods to use pattern matching and nullable annotations, enhancing code readability and maintainability.
  • Updated WebDriver to use the new ShadowRoot.TryCreate method, streamlining shadow root creation logic.

Changes walkthrough 📝

Relevant files
Enhancement
ISearchContext.cs
Enable nullable reference types in ISearchContext               

dotnet/src/webdriver/ISearchContext.cs

  • Enabled nullable reference types.
+2/-0     
IWrapsDriver.cs
Enable nullable reference types in IWrapsDriver                   

dotnet/src/webdriver/IWrapsDriver.cs

  • Enabled nullable reference types.
+2/-0     
IWebDriverObjectReference.cs
Enable nullable reference types in IWebDriverObjectReference

dotnet/src/webdriver/Internal/IWebDriverObjectReference.cs

  • Enabled nullable reference types.
+2/-0     
ShadowRoot.cs
Add nullability annotations and refactor ShadowRoot class

dotnet/src/webdriver/ShadowRoot.cs

  • Enabled nullable reference types.
  • Added null checks and exceptions for constructor parameters.
  • Refactored methods to use pattern matching and nullable annotations.
  • +31/-26 
    WebDriver.cs
    Refactor WebDriver to use ShadowRoot.TryCreate                     

    dotnet/src/webdriver/WebDriver.cs

    • Refactored to use the new TryCreate method for ShadowRoot.
    +2/-2     

    💡 PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    Sorry, something went wrong.

    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 Safety
    Verify that the null check in TryCreate method for shadowRootValue?.ToString()! is safe, as the null-forgiving operator (!) is used

    API Design
    Consider if TryCreate should validate that the shadowRootValue is not null before creating a new ShadowRoot instance

    Copy link
    Contributor

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Add proper null and type validation for shadow root ID to prevent runtime errors

    The shadowRootValue?.ToString()! expression assumes the value is non-null and can be
    converted to string. Add explicit null check and type validation to avoid potential
    runtime errors.

    dotnet/src/webdriver/ShadowRoot.cs [74]

    -shadowRoot = new ShadowRoot(parentDriver, shadowRootValue?.ToString()!);
    +if (shadowRootValue is string shadowRootId)
    +{
    +    shadowRoot = new ShadowRoot(parentDriver, shadowRootId);
    +}
    +else
    +{
    +    throw new ArgumentException("Shadow root value must be a non-null string");
    +}
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: The suggestion addresses a critical safety issue by adding proper type checking and null validation for the shadow root ID, which could prevent runtime errors and improve error handling in a core functionality.

    8
    General
    Ensure dictionary values are non-nullable to prevent potential null reference exceptions

    The Dictionary<string, object> parameters should be marked as non-nullable since
    null values are not handled and would cause runtime errors.

    dotnet/src/webdriver/ShadowRoot.cs [96-99]

    -Dictionary<string, object> parameters = new Dictionary<string, object>();
    -parameters.Add("id", this.shadowRootId);
    -parameters.Add("using", by.Mechanism);
    -parameters.Add("value", by.Criteria);
    +Dictionary<string, object> parameters = new()
    +{
    +    ["id"] = this.shadowRootId,
    +    ["using"] = by.Mechanism,
    +    ["value"] = by.Criteria
    +};
    • Apply this suggestion
    Suggestion importance[1-10]: 4

    Why: While the suggestion offers a more concise and modern syntax using collection initialization, the actual impact on preventing null references is minimal since the existing code already uses non-null values.

    4

    💡 Need additional feedback ? start a PR chat

    Sorry, something went wrong.

    @nvborisenko nvborisenko merged commit 4fe2a56 into SeleniumHQ:trunk Nov 27, 2024
    9 of 10 checks passed
    @RenderMichael RenderMichael deleted the shadow-root-null branch November 27, 2024 19:06
    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.

    None yet

    2 participants