Skip to content

[dotnet] Fully annotate Command for AOT support #15527

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

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from

Conversation

RenderMichael
Copy link
Contributor

@RenderMichael RenderMichael commented Mar 28, 2025

User description

AOT warnings are user-facing, even if those warnings are within Selenium's own code. For that reason, we should ensure we are not emitting any warnings unless such code paths are annotated as AOT-unsafe.

Motivation and Context

Contributes to #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.

PR Type

Enhancement


Description

  • Added AOT-friendly annotations to the Command class.

  • Refactored JsonSerializerOptions into a static nested class for safety.

  • Introduced helper methods for parameter handling in Command.

  • Updated HttpCommandInfo to use new parameter handling methods.


Changes walkthrough 📝

Relevant files
Enhancement
Command.cs
Refactor `Command` class for AOT compatibility                     

dotnet/src/webdriver/Command.cs

  • Added AOT and trimming annotations to methods and properties.
  • Refactored JsonSerializerOptions into a static nested class.
  • Introduced HasParameters and TryGetValueAndRemoveIfNotNull helper
    methods.
  • Updated parameter handling logic for better AOT support.
  • +50/-12 
    HttpCommandInfo.cs
    Update parameter handling in `HttpCommandInfo`                     

    dotnet/src/webdriver/HttpCommandInfo.cs

  • Replaced direct parameter access with HasParameters and
    TryGetValueAndRemoveIfNotNull.
  • Improved parameter extraction logic for AOT compatibility.
  • +3/-7     

    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

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    🎫 Ticket compliance analysis ✅

    14480 - PR Code Verified

    Compliant requirements:

    • Contributes to making Selenium .NET library compatible with Native AOT Deployment
    • Addresses AOT-unfriendly code in the Command class

    Requires further human verification:

    • Verify that the changes actually resolve runtime failures when applications using Selenium are published as native AOT
    • Confirm that all necessary AOT annotations have been added to the Command class
    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    AOT Annotation Completeness

    The PR adds AOT annotations to the Command class, but it's important to verify that all methods that could potentially use reflection or dynamic code generation are properly annotated with RequiresUnreferencedCode and RequiresDynamicCode attributes.

    [RequiresUnreferencedCode("Adding untyped parameter values for JSON serialization has best-effort AOT support. Ensure only Selenium types and well-known .NET types are added, or use the overload that takes pre-serialized string jsonParameters for guaranteed AOT compatibility.")]
    [RequiresDynamicCode("Adding untyped parameter values for JSON serialization has best-effort AOT support. Ensure only Selenium types and well-known .NET types are added, or use the overload that takes pre-serialized string jsonParameters for guaranteed AOT compatibility.")]
    public Command(SessionId? sessionId, string name, Dictionary<string, object?>? parameters)
    Parameter Handling

    The new TryGetValueAndRemoveIfNotNull method changes the behavior of parameter handling. Verify that this change doesn't affect existing functionality, especially in edge cases with null values.

    internal bool TryGetValueAndRemoveIfNotNull(string key, [NotNullWhen(true)] out object? value)
    {
        if (this._parameters.TryGetValue(key, out value))
        {
            if (value is not null)
            {
                this._parameters.Remove(key);
                return true;
            }
        }
    
        return false;
    }

    Copy link
    Contributor

    qodo-merge-pro bot commented Mar 28, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    General
    Set output parameter consistently

    The method should set value to null when returning false to ensure consistent
    behavior. Currently, if the key exists but the value is null, the method returns
    false but value remains set to null from TryGetValue, which could be confusing.

    dotnet/src/webdriver/Command.cs [128-140]

     internal bool TryGetValueAndRemoveIfNotNull(string key, [NotNullWhen(true)] out object? value)
     {
         if (this._parameters.TryGetValue(key, out value))
         {
             if (value is not null)
             {
                 this._parameters.Remove(key);
                 return true;
             }
         }
     
    +    value = null;
         return false;
     }
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    __

    Why: The suggestion correctly identifies an issue with the out parameter behavior. When returning false, explicitly setting value=null ensures consistent behavior regardless of whether the key exists with a null value or doesn't exist at all, which improves method reliability and prevents potential bugs.

    Medium
    • Update

    @@ -31,24 +32,33 @@ namespace OpenQA.Selenium
    /// </summary>
    public class Command
    Copy link
    Contributor Author

    Choose a reason for hiding this comment

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

    Best viewed with whitespace off

    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.

    1 participant