Skip to content
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

NullReference when JsonExtensionData dictionary have a custom JsonConverter #32903

Closed
jozkee opened this issue Feb 27, 2020 · 5 comments · Fixed by #34569
Closed

NullReference when JsonExtensionData dictionary have a custom JsonConverter #32903

jozkee opened this issue Feb 27, 2020 · 5 comments · Fixed by #34569
Labels
area-System.Text.Json bug good first issue Issue should be easy to implement, good for first-time contributors help wanted [up-for-grabs] Good issue for external contributors
Milestone

Comments

@jozkee
Copy link
Member

jozkee commented Feb 27, 2020

Repro:

private class ClassWithExtensionData
{
    [JsonExtensionData]
    public Dictionary<string, object> Overflow { get; set; }
}

public class DictionaryOverflowConverter : JsonConverter<Dictionary<string, object>>
{
    public override Dictionary<string, object> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        throw new NotImplementedException();
    }

    public override void Write(Utf8JsonWriter writer, Dictionary<string, object> value, JsonSerializerOptions options)
    {
        // Do something.
    }
}

[Fact]
public static void QuickTest()
{
    var root = new ClassWithExtensionData();
    root.Overflow = new Dictionary<string, object>();
    root.Overflow.Add("test", "TestValue");

    var opts = new JsonSerializerOptions();
    opts.Converters.Add(new DictionaryOverflowConverter());

    string json = JsonSerializer.Serialize(root, opts);
}

This only happens in master.

@steveharter @layomia

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Feb 27, 2020
@jozkee jozkee added bug and removed untriaged New issue has not been triaged by the area owner labels Feb 27, 2020
@jozkee jozkee added this to the 5.0 milestone Feb 27, 2020
@layomia
Copy link
Contributor

layomia commented Feb 28, 2020

Note: custom converters for extension properties can only be used on serialization. Deserialization is handled by the serializer.

@layomia layomia added help wanted [up-for-grabs] Good issue for external contributors good first issue Issue should be easy to implement, good for first-time contributors labels Feb 28, 2020
@ahsonkhan
Copy link
Member

can only be used on serialization. Deserialization is handled by the serializer.

That's a bit strange. Is there a reason for that and should we (can we?) fix it?

@jozkee
Copy link
Member Author

jozkee commented Mar 2, 2020

@ahsonkhan Overflow properties does not come on a particular order when reading the JSON and the Read method must return a Dictionary<string, JsonElement> or Dictionary<string, object> while overflow properties are parsed to JsonElement entries in such dictionary. That is why we don't use it on deserialization.

@layomia
Copy link
Contributor

layomia commented Mar 2, 2020

can only be used on serialization. Deserialization is handled by the serializer.

That's a bit strange. Is there a reason for that and should we (can we?) fix it?

@ahsonkhan it's because we may not have all the extra JSON in one go - we'd have to parse and wrap all the extension properties in one JSON object, or pass the entire payload to the converter to be filtered.

FWIW Newtonsoft does not honor custom converters for both reading and writing extension data: https://dotnetfiddle.net/qwMrPS. Writing seems doable, so I think the action for this issue is to fix the NRE and make sure custom converters are honored on serialization.

@steveharter, it seems that the assumption in

is that only internal converters are used for serialization. Any thoughts?

@RezaJooyandeh
Copy link
Contributor

I think for writing it makes sense to support custom serialization.

layomia pushed a commit that referenced this issue Apr 14, 2020
* Fix NullReference on JsonExtensionData

Fix NullReferenceException on serialization when JsonExtensionData dictionary has a custom JsonConverter.

Fix #32903

* Add test for converters declared through attribute

* Ensure custom serialization is not used for reading JsonExtensionData

* Cover all the combinations in tests
@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.
Labels
area-System.Text.Json bug good first issue Issue should be easy to implement, good for first-time contributors help wanted [up-for-grabs] Good issue for external contributors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants