Skip to content

NRE when using Immutable dictionary for [JsonExtensionData] #32408

Closed

Description

@mwikstrom

Hi,

I want to use an immutable dictionary to store json extension data.

I tried to see if this is supported by writing the following test in an mstest project targeting netcoreapp3.1:

[TestClass]
public sealed class JsonExtensionDataTest
{
    class HasImmutableExtensionData
    {
        [JsonExtensionData]
        public ImmutableDictionary<string, JsonElement> ExtensionData { get; set; }
    }

    [TestMethod]
    public void Can_deserialize_immutable_extension_data()
    {
        const string json = "{\"foo\":123}";
        var obj = JsonSerializer.Deserialize<HasImmutableExtensionData>(json);
        Assert.IsNotNull(obj.ExtensionData);
        Assert.IsTrue(obj.ExtensionData.ContainsKey("foo"));
    }
}

That test fails with a NullReferenceException which surprised me:

 Can_deserialize_immutable_extension_data
   Source: JsonExtensionDataTest.cs line 42
   Duration: 39 ms

  Message: 
    Test method JsonExtensionDataTest.JsonExtensionDataTest.Can_deserialize_immutable_extension_data threw exception: 
    System.NullReferenceException: Object reference not set to an instance of an object.
  Stack Trace: 
    JsonSerializer.CreateDataExtensionProperty(JsonPropertyInfo jsonPropertyInfo, ReadStack& state)
    JsonSerializer.ReadCore(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& readStack)
    JsonSerializer.ReadCore(Type returnType, JsonSerializerOptions options, Utf8JsonReader& reader)
    JsonSerializer.Deserialize(String json, Type returnType, JsonSerializerOptions options)
    JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)
    JsonExtensionDataTest.Can_deserialize_immutable_extension_data() line 45

I consider the experienced behavior a bug. Another exception should have been thrown.

I would also like to request future support for using ImmutableDictionary<TKey, TValue> as backer for [JsonExtensionData].

Activity

  1. added
    untriaged
    on Feb 16, 2020
  2. mwikstrom commented on Feb 16, 2020

    @mwikstrom
    Author

    I believe area-System.Text.Json is more appropriate for this issue

  3. ahsonkhan commented on Feb 19, 2020

    @ahsonkhan
    Contributor

    cc @maryamariyan regarding bot labeling

  4. added and removed
    untriaged
    on Feb 19, 2020
  5. added this to the 5.0 milestone on Feb 19, 2020
  6. ahsonkhan commented on Feb 21, 2020

    @ahsonkhan
    Contributor

    Similar issue to #31645 (which is a feature request).

    However, in this case, there is a bug where we are getting NRE, rather than NotSupportedException.

    cc @layomia

  7. added
    help wanted
    on Feb 21, 2020
  8. layomia commented on Feb 21, 2020

    @layomia
    Contributor

    Moving the ImmutableDictionary-as-JsonExtensionData request to #31645, and updating this issue to address the null ref.

  9. changed the title [-]Immutable dictionary for [JsonExtensionData][/-] [+]NRE when using Immutable dictionary for [JsonExtensionData][/+] on Feb 21, 2020
  10. added
    good first issue
    on Feb 21, 2020
  11. ghost locked as resolved and limited conversation to collaborators on Dec 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Text.Jsonbuggood first issuehelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions