[bidi] Convert RemoteValue to IDictionary by nvborisenko · Pull Request #17181 · SeleniumHQ/selenium
Adds support for converting MapRemoteValue and ObjectRemoteValue instances to generic dictionary types in the BiDi Script RemoteValue system. The changes include new conversion logic and comprehensive tests to ensure correct behavior for both populated and empty remote values.
🔗 Related Issues
Contributes to #16719
🔄 Types of changes
- New feature (non-breaking change which adds functionality and tests!)
Copilot AI review requested due to automatic review settings
March 5, 2026 16:12Review Summary by Qodo
Add RemoteValue to IDictionary conversion support
✨ Enhancement
Walkthroughs
Description
• Add support for converting MapRemoteValue and ObjectRemoteValue to generic dictionary types • Implement ConvertRemoteValuesToDictionary method for dictionary conversion logic • Add comprehensive tests for populated and empty dictionary conversions
File Changes
1. dotnet/src/webdriver/BiDi/Script/RemoteValue.cs
✨ Enhancement +25/-0
Add dictionary conversion for RemoteValue types
• Added two new conversion patterns for MapRemoteValue and ObjectRemoteValue to generic dictionary types • Implemented ConvertRemoteValuesToDictionary private method to handle dictionary conversion with key-value pair extraction • Uses reflection to dynamically invoke ConvertTo methods for both key and value types
2. dotnet/test/common/BiDi/Script/RemoteValueConversionTests.cs
🧪 Tests +72/-0
Add RemoteValue to dictionary conversion tests
• Added test for converting MapRemoteValue to Dictionary<string, int> and `IDictionary<string, int>` • Added test for converting empty MapRemoteValue to dictionary • Added test for converting ObjectRemoteValue to Dictionary<string, bool> and IDictionary<string, bool> • Added test for converting empty ObjectRemoteValue to dictionarydotnet/test/common/BiDi/Script/RemoteValueConversionTests.cs
Code Review by Qodo
🐞 Bugs (0) 📘 Rule violations (1) 📎 Requirement gaps (0)
1. Dictionary conversion lacks validation ☑ 📘 Rule violation ⛯ Reliability
Description
ConvertRemoteValuesToDictionary assumes each entry has exactly two elements and relies on null-forgiving reflection calls, which can crash with non-actionable exceptions if protocol data or reflection lookup is unexpected.
Code
dotnet/src/webdriver/BiDi/Script/RemoteValue.cs[R158-173]+ private static TResult ConvertRemoteValuesToDictionary<TResult>(IReadOnlyList<IReadOnlyList<RemoteValue>>? remoteValues, Type dictionaryType) + { + var typeArgs = dictionaryType.GetGenericArguments(); + var dict = (System.Collections.IDictionary)Activator.CreateInstance(dictionaryType)!; + + if (remoteValues is not null) + { + var convertKeyMethod = typeof(RemoteValue).GetMethod(nameof(ConvertTo))!.MakeGenericMethod(typeArgs[0]); + var convertValueMethod = typeof(RemoteValue).GetMethod(nameof(ConvertTo))!.MakeGenericMethod(typeArgs[1]); + + foreach (var pair in remoteValues) + { + var convertedKey = convertKeyMethod.Invoke(pair[0], null)!; + var convertedValue = convertValueMethod.Invoke(pair[1], null); + dict.Add(convertedKey, convertedValue); + }
Evidence
PR Compliance ID 12 requires validating required inputs early and failing fast with actionable errors. The new conversion code uses null-forgiving operators for reflection/activation and indexes pair[0]/pair[1] without validating the structure, which can throw NullReferenceException/IndexOutOfRangeException instead of a clear error.
dotnet/src/webdriver/BiDi/Script/RemoteValue.cs[161-172]
Best Practice: Learned patterns
Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution ## Issue description `ConvertRemoteValuesToDictionary` currently assumes inputs are well-formed (each `pair` has 2 elements) and uses null-forgiving reflection/activation. If data is malformed or reflection lookup fails, it can throw non-actionable exceptions like `IndexOutOfRangeException`/`NullReferenceException`. ## Issue Context This conversion is fed by protocol-derived `RemoteValue` payloads and should fail fast with clear, actionable errors when inputs are invalid (per PR Compliance ID 12). ## Fix Focus Areas - dotnet/src/webdriver/BiDi/Script/RemoteValue.cs[158-173]
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
ⓘ The new review experience is currently in Beta. Learn more
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Adds support in the .NET BiDi Script.RemoteValue conversion system to convert MapRemoteValue and ObjectRemoteValue into .NET dictionary types, aligning with the broader goal of implicit/typed RemoteValue conversions.
Changes:
- Extend
RemoteValue.ConvertTo<T>()to support convertingMapRemoteValue/ObjectRemoteValueintoDictionary<TKey, TValue>-compatible targets (e.g.,Dictionary<,>,IDictionary<,>). - Introduce a shared dictionary conversion helper that builds a
Dictionary<TKey, TValue>and populates it from remote key/value pairs. - Add unit tests covering dictionary conversion for populated and empty map/object remote values.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| dotnet/src/webdriver/BiDi/Script/RemoteValue.cs | Adds conversion branches + helper to materialize MapRemoteValue/ObjectRemoteValue as a generic dictionary. |
| dotnet/test/common/BiDi/Script/RemoteValueConversionTests.cs | Adds NUnit tests validating dictionary conversions for map/object remote values (including empty cases). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters