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] Optimize reflection in JsonEnumMemberConverter #15205

Merged
merged 2 commits into from
Jan 31, 2025

Conversation

RenderMichael
Copy link
Contributor

@RenderMichael RenderMichael commented Jan 31, 2025

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.

Optimize reflection in JsonEnumMemberConverter

Motivation and Context

Reflection optimization

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

  • Optimized reflection in JsonEnumMemberConverter for better performance.

  • Added conditional compilation for .NET 8.0 or greater compatibility.

  • Changed GetMember to GetField for retrieving enum metadata.


Changes walkthrough 📝

Relevant files
Enhancement
JsonEnumMemberConverter.cs
Optimize reflection and improve compatibility in
JsonEnumMemberConverter

dotnet/src/webdriver/DevTools/Json/JsonEnumMemberConverter.cs

  • Made the class JsonEnumMemberConverter sealed for better performance.
  • Added conditional compilation to support .NET 8.0 or greater.
  • Replaced GetMember with GetField for enum metadata retrieval.
  • Improved type constraints for TEnum to ensure stricter type safety.
  • +8/-4     

    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

    Null Reference

    The GetField() call could return null for invalid enum values, which should be handled to avoid NullReferenceException

    var enumMember = type.GetField(value.ToString());

    Copy link
    Contributor

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Validate reflection field lookup result

    Add null check for GetField() result as it may return null for invalid enum values

    dotnet/src/webdriver/DevTools/Json/JsonEnumMemberConverter.cs [45]

    -var enumMember = type.GetField(value.ToString());
    +var enumMember = type.GetField(value.ToString()) ?? throw new ArgumentException($"Invalid enum value: {value}");
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Adding a null check for GetField() with an explicit exception is important as it provides better error handling and debugging information when invalid enum values are encountered.

    7
    Add null check for reflection safety

    Add null check for enumMember.GetCustomAttributes() result to prevent potential
    NullReferenceException when casting to EnumMemberAttribute

    dotnet/src/webdriver/DevTools/Json/JsonEnumMemberConverter.cs [46-48]

    -var attr = enumMember.GetCustomAttributes(typeof(EnumMemberAttribute), false)
    -  .Cast<EnumMemberAttribute>()
    -  .FirstOrDefault();
    +var attributes = enumMember.GetCustomAttributes(typeof(EnumMemberAttribute), false);
    +var attr = attributes != null ? attributes.Cast<EnumMemberAttribute>().FirstOrDefault() : null;
    • Apply this suggestion
    Suggestion importance[1-10]: 4

    Why: While GetCustomAttributes() rarely returns null in practice, adding a null check improves robustness. However, the impact is low as this is a defensive programming measure for an unlikely scenario.

    4

    @nvborisenko nvborisenko merged commit b0ee450 into SeleniumHQ:trunk Jan 31, 2025
    10 checks passed
    @RenderMichael RenderMichael deleted the dotnet-json-enum branch February 1, 2025 05:45
    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