-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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] Serialize command parameters directly to UTF-8 #15151
Conversation
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
PR Code Suggestions ✨Explore these optional code suggestions:
|
@@ -42,7 +45,21 @@ public SendingRemoteHttpRequestEventArgs(string method, string fullUrl, string? | |||
{ | |||
this.Method = method ?? throw new ArgumentNullException(nameof(method)); | |||
this.FullUrl = fullUrl ?? throw new ArgumentNullException(nameof(fullUrl)); | |||
this.RequestBody = requestBody; | |||
_utf8RequestBody = requestBody is null ? null : Encoding.UTF8.GetBytes(requestBody); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Old constructor kept around for backwards compatibility, but unused by our code.
@@ -58,7 +75,18 @@ public SendingRemoteHttpRequestEventArgs(string method, string fullUrl, string? | |||
/// <summary> | |||
/// Gets the body of the HTTP request as a string. | |||
/// </summary> | |||
public string? RequestBody { get; } | |||
public string? RequestBody |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This event args seems to be an obscure mechanism to add headers to requests. I don't know how many people are accessing the SendingRemoteHttpRequestEventArgs.RequestBody
property in the world.
For this reason, I feel that the added complexity is warranted: We will not decode our data into a string
unless the user requests it (and we cache it here so users do not have to worry about it).
/// <summary> | ||
/// Returns a string of the Command object | ||
/// </summary> | ||
/// <returns>A string representation of the Command Object</returns> | ||
public override string ToString() | ||
{ | ||
return string.Concat("[", this.SessionId, "]: ", this.Name, " ", this.ParametersAsJsonString); | ||
return $"[{this.SessionId}]: {this.Name} {this.ParametersAsJsonString}"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sidenote: ParametersAsJsonString
does JSON serialization, here inside the ToString()
method. I missed this as part of #14741. Do we want to change this? (Follow-up PR either way)
This is interesting. I propose to create simple benchmark to measure how much memory is allocated before/after. Let's say user wants to get |
/// Serializes the parameters of the comand to JSON as UTF-8 bytes. | ||
/// </summary> | ||
/// <returns></returns> | ||
public byte[] GetParametersAsUtf8Bytes() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Command
is an abstraction. Command to bytes... we should mention json
in method name. Like GetParametersAsJsoonUtf8Bytes()
Per offline discussion, this change is not worth the added complexity and maintenance burden. Closing this PR. |
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
Serialize command parameters directly into UTF-8 bytes, and pass the array around.
Motivation and Context
System.Text.Json
operates natively in UTF-8, and it is much more efficient to serialize to abyte[]
rather than materializing a string.The HTTP request contents are in the form of a
byte[]
anyway, so we are avoiding a lot of back and forth between formats here.Old Flow:
Dictionary<string, object>
-> UTF-8 insideSTJ
->string
returned bySTJ
-> Encoded tobyte[]
New Flow:
Dictionary<string, object>
-> UTF-8 returned bySTJ
Types of changes
Checklist
PR Type
Enhancement, Tests
Description
Introduced UTF-8 byte serialization for command parameters.
Updated HTTP request handling to use byte arrays directly.
Enhanced event arguments to support UTF-8 request bodies.
Added unit tests to validate new serialization logic.
Changes walkthrough 📝
Command.cs
Add UTF-8 serialization for command parameters
dotnet/src/webdriver/Command.cs
GetParametersAsUtf8Bytes
method for UTF-8 serialization.ToString
method for improved parameter representation.HttpCommandExecutor.cs
Use byte arrays for HTTP request bodies
dotnet/src/webdriver/Remote/HttpCommandExecutor.cs
SendingRemoteHttpRequestEventArgs.cs
Support UTF-8 byte arrays in event arguments
dotnet/src/webdriver/Remote/SendingRemoteHttpRequestEventArgs.cs
RequestBody
property to handle string and byte arrays.CommandTests.cs
Add tests for UTF-8 serialization of parameters
dotnet/test/common/CommandTests.cs