Skip to content

Guard against NRE when deserializing extension data into dictionary without parameterless ctor - #32952

Merged
layomia merged 8 commits into
dotnet:masterfrom
alanisaac:fix-not-supported-extension-data
Feb 29, 2020
Merged

layomia merged 8 commits into
dotnet:masterfrom
alanisaac:fix-not-supported-extension-data

Conversation

@alanisaac

Copy link
Copy Markdown
Contributor

fixes #32408

Notes

This change means that attempting to use an ImmutableDictionary<> with [JsonExtensionData] with throw a NotSupportedException where previously a NullReferenceException was thrown from the following line:

extensionData = (IDictionary?)jsonPropertyInfo.RuntimeClassInfo.CreateObject();

{
JsonConverter? converter = Options.GetConverter(declaredPropertyType);
if (converter == null)
if (converter == null || jsonPropertyInfo.RuntimeClassInfo.CreateObject == null)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extensionData = (IDictionary?)jsonPropertyInfo.RuntimeClassInfo.CreateObject();
would be a better place for this check. We shouldn't throw unless there's extension data to store.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 886316c

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should test other classes like IImmutableDictionary<string, JsonElement>, and a custom dictionary without a public parameterless ctor.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can do.

@alanisaac alanisaac Feb 29, 2020 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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; }
}

@layomia layomia Feb 29, 2020 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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; }

@layomia layomia Feb 28, 2020 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@layomia layomia left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@layomia layomia changed the title Changed exception behavior for immutable dictionary Guard against NRE when deserializing extension data into dictionary without parameterless ctor Feb 29, 2020
@layomia
layomia merged commit a159f40 into dotnet:master Feb 29, 2020
@alanisaac
alanisaac deleted the fix-not-supported-extension-data branch February 29, 2020 18:40
// attempt to serialize a null immutable dictionary
string expectedJson = "{}";
var obj = new ClassWithExtensionPropertyAsImmutable();
var json = JsonSerializer.Serialize(obj);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Don't use var here.

Suggested change
var json = JsonSerializer.Serialize(obj);
string json = JsonSerializer.Serialize(obj);

public GenericIImmutableDictionaryWrapper<string, JsonElement> MyOverflow { get; set; }
}


Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove extra new line

@ghost ghost locked as resolved and limited conversation to collaborators Dec 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NRE when using Immutable dictionary for [JsonExtensionData]

4 participants