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] Remove JSON serialization from .ToString() methods #14736

Merged
merged 4 commits into from
Nov 11, 2024

Conversation

RenderMichael
Copy link
Contributor

@RenderMichael RenderMichael commented Nov 9, 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 removes the ToString() method on DriverOptions, ReturnedCapabilities, and RemoteSessionSettings, to avoid heavy and AOT-unsafe JSON serialization.

Offshoot of #14734

Motivation and Context

There is an ongoing effort to make operations AOT-safe. Additionally, this ToString() method is heavy, which can be especially taxing when looking at the debugger display for the instance.

Fixes #14741

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


Description

  • Simplified the DriverOptions.ToString() method by removing JSON serialization and implementing a custom string representation using StringBuilder.
  • Added tests for the ToString() method across different browser options (Chrome, Edge, Firefox, Internet Explorer, Safari) to verify the output format.
  • Ensured the output includes browser name, version, and platform for better clarity and AOT safety.

Changes walkthrough 📝

Relevant files
Enhancement
DriverOptions.cs
Simplify `DriverOptions.ToString()` method for AOT safety

dotnet/src/webdriver/DriverOptions.cs

  • Removed JSON serialization from ToString() method.
  • Implemented custom string representation using StringBuilder.
  • Included browser name, version, and platform in the output.
  • +31/-2   
    Tests
    ChromeSpecificTests.cs
    Add test for ChromeOptions ToString method                             

    dotnet/test/chrome/ChromeSpecificTests.cs

  • Added test for ChromeOptions.ToString() method.
  • Verified output format for browser and platform.
  • +9/-0     
    EdgeSpecificTests.cs
    Add test for EdgeOptions ToString method                                 

    dotnet/test/edge/EdgeSpecificTests.cs

  • Added test for EdgeOptions.ToString() method.
  • Verified output format for browser and platform.
  • +16/-0   
    FirefoxDriverTest.cs
    Add test for FirefoxOptions ToString method                           

    dotnet/test/firefox/FirefoxDriverTest.cs

  • Added test for FirefoxOptions.ToString() method.
  • Verified output format for browser and platform.
  • +9/-0     
    IeSpecificTests.cs
    Add test for InternetExplorerOptions ToString method         

    dotnet/test/ie/IeSpecificTests.cs

  • Added test for InternetExplorerOptions.ToString() method.
  • Verified output format for browser and platform.
  • +8/-0     
    SafariSpecificTests.cs
    Add test for SafariOptions ToString method                             

    dotnet/test/safari/SafariSpecificTests.cs

  • Added test for SafariOptions.ToString() method.
  • Verified output format for browser and platform.
  • +17/-0   

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

    Copy link
    Contributor

    qodo-merge-pro bot commented Nov 9, 2024

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

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

    Null Check
    The ToString() method should handle the case when both browserName and platformName are null/empty. Currently it would return an empty string without any indication of what the object is.

    Code Duplication
    The string concatenation logic with commas and platform info is repeated across multiple places. Consider extracting this into a helper method.

    Copy link
    Contributor

    qodo-merge-pro bot commented Nov 9, 2024

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Maintainability
    ✅ Enable or remove commented test attribute to maintain clean test code
    Suggestion Impact:The commit removed the commented-out test attribute and the entire test method, aligning with the suggestion to clean up the code by removing unused code.

    code diff:

    -        //[Test]
    -        public void InternetExplorerOptionsToString()
    -        {
    -            var options = new InternetExplorerOptions();
    -
    -            Assert.That(options.ToString(), Is.EqualTo("Browser: internet explorer, Platform: windows"));
    -        }

    Remove commented-out test attribute to either enable the test or remove unused code

    dotnet/test/ie/IeSpecificTests.cs [363-364]

    -//[Test]
    +[Test]
     public void InternetExplorerOptionsToString()
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Commented-out test attributes can lead to confusion and maintenance issues. The test should either be properly enabled or removed to maintain code clarity.

    7
    Enhancement
    Use string interpolation for more concise and readable string formatting

    Consider using string interpolation for cleaner and more readable string formatting

    dotnet/src/webdriver/DriverOptions.cs [405-412]

    -builder.Append("Browser: ").Append(browserName);
    -builder.Append(' ');
    -builder.Append(browserVersion);
    +builder.Append($"Browser: {browserName} {browserVersion}");
    • Apply this suggestion
    Suggestion importance[1-10]: 5

    Why: String interpolation would make the code more readable and maintainable while reducing the number of StringBuilder operations, though the functional impact is minimal.

    5
    Performance
    Pre-allocate StringBuilder capacity to improve performance by avoiding buffer resizing

    Initialize StringBuilder with an estimated capacity based on expected string length
    to avoid buffer resizing

    dotnet/src/webdriver/DriverOptions.cs [399]

    -StringBuilder builder = new StringBuilder();
    +StringBuilder builder = new StringBuilder(100);
    • Apply this suggestion
    Suggestion importance[1-10]: 3

    Why: While pre-allocating capacity can improve performance by avoiding buffer resizing, the impact is minimal given the small size of the strings being built and infrequent usage of ToString().

    3
    Best practice
    Use null coalescing operator to ensure null safety when handling string values

    Add null coalescing operator to handle null values for browserName and platformName

    dotnet/src/webdriver/DriverOptions.cs [402-403]

    -string browserName = this.BrowserName;
    +string browserName = this.BrowserName ?? string.Empty;
     if (!string.IsNullOrEmpty(browserName))
    • Apply this suggestion
    Suggestion importance[1-10]: 2

    Why: The existing null check using string.IsNullOrEmpty is already sufficient and more comprehensive than using the null coalescing operator alone.

    2

    💡 Need additional feedback ? start a PR chat

    @nvborisenko
    Copy link
    Member

    nvborisenko commented Nov 10, 2024

    I like that this PR excludes json serialization from .ToString() method. We are cleaning up codebase.

    The next question: what we want to see as a string representation of any object? While I was walking through codebase, I don't see strong rule for objects. Yeah, json representation is still showing everything, but in general it is not ideal way to represent any object as a string.

    @RenderMichael what if I propose just to remove ToString() in this PR? I propose to search for all .ToString() methods where json is involved, and "deprecate/remove" it (as separate unit of work). Let's initiate a discussion about expected string representation of most used objects in selenium (not sure which way is comfortable, issue in GitHub?).

    @RenderMichael
    Copy link
    Contributor Author

    @nvborisenko I completely agree, the three types with JSON serialization in the ToString() method probably do not need those implementations at all. Since there are only 3, I don’t think we need a separate issue (I can change this PR) unless you think some people might disagree. Nobody should depend on the ToString() results, but maybe we want to he careful.

    Should I open a separate issue for discussion?

    @RenderMichael
    Copy link
    Contributor Author

    If we want to be careful and alert users, we can change the ToString to

    public string ToString() => $"{base.ToString()}, Use ToCapabilities() for a serializable representation.";

    @nvborisenko
    Copy link
    Member

    Only 3 types? Then I propose to remove ToString() in this PR, and think of about it in separate PR. Discussion will be happened there, not here. Thank you!

    @RenderMichael
    Copy link
    Contributor Author

    Opened #14741

    @nvborisenko
    Copy link
    Member

    Let's do it: Remove JSON serialization from all ToString() methods here, why not, I am happy to merge. And then think about how to improve it.

    @RenderMichael
    Copy link
    Contributor Author

    Done! PR is ready for review

    @nvborisenko
    Copy link
    Member

    From all ToString(), then we can consider #14741 as fixed.

    @RenderMichael
    Copy link
    Contributor Author

    All 3 types are ready. Such a satisfying change

    @nvborisenko
    Copy link
    Member

    Thank you, while you are adjusting PR's topic/description, I will validate that Appium project doesn't use .ToString() for json representation (I hope so).

    @RenderMichael
    Copy link
    Contributor Author

    Hopefully they are not calling ToString() on any ICapabilities instance, since that could be affected here.

    @RenderMichael RenderMichael changed the title [dotnet] Remove JSON serialization from DriverOptions.ToString() [dotnet] Remove JSON serialization from .ToString() methods Nov 10, 2024
    @nvborisenko
    Copy link
    Member

    nvborisenko commented Nov 10, 2024

    You can help me, they are more friendly for .net: https://github.com/appium/dotnet-client (at least at compilation level)

    @RenderMichael
    Copy link
    Contributor Author

    Luckily they do not. This is the only instance of handling ICapabilities on their side.

    image

    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 1c23e13 into SeleniumHQ:trunk Nov 11, 2024
    10 checks passed
    @RenderMichael RenderMichael deleted the driver-options-tostring branch November 11, 2024 00:09
    jkim2492 pushed a commit to jkim2492/selenium that referenced this pull request Nov 17, 2024
    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.

    [dotnet] Avoid serializing to JSON in ToString() methods.
    2 participants