Skip to content

Guard against invalid TypeToConvert args to read methods of object and collection converter #36605

Closed

Description

@layomia

Depending on the scenario, we could fail with a NRE:

[Fact]
public static void ConverterRead_VerifyInvalidTypeToConvertFails()
{
    var options = new JsonSerializerOptions();
    Type typeToConvert = typeof(KeyValuePair<int, int>);
    byte[] bytes = Encoding.UTF8.GetBytes(@"{""Key"":1,""Value"":2}");

    JsonConverter<KeyValuePair<int, int>> converter =
        (JsonConverter<KeyValuePair<int, int>>)options.GetConverter(typeToConvert);

    // Baseline
    var reader = new Utf8JsonReader(bytes);
    reader.Read();
    KeyValuePair<int, int> kvp = converter.Read(ref reader, typeToConvert, options);
    Assert.Equal(1, kvp.Key);
    Assert.Equal(2, kvp.Value);

    // Test
    reader = new Utf8JsonReader(bytes);
    reader.Read();
    try
    {
        converter.Read(ref reader, typeof(Dictionary<string, int>), options);
    }
    catch (Exception ex)
    {
        if (!(ex is InvalidOperationException))
        {
            throw ex; // NullReferenceException thrown.
        }
    }
}

Validation should take place in this method:

public sealed override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)

The ReadStack Initialize method called downstream may also need some validation logic.

EDIT: the validation may need to take place in the individual object/collection converters.

Activity

  1. added this to the 5.0 milestone on May 17, 2020
  2. self-assigned this
    on May 17, 2020
  3. added
    untriaged
    on May 17, 2020
  4. removed
    untriaged
    on May 17, 2020
  5. changed the title [-]Guard against invalid TypeToConvert args to read methods for object and collection converter[/-] [+]Guard against invalid TypeToConvert args to read methods of object and collection converter[/+] on May 17, 2020
  6. removed their assignment
    on Aug 7, 2020
  7. layomia commented on Aug 27, 2020

    @layomia
    ContributorAuthor

    Moving this to 6.0 since this is not a regression and will not be experienced if a compatible typeToConvert is passed to the converter.

  8. modified the milestones: 5.0.0, 6.0.0 on Aug 27, 2020
  9. added
    help wanted
    on Aug 27, 2020
  10. ShreyasJejurkar commented on Oct 8, 2020

    @ShreyasJejurkar
    Contributor

    Hi, @layomia So, in this should we throw an exception like stating that type mismatched or just return null?
    In case of throwing an exception, what would be appropriate exception type should be thrown (InvalidOperationException?) and with which message?

  11. layomia commented on Oct 13, 2020

    @layomia
    ContributorAuthor

    @MCCshreyas I believe an InvalidOperationException with a message along the lines of "Type 'NameOfType' is not compatible with converter 'ConverterName'." is appropriate.

  12. ShreyasJejurkar commented on Oct 14, 2020

    @ShreyasJejurkar
    Contributor

    OK @layomia, thanks for clarification! I will get that implemented!

  13. modified the milestones: 6.0.0, Future on Jun 9, 2021
  14. modified the milestones: Future, 7.0.0 on Oct 22, 2021
  15. modified the milestones: 7.0.0, Future on Apr 18, 2022
  16. eiriktsarpalis commented on Apr 18, 2022

    @eiriktsarpalis
    Member

    Moving to Future, as we won't have time to work on this in the .NET 7 timeframe.

  17. added
    enhancement
    on Sep 2, 2022
  18. added a commit that references this issue on Dec 7, 2023
    1148958
  19. ghost added
    in-pr
    on Dec 7, 2023
  20. ghost removed
    in-pr
    on Dec 7, 2023
  21. eiriktsarpalis commented on Dec 7, 2023

    @eiriktsarpalis
    Member

    Closing in favor of #50205 which describes the wider problem that is at play here. TL;DR built-in object converter cannot function independently of the correct JsonTypeInfo metadata associated with the type. This would need to be fixed whenever #63791 is implemented.

  22. locked and limited conversation to collaborators on Jan 7, 2024
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.Jsonbugenhancementhelp wanted

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions