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

Do not pass null value for parameter Type inputType in JsonSerializer.Serialize #20823

Closed
2 of 22 tasks
buyaa-n opened this issue Sep 29, 2020 · 0 comments · Fixed by #20973
Closed
2 of 22 tasks

Do not pass null value for parameter Type inputType in JsonSerializer.Serialize #20823

buyaa-n opened this issue Sep 29, 2020 · 0 comments · Fixed by #20973
Assignees
Labels
breaking-change Indicates a .NET Core breaking change 🏁 Release: .NET 5 Work items for the .NET 5 release

Comments

@buyaa-n
Copy link
Contributor

buyaa-n commented Sep 29, 2020

Do not pass a null for parameter Type inputType in JsonSerializer.Serialize(...) overloads

Passing in null for the Type parameter of the JsonSerialaizer.Serialize is unacceptable and should throw ArgumentNullException in that case. It was throwing the exception in most cases except one edge case: for certain JsonSerializer.Serialize, JsonSerializer.SerializeAsync and JsonSerializer.SerializeToUtf8Bytes() overloads which accepting object value, Type inputType as parameters, and when those value and inputType parameters are both null it was producing "null" JSON string. Now it throws ArgumentNullException correctly whenever Type inputType parameter is passed as null. Related to dotnet/runtime#528 (comment)

Version introduced

5.0

Old behavior

public static void TestSerializeNullType()
{
    // Before (3.1): Returned a string with value "null"
    // Same with other Serialize{Async} overloads
    JsonSerializer.Serialize(null, null);

    // Before (3.1): Returned a byte[] with value "null"
    JsonSerializer.SerializeToUtf8Bytes(null, null);
}

New behavior

Passing in null for the Type parameter while serializing now correctly throws ArgumentNullException rather than returning "null" JSON:

public static void TestSerializeNullType()
{
    // After (5.0): ArgumentNullException: Value cannot be null. (Parameter 'inputType')
    // Same with other Serialize{Async} overloads
    JsonSerializer.Serialize(null, null);

    // After (5.0): ArgumentNullException: Value cannot be null. (Parameter 'inputType')
    JsonSerializer.SerializeToUtf8Bytes(null, null);
}

Reason for change

Passing in null for the Type inputType parameter of the JsonSerialaizer is unacceptable and should always throw ArgumentNullException.

Recommended action

Avoid passing null value for Type inputType parameter

Category

  • ASP.NET Core
  • C#
  • Code analysis
  • Core .NET libraries
  • Cryptography
  • Data
  • Debugger
  • Deployment
  • Globalization
  • Interop
  • JIT
  • LINQ
  • Managed Extensibility Framework (MEF)
  • MSBuild
  • Networking
  • Printing
  • Security
  • Serialization
  • Visual Basic
  • Windows Forms
  • Windows Presentation Foundation (WPF)
  • XML, XSLT

Affected APIs

System.Text.Json.JsonSerializer.Serialize(Object, Type, JsonSerializerOptions)
System.Text.Json.JsonSerializer.Serialize(Utf8JsonWriter, Object, Type, JsonSerializerOptions)
System.Text.Json.JsonSerializer.SerializeAsync(Stream, Object, Type, JsonSerializerOptions, CancellationToken)
System.Text.Json.JsonSerializer.SerializeToUtf8Bytes(Object, Type, JsonSerializerOptions)


Issue metadata

  • Issue type: breaking-change
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change Indicates a .NET Core breaking change 🏁 Release: .NET 5 Work items for the .NET 5 release
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants