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] Allow UTF-16 tolerant string converter as a dictionary key #15203

Merged
merged 1 commit 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.

Allow UTF-16 tolerant string converter as a dictionary key

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

Enhancement


Description

  • Updated StringConverter to handle nullable strings.

  • Added methods to read and write property names in JSON.

  • Enhanced UTF-16 string handling in JSON serialization.


Changes walkthrough 📝

Relevant files
Enhancement
StringConverter.cs
Enhanced `StringConverter` for nullable strings and property names

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

  • Changed StringConverter to support nullable strings.
  • Added ReadAsPropertyName and WriteAsPropertyName methods.
  • Updated Write method to handle nullable strings.
  • +12/-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

    Null Handling

    The ReadAsPropertyName method returns raw string without the UTF-16 escape sequence handling that is implemented in the Read method. This could lead to inconsistent string handling between property names and values.

    public override string? ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        return reader.GetString();
    }

    Copy link
    Contributor

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Add null validation for property names

    Add null check in WriteAsPropertyName since property names cannot be null in JSON

    dotnet/src/webdriver/DevTools/Json/StringConverter.cs [67-70]

     public override void WriteAsPropertyName(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
     {
    +    if (value == null) throw new ArgumentNullException(nameof(value));
         writer.WritePropertyName(value);
     }
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Property names in JSON cannot be null, so adding null validation is crucial to prevent runtime errors and provide clear error messages. This is an important defensive programming practice.

    8
    Validate JSON writer parameter

    Add writer null check in Write and WriteAsPropertyName methods to prevent
    NullReferenceException

    dotnet/src/webdriver/DevTools/Json/StringConverter.cs [59-60]

    -public override void Write(Utf8JsonWriter writer, string? value, JsonSerializerOptions options) =>
    +public override void Write(Utf8JsonWriter writer, string? value, JsonSerializerOptions options)
    +{
    +    if (writer == null) throw new ArgumentNullException(nameof(writer));
         writer.WriteStringValue(value);
    +}
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Adding null validation for the writer parameter is a good defensive programming practice that prevents NullReferenceException and provides clearer error messages. This improves the robustness of the code.

    7

    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.

    Thanks!

    @nvborisenko nvborisenko changed the title Allow UTF-16 tolerant string converter as a dictionary key [dotnet] Allow UTF-16 tolerant string converter as a dictionary key Jan 31, 2025
    @nvborisenko nvborisenko merged commit 9e28ba8 into SeleniumHQ:trunk Jan 31, 2025
    10 checks passed
    @RenderMichael RenderMichael deleted the json-sting-converter branch January 31, 2025 18:41
    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