-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Remove compiler downgrade #32474
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
Remove compiler downgrade #32474
Conversation
|
@safern would you mind pushing the necessary nullability fixes into this branch? |
|
Of course. Working on it. |
...ries/System.Text.Json/src/System/Text/Json/Serialization/JsonPropertyInfoOfTTypeToConvert.cs
Show resolved
Hide resolved
.../System.Text.Json/src/System/Text/Json/Serialization/Converters/Collection/ArrayConverter.cs
Show resolved
Hide resolved
| { | ||
| string key = GetKeyName(enumerator.Current.Key, ref state, options); | ||
| writer.WritePropertyName(key); | ||
| Debug.Assert(enumerator.Current.Value != null); |
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.
I believe this assert is incorrect and will fail with this, so consider just using null-forgiving (!).
[Fact]
public static void WriteDictionaryNullValue()
{
var input = new Dictionary<string, string> { { "a", null }, {"b", "foo" } };
string json = JsonSerializer.Serialize(input);
}Since this is calling the built-in JsonConverter<string?>, null is allowed and will work if passed in. The StringConverter overwrites the nullability, but I suppose it can't be expressed statically.
Lines 14 to 17 in 5d8f924
| public override void Write(Utf8JsonWriter writer, string? value, JsonSerializerOptions options) | |
| { | |
| writer.WriteStringValue(value); | |
| } |
Is there something we can do here?
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.
I don't think we can do anything here. Since the converter here is declared as the abstract type, the compiler will resolve the signature that contains the DisallowNull annotation. So I think we should just use a !. I think we can't declare the converter variable type as JsonConverter<string?>, right?
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.
No, it has to remain generic. It just happens to be that all the Ts in the particular set of JsonConverter<T> work with null values (because our internal converters know how to deal with it).
I think, ideally, we will re-write parts of this to add special handling for null and honor the DisallowNull even internally.
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.
I think that would make sense. Internal usage of these APIs should overall honor the intent and what we tell our customers. It would be great if the usage of ! was lower.
.../System.Text.Json/src/System/Text/Json/Serialization/Converters/Collection/ArrayConverter.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterOfT.cs
Outdated
Show resolved
Hide resolved
54ae3ce to
c923dce
Compare
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterOfT.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterOfT.cs
Outdated
Show resolved
Hide resolved
...ystem.Text.Json/src/System/Text/Json/Serialization/Converters/Collection/ListOfTConverter.cs
Outdated
Show resolved
Hide resolved
c923dce to
50c206b
Compare
...rc/System/Text/Json/Serialization/Converters/Collection/DictionaryOfStringTValueConverter.cs
Outdated
Show resolved
Hide resolved
ahsonkhan
left a comment
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.
Thanks for doing this, @safern.
50c206b to
ca2e3bf
Compare
ca2e3bf to
f2562bf
Compare
|
Thanks Santi, you are my hero :) |
Let's see what's going to crash :)