You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The JSON serialization/deserialization operations do not have explicit error handling. Consider adding try-catch blocks to handle potential serialization exceptions gracefully.
Missing null check for valueChangeData after deserialization before accessing its properties. This could lead to NullReferenceException if deserialization fails.
Why: Adding error handling for JSON serialization is crucial for robustness, as serialization failures could occur with malformed data. The suggestion properly logs the error and maintains the exception chain.
Medium
✅ Add null check after deserializationSuggestion Impact:The commit implements null safety check for DomMutationData but in a different way - using null-coalescing operator with throw instead of an if-check
code diff:
+ DomMutationData valueChangeData = JsonSerializer.Deserialize(e.Payload, JsonEngineSerializerContext.Default.DomMutationData) ?? throw new JsonException("DomMutationData returned null");
Add null check for the deserialized DomMutationData before accessing its properties to prevent potential NullReferenceException.
Why: The suggestion adds important null-safety check to prevent NullReferenceException when deserialization fails. This is a critical defensive programming practice for handling potentially invalid JSON data.
Medium
Learned best practice
✅ Add null validation checks before deserializing JSON data and accessing its properties to prevent NullReferenceExceptionsSuggestion Impact:The commit added null validation for the versionInfo object after deserialization using null coalescing operator and exception throwing
code diff:
+ var versionInfo = JsonSerializer.Deserialize(rawVersionInfo, DevToolsSessionSerializerContext.Default.DevToolsVersionInfo) ?? throw new JsonException("/json/version endpoint returned null response");+ this.EndpointAddress = versionInfo.WebSocketDebuggerUrl;
Add null validation for rawVersionInfo before deserializing it to prevent potential NullReferenceException. Use ArgumentNullException.ThrowIfNull() to validate the input.
Why: Adding JsonSourceGenerationOptions with CamelCase naming policy would ensure consistent JSON property naming across all DevTools types, improving the robustness of the serialization process and maintaining compatibility with the DevTools protocol.
RenderMichael
changed the title
[dotnet] Use source-generated JSON serialization for DevTools types
[dotnet] Make the devtools SendCommand method AOT-safe
Jan 29, 2025
We'll still allow access to lower level CDP functionality (see #13990), but the high level approach will be removed.
This is the lower-level CDP functionality: sending string commandName + JsonNode parameters and getting back JsonElement? response. For that reason, this method will not be depreciated and should support AOT (minimal effort to do so, as this PR shows).
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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
This uses source-generated JSON serialization on more devtools-related types.
Motivation and Context
Contributes to #14480
Types of changes
Checklist
PR Type
Enhancement
Description
Introduced source-generated JSON serialization for DevTools types.
Added
JsonSerializerContextfor improved serialization performance.Ignored
DomMutationData.Elementduring JSON operations.Replaced generic serialization calls with context-specific serialization.
Changes walkthrough 📝
DevToolsSession.cs
Use context-specific JSON serialization in DevToolsSessiondotnet/src/webdriver/DevTools/DevToolsSession.cs
serialization.
DevToolsSessionSerializerContextfor DevTools types.DomMutationData.cs
Ignore `Element` property during JSON serializationdotnet/src/webdriver/DomMutationData.cs
JsonIgnoreattribute toElementproperty.Elementduring JSON operations.JavaScriptEngine.cs
Use context-specific JSON deserialization in JavaScriptEnginedotnet/src/webdriver/JavaScriptEngine.cs
deserialization.
JsonEngineSerializerContextforDomMutationData.