Skip to content

Loosen strict KeyValuePair deserialization logic in reflection JSON serializer to match src-gen #59450

Closed

Description

@layomia

In the reflection serializer, both "Key" and "Value" properties exactly must be present in the object payload (otherwise JsonException is thrown), while in the src-gen implementation, any valid object payload is allowed (potentially resulting in a partially populated KVP instance). We should loosen the behavior in the reflection case for consistency.

Activity

  1. added this to the 7.0.0 milestone on Sep 22, 2021
  2. ghost added
    untriaged
    on Sep 22, 2021
  3. added
    help wanted
    source-generator
    and removed
    untriaged
    on Sep 22, 2021
  4. FatTigerWang commented on Sep 23, 2021

    @FatTigerWang
    Contributor

    Can you give me a practical example to help me understand it? @layomia

  5. layomia commented on Sep 24, 2021

    @layomia
    ContributorAuthor

    @FatTigerWang

    string json = "{}";
    
    // JsonException thrown because "Key" and "Value" props are not both present
    JsonSerializer.Deserialize<KeyValuePair<string, string>>(json); 
    
    // empty KVP returned
    JsonSerializer.Deserialize(json, MyContext.Default.KeyValuePairStringString);
    
    [JsonSerializable(typeof(KeyValuePair<string, string>))]
    public partial sealed class MyContext : JsonSerializerContext { }

    We want an empty KVP in both cases. I believe the solution is to remove the use of the internal KeyValuePair converter, and just let the ObjectConverterFactory create a regular SmallObjectWithParameterizedCtorConverter for KeyValuePair types.

  6. eiriktsarpalis commented on Sep 27, 2021

    @eiriktsarpalis
    Member

    Since most dictionary implementations require non-null keys. Assuming most KeyValuePair instances in use will eventually get fed to a dictionary-like structure, wouldn't it make sense to at least require a populated key property?

  7. dmitry-bym commented on Sep 29, 2021

    @dmitry-bym

    @eiriktsarpalis I think no, because it isn't the same. Usualy dictionary representation is

    { 
      "key1":"value1", 
      "key2":"value2"
    }

    And if will be structure with KVP and it will be like that i think create empty object will be eligible behavior instead of throw. I think the logic should be the same as with regular objects

    [
       {},
       {"Key": "key", "Value": "value"}
    ]
  8. SkiFoD commented on Sep 29, 2021

    @SkiFoD
    Contributor

    @layomia @eiriktsarpalis Are we ok with returning an empty KVP in case of NameCaseSensitive miss match? There are tests that expect the throwing exception behavior. Do we want to remove the expectation? is that it?

    @eiriktsarpalis As long as I know, one can create an empty KVP and there is an exception being thrown in case
    IDictionary<int, string> dictionary = new Dictionary<string, string>();
    dictionary.Add(new KeyValuePair<string,string>());
    So if we return an empty KVP as the result of deserialization we will meet the behavior.

  9. eiriktsarpalis commented on Oct 15, 2021

    @eiriktsarpalis
    Member

    Fixed by #59757.

  10. ghost locked as resolved and limited conversation to collaborators on Nov 14, 2021
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

    Priority:3area-System.Text.Jsonhelp wantedsource-generator

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions