-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[dotnet] Strongly-typed handling for firefox profile's default preferences #14646
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
Conversation
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
| if (!this.IsSettablePreference(key)) | ||
| { | ||
| string message = string.Format(CultureInfo.InvariantCulture, "Preference {0} may not be overridden: frozen value={1}, requested value={2}", key, this.immutablePreferences[key], value.ToString()); | ||
| string message = string.Format(CultureInfo.InvariantCulture, "Preference {0} may not be overridden: frozen value={1}, requested value={2}", key, this.preferences[key], value?.ToString()); |
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.
We can take advantage of the fact that preferences and immutablePreferences have the exact same values (hence this error message). We can store just the keys of the immutablePreferences and use a hashset instead,
PR Code Suggestions ✨Explore these optional code suggestions:
|
|
These changes are so much easier to read with the whitespace turned off https://github.com/SeleniumHQ/selenium/pull/14646/files?w=1 |
| return; | ||
| if (jValue.TryGetValue(out long longValue)) | ||
| { | ||
| this.preferences[key] = Convert.ToInt32(longValue, CultureInfo.InvariantCulture).ToString(CultureInfo.InvariantCulture); |
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 is just to maintain the same exception-for-exception handling as before. Let me know if JSON numbers > int.MaxValue should be handled differently.
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 removes untyped
objecthandling from a JSON file whose structure we know, and doesn't have an untyped parameter exposed publicly. Quick win.Alternatively, we can create a
DefaultPreferencesmodel to de-serialize to. The changes here are less invasive than that.Handling of pathological input such as
abcor{ "frozen": 123, "mutable": "nonsense" }is unchanged: the root node throws if it's not an object, or if it's null. The properties are ignored unless they are objects.Motivation and Context
It's always good to move away from weakly typed code. There was a lot of complex ceremony around type-checking and the
Convertclass, which usually has to be involved because the values aren't actuallystrings orints or any other primitive type: they wereJTokens when using Newtonsoft, and needed theResponseValueJsonConverterafter the migration to STJ. UsingJsonNodedirectly makes things much easier to reason about.Types of changes
Checklist
I tested this by uncommenting all the tests in
FirefoxProfileManagerTest. The same number of tests passed before and after the changes.PR Type
enhancement
Description
JsonNodeinstead ofJsonSerializer, improving type safety and readability.JsonObjectfor JSON parsing.Preferencesclass to handle preference values usingJsonNode, with improved type checking and conversion logic.Dictionaryto aHashSetfor better performance and clarity.Changes walkthrough 📝
FirefoxProfile.cs
Use JsonNode for parsing Firefox profile preferencesdotnet/src/webdriver/Firefox/FirefoxProfile.cs
JsonSerializerwithJsonNodefor parsing JSON.JsonSerializerOptionsandResponseValueJsonConverter.JsonObject.Preferences.cs
Refactor preference handling to use JsonNodedotnet/src/webdriver/Firefox/Preferences.cs
DictionarytoHashSetfor immutables.SetPreferenceValueto handleJsonNodetypes.