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] Make Selenium Manager's AOT safety explicit #14733

Merged
merged 1 commit into from
Nov 8, 2024

Conversation

RenderMichael
Copy link
Contributor

@RenderMichael RenderMichael commented Nov 8, 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

The JsonSerializerOptions instance passed to the SeleniumManagerResponse deserialization is AOT-safe, because the TypeResolver was set to a source-generated instance.

However, the JSON de-serialization method cannot know that. For that reason, we can use the de-serialization method that takes a JsonTypeInfo<T> overload.

Motivation and Context

Satisfies AOT analyzer warning
image

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

  • Replaced the JsonSerializerOptions with JsonSerializerContext.Default.SeleniumManagerResponse to ensure AOT safety in the deserialization process.
  • Converted SeleniumManagerResponse, LogEntryResponse, and ResultResponse to sealed records to enhance performance.
  • Added JsonSourceGenerationOptions with PropertyNameCaseInsensitive set to true for improved JSON property handling.

Changes walkthrough 📝

Relevant files
Enhancement
SeleniumManager.cs
Improve AOT safety and performance in SeleniumManager       

dotnet/src/webdriver/SeleniumManager.cs

  • Replaced JsonSerializerOptions with JsonSerializerContext for AOT
    safety.
  • Changed records to sealed records for better performance.
  • Added JsonSourceGenerationOptions attribute for case insensitivity.
  • +6/-7     

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

    Copy link
    Contributor

    qodo-merge-pro bot commented Nov 8, 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 Reference
    The forced null forgiving operator (!) is used when deserializing JSON response. Consider adding null check or validation to handle potential null values gracefully.

    Copy link
    Contributor

    qodo-merge-pro bot commented Nov 8, 2024

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Add input validation to handle empty responses before JSON deserialization

    Add null check for output before attempting deserialization to handle empty or null
    responses more gracefully.

    dotnet/src/webdriver/SeleniumManager.cs [182-188]

     string output = outputBuilder.ToString().Trim();
    +if (string.IsNullOrEmpty(output))
    +{
    +    throw new WebDriverException("Selenium Manager returned empty response");
    +}
     
     SeleniumManagerResponse jsonResponse;
    -
     try
     {
         jsonResponse = JsonSerializer.Deserialize(output, SeleniumManagerSerializerContext.Default.SeleniumManagerResponse)!;
     }
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Adding validation for empty responses before deserialization is a crucial defensive programming practice that prevents cryptic JSON parsing errors and provides clearer error messages to users.

    8
    Best practice
    Replace force unwrap operator with null-coalescing throw for safer null handling

    Remove the force unwrap operator (!) from the JsonSerializer.Deserialize call to
    handle potential null responses more safely.

    dotnet/src/webdriver/SeleniumManager.cs [188]

    -jsonResponse = JsonSerializer.Deserialize(output, SeleniumManagerSerializerContext.Default.SeleniumManagerResponse)!;
    +jsonResponse = JsonSerializer.Deserialize(output, SeleniumManagerSerializerContext.Default.SeleniumManagerResponse)
    +    ?? throw new WebDriverException("Failed to deserialize Selenium Manager response");
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Using null-coalescing throw instead of force unwrap provides more explicit and safer null handling, with a clear error message when deserialization fails.

    7

    💡 Need additional feedback ? start a PR chat

    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.

    Yeap, this is just a warning, because analyzer cannot understand whether options are "good", which is good. I don't mind to satisfy him. Thank you.

    @RenderMichael
    Copy link
    Contributor Author

    It was considered and rejected to tie the "AOT warning" to the serialization instance: dotnet/runtime#108781

    If we are to support AOT, we will eventually enable the warnings and these will light up the sky, unless we use this approach.

    @nvborisenko nvborisenko merged commit fbe116c into SeleniumHQ:trunk Nov 8, 2024
    10 checks passed
    @RenderMichael RenderMichael deleted the selenium-manager-json branch November 11, 2024 04:55
    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