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] Simplify and modernize DevToolsDomains.InitializeDomains #15198

Merged
merged 5 commits into from
Jan 31, 2025

Conversation

RenderMichael
Copy link
Contributor

@RenderMichael RenderMichael commented Jan 31, 2025

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.

This removes reflection paths from DevToolsDomains and annotates it for nullability.

Motivation and Context

Contributes to #14640 and #14480

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.

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

The CreateDevToolsDomain method can return null but is used without null check in line 128. While the code will likely work due to the logic flow, explicit null checking would improve robustness.

private static DevToolsDomains? CreateDevToolsDomain(int protocolVersion, DevToolsSession session) => protocolVersion switch
{
    130 => new V130.V130Domains(session),
    132 => new V132.V132Domains(session),
    131 => new V131.V131Domains(session),
    85 => new V85.V85Domains(session),
    _ => null
};
Version Ordering

The SupportedProtocolVersions array defines versions in a seemingly random order (130,132,131,85). Consider ordering them consistently to improve maintainability.

private static int[] SupportedProtocolVersions =>
[
    130,
    132,
    131,
    85
];

Copy link
Contributor

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Score
Possible issue
Add parameter validation

Add null check for the 'session' parameter in InitializeDomains methods to prevent
potential NullReferenceException when creating domain instances.

dotnet/src/webdriver/DevTools/DevToolsDomains.cs [89-92]

 public static DevToolsDomains InitializeDomains(int protocolVersion, DevToolsSession session)
 {
+    ArgumentNullException.ThrowIfNull(session);
     return InitializeDomains(protocolVersion, session, DefaultVersionRange);
 }
  • Apply this suggestion
Suggestion importance[1-10]: 8

Why: Adding null validation for the session parameter is crucial for preventing NullReferenceException and providing clear error messages. This is particularly important as the session parameter is used throughout the domain initialization process.

8
Validate protocol version parameter

Add validation for negative protocol version to prevent potential issues with
version matching logic.

dotnet/src/webdriver/DevTools/DevToolsDomains.cs [103-108]

 public static DevToolsDomains InitializeDomains(int protocolVersion, DevToolsSession session, int versionRange)
 {
+    if (protocolVersion < 0)
+    {
+        throw new ArgumentOutOfRangeException(nameof(protocolVersion), "Protocol version must not be negative");
+    }
     if (versionRange < 0)
     {
         throw new ArgumentOutOfRangeException(nameof(versionRange), "Version range must not be negative");
     }
  • Apply this suggestion
Suggestion importance[1-10]: 7

Why: Adding validation for negative protocol versions is important for preventing invalid inputs early, especially since the version matching logic assumes positive version numbers. This complements the existing versionRange validation.

7

Copy link
Member

@nvborisenko nvborisenko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Michael!

@nvborisenko nvborisenko merged commit 5bad332 into SeleniumHQ:trunk Jan 31, 2025
10 checks passed
@RenderMichael RenderMichael deleted the devtools_domains branch January 31, 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