Skip to content

[dotnet] Add back in a public parameterless constructor to HttpRequestData #15258

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

Merged
merged 7 commits into from
Feb 9, 2025

Conversation

RenderMichael
Copy link
Contributor

@RenderMichael RenderMichael commented Feb 9, 2025

User description

…stData`

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.

Motivation and Context

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

Bug fix


Description

  • Added a public parameterless constructor to HttpRequestData.

  • Updated RequestId property to allow null values.


Changes walkthrough 📝

Relevant files
Bug fix
HttpRequestData.cs
Add parameterless constructor and nullable `RequestId`     

dotnet/src/webdriver/HttpRequestData.cs

  • Added a public parameterless constructor initializing default values.
  • Changed RequestId property to nullable type.
  • +11/-1   

    Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • Copy link
    Contributor

    qodo-merge-pro bot commented Feb 9, 2025

    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 Reference

    The parameterless constructor initializes required properties with null values which could lead to NullReferenceException when these properties are accessed. Consider initializing with empty values instead.

    this.Method = null!;
    this.Url = null!;
    this.Headers = null!;

    @RenderMichael RenderMichael changed the title [dotnet] Add back in a public parameterless constructor to `HttpReque… [dotnet] Add back in a public parameterless constructor to HttpRequestData Feb 9, 2025
    Copy link
    Contributor

    qodo-merge-pro bot commented Feb 9, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    Initialize collection to prevent nulls
    Suggestion Impact:The commit implements the suggestion by initializing Headers with a new Dictionary instance, but does it at the property level instead of in the constructor

    code diff:

    -        public Dictionary<string, string> Headers { get; set; }
    +        public Dictionary<string, string> Headers { get; set; } = new Dictionary<string, string>();

    Initialize the Headers dictionary in the parameterless constructor to prevent
    NullReferenceException when adding headers.

    dotnet/src/webdriver/HttpRequestData.cs [34-39]

     public HttpRequestData()
     {
         this.Method = null!;
         this.Url = null!;
    -    this.Headers = null!;
    +    this.Headers = new Dictionary<string, string>();
     }
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    __

    Why: Initializing Headers with null! could lead to NullReferenceException at runtime. Using a new Dictionary instance is a safer approach that prevents potential crashes.

    Medium
    General
    Use meaningful defaults over nulls
    Suggestion Impact:The commit addresses the same issue but takes a different approach by making properties nullable and initializing Headers with default value

    code diff:

    -        public string Method { get; set; }
    +        public string? Method { get; set; }
     
             /// <summary>
             /// Gets the URL of the HTTP request.
             /// </summary>
    -        public string Url { get; set; }
    +        public string? Url { get; set; }
     
             /// <summary>
             /// Gets the POST data of the HTTP request, if any.
    @@ -73,7 +70,7 @@
             /// <summary>
             /// Gets the headers of the HTTP request.
             /// </summary>
    -        public Dictionary<string, string> Headers { get; set; }
    +        public Dictionary<string, string> Headers { get; set; } = new Dictionary<string, string>();

    Avoid using null-forgiving operator (!) for required properties. Instead,
    initialize with meaningful default values.

    dotnet/src/webdriver/HttpRequestData.cs [34-39]

     public HttpRequestData()
     {
    -    this.Method = null!;
    -    this.Url = null!;
    -    this.Headers = null!;
    +    this.Method = "GET";
    +    this.Url = string.Empty;
    +    this.Headers = new Dictionary<string, string>();
     }
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    __

    Why: Using meaningful default values instead of null-forgiving operators improves code safety and clarity. The suggestion provides sensible defaults that better represent the initial state of an HTTP request.

    Medium

    @RenderMichael RenderMichael merged commit d9093cc into SeleniumHQ:trunk Feb 9, 2025
    9 of 10 checks passed
    @RenderMichael RenderMichael deleted the http-request-data branch February 9, 2025 21:58
    sandeepsuryaprasad pushed a commit to sandeepsuryaprasad/selenium that referenced this pull request Mar 23, 2025
    …stData` (SeleniumHQ#15258)
    
    * [dotnet] Add back in a public parameterless constructor to `HttpRequestData`
    
    * Improve nullability
    
    * Make RequestId only settable internally
    
    * Revert "Make RequestId only settable internally"
    
    This reverts commit d0afea9.
    
    * Handle nullability, make new constructor internal
    
    * Apply suggestions from code review
    
    Simplify null check
    
    Co-authored-by: Nikolay Borisenko <[email protected]>
    
    * Update dotnet/src/webdriver/HttpRequestData.cs
    
    ---------
    
    Co-authored-by: Nikolay Borisenko <[email protected]>
    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