Repository navigation
Guard against NRE when deserializing extension data into dictionary without parameterless ctor - #32952
Conversation
| { | ||
| JsonConverter? converter = Options.GetConverter(declaredPropertyType); | ||
| if (converter == null) | ||
| if (converter == null || jsonPropertyInfo.RuntimeClassInfo.CreateObject == null) |
There was a problem hiding this comment.
We'll need tests for both cases, showing that deserialization works fine when there's no extension data, and one showing the NSE when theres extra data in the payload.
There was a problem hiding this comment.
Got it -- that was my first instinct when I ran this through the debugger, but then I saw that other exceptions for this property's type were thrown elsewhere so I second-guessed myself. Will make this change.
There was a problem hiding this comment.
It might make sense move those checks to the point of (de)serialization if they can be done in way that doesn't hurt runtime performance. This can be considered as part of #32903.
| public Dictionary<string, object> MyOverflow2 { get; set; } | ||
| } | ||
|
|
||
| private class ClassWithNotSupportedExtensionPropertyImmutableDictionary |
There was a problem hiding this comment.
We should test other classes like IImmutableDictionary<string, JsonElement>, and a custom dictionary without a public parameterless ctor.
There was a problem hiding this comment.
I've added additional collection classes and tests for these cases.
When trying to test for these cases I noticed that classes that implement IDictionary<TKey, TValue> but do not have two generic type parameters end up throwing IndexOutOfRangeException on de-serialization when marked with [JsonExtensionData]. Should this also be a NotSupportedException instead?
For example, by using the StringToGenericIDictionaryWrapper<TValue> class already in this test project, the following simple test will reproduce the IndexOutOfRangeException:
[Fact]
public static void DeserializeIntoSingleGenericDictionary()
{
JsonSerializer.Deserialize<ClassWithExtensionPropertyStringToGeneric>("{\"hello\":\"world\"}");
}
private class ClassWithExtensionPropertyStringToGeneric
{
[JsonExtensionData]
public StringToGenericIDictionaryWrapper<object> MyOverflow { get; set; }
}
There was a problem hiding this comment.
These types should be supported if they implement IDictionary<string, object/JsonElement>. The problem here is that the debug assertions are on the declared type itself, not the underlying IDictionary<,> type. This test will work fine in a Release build of the library.
Here's what the fix could look like: https://gist.github.com/layomia/d77aeea5d3250785de5a7aa4dee95c46#file-create_data_extension_property-cs-L8-L20. This fix could be added in a follow up to this PR, with tests (for classes with 0, 1, and 3 generic parameters). Here's a tracking issue: #33011.
| private class ClassWithNotSupportedExtensionPropertyImmutableDictionary | ||
| { | ||
| [JsonExtensionData] | ||
| public ImmutableDictionary<string, object> MyOverflow { get; set; } |
There was a problem hiding this comment.
We should test what happens when we have a custom converter specified -
- with an attribute on the dictionary type
- with an attribute on the extension property
- added at runtime on the options.Converters collection
This can be deferred to work in #32903.
There was a problem hiding this comment.
Thanks, @alanisaac!
The fix for the issue you brought up in #32952 (comment) can be added in a follow up PR. I filed a tracking issue here: #33011.
Merging this now to avoid restarting CI and to isolate the fix for #32408.
| // attempt to serialize a null immutable dictionary | ||
| string expectedJson = "{}"; | ||
| var obj = new ClassWithExtensionPropertyAsImmutable(); | ||
| var json = JsonSerializer.Serialize(obj); |
There was a problem hiding this comment.
nit: Don't use var here.
| var json = JsonSerializer.Serialize(obj); | |
| string json = JsonSerializer.Serialize(obj); |
| public GenericIImmutableDictionaryWrapper<string, JsonElement> MyOverflow { get; set; } | ||
| } | ||
|
|
||
|
|
There was a problem hiding this comment.
nit: remove extra new line
fixes #32408
Notes
This change means that attempting to use an
ImmutableDictionary<>with[JsonExtensionData]with throw aNotSupportedExceptionwhere previously aNullReferenceExceptionwas thrown from the following line:runtime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandlePropertyName.cs
Line 121 in 53c67cb