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] Modernize Response type #14839

Merged
merged 11 commits into from
Dec 8, 2024
Merged

Conversation

RenderMichael
Copy link
Contributor

@RenderMichael RenderMichael commented Dec 1, 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

Modernizes the Response class, in preparation for making it null-safe. I separated this out into a separate PR because the diffs were becoming unwieldy. The changes in this PR may be uncontroversial, and should be considered separate from any other changes to it.

Motivation and Context

This simplifies and shrinks the code.

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

  • Modernized the Response class by removing redundant fields and using auto-properties, which simplifies the code and reduces its size.
  • Improved null handling in JSON deserialization methods to prevent potential null reference exceptions.
  • Simplified the constructor logic for setting properties, enhancing code readability and maintainability.
  • Updated error handling in the FromErrorJson method to use the new Response constructor, ensuring consistent error response creation.
  • Simplified error handling in the ExecuteAsync method of WebDriver by utilizing the new Response constructor for error responses.

Changes walkthrough 📝

Relevant files
Enhancement
Response.cs
Modernize and simplify the `Response` class in WebDriver 

dotnet/src/webdriver/Response.cs

  • Modernized the Response class by removing redundant fields and using
    auto-properties.
  • Improved null handling in JSON deserialization methods.
  • Simplified constructor logic for setting properties.
  • Updated error handling in FromErrorJson method.
  • +32/-50 
    WebDriver.cs
    Simplify error handling in WebDriver's ExecuteAsync method

    dotnet/src/webdriver/WebDriver.cs

  • Simplified error handling in ExecuteAsync method.
  • Utilized new Response constructor for error responses.
  • +1/-5     

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

    Copy link
    Contributor

    qodo-merge-pro bot commented Dec 1, 2024

    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 Handling
    The new null handling in FromJson and FromErrorJson methods throws exceptions for null responses. Verify this is the intended behavior and won't break existing code.

    Property Access
    The change from private fields to auto-properties could potentially impact serialization behavior. Verify serialization/deserialization still works as expected.

    Copy link
    Contributor

    qodo-merge-pro bot commented Dec 1, 2024

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Add parameter validation to prevent null reference exceptions

    Add null check for rawResponse parameter in the private constructor to prevent
    potential NullReferenceException when accessing dictionary keys.

    dotnet/src/webdriver/Response.cs [65-72]

     private Response(Dictionary<string, object> rawResponse)
     {
    +    if (rawResponse == null)
    +        throw new ArgumentNullException(nameof(rawResponse));
    +        
         if (rawResponse.ContainsKey("sessionId"))
         {
             if (rawResponse["sessionId"] != null)
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Adding null validation for the rawResponse parameter is important for preventing NullReferenceException and providing clear error messages. This is a critical defensive programming practice for a core constructor.

    8
    Security
    Prevent runtime modification of shared serialization settings

    Make the s_jsonSerializerOptions field read-only to prevent accidental modification
    of serialization settings at runtime.

    dotnet/src/webdriver/Response.cs [34-38]

    -private static readonly JsonSerializerOptions s_jsonSerializerOptions = new()
    +private static readonly JsonSerializerOptions s_jsonSerializerOptions = new(JsonSerializerOptions.Default)
     {
         TypeInfoResolver = ResponseJsonSerializerContext.Default,
         Converters = { new ResponseValueJsonConverter() }
    -};
    +}.MakeReadOnly();
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Making the JsonSerializerOptions immutable through MakeReadOnly() prevents accidental modifications to shared serialization settings that could affect the entire application's behavior. This is an important security and reliability improvement.

    7

    💡 Need additional feedback ? start a PR chat

    @nvborisenko nvborisenko self-requested a review December 6, 2024 23:37
    @nvborisenko
    Copy link
    Member

    Thank you @RenderMichael !

    @nvborisenko nvborisenko merged commit abb8440 into SeleniumHQ:trunk Dec 8, 2024
    9 of 10 checks passed
    @RenderMichael RenderMichael deleted the response branch December 9, 2024 01: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