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

Fix exception for KeyValuePair<TKey, TValue> when ReferenceHandler is on to match exception thrown by POCOs with parameterized ctor. #1882

Closed
Jozkee opened this issue Jan 17, 2020 · 1 comment

Comments

@Jozkee
Copy link
Member

Jozkee commented Jan 17, 2020

The KeyValuePairConverter<TKey, TValue> is an special case that uses an internal converter that inherits from SmallObjectWithParameterizedConstructorConverter, and that converter validates that the only valid properties are Key and Value.

public static void DeserializeIdOnKVP()
{
    string json = @"
    {
        ""$id"": ""1"",
        ""Key"": ""TestKey"",
        ""Value"": ""TestValue""
    }";

    var opts = new JsonSerializerOptions
    {
        ReferenceHandling = ReferenceHandling.Preserve
    };

    KeyValuePair<string, string> kvp = JsonSerializer.Deserialize<KeyValuePair<string, string>>(json, opts);
    Console.WriteLine(kvp.Key);
}

Therefore when a metadata property $id is also present in the JSON object, the deserializer throws the following, even when the ReferenceHandler feature is set:

System.Text.Json.JsonException : The JSON value could not be converted to System.Collections.Generic.KeyValuePair`2[System.String,System.String]. Path: $ | LineNumber: 2 | BytePositionInLine: 22.

While other types that also use SmallObjectWithParameterizedConstructorConverter throw the following on ReferenceHandler scenarios:

System.NotSupportedException: Reference metadata is not honored when deserializing types using parameterized constructors. See type 'System.Text.Json.Serialization.Tests.ConstructorTests+Employee'. Path: $.$id | LineNumber: 0 | BytePositionInLine: 7.
@Jozkee Jozkee added this to the 5.0 milestone Jan 17, 2020
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label Jan 17, 2020
@Jozkee Jozkee removed the untriaged New issue has not been triaged by the area owner label Jan 17, 2020
@ahsonkhan ahsonkhan added the bug label Jan 18, 2020
@layomia layomia added this to Backlog in System.Text.Json - 6.0 via automation Apr 7, 2020
@Jozkee Jozkee modified the milestones: 5.0.0, Future Jul 1, 2020
@Jozkee Jozkee removed this from Backlog in System.Text.Json - 6.0 Jul 1, 2020
@Jozkee Jozkee changed the title Avoid throw when a JSON object containing $id tries to deserialize into a KeyValuePair<TKey, TValue> Fix exception for KeyValuePair<TKey, TValue> when ReferenceHandler is on to match exception thrown by POCOs with parameterized ctor. Jul 15, 2020
@eiriktsarpalis
Copy link
Member

Superseded by #59757.

@ghost ghost locked as resolved and limited conversation to collaborators Nov 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants