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 print types #14773

Merged
merged 10 commits into from
Nov 25, 2024

Conversation

RenderMichael
Copy link
Contributor

@RenderMichael RenderMichael commented Nov 18, 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

This adds nullability annotations to the PDF-print stack, as well as documenting potential exceptions alongside some code modernization.

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, Tests


Description

  • Enabled nullability annotations across multiple classes to improve code safety and clarity.
  • Added detailed exception documentation to constructors and methods to specify potential errors.
  • Refactored properties to use auto-properties for cleaner code.
  • Added validation checks and exception handling for invalid input values in PrintOptions.
  • Enhanced test coverage by adding tests for invalid page numbers, ranges, and negative values.

Changes walkthrough 📝

Relevant files
Enhancement
EncodedFile.cs
Add nullability and refactor EncodedFile class                     

dotnet/src/webdriver/EncodedFile.cs

  • Enabled nullability annotations.
  • Added exception documentation for constructor.
  • Refactored properties to use auto-properties.
  • Simplified ToString method.
  • +14/-17 
    ISupportsPrint.cs
    Add nullability and exception documentation to ISupportsPrint

    dotnet/src/webdriver/ISupportsPrint.cs

  • Enabled nullability annotations.
  • Added exception documentation for Print method.
  • +7/-2     
    PrintDocument.cs
    Enhance PrintDocument with nullability and exceptions       

    dotnet/src/webdriver/PrintDocument.cs

  • Enabled nullability annotations.
  • Added exception documentation for constructor and SaveAsFile method.
  • +17/-0   
    PrintOptions.cs
    Improve PrintOptions with nullability and validation         

    dotnet/src/webdriver/PrintOptions.cs

  • Enabled nullability annotations.
  • Added exception handling for invalid values.
  • Refactored properties and methods for clarity.
  • +89/-79 
    Screenshot.cs
    Add nullability and exceptions to Screenshot class             

    dotnet/src/webdriver/Screenshot.cs

  • Enabled nullability annotations.
  • Added exception documentation for constructor.
  • +9/-0     
    Bug fix
    WebDriver.cs
    Add null check in WebDriver Print method                                 

    dotnet/src/webdriver/WebDriver.cs

    • Added null check for printOptions in Print method.
    +6/-0     
    Tests
    PrintTest.cs
    Add validation tests for PrintOptions                                       

    dotnet/test/common/PrintTest.cs

  • Added tests for invalid page numbers and ranges.
  • Added tests for negative page sizes and margins.
  • +34/-0   

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

    Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Validation Gap
    The AddPageRangeToPrint method validates the start page is >= 1 but allows end page to be int.MaxValue without validation. Consider adding an upper bound validation.

    Possible Bug
    The ToDictionary method returns null values in the dictionary which could cause issues for consumers. Consider filtering out null values or documenting this behavior.

    Error Handling
    The SaveAsFile method checks for null/empty filename but doesn't validate file extension or path validity before attempting to write.

    Copy link
    Contributor

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    General
    Specify non-nullable dictionary value type to better reflect actual usage

    The ToDictionary() method should specify a non-nullable return type since all
    dictionary values are non-null. Change the return type from Dictionary<string,
    object?> to Dictionary<string, object>.

    dotnet/src/webdriver/PrintOptions.cs [187]

    -internal Dictionary<string, object?> ToDictionary()
    +internal Dictionary<string, object> ToDictionary()
    • Apply this suggestion
    Suggestion importance[1-10]: 5

    Why: The suggestion improves type safety by making the dictionary value type non-nullable since the code never adds null values. This enhances code clarity and prevents potential null-related issues.

    5
    Initialize collection fields in constructor to ensure proper object initialization

    The pageRanges field should be initialized in the constructor rather than at
    declaration to ensure proper initialization in all cases.

    dotnet/src/webdriver/PrintOptions.cs [57]

    -private readonly HashSet<object> pageRanges = new HashSet<object>();
    +private readonly HashSet<object> pageRanges;
     
    +public PrintOptions()
    +{
    +    pageRanges = new HashSet<object>();
    +}
    +
    • Apply this suggestion
    Suggestion importance[1-10]: 3

    Why: While the suggestion follows a good practice of initializing collections in constructors, the current field initialization is already safe and works correctly. The change would be mostly stylistic with minimal impact.

    3

    💡 Need additional feedback ? start a PR chat

    @nvborisenko
    Copy link
    Member

    Thanks @RenderMichael, safe to merge (if CI is green), after release process of 4.27 is finished.

    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.

    Please fix one comment.

    @RenderMichael
    Copy link
    Contributor Author

    Thanks!

    I saw the recent 4.27 release; it was nice to see the .NET release notes section longer than previous releases.

    @nvborisenko
    Copy link
    Member

    The changes are not related to failed CI tests (ShadowRoot). Merging it.

    @nvborisenko nvborisenko merged commit b501589 into SeleniumHQ:trunk Nov 25, 2024
    9 of 10 checks passed
    @RenderMichael RenderMichael deleted the print branch November 25, 2024 19:31
    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