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 GetDiscriminator method doesn't handle the case where the discriminator property exists but has a non-string value. This could lead to unexpected behavior if the JSON structure changes.
✅ Validate discriminator existenceSuggestion Impact:The commit implemented the same validation logic as suggested, but with different implementation. Instead of checking for null before returning, it used the null-coalescing operator with throw to validate the discriminator's existence and throw an exception with a descriptive message when missing.
The method doesn't handle the case where the discriminator property isn't found in the JSON object. If the property doesn't exist, the method will return null, which could lead to unexpected behavior in the converters that use this method. Consider adding validation to throw a more descriptive exception when the discriminator property is missing.
public static string? GetDiscriminator(this ref Utf8JsonReader reader, string name)
{
Utf8JsonReader readerClone = reader;
if (readerClone.TokenType != JsonTokenType.StartObject)
throw new JsonException();
string? discriminator = null;
readerClone.Read();
while (readerClone.TokenType == JsonTokenType.PropertyName)
{
string? propertyName = readerClone.GetString();
readerClone.Read();
if (propertyName == name)
{
discriminator = readerClone.GetString();
break;
}
readerClone.Skip();
readerClone.Read();
}
+ if (discriminator == null)+ throw new JsonException($"Required discriminator property '{name}' not found in JSON object.");+
return discriminator;
}
[Suggestion has been applied]
Suggestion importance[1-10]: 8
__
Why: The suggestion addresses a potential bug where missing discriminator properties would return null without error, causing silent failures in the converters. Adding validation with a descriptive exception improves error handling and debugging significantly.
Medium
Learned best practice
Add parameter validation to prevent null reference exceptions when comparing string values
The name parameter should be validated to ensure it's not null before using it in the comparison. Add a null check at the beginning of the method to prevent potential NullReferenceException when comparing propertyName == name.
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
Motivation and Context
Improve memory allocation.
Before:
After:
Types of changes
Checklist
PR Type
Enhancement
Description
Replaced
JsonDocumentusage withUtf8JsonReaderfor better performance.Introduced
GetDiscriminatorextension method for cleaner discriminator extraction.Reduced memory allocation in JSON deserialization processes.
Updated multiple converters to use the new
GetDiscriminatormethod.Changes walkthrough 📝
EvaluateResultConverter.cs
Optimize `EvaluateResult` deserialization with `Utf8JsonReader`dotnet/src/webdriver/BiDi/Communication/Json/Converters/Polymorphic/EvaluateResultConverter.cs
JsonDocumentwithUtf8JsonReaderfor deserialization.GetDiscriminatorfor discriminator extraction.EvaluateResultdeserialization.LogEntryConverter.cs
Enhance `LogEntry` deserialization with `Utf8JsonReader`dotnet/src/webdriver/BiDi/Communication/Json/Converters/Polymorphic/LogEntryConverter.cs
Utf8JsonReaderforLogEntrydeserialization.JsonDocumentwithGetDiscriminatorfor type handling.MessageConverter.cs
Improve `Message` deserialization using `Utf8JsonReader`dotnet/src/webdriver/BiDi/Communication/Json/Converters/Polymorphic/MessageConverter.cs
JsonDocumentwithUtf8JsonReaderforMessagedeserialization.GetDiscriminatorfor cleaner type extraction.RealmInfoConverter.cs
Optimize `RealmInfo` deserialization with `Utf8JsonReader`dotnet/src/webdriver/BiDi/Communication/Json/Converters/Polymorphic/RealmInfoConverter.cs
RealmInfodeserialization to useUtf8JsonReader.GetDiscriminatorfor type determination.RemoteValueConverter.cs
Enhance `RemoteValue` deserialization with `Utf8JsonReader`dotnet/src/webdriver/BiDi/Communication/Json/Converters/Polymorphic/RemoteValueConverter.cs
JsonDocumentwithUtf8JsonReaderforRemoteValuedeserialization.
GetDiscriminator.JsonExtensions.cs
Introduce `GetDiscriminator` extension for `Utf8JsonReader`dotnet/src/webdriver/BiDi/Communication/Json/Internal/JsonExtensions.cs
GetDiscriminatorextension method forUtf8JsonReader.