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] Address lingering AOT warnings #15506

Merged
merged 3 commits into from
Mar 28, 2025

Conversation

RenderMichael
Copy link
Contributor

@RenderMichael RenderMichael commented Mar 25, 2025

User description

Unlike other analyzers, AOT warnings are user-facing. It is important to address them on our side, for a clear and AOT-safe user experience.

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


Description

  • Added AOT-specific attributes to CloseDevToolsSession for compatibility.

  • Refactored JSON serialization logic in Command class.

    • Updated TypeInfoResolver to TypeInfoResolverChain.
    • Simplified ParametersAsJsonString property logic.
    • Adjusted deserialization to use CommandJsonSerializerContext.
  • Enhanced FileUtilities.GetCurrentDirectory for better null handling.


Changes walkthrough 📝

Relevant files
Enhancement
ChromiumDriver.cs
Add AOT-specific attributes to `CloseDevToolsSession`       

dotnet/src/webdriver/Chromium/ChromiumDriver.cs

  • Added [RequiresUnreferencedCode] and [RequiresDynamicCode] attributes.
  • Improved AOT compatibility for CloseDevToolsSession.
  • +2/-0     
    Command.cs
    Refactor JSON serialization and deserialization logic       

    dotnet/src/webdriver/Command.cs

  • Replaced TypeInfoResolver with TypeInfoResolverChain.
  • Simplified ParametersAsJsonString property logic.
  • Updated deserialization to use CommandJsonSerializerContext.
  • Added JsonSourceGenerationOptions for custom converters.
  • +9/-7     
    Bug fix
    FileUtilities.cs
    Enhance null handling in `GetCurrentDirectory`                     

    dotnet/src/webdriver/Internal/FileUtilities.cs

  • Improved null handling for executingAssembly.Location.
  • Refactored GetCurrentDirectory for better readability.
  • +3/-2     

    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:

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

    Serialization Change

    The PR changes the JSON serialization options by replacing TypeInfoResolver with TypeInfoResolverChain and modifies deserialization to use CommandJsonSerializerContext.Default instead of s_jsonSerializerOptions. Verify this doesn't affect serialization behavior.

    TypeInfoResolverChain =
    {
        CommandJsonSerializerContext.Default,
        new DefaultJsonTypeInfoResolver()
    },
    Variable Assignment

    The refactored code assigns executingAssembly.Location to a local variable before checking if it's null or empty. Ensure this doesn't introduce any unexpected behavior changes.

    string assemblyLocation = executingAssembly.Location;
    if (!string.IsNullOrEmpty(assemblyLocation))

    Copy link
    Contributor

    qodo-merge-pro bot commented Mar 25, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    Add null check

    The code is using a nullable reference type with the null-forgiving operator
    (!), which can lead to runtime exceptions if the context is null. Consider
    adding a null check before using the context or handle potential null values
    properly.

    dotnet/src/webdriver/Command.cs [122]

    -Dictionary<string, object?>? parameters = JsonSerializer.Deserialize<Dictionary<string, object?>>(value, CommandJsonSerializerContext.Default.DictionaryStringObject!);
    +var context = CommandJsonSerializerContext.Default.DictionaryStringObject;
    +if (context == null)
    +    throw new InvalidOperationException("JSON serialization context is not available");
    +Dictionary<string, object?>? parameters = JsonSerializer.Deserialize<Dictionary<string, object?>>(value, context);
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    __

    Why: The suggestion addresses a potential null reference exception by adding a proper null check before using the null-forgiving operator. This is a good defensive programming practice that improves the robustness of the code by explicitly handling the case where the serialization context might be null.

    Medium
    Learned
    best practice
    Add parameter validation to ensure null values are properly handled with appropriate exceptions

    The method ConvertParametersFromJson should validate that the value parameter is
    not null before using it. According to the method documentation, it should throw
    an ArgumentNullException if the value is null, but the implementation doesn't
    include this check. Add a null check using ArgumentNullException.ThrowIfNull()
    at the beginning of the method.

    dotnet/src/webdriver/Command.cs [120-124]

     private static Dictionary<string, object?>? ConvertParametersFromJson(string value)
     {
    +    ArgumentNullException.ThrowIfNull(value, nameof(value));
         Dictionary<string, object?>? parameters = JsonSerializer.Deserialize<Dictionary<string, object?>>(value, CommandJsonSerializerContext.Default.DictionaryStringObject!);
         return parameters;
     }
    • Apply this suggestion
    Suggestion importance[1-10]: 6
    Low
    • Update

    @RenderMichael
    Copy link
    Contributor Author

    CI failures are unrelated to this PR:

    //dotnet/test/common:BiDi/Script/EvaluateParametersTest-firefox
    //dotnet/test/common:PageLoadingTest-firefox
    //dotnet/test/common:BiDi/Storage/StorageTest-edge

    Formatting failure is unrelated to .NET:

    index 8b019db..e02d602 100644
    --- a/rust/tests/BUILD.bazel
    +++ b/rust/tests/BUILD.bazel
    @@ -1,5 +1,5 @@
     load("@crates//:defs.bzl", "all_crate_deps")
    -load("//rust:defs.bzl", "rust_library", "rust_test_suite", "rustfmt_config")
    +load("//rust:defs.bzl", "rust_test_suite", "rustfmt_config")

    @RenderMichael RenderMichael merged commit 014d1c3 into SeleniumHQ:trunk Mar 28, 2025
    8 of 10 checks passed
    @RenderMichael RenderMichael deleted the aot-warnings branch March 28, 2025 04:38
    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