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
In both the Read and Write methods, add a null check for the result of options.GetTypeInfo() and throw a NotSupportedException if it is null to provide a clearer error message.
public override Optional<T> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
{
reader.Read(); // consume null
return new Optional<T>(default!);
}
- T value = JsonSerializer.Deserialize(ref reader, options.GetTypeInfo<T>())!;+ var typeInfo = options.GetTypeInfo<T>();+ if (typeInfo is null)+ {+ throw new NotSupportedException($"The type '{typeof(T)}' is not configured for JSON serialization. Ensure it is included in a source-generated context.");+ }++ T value = JsonSerializer.Deserialize(ref reader, typeInfo)!;
return new Optional<T>(value);
}
public override void Write(Utf8JsonWriter writer, Optional<T> value, JsonSerializerOptions options)
{
if (value.TryGetValue(out var optionalValue))
{
- JsonSerializer.Serialize(writer, optionalValue, options.GetTypeInfo<T>());+ var typeInfo = options.GetTypeInfo<T>();+ if (typeInfo is null)+ {+ throw new NotSupportedException($"The type '{typeof(T)}' is not configured for JSON serialization. Ensure it is included in a source-generated context.");+ }++ JsonSerializer.Serialize(writer, optionalValue, typeInfo);
}
else
{
writer.WriteNullValue();
}
}
[To ensure code accuracy, apply this suggestion manually]
Suggestion importance[1-10]: 7
__
Why: The suggestion correctly identifies that options.GetTypeInfo<T>() can return null and proposes adding a null check with a more descriptive exception, which improves the robustness and debuggability of the code.
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
💥 What does this PR do?
Resolves AOT trimming warning for OptionalConverter.
🔄 Types of changes
PR Type
Bug fix
Description
Replace generic
JsonSerializercalls with AOT-safe type info overloadsUse
options.GetTypeInfo<T>()for deserialization and serializationResolves AOT trimming warnings in OptionalConverter
Diagram Walkthrough
File Walkthrough
OptionalConverter.cs
Replace generic JSON serializer calls with AOT-safe overloadsdotnet/src/webdriver/BiDi/Json/Converters/OptionalConverter.cs
Read()method to useoptions.GetTypeInfo()instead of genericJsonSerializer.Deserialize()Write()method to useoptions.GetTypeInfo()instead of genericJsonSerializer.Serialize()information to the serializer